FS#7487 - mpegplayer - video start time seek with resume
[Rockbox.git] / apps / plugins / lib / configfile.c
blob476f776878e866bd2a6d38f04246723192b03fe7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "plugin.h"
20 #include "configfile.h"
22 static struct plugin_api *cfg_rb;
24 void configfile_init(struct plugin_api* newrb)
26 cfg_rb = newrb;
29 void get_cfg_filename(char* buf, int buf_len, const char* filename)
31 char *s;
32 cfg_rb->strcpy(buf, cfg_rb->plugin_get_current_filename());
33 s = cfg_rb->strrchr(buf, '/');
34 if (!s) /* should never happen */
36 cfg_rb->snprintf(buf, buf_len, PLUGIN_DIR "/%s", filename);
38 else
40 s++;
41 *s = '\0';
42 cfg_rb->strcat(s, filename);
46 int configfile_save(const char *filename, struct configdata *cfg,
47 int num_items, int version)
49 int fd;
50 int i;
51 char buf[MAX_PATH];
53 get_cfg_filename(buf, MAX_PATH, filename);
54 fd = cfg_rb->creat(buf);
55 if(fd < 0)
56 return fd*10 - 1;
58 /* pre-allocate 10 bytes for INT */
59 cfg_rb->fdprintf(fd, "file version: %10d\n", version);
61 for(i = 0;i < num_items;i++) {
62 switch(cfg[i].type) {
63 case TYPE_INT:
64 /* pre-allocate 10 bytes for INT */
65 cfg_rb->fdprintf(fd, "%s: %10d\n",
66 cfg[i].name,
67 *cfg[i].val);
68 break;
70 case TYPE_ENUM:
71 cfg_rb->fdprintf(fd, "%s: %s\n",
72 cfg[i].name,
73 cfg[i].values[*cfg[i].val]);
74 break;
76 case TYPE_STRING:
77 cfg_rb->fdprintf(fd, "%s: %s\n",
78 cfg[i].name,
79 cfg[i].string);
80 break;
85 cfg_rb->close(fd);
86 return 0;
89 int configfile_load(const char *filename, struct configdata *cfg,
90 int num_items, int min_version)
92 int fd;
93 int i, j;
94 char *name;
95 char *val;
96 char buf[MAX_PATH];
97 int file_version = -1;
98 int tmp;
100 get_cfg_filename(buf, MAX_PATH, filename);
101 fd = cfg_rb->open(buf, O_RDONLY);
102 if(fd < 0)
103 return fd*10 - 1;
105 while(cfg_rb->read_line(fd, buf, MAX_PATH) > 0) {
106 cfg_rb->settings_parseline(buf, &name, &val);
108 /* Bail out if the file version is too old */
109 if(!cfg_rb->strcmp("file version", name)) {
110 file_version = cfg_rb->atoi(val);
111 if(file_version < min_version) {
112 cfg_rb->close(fd);
113 return -1;
117 for(i = 0;i < num_items;i++) {
118 if(!cfg_rb->strcmp(cfg[i].name, name)) {
119 switch(cfg[i].type) {
120 case TYPE_INT:
121 tmp = cfg_rb->atoi(val);
122 /* Only set it if it's within range */
123 if(tmp >= cfg[i].min && tmp <= cfg[i].max)
124 *cfg[i].val = tmp;
125 break;
127 case TYPE_ENUM:
128 for(j = 0;j < cfg[i].max;j++) {
129 if(!cfg_rb->strcmp(cfg[i].values[j], val)) {
130 *cfg[i].val = j;
133 break;
135 case TYPE_STRING:
136 cfg_rb->strncpy(cfg[i].string, val, cfg[i].max);
137 break;
143 cfg_rb->close(fd);
144 return 0;
147 int configfile_get_value(const char* filename, const char* name)
149 int fd;
150 char *pname;
151 char *pval;
152 char buf[MAX_PATH];
154 get_cfg_filename(buf, MAX_PATH, filename);
155 fd = cfg_rb->open(buf, O_RDONLY);
156 if(fd < 0)
157 return -1;
159 while(cfg_rb->read_line(fd, buf, MAX_PATH) > 0)
161 cfg_rb->settings_parseline(buf, &pname, &pval);
162 if(!cfg_rb->strcmp(name, pname))
164 cfg_rb->close(fd);
165 return cfg_rb->atoi(pval);
169 cfg_rb->close(fd);
170 return -1;
173 int configfile_update_entry(const char* filename, const char* name, int val)
175 int fd;
176 char *pname;
177 char *pval;
178 char path[MAX_PATH];
179 char buf[256];
180 int found = 0;
181 int line_len = 0;
182 int pos = 0;
184 /* open the current config file */
185 get_cfg_filename(path, MAX_PATH, filename);
186 fd = cfg_rb->open(path, O_RDWR);
187 if(fd < 0)
188 return -1;
190 /* read in the current stored settings */
191 while((line_len = cfg_rb->read_line(fd, buf, 256)) > 0)
193 cfg_rb->settings_parseline(buf, &pname, &pval);
195 if(!cfg_rb->strcmp(name, pname))
197 found = 1;
198 cfg_rb->lseek(fd, pos, SEEK_SET);
199 /* pre-allocate 10 bytes for INT */
200 cfg_rb->fdprintf(fd, "%s: %10d\n", pname, val);
201 break;
203 pos += line_len;
206 /* if (name/val) is a new entry just append to file */
207 if (found == 0)
208 /* pre-allocate 10 bytes for INT */
209 cfg_rb->fdprintf(fd, "%s: %10d\n", name, val);
211 cfg_rb->close(fd);
213 return found;