diff options
author | Thomas Martitz <kugel@rockbox.org> | 2010-07-25 22:45:57 +0000 |
---|---|---|
committer | Thomas Martitz <kugel@rockbox.org> | 2010-07-25 22:45:57 +0000 |
commit | a3e6a865df7f40a01e38381881b29b0b16f6ca8c (patch) | |
tree | 1472620cae01a035010bfac7bf341359aee7dd2d | |
parent | 6325ef978b2c26445721cae14028c3d429b63b3e (diff) | |
download | rockbox-a3e6a865df7f40a01e38381881b29b0b16f6ca8c.tar.gz rockbox-a3e6a865df7f40a01e38381881b29b0b16f6ca8c.zip |
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.c | 31 | ||||
-rw-r--r-- | apps/settings.h | 4 |
2 files changed, 19 insertions, 16 deletions
diff --git a/apps/settings.c b/apps/settings.c index abc1d6af93..938281f4c2 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, | |||
1188 | return true; | 1188 | return true; |
1189 | } | 1189 | } |
1190 | 1190 | ||
1191 | 1191 | /* | |
1192 | void set_file(const char* filename, char* setting, int maxlen) | 1192 | * Takes filename, removes the directory (assumed to be ROCKBOX_DIR) its in |
1193 | * and the extension, and then copies the basename into setting | ||
1194 | **/ | ||
1195 | void set_file(const char* filename, char* setting, const int maxlen) | ||
1193 | { | 1196 | { |
1194 | const char* fptr = strrchr(filename,'/'); | 1197 | const char* fptr = strrchr(filename,'/'); |
1198 | const char* extptr; | ||
1195 | int len; | 1199 | int len; |
1196 | int extlen = 0; | 1200 | int extlen = 0; |
1197 | const char* ptr; | ||
1198 | 1201 | ||
1199 | if (!fptr) | 1202 | if (!fptr) |
1200 | return; | 1203 | return; |
1201 | 1204 | ||
1202 | fptr++; | 1205 | fptr++; |
1203 | 1206 | ||
1204 | len = strlen(fptr); | 1207 | extptr = strrchr(fptr, '.'); |
1205 | ptr = fptr + len; | ||
1206 | while ((*ptr != '.') && (ptr != fptr)) { | ||
1207 | extlen++; | ||
1208 | ptr--; | ||
1209 | } | ||
1210 | if(ptr == fptr) extlen = 0; | ||
1211 | 1208 | ||
1212 | if (strncasecmp(ROCKBOX_DIR, filename, strlen(ROCKBOX_DIR)) || | 1209 | if (!extptr || extptr < fptr) |
1213 | (len-extlen > maxlen)) | 1210 | extlen = 0; |
1214 | return; | 1211 | else |
1212 | extlen = strlen(extptr); | ||
1213 | |||
1214 | len = strlen(fptr) - extlen; | ||
1215 | 1215 | ||
1216 | strlcpy(setting, fptr, len-extlen+1); | 1216 | /* error if filename isn't in ROCKBOX_DIR */ |
1217 | if (strncasecmp(ROCKBOX_DIR, filename, ROCKBOX_DIR_LEN) || (len > maxlen)) | ||
1218 | return; | ||
1217 | 1219 | ||
1220 | strlcpy(setting, fptr, len+1); | ||
1218 | settings_save(); | 1221 | settings_save(); |
1219 | } | 1222 | } |
1220 | 1223 | ||
diff --git a/apps/settings.h b/apps/settings.h index 75eddb41f6..1cf43d9a2c 100644 --- a/apps/settings.h +++ b/apps/settings.h | |||
@@ -55,7 +55,7 @@ struct opt_items { | |||
55 | #ifndef ROCKBOX_DIR | 55 | #ifndef ROCKBOX_DIR |
56 | #error ROCKBOX_DIR not defined (should be in autoconf.h) | 56 | #error ROCKBOX_DIR not defined (should be in autoconf.h) |
57 | #endif | 57 | #endif |
58 | #define ROCKBOX_DIR_LEN sizeof(ROCKBOX_DIR) | 58 | #define ROCKBOX_DIR_LEN (sizeof(ROCKBOX_DIR)-1) |
59 | #endif /* def __PCTOOL__ */ | 59 | #endif /* def __PCTOOL__ */ |
60 | 60 | ||
61 | 61 | ||
@@ -288,7 +288,7 @@ bool set_int_ex(const unsigned char* string, const char* unit, int voice_unit, | |||
288 | const char* (*formatter)(char*, size_t, int, const char*), | 288 | const char* (*formatter)(char*, size_t, int, const char*), |
289 | int32_t (*get_talk_id)(int, int)); | 289 | int32_t (*get_talk_id)(int, int)); |
290 | 290 | ||
291 | void set_file(const char* filename, char* setting, int maxlen); | 291 | void set_file(const char* filename, char* setting, const int maxlen); |
292 | 292 | ||
293 | bool set_option(const char* string, const void* variable, enum optiontype type, | 293 | bool set_option(const char* string, const void* variable, enum optiontype type, |
294 | const struct opt_items* options, int numoptions, void (*function)(int)); | 294 | const struct opt_items* options, int numoptions, void (*function)(int)); |