From ac50839423d12432f68b041c52deaa579c25d5c2 Mon Sep 17 00:00:00 2001 From: Jens Arnold Date: Fri, 2 Sep 2005 16:50:51 +0000 Subject: [PATCH] Optimised new file association handling. Fixed NULL pointer accesses. Const policed. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@7459 a1c6a512-1295-4272-9138-f99709370657 --- apps/filetypes.c | 12 +++++----- apps/filetypes.h | 2 +- apps/tree.c | 57 +++++++++++++++++++++++++----------------------- 3 files changed, 38 insertions(+), 33 deletions(-) diff --git a/apps/filetypes.c b/apps/filetypes.c index 30ab2c34e5..721d5326ea 100644 --- a/apps/filetypes.c +++ b/apps/filetypes.c @@ -209,18 +209,20 @@ bool filetype_supported(int attr) } /* get the "dynamic" attribute for an extension */ -int filetype_get_attr(char* name) +int filetype_get_attr(const char* name) { int i; - char *cp; + const char *cp = strrchr(name,'.'); + + if (!cp) /* no extension? -> can't be a supported type */ + return 0; + cp++; for (i=0; i < cnt_exttypes; i++) { if (exttypes[i].extension) { - cp=strrchr(name,'.'); - if (cp) cp++; - if ((!strcasecmp(cp,exttypes[i].extension)) && (cp)) + if (!strcasecmp(cp,exttypes[i].extension)) { return ((((unsigned long)exttypes[i].type - (unsigned long)&filetypes[0]) / diff --git a/apps/filetypes.h b/apps/filetypes.h index 200d338039..e72dd6ffd3 100644 --- a/apps/filetypes.h +++ b/apps/filetypes.h @@ -23,7 +23,7 @@ #include #include -int filetype_get_attr(char*); +int filetype_get_attr(const char*); #ifdef HAVE_LCD_BITMAP const char* filetype_get_icon(int); #else diff --git a/apps/tree.c b/apps/tree.c index eec71aeb93..a88d58b165 100644 --- a/apps/tree.c +++ b/apps/tree.c @@ -1376,40 +1376,43 @@ static bool add_dir(char* dirname, int len, int fd) else { int x = strlen(entry->d_name); unsigned int i; - char *cp; + char *cp = strrchr(entry->d_name,'.'); + + if (cp) { + cp++; - /* add all supported audio files to playlists */ - for (i=0; i < sizeof(filetypes); i++) { - if (filetypes[i].tree_attr == TREE_ATTR_MPA) { - cp=strrchr(entry->d_name,'.'); - if (cp) cp++; - if ((!strcasecmp(cp,filetypes[i].extension)) && (cp)) - { - char buf[8]; - write(fd, dirname, strlen(dirname)); - write(fd, "/", 1); - write(fd, entry->d_name, x); - write(fd, "\n", 1); + /* add all supported audio files to playlists */ + for (i=0; i < sizeof(filetypes); i++) { + if (filetypes[i].tree_attr == TREE_ATTR_MPA) { + if (!strcasecmp(cp, filetypes[i].extension)) + { + char buf[8]; + write(fd, dirname, strlen(dirname)); + write(fd, "/", 1); + write(fd, entry->d_name, x); + write(fd, "\n", 1); - plsize++; - snprintf(buf, sizeof buf, "%d", plsize); + plsize++; + snprintf(buf, sizeof buf, "%d", plsize); #ifdef HAVE_LCD_BITMAP - lcd_puts(0,4,buf); - lcd_update(); + lcd_puts(0,4,buf); + lcd_update(); #else - x = 10; - if (plsize > 999) - x=7; - else { - if (plsize > 99) - x=8; + x = 10; + if (plsize > 999) + x=7; else { - if (plsize > 9) - x=9; + if (plsize > 99) + x=8; + else { + if (plsize > 9) + x=9; + } } - } - lcd_puts(x,0,buf); + lcd_puts(x,0,buf); #endif + break; + } } } }