00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "filelist.h"
00028
00029 static filelist_t *filelist_new(void)
00030 {
00031 filelist_t *flist;
00032
00033 flist = nmalloc(sizeof(filelist_t));
00034 flist->tot = 0;
00035 flist->elements = NULL;
00036 return flist;
00037 }
00038
00039 static void filelist_free(filelist_t *flist)
00040 {
00041 int i;
00042
00043 if (!flist)
00044 return;
00045 for (i = 0; i < flist->tot; i++) {
00046 if (flist->elements[i].output)
00047 my_free(flist->elements[i].output);
00048 my_free(flist->elements[i].fn);
00049 }
00050 if (flist->elements)
00051 my_free(flist->elements);
00052 my_free(flist);
00053 }
00054
00055
00056
00057 static void filelist_add(filelist_t *flist, char *filename)
00058 {
00059 flist->tot++;
00060 flist->elements = nrealloc(flist->elements, flist->tot * sizeof(filelist_t));
00061 FILELIST_LE(flist).fn = nmalloc(strlen(filename) + 1);
00062 strcpy(FILELIST_LE(flist).fn, filename);
00063 FILELIST_LE(flist).output = NULL;
00064 }
00065
00066
00067
00068 static void filelist_addout(filelist_t *flist, char *desc)
00069 {
00070 if (FILELIST_LE(flist).output) {
00071 FILELIST_LE(flist).output = nrealloc(FILELIST_LE(flist).output,
00072 strlen(FILELIST_LE(flist).output) +
00073 strlen(desc) + 1);
00074 strcat(FILELIST_LE(flist).output, desc);
00075 } else {
00076 FILELIST_LE(flist).output = nmalloc(strlen(desc) + 1);
00077 strcpy(FILELIST_LE(flist).output, desc);
00078 }
00079 }
00080
00081
00082 static inline void filelist_idxshow(filelist_t *flist, int idx)
00083 {
00084 int i;
00085
00086 for (i = 0; i < flist->tot; i++)
00087 dprintf(idx, "%s", flist->elements[i].output);
00088 }
00089
00090
00091
00092
00093 static void filelist_qsort(filelist_t *flist, int l, int r)
00094 {
00095 int i = l, j = r, middle;
00096 filelist_element_t *el = flist->elements, elt;
00097
00098 middle = ((l + r) / 2);
00099 do {
00100 while (strcmp(el[i].fn, el[middle].fn) < 0)
00101 i++;
00102 while (strcmp(el[j].fn, el[middle].fn) > 0)
00103 j--;
00104 if (i <= j) {
00105 if (strcmp(el[j].fn, el[i].fn)) {
00106 elt.fn = el[j].fn;
00107 elt.output = el[j].output;
00108 el[j].fn = el[i].fn;
00109 el[j].output = el[i].output;
00110 el[i].fn = elt.fn;
00111 el[i].output = elt.output;
00112 }
00113 i++;
00114 j--;
00115 }
00116 } while (i <= j);
00117 if (l < j)
00118 filelist_qsort(flist, l, j);
00119 if (i < r)
00120 filelist_qsort(flist, i, r);
00121 }
00122
00123
00124
00125 static void filelist_sort(filelist_t *flist)
00126 {
00127 if (flist->tot < 2)
00128 return;
00129 filelist_qsort(flist, 0, (flist->tot - 1));
00130 }