1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 Linus Nielsen Feltzing
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "configfile.h"
24 static const struct plugin_api
*cfg_rb
;
26 void configfile_init(const struct plugin_api
* newrb
)
31 static void get_cfg_filename(char* buf
, int buf_len
, const char* filename
)
34 cfg_rb
->strcpy(buf
, cfg_rb
->plugin_get_current_filename());
35 s
= cfg_rb
->strrchr(buf
, '/');
36 if (!s
) /* should never happen */
38 cfg_rb
->snprintf(buf
, buf_len
, PLUGIN_DIR
"/%s", filename
);
44 cfg_rb
->strcat(s
, filename
);
48 int configfile_save(const char *filename
, struct configdata
*cfg
,
49 int num_items
, int version
)
55 get_cfg_filename(buf
, MAX_PATH
, filename
);
56 fd
= cfg_rb
->creat(buf
);
60 /* pre-allocate 10 bytes for INT */
61 cfg_rb
->fdprintf(fd
, "file version: %10d\n", version
);
63 for(i
= 0;i
< num_items
;i
++) {
66 /* pre-allocate 10 bytes for INT */
67 cfg_rb
->fdprintf(fd
, "%s: %10d\n",
73 cfg_rb
->fdprintf(fd
, "%s: %s\n",
75 cfg
[i
].values
[*cfg
[i
].val
]);
79 cfg_rb
->fdprintf(fd
, "%s: %s\n",
91 int configfile_load(const char *filename
, struct configdata
*cfg
,
92 int num_items
, int min_version
)
99 int file_version
= -1;
102 get_cfg_filename(buf
, MAX_PATH
, filename
);
103 fd
= cfg_rb
->open(buf
, O_RDONLY
);
107 while(cfg_rb
->read_line(fd
, buf
, MAX_PATH
) > 0) {
108 cfg_rb
->settings_parseline(buf
, &name
, &val
);
110 /* Bail out if the file version is too old */
111 if(!cfg_rb
->strcmp("file version", name
)) {
112 file_version
= cfg_rb
->atoi(val
);
113 if(file_version
< min_version
) {
119 for(i
= 0;i
< num_items
;i
++) {
120 if(!cfg_rb
->strcmp(cfg
[i
].name
, name
)) {
121 switch(cfg
[i
].type
) {
123 tmp
= cfg_rb
->atoi(val
);
124 /* Only set it if it's within range */
125 if(tmp
>= cfg
[i
].min
&& tmp
<= cfg
[i
].max
)
130 for(j
= 0;j
< cfg
[i
].max
;j
++) {
131 if(!cfg_rb
->strcmp(cfg
[i
].values
[j
], val
)) {
138 cfg_rb
->strncpy(cfg
[i
].string
, val
, cfg
[i
].max
);
149 int configfile_get_value(const char* filename
, const char* name
)
156 get_cfg_filename(buf
, MAX_PATH
, filename
);
157 fd
= cfg_rb
->open(buf
, O_RDONLY
);
161 while(cfg_rb
->read_line(fd
, buf
, MAX_PATH
) > 0)
163 cfg_rb
->settings_parseline(buf
, &pname
, &pval
);
164 if(!cfg_rb
->strcmp(name
, pname
))
167 return cfg_rb
->atoi(pval
);
175 int configfile_update_entry(const char* filename
, const char* name
, int val
)
186 /* open the current config file */
187 get_cfg_filename(path
, MAX_PATH
, filename
);
188 fd
= cfg_rb
->open(path
, O_RDWR
);
192 /* read in the current stored settings */
193 while((line_len
= cfg_rb
->read_line(fd
, buf
, 256)) > 0)
195 cfg_rb
->settings_parseline(buf
, &pname
, &pval
);
196 if(!cfg_rb
->strcmp(name
, pname
))
199 cfg_rb
->lseek(fd
, pos
, SEEK_SET
);
200 /* pre-allocate 10 bytes for INT */
201 cfg_rb
->fdprintf(fd
, "%s: %10d\n", pname
, val
);
207 /* if (name/val) is a new entry just append to file */
209 /* pre-allocate 10 bytes for INT */
210 cfg_rb
->fdprintf(fd
, "%s: %10d\n", name
, val
);