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 void get_cfg_filename(char* buf
, int buf_len
, const char* filename
)
27 rb
->strcpy(buf
, rb
->plugin_get_current_filename());
28 s
= rb
->strrchr(buf
, '/');
29 if (!s
) /* should never happen */
31 rb
->snprintf(buf
, buf_len
, PLUGIN_DIR
"/%s", filename
);
37 rb
->strcat(s
, filename
);
41 int configfile_save(const char *filename
, struct configdata
*cfg
,
42 int num_items
, int version
)
48 get_cfg_filename(buf
, MAX_PATH
, filename
);
53 /* pre-allocate 10 bytes for INT */
54 rb
->fdprintf(fd
, "file version: %10d\n", version
);
56 for(i
= 0;i
< num_items
;i
++) {
59 /* pre-allocate 10 bytes for INT */
60 rb
->fdprintf(fd
, "%s: %10d\n",
66 rb
->fdprintf(fd
, "%s: %10d\n",
72 rb
->fdprintf(fd
, "%s: %s\n",
74 cfg
[i
].values
[*cfg
[i
].int_p
]);
78 rb
->fdprintf(fd
, "%s: %s\n",
90 int configfile_load(const char *filename
, struct configdata
*cfg
,
91 int num_items
, int min_version
)
98 int file_version
= -1;
101 get_cfg_filename(buf
, MAX_PATH
, filename
);
102 fd
= rb
->open(buf
, O_RDONLY
);
106 while(rb
->read_line(fd
, buf
, MAX_PATH
) > 0) {
107 rb
->settings_parseline(buf
, &name
, &val
);
109 /* Bail out if the file version is too old */
110 if(!rb
->strcmp("file version", name
)) {
111 file_version
= rb
->atoi(val
);
112 if(file_version
< min_version
) {
118 for(i
= 0;i
< num_items
;i
++) {
119 if(!rb
->strcmp(cfg
[i
].name
, name
)) {
120 switch(cfg
[i
].type
) {
123 /* Only set it if it's within range */
124 if(tmp
>= cfg
[i
].min
&& tmp
<= cfg
[i
].max
)
130 *cfg
[i
].bool_p
= (bool)tmp
;
134 for(j
= 0;j
< cfg
[i
].max
;j
++) {
135 if(!rb
->strcmp(cfg
[i
].values
[j
], val
)) {
142 rb
->strncpy(cfg
[i
].string
, val
, cfg
[i
].max
);
153 int configfile_get_value(const char* filename
, const char* name
)
160 get_cfg_filename(buf
, MAX_PATH
, filename
);
161 fd
= rb
->open(buf
, O_RDONLY
);
165 while(rb
->read_line(fd
, buf
, MAX_PATH
) > 0)
167 rb
->settings_parseline(buf
, &pname
, &pval
);
168 if(!rb
->strcmp(name
, pname
))
171 return rb
->atoi(pval
);
179 int configfile_update_entry(const char* filename
, const char* name
, int val
)
190 /* open the current config file */
191 get_cfg_filename(path
, MAX_PATH
, filename
);
192 fd
= rb
->open(path
, O_RDWR
);
196 /* read in the current stored settings */
197 while((line_len
= rb
->read_line(fd
, buf
, 256)) > 0)
199 rb
->settings_parseline(buf
, &pname
, &pval
);
200 if(!rb
->strcmp(name
, pname
))
203 rb
->lseek(fd
, pos
, SEEK_SET
);
204 /* pre-allocate 10 bytes for INT */
205 rb
->fdprintf(fd
, "%s: %10d\n", pname
, val
);
211 /* if (name/val) is a new entry just append to file */
213 /* pre-allocate 10 bytes for INT */
214 rb
->fdprintf(fd
, "%s: %10d\n", name
, val
);