1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
20 #include "configfile.h"
22 static struct plugin_api
*cfg_rb
;
24 void configfile_init(struct plugin_api
* newrb
)
29 void get_cfg_filename(char* buf
, int buf_len
, const char* filename
)
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
);
42 cfg_rb
->strcat(s
, filename
);
46 int configfile_save(const char *filename
, struct configdata
*cfg
,
47 int num_items
, int version
)
53 get_cfg_filename(buf
, MAX_PATH
, filename
);
54 fd
= cfg_rb
->creat(buf
);
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
++) {
64 /* pre-allocate 10 bytes for INT */
65 cfg_rb
->fdprintf(fd
, "%s: %10d\n",
71 cfg_rb
->fdprintf(fd
, "%s: %s\n",
73 cfg
[i
].values
[*cfg
[i
].val
]);
77 cfg_rb
->fdprintf(fd
, "%s: %s\n",
89 int configfile_load(const char *filename
, struct configdata
*cfg
,
90 int num_items
, int min_version
)
97 int file_version
= -1;
100 get_cfg_filename(buf
, MAX_PATH
, filename
);
101 fd
= cfg_rb
->open(buf
, O_RDONLY
);
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
) {
117 for(i
= 0;i
< num_items
;i
++) {
118 if(!cfg_rb
->strcmp(cfg
[i
].name
, name
)) {
119 switch(cfg
[i
].type
) {
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
)
128 for(j
= 0;j
< cfg
[i
].max
;j
++) {
129 if(!cfg_rb
->strcmp(cfg
[i
].values
[j
], val
)) {
136 cfg_rb
->strncpy(cfg
[i
].string
, val
, cfg
[i
].max
);
147 int configfile_get_value(const char* filename
, const char* name
)
154 get_cfg_filename(buf
, MAX_PATH
, filename
);
155 fd
= cfg_rb
->open(buf
, O_RDONLY
);
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
))
165 return cfg_rb
->atoi(pval
);
173 int configfile_update_entry(const char* filename
, const char* name
, int val
)
184 /* open the current config file */
185 get_cfg_filename(path
, MAX_PATH
, filename
);
186 fd
= cfg_rb
->open(path
, O_RDWR
);
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
);
194 if(!cfg_rb
->strcmp(name
, pname
))
197 cfg_rb
->lseek(fd
, pos
, SEEK_SET
);
198 /* pre-allocate 10 bytes for INT */
199 cfg_rb
->fdprintf(fd
, "%s: %10d\n", pname
, val
);
205 /* if (name/val) is a new entry just append to file */
207 /* pre-allocate 10 bytes for INT */
208 cfg_rb
->fdprintf(fd
, "%s: %10d\n", name
, val
);