diff options
Diffstat (limited to 'apps/plugins/lib/configfile.c')
-rw-r--r-- | apps/plugins/lib/configfile.c | 79 |
1 files changed, 36 insertions, 43 deletions
diff --git a/apps/plugins/lib/configfile.c b/apps/plugins/lib/configfile.c index 4fb037cb8e..419109f904 100644 --- a/apps/plugins/lib/configfile.c +++ b/apps/plugins/lib/configfile.c | |||
@@ -21,27 +21,20 @@ | |||
21 | #include "plugin.h" | 21 | #include "plugin.h" |
22 | #include "configfile.h" | 22 | #include "configfile.h" |
23 | 23 | ||
24 | static const struct plugin_api *cfg_rb; | ||
25 | |||
26 | void configfile_init(const struct plugin_api* newrb) | ||
27 | { | ||
28 | cfg_rb = newrb; | ||
29 | } | ||
30 | |||
31 | static void get_cfg_filename(char* buf, int buf_len, const char* filename) | 24 | static void get_cfg_filename(char* buf, int buf_len, const char* filename) |
32 | { | 25 | { |
33 | char *s; | 26 | char *s; |
34 | cfg_rb->strcpy(buf, cfg_rb->plugin_get_current_filename()); | 27 | rb->strcpy(buf, rb->plugin_get_current_filename()); |
35 | s = cfg_rb->strrchr(buf, '/'); | 28 | s = rb->strrchr(buf, '/'); |
36 | if (!s) /* should never happen */ | 29 | if (!s) /* should never happen */ |
37 | { | 30 | { |
38 | cfg_rb->snprintf(buf, buf_len, PLUGIN_DIR "/%s", filename); | 31 | rb->snprintf(buf, buf_len, PLUGIN_DIR "/%s", filename); |
39 | } | 32 | } |
40 | else | 33 | else |
41 | { | 34 | { |
42 | s++; | 35 | s++; |
43 | *s = '\0'; | 36 | *s = '\0'; |
44 | cfg_rb->strcat(s, filename); | 37 | rb->strcat(s, filename); |
45 | } | 38 | } |
46 | } | 39 | } |
47 | 40 | ||
@@ -53,30 +46,30 @@ int configfile_save(const char *filename, struct configdata *cfg, | |||
53 | char buf[MAX_PATH]; | 46 | char buf[MAX_PATH]; |
54 | 47 | ||
55 | get_cfg_filename(buf, MAX_PATH, filename); | 48 | get_cfg_filename(buf, MAX_PATH, filename); |
56 | fd = cfg_rb->creat(buf); | 49 | fd = rb->creat(buf); |
57 | if(fd < 0) | 50 | if(fd < 0) |
58 | return fd*10 - 1; | 51 | return fd*10 - 1; |
59 | 52 | ||
60 | /* pre-allocate 10 bytes for INT */ | 53 | /* pre-allocate 10 bytes for INT */ |
61 | cfg_rb->fdprintf(fd, "file version: %10d\n", version); | 54 | rb->fdprintf(fd, "file version: %10d\n", version); |
62 | 55 | ||
63 | for(i = 0;i < num_items;i++) { | 56 | for(i = 0;i < num_items;i++) { |
64 | switch(cfg[i].type) { | 57 | switch(cfg[i].type) { |
65 | case TYPE_INT: | 58 | case TYPE_INT: |
66 | /* pre-allocate 10 bytes for INT */ | 59 | /* pre-allocate 10 bytes for INT */ |
67 | cfg_rb->fdprintf(fd, "%s: %10d\n", | 60 | rb->fdprintf(fd, "%s: %10d\n", |
68 | cfg[i].name, | 61 | cfg[i].name, |
69 | *cfg[i].val); | 62 | *cfg[i].val); |
70 | break; | 63 | break; |
71 | 64 | ||
72 | case TYPE_ENUM: | 65 | case TYPE_ENUM: |
73 | cfg_rb->fdprintf(fd, "%s: %s\n", | 66 | rb->fdprintf(fd, "%s: %s\n", |
74 | cfg[i].name, | 67 | cfg[i].name, |
75 | cfg[i].values[*cfg[i].val]); | 68 | cfg[i].values[*cfg[i].val]); |
76 | break; | 69 | break; |
77 | 70 | ||
78 | case TYPE_STRING: | 71 | case TYPE_STRING: |
79 | cfg_rb->fdprintf(fd, "%s: %s\n", | 72 | rb->fdprintf(fd, "%s: %s\n", |
80 | cfg[i].name, | 73 | cfg[i].name, |
81 | cfg[i].string); | 74 | cfg[i].string); |
82 | break; | 75 | break; |
@@ -84,7 +77,7 @@ int configfile_save(const char *filename, struct configdata *cfg, | |||
84 | } | 77 | } |
85 | } | 78 | } |
86 | 79 | ||
87 | cfg_rb->close(fd); | 80 | rb->close(fd); |
88 | return 0; | 81 | return 0; |
89 | } | 82 | } |
90 | 83 | ||
@@ -100,27 +93,27 @@ int configfile_load(const char *filename, struct configdata *cfg, | |||
100 | int tmp; | 93 | int tmp; |
101 | 94 | ||
102 | get_cfg_filename(buf, MAX_PATH, filename); | 95 | get_cfg_filename(buf, MAX_PATH, filename); |
103 | fd = cfg_rb->open(buf, O_RDONLY); | 96 | fd = rb->open(buf, O_RDONLY); |
104 | if(fd < 0) | 97 | if(fd < 0) |
105 | return fd*10 - 1; | 98 | return fd*10 - 1; |
106 | 99 | ||
107 | while(cfg_rb->read_line(fd, buf, MAX_PATH) > 0) { | 100 | while(rb->read_line(fd, buf, MAX_PATH) > 0) { |
108 | cfg_rb->settings_parseline(buf, &name, &val); | 101 | rb->settings_parseline(buf, &name, &val); |
109 | 102 | ||
110 | /* Bail out if the file version is too old */ | 103 | /* Bail out if the file version is too old */ |
111 | if(!cfg_rb->strcmp("file version", name)) { | 104 | if(!rb->strcmp("file version", name)) { |
112 | file_version = cfg_rb->atoi(val); | 105 | file_version = rb->atoi(val); |
113 | if(file_version < min_version) { | 106 | if(file_version < min_version) { |
114 | cfg_rb->close(fd); | 107 | rb->close(fd); |
115 | return -1; | 108 | return -1; |
116 | } | 109 | } |
117 | } | 110 | } |
118 | 111 | ||
119 | for(i = 0;i < num_items;i++) { | 112 | for(i = 0;i < num_items;i++) { |
120 | if(!cfg_rb->strcmp(cfg[i].name, name)) { | 113 | if(!rb->strcmp(cfg[i].name, name)) { |
121 | switch(cfg[i].type) { | 114 | switch(cfg[i].type) { |
122 | case TYPE_INT: | 115 | case TYPE_INT: |
123 | tmp = cfg_rb->atoi(val); | 116 | tmp = rb->atoi(val); |
124 | /* Only set it if it's within range */ | 117 | /* Only set it if it's within range */ |
125 | if(tmp >= cfg[i].min && tmp <= cfg[i].max) | 118 | if(tmp >= cfg[i].min && tmp <= cfg[i].max) |
126 | *cfg[i].val = tmp; | 119 | *cfg[i].val = tmp; |
@@ -128,21 +121,21 @@ int configfile_load(const char *filename, struct configdata *cfg, | |||
128 | 121 | ||
129 | case TYPE_ENUM: | 122 | case TYPE_ENUM: |
130 | for(j = 0;j < cfg[i].max;j++) { | 123 | for(j = 0;j < cfg[i].max;j++) { |
131 | if(!cfg_rb->strcmp(cfg[i].values[j], val)) { | 124 | if(!rb->strcmp(cfg[i].values[j], val)) { |
132 | *cfg[i].val = j; | 125 | *cfg[i].val = j; |
133 | } | 126 | } |
134 | } | 127 | } |
135 | break; | 128 | break; |
136 | 129 | ||
137 | case TYPE_STRING: | 130 | case TYPE_STRING: |
138 | cfg_rb->strncpy(cfg[i].string, val, cfg[i].max); | 131 | rb->strncpy(cfg[i].string, val, cfg[i].max); |
139 | break; | 132 | break; |
140 | } | 133 | } |
141 | } | 134 | } |
142 | } | 135 | } |
143 | } | 136 | } |
144 | 137 | ||
145 | cfg_rb->close(fd); | 138 | rb->close(fd); |
146 | return 0; | 139 | return 0; |
147 | } | 140 | } |
148 | 141 | ||
@@ -154,21 +147,21 @@ int configfile_get_value(const char* filename, const char* name) | |||
154 | char buf[MAX_PATH]; | 147 | char buf[MAX_PATH]; |
155 | 148 | ||
156 | get_cfg_filename(buf, MAX_PATH, filename); | 149 | get_cfg_filename(buf, MAX_PATH, filename); |
157 | fd = cfg_rb->open(buf, O_RDONLY); | 150 | fd = rb->open(buf, O_RDONLY); |
158 | if(fd < 0) | 151 | if(fd < 0) |
159 | return -1; | 152 | return -1; |
160 | 153 | ||
161 | while(cfg_rb->read_line(fd, buf, MAX_PATH) > 0) | 154 | while(rb->read_line(fd, buf, MAX_PATH) > 0) |
162 | { | 155 | { |
163 | cfg_rb->settings_parseline(buf, &pname, &pval); | 156 | rb->settings_parseline(buf, &pname, &pval); |
164 | if(!cfg_rb->strcmp(name, pname)) | 157 | if(!rb->strcmp(name, pname)) |
165 | { | 158 | { |
166 | cfg_rb->close(fd); | 159 | rb->close(fd); |
167 | return cfg_rb->atoi(pval); | 160 | return rb->atoi(pval); |
168 | } | 161 | } |
169 | } | 162 | } |
170 | 163 | ||
171 | cfg_rb->close(fd); | 164 | rb->close(fd); |
172 | return -1; | 165 | return -1; |
173 | } | 166 | } |
174 | 167 | ||
@@ -185,20 +178,20 @@ int configfile_update_entry(const char* filename, const char* name, int val) | |||
185 | 178 | ||
186 | /* open the current config file */ | 179 | /* open the current config file */ |
187 | get_cfg_filename(path, MAX_PATH, filename); | 180 | get_cfg_filename(path, MAX_PATH, filename); |
188 | fd = cfg_rb->open(path, O_RDWR); | 181 | fd = rb->open(path, O_RDWR); |
189 | if(fd < 0) | 182 | if(fd < 0) |
190 | return -1; | 183 | return -1; |
191 | 184 | ||
192 | /* read in the current stored settings */ | 185 | /* read in the current stored settings */ |
193 | while((line_len = cfg_rb->read_line(fd, buf, 256)) > 0) | 186 | while((line_len = rb->read_line(fd, buf, 256)) > 0) |
194 | { | 187 | { |
195 | cfg_rb->settings_parseline(buf, &pname, &pval); | 188 | rb->settings_parseline(buf, &pname, &pval); |
196 | if(!cfg_rb->strcmp(name, pname)) | 189 | if(!rb->strcmp(name, pname)) |
197 | { | 190 | { |
198 | found = 1; | 191 | found = 1; |
199 | cfg_rb->lseek(fd, pos, SEEK_SET); | 192 | rb->lseek(fd, pos, SEEK_SET); |
200 | /* pre-allocate 10 bytes for INT */ | 193 | /* pre-allocate 10 bytes for INT */ |
201 | cfg_rb->fdprintf(fd, "%s: %10d\n", pname, val); | 194 | rb->fdprintf(fd, "%s: %10d\n", pname, val); |
202 | break; | 195 | break; |
203 | } | 196 | } |
204 | pos += line_len; | 197 | pos += line_len; |
@@ -207,9 +200,9 @@ int configfile_update_entry(const char* filename, const char* name, int val) | |||
207 | /* if (name/val) is a new entry just append to file */ | 200 | /* if (name/val) is a new entry just append to file */ |
208 | if (found == 0) | 201 | if (found == 0) |
209 | /* pre-allocate 10 bytes for INT */ | 202 | /* pre-allocate 10 bytes for INT */ |
210 | cfg_rb->fdprintf(fd, "%s: %10d\n", name, val); | 203 | rb->fdprintf(fd, "%s: %10d\n", name, val); |
211 | 204 | ||
212 | cfg_rb->close(fd); | 205 | rb->close(fd); |
213 | 206 | ||
214 | return found; | 207 | return found; |
215 | } | 208 | } |