summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Martitz <kugel@rockbox.org>2010-07-25 22:45:57 +0000
committerThomas Martitz <kugel@rockbox.org>2010-07-25 22:45:57 +0000
commita3e6a865df7f40a01e38381881b29b0b16f6ca8c (patch)
tree1472620cae01a035010bfac7bf341359aee7dd2d
parent6325ef978b2c26445721cae14028c3d429b63b3e (diff)
downloadrockbox-a3e6a865df7f40a01e38381881b29b0b16f6ca8c.zip
rockbox-a3e6a865df7f40a01e38381881b29b0b16f6ca8c.tar.gz
rockbox-a3e6a865df7f40a01e38381881b29b0b16f6ca8c.tar.bz2
rockbox-a3e6a865df7f40a01e38381881b29b0b16f6ca8c.tar.xz
Rewrite set_file() to be smaller and better readable, and a comment describing what it does.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27567 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--apps/settings.c31
-rw-r--r--apps/settings.h4
2 files changed, 19 insertions, 16 deletions
diff --git a/apps/settings.c b/apps/settings.c
index abc1d6a..938281f 100644
--- a/apps/settings.c
+++ b/apps/settings.c
@@ -1188,33 +1188,36 @@ bool set_option(const char* string, const void* variable, enum optiontype type,
return true;
}
-
-void set_file(const char* filename, char* setting, int maxlen)
+/*
+ * Takes filename, removes the directory (assumed to be ROCKBOX_DIR) its in
+ * and the extension, and then copies the basename into setting
+ **/
+void set_file(const char* filename, char* setting, const int maxlen)
{
const char* fptr = strrchr(filename,'/');
+ const char* extptr;
int len;
int extlen = 0;
- const char* ptr;
if (!fptr)
return;
fptr++;
- len = strlen(fptr);
- ptr = fptr + len;
- while ((*ptr != '.') && (ptr != fptr)) {
- extlen++;
- ptr--;
- }
- if(ptr == fptr) extlen = 0;
+ extptr = strrchr(fptr, '.');
- if (strncasecmp(ROCKBOX_DIR, filename, strlen(ROCKBOX_DIR)) ||
- (len-extlen > maxlen))
- return;
+ if (!extptr || extptr < fptr)
+ extlen = 0;
+ else
+ extlen = strlen(extptr);
+
+ len = strlen(fptr) - extlen;
- strlcpy(setting, fptr, len-extlen+1);
+ /* error if filename isn't in ROCKBOX_DIR */
+ if (strncasecmp(ROCKBOX_DIR, filename, ROCKBOX_DIR_LEN) || (len > maxlen))
+ return;
+ strlcpy(setting, fptr, len+1);
settings_save();
}
diff --git a/apps/settings.h b/apps/settings.h
index 75eddb4..1cf43d9 100644
--- a/apps/settings.h
+++ b/apps/settings.h
@@ -55,7 +55,7 @@ struct opt_items {
#ifndef ROCKBOX_DIR
#error ROCKBOX_DIR not defined (should be in autoconf.h)
#endif
-#define ROCKBOX_DIR_LEN sizeof(ROCKBOX_DIR)
+#define ROCKBOX_DIR_LEN (sizeof(ROCKBOX_DIR)-1)
#endif /* def __PCTOOL__ */
@@ -288,7 +288,7 @@ bool set_int_ex(const unsigned char* string, const char* unit, int voice_unit,
const char* (*formatter)(char*, size_t, int, const char*),
int32_t (*get_talk_id)(int, int));
-void set_file(const char* filename, char* setting, int maxlen);
+void set_file(const char* filename, char* setting, const int maxlen);
bool set_option(const char* string, const void* variable, enum optiontype type,
const struct opt_items* options, int numoptions, void (*function)(int));