summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Nielsen Feltzing <linus@haxx.se>2004-07-22 07:51:02 +0000
committerLinus Nielsen Feltzing <linus@haxx.se>2004-07-22 07:51:02 +0000
commite93aa4b09ce05117d08d52487f72417413edfbb1 (patch)
treee7bf2269da17c6f04373befe0e71e0cdcd6fcd5e
parent1f019981e437ff4c92c183d9bcbb3907915ab3ff (diff)
downloadrockbox-e93aa4b09ce05117d08d52487f72417413edfbb1.tar.gz
rockbox-e93aa4b09ce05117d08d52487f72417413edfbb1.zip
Now keeps integers within range, and saves a version number in the cfg file, refusing to load too old versions
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@4916 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--apps/plugins/lib/configfile.c24
-rw-r--r--apps/plugins/lib/configfile.h6
2 files changed, 25 insertions, 5 deletions
diff --git a/apps/plugins/lib/configfile.c b/apps/plugins/lib/configfile.c
index ffa325038c..ff4809a3f4 100644
--- a/apps/plugins/lib/configfile.c
+++ b/apps/plugins/lib/configfile.c
@@ -26,7 +26,8 @@ void configfile_init(struct plugin_api* newrb)
26 cfg_rb = newrb; 26 cfg_rb = newrb;
27} 27}
28 28
29int configfile_save(char *filename, struct configdata *cfg, int num_items) 29int configfile_save(const char *filename, struct configdata *cfg,
30 int num_items, int version)
30{ 31{
31 int fd; 32 int fd;
32 int i; 33 int i;
@@ -37,6 +38,8 @@ int configfile_save(char *filename, struct configdata *cfg, int num_items)
37 if(fd < 0) 38 if(fd < 0)
38 return fd*10 - 1; 39 return fd*10 - 1;
39 40
41 cfg_rb->fprintf(fd, "file version: %d\n", version);
42
40 for(i = 0;i < num_items;i++) { 43 for(i = 0;i < num_items;i++) {
41 switch(cfg[i].type) { 44 switch(cfg[i].type) {
42 case TYPE_INT: 45 case TYPE_INT:
@@ -64,13 +67,16 @@ int configfile_save(char *filename, struct configdata *cfg, int num_items)
64 return 0; 67 return 0;
65} 68}
66 69
67int configfile_load(char *filename, struct configdata *cfg, int num_items) 70int configfile_load(const char *filename, struct configdata *cfg,
71 int num_items, int min_version)
68{ 72{
69 int fd; 73 int fd;
70 int i, j; 74 int i, j;
71 char *name; 75 char *name;
72 char *val; 76 char *val;
73 char buf[MAX_PATH]; 77 char buf[MAX_PATH];
78 int file_version = -1;
79 int tmp;
74 80
75 cfg_rb->snprintf(buf, MAX_PATH, "/.rockbox/rocks/%s", filename); 81 cfg_rb->snprintf(buf, MAX_PATH, "/.rockbox/rocks/%s", filename);
76 fd = cfg_rb->open(buf, O_RDONLY); 82 fd = cfg_rb->open(buf, O_RDONLY);
@@ -79,12 +85,24 @@ int configfile_load(char *filename, struct configdata *cfg, int num_items)
79 85
80 while(cfg_rb->read_line(fd, buf, MAX_PATH) > 0) { 86 while(cfg_rb->read_line(fd, buf, MAX_PATH) > 0) {
81 cfg_rb->settings_parseline(buf, &name, &val); 87 cfg_rb->settings_parseline(buf, &name, &val);
88
89 /* Bail out if the file version is too old */
90 if(!cfg_rb->strcmp("file version", name)) {
91 file_version = cfg_rb->atoi(val);
92 if(file_version < min_version) {
93 cfg_rb->close(fd);
94 return -1;
95 }
96 }
82 97
83 for(i = 0;i < num_items;i++) { 98 for(i = 0;i < num_items;i++) {
84 if(!cfg_rb->strcmp(cfg[i].name, name)) { 99 if(!cfg_rb->strcmp(cfg[i].name, name)) {
85 switch(cfg[i].type) { 100 switch(cfg[i].type) {
86 case TYPE_INT: 101 case TYPE_INT:
87 *cfg[i].val = cfg_rb->atoi(val); 102 tmp = cfg_rb->atoi(val);
103 /* Only set it if it's within range */
104 if(tmp >= cfg[i].min && tmp <= cfg[i].max)
105 *cfg[i].val = tmp;
88 break; 106 break;
89 107
90 case TYPE_ENUM: 108 case TYPE_ENUM:
diff --git a/apps/plugins/lib/configfile.h b/apps/plugins/lib/configfile.h
index 78cc8b1b6c..fcce7de275 100644
--- a/apps/plugins/lib/configfile.h
+++ b/apps/plugins/lib/configfile.h
@@ -38,7 +38,9 @@ struct configdata
38}; 38};
39 39
40void configfile_init(struct plugin_api* newrb); 40void configfile_init(struct plugin_api* newrb);
41int configfile_save(char *filename, struct configdata *cfg, int num_items); 41int configfile_save(const char *filename, struct configdata *cfg,
42int configfile_load(char *filename, struct configdata *cfg, int num_items); 42 int num_items, int version);
43int configfile_load(const char *filename, struct configdata *cfg,
44 int num_items, int min_version);
43 45
44#endif 46#endif