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
->snprintf(buf
, buf_len
, PLUGIN_DATA_DIR
"/%s", filename
);
30 rb
->strcpy(buf
, rb
->plugin_get_current_filename());
31 s
= rb
->strrchr(buf
, '/');
32 if (!s
) /* should never happen */
34 rb
->snprintf(buf
, buf_len
, PLUGIN_DATA_DIR
"/%s", filename
);
40 rb
->strcat(s
, filename
);
45 int configfile_save(const char *filename
, struct configdata
*cfg
,
46 int num_items
, int version
)
52 get_cfg_filename(buf
, MAX_PATH
, filename
);
53 fd
= rb
->creat(buf
, 0666);
57 /* pre-allocate 10 bytes for INT */
58 rb
->fdprintf(fd
, "file version: %10d\n", version
);
60 for(i
= 0;i
< num_items
;i
++) {
63 /* pre-allocate 10 bytes for INT */
64 rb
->fdprintf(fd
, "%s: %10d\n",
70 rb
->fdprintf(fd
, "%s: %10d\n",
76 rb
->fdprintf(fd
, "%s: %s\n",
78 cfg
[i
].values
[*cfg
[i
].int_p
]);
82 rb
->fdprintf(fd
, "%s: %s\n",
94 int configfile_load(const char *filename
, struct configdata
*cfg
,
95 int num_items
, int min_version
)
102 int file_version
= -1;
105 get_cfg_filename(buf
, MAX_PATH
, filename
);
106 fd
= rb
->open(buf
, O_RDONLY
);
110 while(rb
->read_line(fd
, buf
, MAX_PATH
) > 0) {
111 rb
->settings_parseline(buf
, &name
, &val
);
113 /* Bail out if the file version is too old */
114 if(!rb
->strcmp("file version", name
)) {
115 file_version
= rb
->atoi(val
);
116 if(file_version
< min_version
) {
122 for(i
= 0;i
< num_items
;i
++) {
123 if(!rb
->strcmp(cfg
[i
].name
, name
)) {
124 switch(cfg
[i
].type
) {
127 /* Only set it if it's within range */
128 if(tmp
>= cfg
[i
].min
&& tmp
<= cfg
[i
].max
)
134 *cfg
[i
].bool_p
= (bool)tmp
;
138 for(j
= 0;j
< cfg
[i
].max
;j
++) {
139 if(!rb
->strcmp(cfg
[i
].values
[j
], val
)) {
146 rb
->strlcpy(cfg
[i
].string
, val
, cfg
[i
].max
);
157 int configfile_get_value(const char* filename
, const char* name
)
164 get_cfg_filename(buf
, MAX_PATH
, filename
);
165 fd
= rb
->open(buf
, O_RDONLY
);
169 while(rb
->read_line(fd
, buf
, MAX_PATH
) > 0)
171 rb
->settings_parseline(buf
, &pname
, &pval
);
172 if(!rb
->strcmp(name
, pname
))
175 return rb
->atoi(pval
);
183 int configfile_update_entry(const char* filename
, const char* name
, int val
)
194 /* open the current config file */
195 get_cfg_filename(path
, MAX_PATH
, filename
);
196 fd
= rb
->open(path
, O_RDWR
);
200 /* read in the current stored settings */
201 while((line_len
= rb
->read_line(fd
, buf
, 256)) > 0)
203 rb
->settings_parseline(buf
, &pname
, &pval
);
204 if(!rb
->strcmp(name
, pname
))
207 rb
->lseek(fd
, pos
, SEEK_SET
);
208 /* pre-allocate 10 bytes for INT */
209 rb
->fdprintf(fd
, "%s: %10d\n", pname
, val
);
215 /* if (name/val) is a new entry just append to file */
217 /* pre-allocate 10 bytes for INT */
218 rb
->fdprintf(fd
, "%s: %10d\n", name
, val
);