summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Nielsen Feltzing <linus@haxx.se>2004-07-21 13:46:42 +0000
committerLinus Nielsen Feltzing <linus@haxx.se>2004-07-21 13:46:42 +0000
commit897fb63ec8ac9b18add6a61e46807309a28668b0 (patch)
tree59e09fa3c8bfc5da8d50f0571cf7be72e099eff2
parentd1df184f0f74ed201614b17ea1d1d67cffe5773b (diff)
downloadrockbox-897fb63ec8ac9b18add6a61e46807309a28668b0.tar.gz
rockbox-897fb63ec8ac9b18add6a61e46807309a28668b0.zip
New plugin library framework for loading and saving .cfg files
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@4912 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--apps/plugins/lib/configfile.c108
-rw-r--r--apps/plugins/lib/configfile.h44
2 files changed, 152 insertions, 0 deletions
diff --git a/apps/plugins/lib/configfile.c b/apps/plugins/lib/configfile.c
new file mode 100644
index 0000000000..ffa325038c
--- /dev/null
+++ b/apps/plugins/lib/configfile.c
@@ -0,0 +1,108 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 Linus Nielsen Feltzing
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19#include "plugin.h"
20#include "configfile.h"
21
22static struct plugin_api *cfg_rb;
23
24void configfile_init(struct plugin_api* newrb)
25{
26 cfg_rb = newrb;
27}
28
29int configfile_save(char *filename, struct configdata *cfg, int num_items)
30{
31 int fd;
32 int i;
33 char buf[MAX_PATH];
34
35 cfg_rb->snprintf(buf, MAX_PATH, "/.rockbox/rocks/%s", filename);
36 fd = cfg_rb->creat(buf, 0);
37 if(fd < 0)
38 return fd*10 - 1;
39
40 for(i = 0;i < num_items;i++) {
41 switch(cfg[i].type) {
42 case TYPE_INT:
43 cfg_rb->fprintf(fd, "%s: %d\n",
44 cfg[i].name,
45 *cfg[i].val);
46 break;
47
48 case TYPE_ENUM:
49 cfg_rb->fprintf(fd, "%s: %s\n",
50 cfg[i].name,
51 cfg[i].values[*cfg[i].val]);
52 break;
53
54 case TYPE_STRING:
55 cfg_rb->fprintf(fd, "%s: %s\n",
56 cfg[i].name,
57 cfg[i].string);
58 break;
59
60 }
61 }
62
63 cfg_rb->close(fd);
64 return 0;
65}
66
67int configfile_load(char *filename, struct configdata *cfg, int num_items)
68{
69 int fd;
70 int i, j;
71 char *name;
72 char *val;
73 char buf[MAX_PATH];
74
75 cfg_rb->snprintf(buf, MAX_PATH, "/.rockbox/rocks/%s", filename);
76 fd = cfg_rb->open(buf, O_RDONLY);
77 if(fd < 0)
78 return fd*10 - 1;
79
80 while(cfg_rb->read_line(fd, buf, MAX_PATH) > 0) {
81 cfg_rb->settings_parseline(buf, &name, &val);
82
83 for(i = 0;i < num_items;i++) {
84 if(!cfg_rb->strcmp(cfg[i].name, name)) {
85 switch(cfg[i].type) {
86 case TYPE_INT:
87 *cfg[i].val = cfg_rb->atoi(val);
88 break;
89
90 case TYPE_ENUM:
91 for(j = 0;j < cfg[i].max;j++) {
92 if(!cfg_rb->strcmp(cfg[i].values[j], val)) {
93 *cfg[i].val = j;
94 }
95 }
96 break;
97
98 case TYPE_STRING:
99 cfg_rb->strncpy(cfg[i].string, val, cfg[i].max);
100 break;
101 }
102 }
103 }
104 }
105
106 cfg_rb->close(fd);
107 return 0;
108}
diff --git a/apps/plugins/lib/configfile.h b/apps/plugins/lib/configfile.h
new file mode 100644
index 0000000000..78cc8b1b6c
--- /dev/null
+++ b/apps/plugins/lib/configfile.h
@@ -0,0 +1,44 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 Linus Nielsen Feltzing
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19#ifndef CONFIGFILE_H
20#define CONFIGFILE_H
21
22#define TYPE_INT 1
23#define TYPE_ENUM 2
24#define TYPE_STRING 3
25
26struct configdata
27{
28 int type; /* See TYPE_ macros above */
29 int min; /* Min value for integers, should be 0 for enums */
30 int max; /* Max value for enums and integers,
31 buffer size for strings */
32 int *val; /* Pointer to integer/enum value,
33 NULL if the item is a string */
34 char *name; /* Pointer to the name of the item */
35 char **values; /* List of strings for enums, NULL if not enum */
36 char *string; /* Pointer to a string buffer if the item is a string,
37 NULL otherwise */
38};
39
40void configfile_init(struct plugin_api* newrb);
41int configfile_save(char *filename, struct configdata *cfg, int num_items);
42int configfile_load(char *filename, struct configdata *cfg, int num_items);
43
44#endif