diff options
author | Björn Stenberg <bjorn@haxx.se> | 2002-05-30 14:01:36 +0000 |
---|---|---|
committer | Björn Stenberg <bjorn@haxx.se> | 2002-05-30 14:01:36 +0000 |
commit | 1fc4fee8cb70e577e514d9f675501af6c30ce963 (patch) | |
tree | a4177da5e94b9d74eeacd6956fe863c915ac8c3c | |
parent | f88f91153fe1827d7f63e9161a39ede47a7e909f (diff) | |
download | rockbox-1fc4fee8cb70e577e514d9f675501af6c30ce963.tar.gz rockbox-1fc4fee8cb70e577e514d9f675501af6c30ce963.zip |
Added directory sorting
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@822 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r-- | apps/tree.c | 48 |
1 files changed, 30 insertions, 18 deletions
diff --git a/apps/tree.c b/apps/tree.c index fd2ce3b02e..10ad707262 100644 --- a/apps/tree.c +++ b/apps/tree.c | |||
@@ -46,7 +46,8 @@ struct entry { | |||
46 | char name[TREE_MAX_FILENAMELEN]; | 46 | char name[TREE_MAX_FILENAMELEN]; |
47 | }; | 47 | }; |
48 | 48 | ||
49 | static struct entry buffer[MAX_FILES_IN_DIR]; | 49 | static struct entry dircache[MAX_FILES_IN_DIR]; |
50 | static struct entry* dircacheptr[MAX_FILES_IN_DIR]; | ||
50 | static int filesindir; | 51 | static int filesindir; |
51 | 52 | ||
52 | void browse_root(void) | 53 | void browse_root(void) |
@@ -77,6 +78,12 @@ extern unsigned char bitmap_icons_6x8[LastIcon][6]; | |||
77 | 78 | ||
78 | #endif /* HAVE_LCD_BITMAP */ | 79 | #endif /* HAVE_LCD_BITMAP */ |
79 | 80 | ||
81 | static int compare(const void* e1, const void* e2) | ||
82 | { | ||
83 | return strncmp((*(struct entry**)e1)->name, (*(struct entry**)e2)->name, | ||
84 | TREE_MAX_FILENAMELEN); | ||
85 | } | ||
86 | |||
80 | static int showdir(char *path, int start) | 87 | static int showdir(char *path, int start) |
81 | { | 88 | { |
82 | static char lastdir[256] = {0}; | 89 | static char lastdir[256] = {0}; |
@@ -91,6 +98,7 @@ static int showdir(char *path, int start) | |||
91 | DIR *dir = opendir(path); | 98 | DIR *dir = opendir(path); |
92 | if(!dir) | 99 | if(!dir) |
93 | return -1; /* not a directory */ | 100 | return -1; /* not a directory */ |
101 | memset(dircacheptr,0,sizeof(dircacheptr)); | ||
94 | for ( i=0; i<MAX_FILES_IN_DIR; i++ ) { | 102 | for ( i=0; i<MAX_FILES_IN_DIR; i++ ) { |
95 | struct dirent *entry = readdir(dir); | 103 | struct dirent *entry = readdir(dir); |
96 | if (!entry) | 104 | if (!entry) |
@@ -100,14 +108,16 @@ static int showdir(char *path, int start) | |||
100 | i--; | 108 | i--; |
101 | continue; | 109 | continue; |
102 | } | 110 | } |
103 | buffer[i].file = !(entry->attribute & ATTR_DIRECTORY); | 111 | dircache[i].file = !(entry->attribute & ATTR_DIRECTORY); |
104 | strncpy(buffer[i].name,entry->d_name,TREE_MAX_FILENAMELEN); | 112 | strncpy(dircache[i].name,entry->d_name,TREE_MAX_FILENAMELEN); |
105 | buffer[i].name[TREE_MAX_FILENAMELEN-1]=0; | 113 | dircache[i].name[TREE_MAX_FILENAMELEN-1]=0; |
114 | dircacheptr[i] = &dircache[i]; | ||
106 | } | 115 | } |
107 | filesindir = i; | 116 | filesindir = i; |
108 | closedir(dir); | 117 | closedir(dir); |
109 | strncpy(lastdir,path,sizeof(lastdir)); | 118 | strncpy(lastdir,path,sizeof(lastdir)); |
110 | lastdir[sizeof(lastdir)-1] = 0; | 119 | lastdir[sizeof(lastdir)-1] = 0; |
120 | qsort(dircacheptr,filesindir,sizeof(struct entry*),compare); | ||
111 | } | 121 | } |
112 | 122 | ||
113 | lcd_clear_display(); | 123 | lcd_clear_display(); |
@@ -122,10 +132,10 @@ static int showdir(char *path, int start) | |||
122 | if ( i >= filesindir ) | 132 | if ( i >= filesindir ) |
123 | break; | 133 | break; |
124 | 134 | ||
125 | len = strlen(buffer[i].name); | 135 | len = strlen(dircacheptr[i]->name); |
126 | 136 | ||
127 | #ifdef HAVE_LCD_BITMAP | 137 | #ifdef HAVE_LCD_BITMAP |
128 | if ( buffer[i].file ) | 138 | if ( dircacheptr[i]->file ) |
129 | icon_type=File; | 139 | icon_type=File; |
130 | else | 140 | else |
131 | icon_type=Folder; | 141 | icon_type=Folder; |
@@ -134,12 +144,12 @@ static int showdir(char *path, int start) | |||
134 | #endif | 144 | #endif |
135 | 145 | ||
136 | if(len < TREE_MAX_LEN_DISPLAY) | 146 | if(len < TREE_MAX_LEN_DISPLAY) |
137 | lcd_puts(LINE_X, LINE_Y+i-start, buffer[i].name); | 147 | lcd_puts(LINE_X, LINE_Y+i-start, dircacheptr[i]->name); |
138 | else { | 148 | else { |
139 | char storage = buffer[i].name[TREE_MAX_LEN_DISPLAY]; | 149 | char storage = dircacheptr[i]->name[TREE_MAX_LEN_DISPLAY]; |
140 | buffer[i].name[TREE_MAX_LEN_DISPLAY]=0; | 150 | dircacheptr[i]->name[TREE_MAX_LEN_DISPLAY]=0; |
141 | lcd_puts(LINE_X, LINE_Y+i-start, buffer[i].name); | 151 | lcd_puts(LINE_X, LINE_Y+i-start, dircacheptr[i]->name); |
142 | buffer[i].name[TREE_MAX_LEN_DISPLAY]=storage; | 152 | dircacheptr[i]->name[TREE_MAX_LEN_DISPLAY]=storage; |
143 | } | 153 | } |
144 | } | 154 | } |
145 | 155 | ||
@@ -205,8 +215,8 @@ bool dirbrowse(char *root) | |||
205 | start = 0; | 215 | start = 0; |
206 | numentries = showdir(currdir, start); | 216 | numentries = showdir(currdir, start); |
207 | dircursor=0; | 217 | dircursor=0; |
208 | while ( (dircursor < TREE_MAX_ON_SCREEN) && | 218 | while ( (dircursor < TREE_MAX_ON_SCREEN) && |
209 | (strcmp(buffer[dircursor+start].name,buf)!=0)) | 219 | (strcmp(dircacheptr[dircursor+start]->name,buf))) |
210 | dircursor++; | 220 | dircursor++; |
211 | if (dircursor==TREE_MAX_ON_SCREEN) | 221 | if (dircursor==TREE_MAX_ON_SCREEN) |
212 | dircursor=0; | 222 | dircursor=0; |
@@ -222,12 +232,14 @@ bool dirbrowse(char *root) | |||
222 | #endif | 232 | #endif |
223 | case BUTTON_PLAY: | 233 | case BUTTON_PLAY: |
224 | if ((currdir[0]=='/') && (currdir[1]==0)) { | 234 | if ((currdir[0]=='/') && (currdir[1]==0)) { |
225 | snprintf(buf,sizeof(buf),"%s%s",currdir,buffer[dircursor+start].name); | 235 | snprintf(buf,sizeof(buf),"%s%s",currdir, |
236 | dircacheptr[dircursor+start]->name); | ||
226 | } else { | 237 | } else { |
227 | snprintf(buf,sizeof(buf),"%s/%s",currdir,buffer[dircursor+start].name); | 238 | snprintf(buf,sizeof(buf),"%s/%s",currdir, |
239 | dircacheptr[dircursor+start]->name); | ||
228 | } | 240 | } |
229 | 241 | ||
230 | if (!buffer[dircursor+start].file) { | 242 | if (!dircacheptr[dircursor+start]->file) { |
231 | memcpy(currdir,buf,sizeof(currdir)); | 243 | memcpy(currdir,buf,sizeof(currdir)); |
232 | if ( dirlevel < MAX_DIR_LEVELS ) | 244 | if ( dirlevel < MAX_DIR_LEVELS ) |
233 | dirpos[dirlevel] = start+dircursor; | 245 | dirpos[dirlevel] = start+dircursor; |
@@ -235,7 +247,7 @@ bool dirbrowse(char *root) | |||
235 | dircursor=0; | 247 | dircursor=0; |
236 | start=0; | 248 | start=0; |
237 | } else { | 249 | } else { |
238 | playtune(currdir, buffer[dircursor+start].name); | 250 | playtune(currdir, dircacheptr[dircursor+start]->name); |
239 | #ifdef HAVE_LCD_BITMAP | 251 | #ifdef HAVE_LCD_BITMAP |
240 | lcd_setmargins(0, MARGIN_Y); | 252 | lcd_setmargins(0, MARGIN_Y); |
241 | lcd_setfont(0); | 253 | lcd_setfont(0); |
@@ -305,7 +317,7 @@ bool dirbrowse(char *root) | |||
305 | numentries = showdir(currdir, start); | 317 | numentries = showdir(currdir, start); |
306 | dircursor=0; | 318 | dircursor=0; |
307 | while ( (dircursor < TREE_MAX_ON_SCREEN) && | 319 | while ( (dircursor < TREE_MAX_ON_SCREEN) && |
308 | (strcmp(buffer[dircursor+start].name,buf)!=0)) | 320 | (strcmp(dircacheptr[dircursor+start]->name,buf))) |
309 | dircursor++; | 321 | dircursor++; |
310 | if (dircursor==TREE_MAX_ON_SCREEN) | 322 | if (dircursor==TREE_MAX_ON_SCREEN) |
311 | dircursor=0; | 323 | dircursor=0; |