PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mod/filesys.mod/filelist.c

https://github.com/eggheads/eggdrop-1.8
C | 130 lines | 83 code | 12 blank | 35 comment | 15 complexity | 3f1c37a5ffd4cf5330e4b6b579797674 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * filelist.c -- part of filesys.mod
  3. * functions to sort and manage file lists
  4. *
  5. * Written by Fabian Knittel <fknittel@gmx.de>
  6. *
  7. * $Id: filelist.c,v 1.1 2010/07/26 21:11:06 simple Exp $
  8. */
  9. /*
  10. * Copyright (C) 1999 - 2010 Eggheads Development Team
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version 2
  15. * of the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  25. */
  26. #include "filelist.h"
  27. static filelist_t *filelist_new(void)
  28. {
  29. filelist_t *flist;
  30. flist = nmalloc(sizeof(filelist_t));
  31. flist->tot = 0;
  32. flist->elements = NULL;
  33. return flist;
  34. }
  35. static void filelist_free(filelist_t *flist)
  36. {
  37. int i;
  38. if (!flist)
  39. return;
  40. for (i = 0; i < flist->tot; i++) {
  41. if (flist->elements[i].output)
  42. my_free(flist->elements[i].output);
  43. my_free(flist->elements[i].fn);
  44. }
  45. if (flist->elements)
  46. my_free(flist->elements);
  47. my_free(flist);
  48. }
  49. /* Increase number of filelist entries.
  50. */
  51. static void filelist_add(filelist_t *flist, char *filename)
  52. {
  53. flist->tot++;
  54. flist->elements = nrealloc(flist->elements, flist->tot * sizeof(filelist_t));
  55. FILELIST_LE(flist).fn = nmalloc(strlen(filename) + 1);
  56. strcpy(FILELIST_LE(flist).fn, filename);
  57. FILELIST_LE(flist).output = NULL;
  58. }
  59. /* Add data to the end of filelist entry's output string
  60. */
  61. static void filelist_addout(filelist_t *flist, char *desc)
  62. {
  63. if (FILELIST_LE(flist).output) {
  64. FILELIST_LE(flist).output = nrealloc(FILELIST_LE(flist).output,
  65. strlen(FILELIST_LE(flist).output) +
  66. strlen(desc) + 1);
  67. strcat(FILELIST_LE(flist).output, desc);
  68. } else {
  69. FILELIST_LE(flist).output = nmalloc(strlen(desc) + 1);
  70. strcpy(FILELIST_LE(flist).output, desc);
  71. }
  72. }
  73. /* Dump all data to specified idx */
  74. static inline void filelist_idxshow(filelist_t *flist, int idx)
  75. {
  76. int i;
  77. for (i = 0; i < flist->tot; i++)
  78. dprintf(idx, "%s", flist->elements[i].output);
  79. }
  80. /* Uses QSort to sort the list of filenames. This function is
  81. * called recursively.
  82. */
  83. static void filelist_qsort(filelist_t *flist, int l, int r)
  84. {
  85. int i = l, j = r, middle;
  86. filelist_element_t *el = flist->elements, elt;
  87. middle = ((l + r) / 2);
  88. do {
  89. while (strcmp(el[i].fn, el[middle].fn) < 0)
  90. i++;
  91. while (strcmp(el[j].fn, el[middle].fn) > 0)
  92. j--;
  93. if (i <= j) {
  94. if (strcmp(el[j].fn, el[i].fn)) {
  95. elt.fn = el[j].fn;
  96. elt.output = el[j].output;
  97. el[j].fn = el[i].fn;
  98. el[j].output = el[i].output;
  99. el[i].fn = elt.fn;
  100. el[i].output = elt.output;
  101. }
  102. i++;
  103. j--;
  104. }
  105. } while (i <= j);
  106. if (l < j)
  107. filelist_qsort(flist, l, j);
  108. if (i < r)
  109. filelist_qsort(flist, i, r);
  110. }
  111. /* Sort list of filenames.
  112. */
  113. static void filelist_sort(filelist_t *flist)
  114. {
  115. if (flist->tot < 2)
  116. return;
  117. filelist_qsort(flist, 0, (flist->tot - 1));
  118. }