Wed, 22 May 2013 10:26:22 +0200
file list is now sorted
src/scanner.c | file | annotate | diff | comparison | revisions |
1.1 --- a/src/scanner.c Tue May 21 13:19:37 2013 +0200 1.2 +++ b/src/scanner.c Wed May 22 10:26:22 2013 +0200 1.3 @@ -52,7 +52,6 @@ 1.4 DIR *dirf; 1.5 struct dirent *entry; 1.6 struct stat statbuf; 1.7 - filelist_t *listentry = list; 1.8 1.9 if ((dirf = opendir(scanner.dir)) == NULL) { 1.10 printf("%s", scanner.dir); 1.11 @@ -65,40 +64,51 @@ 1.12 1.13 /* Create new filelist entry */ 1.14 filelist_t *newentry = (filelist_t*) malloc(sizeof(filelist_t)); 1.15 - // TODO: don't just append - create a sorted list! 1.16 - if (listentry) { 1.17 - listentry->next = newentry; 1.18 - } 1.19 - listentry = newentry; 1.20 - if (!list) { 1.21 - list = listentry; 1.22 - } 1.23 + newentry->next = NULL; 1.24 1.25 - listentry->next = NULL; 1.26 + newentry->displayname_len = strlen(entry->d_name); 1.27 + newentry->displayname = (char*) malloc(newentry->displayname_len+1); 1.28 + memcpy(newentry->displayname, entry->d_name, newentry->displayname_len); 1.29 + newentry->displayname[newentry->displayname_len] = 0; 1.30 1.31 - listentry->displayname_len = strlen(entry->d_name); 1.32 - listentry->displayname = (char*) malloc(listentry->displayname_len+1); 1.33 - memcpy(listentry->displayname, entry->d_name, listentry->displayname_len); 1.34 - listentry->displayname[listentry->displayname_len] = 0; 1.35 - 1.36 - listentry->st_mode = 0; 1.37 + newentry->st_mode = 0; 1.38 1.39 /* Construct absolute pathname string */ 1.40 size_t dirnamelen = strlen(scanner.dir); 1.41 - char *filename = (char*) malloc(2+dirnamelen+listentry->displayname_len); 1.42 + char *filename = (char*) malloc(2+dirnamelen+newentry->displayname_len); 1.43 memcpy(filename, scanner.dir, dirnamelen); 1.44 filename[dirnamelen] = settings->fileSeparator; 1.45 - memcpy(filename+dirnamelen+1, entry->d_name, listentry->displayname_len); 1.46 - filename[1+dirnamelen+listentry->displayname_len] = 0; 1.47 - listentry->filename = filename; 1.48 + memcpy(filename+dirnamelen+1, entry->d_name, newentry->displayname_len); 1.49 + filename[1+dirnamelen+newentry->displayname_len] = 0; 1.50 + newentry->filename = filename; 1.51 1.52 /* Check for subdirectory */ 1.53 if (stat(filename, &statbuf) == 0) { 1.54 - listentry->st_mode = statbuf.st_mode; 1.55 + newentry->st_mode = statbuf.st_mode; 1.56 } else { 1.57 perror(" Error in stat call"); 1.58 continue; 1.59 } 1.60 + 1.61 + if (list) { 1.62 + // create fake root to have a pointer on the true root 1.63 + filelist_t root; 1.64 + root.next = list; 1.65 + filelist_t *parent = &root; 1.66 + while (parent->next && 1.67 + (strcasecmp(parent->next->displayname, newentry->displayname) < 0 || 1.68 + (!S_ISDIR(newentry->st_mode) && S_ISDIR(parent->next->st_mode)) 1.69 + ) && 1.70 + (!S_ISDIR(newentry->st_mode) || S_ISDIR(parent->next->st_mode)) 1.71 + ) { 1.72 + parent = parent->next; 1.73 + } 1.74 + newentry->next = parent->next; 1.75 + parent->next = newentry; 1.76 + list = root.next; 1.77 + } else { 1.78 + list = newentry; 1.79 + } 1.80 } 1.81 } 1.82 1.83 @@ -118,10 +128,10 @@ 1.84 while (filelist != NULL) { 1.85 1.86 /* Scan subdirectories */ 1.87 - if (!(filelist->st_mode & S_IFREG)) { 1.88 + if (!S_ISREG(filelist->st_mode)) { 1.89 printf("%*s\n", filelist->displayname_len+scanner.spaces, 1.90 filelist->displayname); 1.91 - if (settings->recursive && (filelist->st_mode & S_IFDIR)) { 1.92 + if (settings->recursive && S_ISDIR(filelist->st_mode)) { 1.93 scanDirectory((scanner_t) {filelist->filename, scanner.spaces+1}, 1.94 settings); 1.95 }