blob: 4fb037cb8ed6a7a46678fc8fd8bb66470d8e1019 [file] [log] [blame]
Linus Nielsen Feltzing897fb632004-07-21 13:46:42 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 Linus Nielsen Feltzing
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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.
Linus Nielsen Feltzing897fb632004-07-21 13:46:42 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#include "plugin.h"
22#include "configfile.h"
23
Steve Bavin65265772008-05-13 09:57:56 +000024static const struct plugin_api *cfg_rb;
Linus Nielsen Feltzing897fb632004-07-21 13:46:42 +000025
Steve Bavin65265772008-05-13 09:57:56 +000026void configfile_init(const struct plugin_api* newrb)
Linus Nielsen Feltzing897fb632004-07-21 13:46:42 +000027{
28 cfg_rb = newrb;
29}
30
Bertrik Sikkena36c8f12008-05-04 13:21:07 +000031static void get_cfg_filename(char* buf, int buf_len, const char* filename)
Jonathan Gordonfda7d722007-08-06 13:42:52 +000032{
33 char *s;
34 cfg_rb->strcpy(buf, cfg_rb->plugin_get_current_filename());
35 s = cfg_rb->strrchr(buf, '/');
36 if (!s) /* should never happen */
37 {
Nils Wallménius04b34352007-09-10 09:46:36 +000038 cfg_rb->snprintf(buf, buf_len, PLUGIN_DIR "/%s", filename);
Jonathan Gordonfda7d722007-08-06 13:42:52 +000039 }
40 else
41 {
42 s++;
43 *s = '\0';
44 cfg_rb->strcat(s, filename);
45 }
46}
47
Linus Nielsen Feltzinge93aa4b2004-07-22 07:51:02 +000048int configfile_save(const char *filename, struct configdata *cfg,
49 int num_items, int version)
Linus Nielsen Feltzing897fb632004-07-21 13:46:42 +000050{
51 int fd;
52 int i;
53 char buf[MAX_PATH];
54
Jonathan Gordonfda7d722007-08-06 13:42:52 +000055 get_cfg_filename(buf, MAX_PATH, filename);
Jens Arnold67eb1542007-02-01 23:08:15 +000056 fd = cfg_rb->creat(buf);
Linus Nielsen Feltzing897fb632004-07-21 13:46:42 +000057 if(fd < 0)
58 return fd*10 - 1;
59
Robert Kuklafd3fe452007-10-09 20:42:20 +000060 /* pre-allocate 10 bytes for INT */
61 cfg_rb->fdprintf(fd, "file version: %10d\n", version);
Linus Nielsen Feltzinge93aa4b2004-07-22 07:51:02 +000062
Linus Nielsen Feltzing897fb632004-07-21 13:46:42 +000063 for(i = 0;i < num_items;i++) {
64 switch(cfg[i].type) {
65 case TYPE_INT:
Robert Kuklafd3fe452007-10-09 20:42:20 +000066 /* pre-allocate 10 bytes for INT */
67 cfg_rb->fdprintf(fd, "%s: %10d\n",
Linus Nielsen Feltzing897fb632004-07-21 13:46:42 +000068 cfg[i].name,
69 *cfg[i].val);
70 break;
71
72 case TYPE_ENUM:
Daniel Stenberg22b77012005-02-22 12:19:12 +000073 cfg_rb->fdprintf(fd, "%s: %s\n",
Linus Nielsen Feltzing897fb632004-07-21 13:46:42 +000074 cfg[i].name,
75 cfg[i].values[*cfg[i].val]);
76 break;
77
78 case TYPE_STRING:
Daniel Stenberg22b77012005-02-22 12:19:12 +000079 cfg_rb->fdprintf(fd, "%s: %s\n",
Linus Nielsen Feltzing897fb632004-07-21 13:46:42 +000080 cfg[i].name,
81 cfg[i].string);
82 break;
83
84 }
85 }
86
87 cfg_rb->close(fd);
88 return 0;
89}
90
Linus Nielsen Feltzinge93aa4b2004-07-22 07:51:02 +000091int configfile_load(const char *filename, struct configdata *cfg,
92 int num_items, int min_version)
Linus Nielsen Feltzing897fb632004-07-21 13:46:42 +000093{
94 int fd;
95 int i, j;
96 char *name;
97 char *val;
98 char buf[MAX_PATH];
Linus Nielsen Feltzinge93aa4b2004-07-22 07:51:02 +000099 int file_version = -1;
100 int tmp;
Linus Nielsen Feltzing897fb632004-07-21 13:46:42 +0000101
Jonathan Gordonfda7d722007-08-06 13:42:52 +0000102 get_cfg_filename(buf, MAX_PATH, filename);
Linus Nielsen Feltzing897fb632004-07-21 13:46:42 +0000103 fd = cfg_rb->open(buf, O_RDONLY);
104 if(fd < 0)
105 return fd*10 - 1;
106
107 while(cfg_rb->read_line(fd, buf, MAX_PATH) > 0) {
108 cfg_rb->settings_parseline(buf, &name, &val);
Linus Nielsen Feltzinge93aa4b2004-07-22 07:51:02 +0000109
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) {
114 cfg_rb->close(fd);
115 return -1;
116 }
117 }
Linus Nielsen Feltzing897fb632004-07-21 13:46:42 +0000118
119 for(i = 0;i < num_items;i++) {
120 if(!cfg_rb->strcmp(cfg[i].name, name)) {
121 switch(cfg[i].type) {
122 case TYPE_INT:
Linus Nielsen Feltzinge93aa4b2004-07-22 07:51:02 +0000123 tmp = cfg_rb->atoi(val);
124 /* Only set it if it's within range */
125 if(tmp >= cfg[i].min && tmp <= cfg[i].max)
126 *cfg[i].val = tmp;
Linus Nielsen Feltzing897fb632004-07-21 13:46:42 +0000127 break;
128
129 case TYPE_ENUM:
130 for(j = 0;j < cfg[i].max;j++) {
131 if(!cfg_rb->strcmp(cfg[i].values[j], val)) {
132 *cfg[i].val = j;
133 }
134 }
135 break;
136
137 case TYPE_STRING:
138 cfg_rb->strncpy(cfg[i].string, val, cfg[i].max);
139 break;
140 }
141 }
142 }
143 }
144
145 cfg_rb->close(fd);
146 return 0;
147}
Robert Kuklafd3fe452007-10-09 20:42:20 +0000148
149int configfile_get_value(const char* filename, const char* name)
150{
151 int fd;
152 char *pname;
153 char *pval;
154 char buf[MAX_PATH];
155
156 get_cfg_filename(buf, MAX_PATH, filename);
157 fd = cfg_rb->open(buf, O_RDONLY);
158 if(fd < 0)
Robert Kuklaa07e9392007-10-09 21:28:51 +0000159 return -1;
Robert Kuklafd3fe452007-10-09 20:42:20 +0000160
161 while(cfg_rb->read_line(fd, buf, MAX_PATH) > 0)
162 {
163 cfg_rb->settings_parseline(buf, &pname, &pval);
164 if(!cfg_rb->strcmp(name, pname))
165 {
166 cfg_rb->close(fd);
167 return cfg_rb->atoi(pval);
168 }
169 }
170
171 cfg_rb->close(fd);
172 return -1;
173}
174
175int configfile_update_entry(const char* filename, const char* name, int val)
176{
Robert Kuklaa07e9392007-10-09 21:28:51 +0000177 int fd;
178 char *pname;
179 char *pval;
180 char path[MAX_PATH];
181 char buf[256];
182 int found = 0;
183 int line_len = 0;
184 int pos = 0;
185
186 /* open the current config file */
187 get_cfg_filename(path, MAX_PATH, filename);
188 fd = cfg_rb->open(path, O_RDWR);
189 if(fd < 0)
190 return -1;
191
192 /* read in the current stored settings */
193 while((line_len = cfg_rb->read_line(fd, buf, 256)) > 0)
Robert Kuklafd3fe452007-10-09 20:42:20 +0000194 {
Robert Kuklaa07e9392007-10-09 21:28:51 +0000195 cfg_rb->settings_parseline(buf, &pname, &pval);
196 if(!cfg_rb->strcmp(name, pname))
197 {
198 found = 1;
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);
202 break;
203 }
204 pos += line_len;
Robert Kuklafd3fe452007-10-09 20:42:20 +0000205 }
Robert Kuklaa07e9392007-10-09 21:28:51 +0000206
207 /* if (name/val) is a new entry just append to file */
208 if (found == 0)
209 /* pre-allocate 10 bytes for INT */
210 cfg_rb->fdprintf(fd, "%s: %10d\n", name, val);
211
212 cfg_rb->close(fd);
213
214 return found;
Robert Kuklafd3fe452007-10-09 20:42:20 +0000215}