Rename h100 specific screenshots so they are used again
[kugel-rb.git] / apps / plugins / lib / configfile.c
blob5e1e776f3988a524bd2ddda76d61fc7a511059ac
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #include "plugin.h"
22 #include "configfile.h"
24 static void get_cfg_filename(char* buf, int buf_len, const char* filename)
26 char *s;
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);
33 else
35 s++;
36 *s = '\0';
37 rb->strcat(s, filename);
41 int configfile_save(const char *filename, struct configdata *cfg,
42 int num_items, int version)
44 int fd;
45 int i;
46 char buf[MAX_PATH];
48 get_cfg_filename(buf, MAX_PATH, filename);
49 fd = rb->creat(buf);
50 if(fd < 0)
51 return fd*10 - 1;
53 /* pre-allocate 10 bytes for INT */
54 rb->fdprintf(fd, "file version: %10d\n", version);
56 for(i = 0;i < num_items;i++) {
57 switch(cfg[i].type) {
58 case TYPE_INT:
59 /* pre-allocate 10 bytes for INT */
60 rb->fdprintf(fd, "%s: %10d\n",
61 cfg[i].name,
62 *cfg[i].int_p);
63 break;
65 case TYPE_BOOL:
66 rb->fdprintf(fd, "%s: %10d\n",
67 cfg[i].name,
68 (int)*cfg[i].bool_p);
69 break;
71 case TYPE_ENUM:
72 rb->fdprintf(fd, "%s: %s\n",
73 cfg[i].name,
74 cfg[i].values[*cfg[i].int_p]);
75 break;
77 case TYPE_STRING:
78 rb->fdprintf(fd, "%s: %s\n",
79 cfg[i].name,
80 cfg[i].string);
81 break;
86 rb->close(fd);
87 return 0;
90 int configfile_load(const char *filename, struct configdata *cfg,
91 int num_items, int min_version)
93 int fd;
94 int i, j;
95 char *name;
96 char *val;
97 char buf[MAX_PATH];
98 int file_version = -1;
99 int tmp;
101 get_cfg_filename(buf, MAX_PATH, filename);
102 fd = rb->open(buf, O_RDONLY);
103 if(fd < 0)
104 return fd*10 - 1;
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) {
113 rb->close(fd);
114 return -1;
118 for(i = 0;i < num_items;i++) {
119 if(!rb->strcmp(cfg[i].name, name)) {
120 switch(cfg[i].type) {
121 case TYPE_INT:
122 tmp = rb->atoi(val);
123 /* Only set it if it's within range */
124 if(tmp >= cfg[i].min && tmp <= cfg[i].max)
125 *cfg[i].int_p = tmp;
126 break;
128 case TYPE_BOOL:
129 tmp = rb->atoi(val);
130 *cfg[i].bool_p = (bool)tmp;
131 break;
133 case TYPE_ENUM:
134 for(j = 0;j < cfg[i].max;j++) {
135 if(!rb->strcmp(cfg[i].values[j], val)) {
136 *cfg[i].int_p = j;
139 break;
141 case TYPE_STRING:
142 rb->strncpy(cfg[i].string, val, cfg[i].max);
143 break;
149 rb->close(fd);
150 return 0;
153 int configfile_get_value(const char* filename, const char* name)
155 int fd;
156 char *pname;
157 char *pval;
158 char buf[MAX_PATH];
160 get_cfg_filename(buf, MAX_PATH, filename);
161 fd = rb->open(buf, O_RDONLY);
162 if(fd < 0)
163 return -1;
165 while(rb->read_line(fd, buf, MAX_PATH) > 0)
167 rb->settings_parseline(buf, &pname, &pval);
168 if(!rb->strcmp(name, pname))
170 rb->close(fd);
171 return rb->atoi(pval);
175 rb->close(fd);
176 return -1;
179 int configfile_update_entry(const char* filename, const char* name, int val)
181 int fd;
182 char *pname;
183 char *pval;
184 char path[MAX_PATH];
185 char buf[256];
186 int found = 0;
187 int line_len = 0;
188 int pos = 0;
190 /* open the current config file */
191 get_cfg_filename(path, MAX_PATH, filename);
192 fd = rb->open(path, O_RDWR);
193 if(fd < 0)
194 return -1;
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))
202 found = 1;
203 rb->lseek(fd, pos, SEEK_SET);
204 /* pre-allocate 10 bytes for INT */
205 rb->fdprintf(fd, "%s: %10d\n", pname, val);
206 break;
208 pos += line_len;
211 /* if (name/val) is a new entry just append to file */
212 if (found == 0)
213 /* pre-allocate 10 bytes for INT */
214 rb->fdprintf(fd, "%s: %10d\n", name, val);
216 rb->close(fd);
218 return found;