diff options
Diffstat (limited to 'apps/plugins/lib/configfile.c')
-rw-r--r-- | apps/plugins/lib/configfile.c | 108 |
1 files changed, 108 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 | |||
22 | static struct plugin_api *cfg_rb; | ||
23 | |||
24 | void configfile_init(struct plugin_api* newrb) | ||
25 | { | ||
26 | cfg_rb = newrb; | ||
27 | } | ||
28 | |||
29 | int 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 | |||
67 | int 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 | } | ||