diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/ecore/src/lib/ecore_config/ecore_config_db.c | 296 |
1 files changed, 296 insertions, 0 deletions
diff --git a/libraries/ecore/src/lib/ecore_config/ecore_config_db.c b/libraries/ecore/src/lib/ecore_config/ecore_config_db.c new file mode 100644 index 0000000..6238958 --- /dev/null +++ b/libraries/ecore/src/lib/ecore_config/ecore_config_db.c | |||
@@ -0,0 +1,296 @@ | |||
1 | #ifdef HAVE_CONFIG_H | ||
2 | # include <config.h> | ||
3 | #endif | ||
4 | |||
5 | #include <stdlib.h> | ||
6 | #include <stdio.h> | ||
7 | #include <string.h> | ||
8 | #include <limits.h> | ||
9 | |||
10 | #include <sys/param.h> | ||
11 | #include <sys/types.h> | ||
12 | #include <sys/stat.h> | ||
13 | #include <unistd.h> | ||
14 | #include <locale.h> | ||
15 | |||
16 | #include <Eet.h> | ||
17 | |||
18 | #include "Ecore_Config.h" | ||
19 | #include "ecore_config_private.h" | ||
20 | #include "ecore_config_util.h" | ||
21 | |||
22 | struct _Ecore_Config_DB_File | ||
23 | { | ||
24 | Eet_File *ef; | ||
25 | }; | ||
26 | |||
27 | Ecore_Config_DB_File * | ||
28 | _ecore_config_db_open_read(const char *file) | ||
29 | { | ||
30 | Eet_File *ef; | ||
31 | Ecore_Config_DB_File *db; | ||
32 | |||
33 | eet_init(); | ||
34 | db = malloc(sizeof(Ecore_Config_DB_File)); | ||
35 | if (!db) return NULL; | ||
36 | ef = eet_open((char*)file, EET_FILE_MODE_READ); | ||
37 | if (!ef) | ||
38 | { | ||
39 | free(db); | ||
40 | return NULL; | ||
41 | } | ||
42 | db->ef = ef; | ||
43 | return db; | ||
44 | } | ||
45 | |||
46 | Ecore_Config_DB_File * | ||
47 | _ecore_config_db_open_write(const char *file) | ||
48 | { | ||
49 | Eet_File *ef; | ||
50 | Ecore_Config_DB_File *db; | ||
51 | |||
52 | eet_init(); | ||
53 | db = malloc(sizeof(Ecore_Config_DB_File)); | ||
54 | if (!db) return NULL; | ||
55 | ef = eet_open((char*)file, EET_FILE_MODE_WRITE); | ||
56 | if (!ef) | ||
57 | { | ||
58 | free(db); | ||
59 | return NULL; | ||
60 | } | ||
61 | db->ef = ef; | ||
62 | return db; | ||
63 | } | ||
64 | |||
65 | void | ||
66 | _ecore_config_db_close(Ecore_Config_DB_File *db) | ||
67 | { | ||
68 | eet_close(db->ef); | ||
69 | free(db); | ||
70 | eet_shutdown(); | ||
71 | } | ||
72 | |||
73 | char ** | ||
74 | _ecore_config_db_keys_get(Ecore_Config_DB_File *db, int *num_ret) | ||
75 | { | ||
76 | char **keys; | ||
77 | int key_count; | ||
78 | int i; | ||
79 | |||
80 | keys = eet_list(db->ef, (char*)"*", &key_count); | ||
81 | if (!keys) | ||
82 | { | ||
83 | *num_ret = 0; | ||
84 | return NULL; | ||
85 | } | ||
86 | /* make keys freeable - this is safe to do */ | ||
87 | for (i = 0; i < key_count; i++) keys[i] = strdup(keys[i]); | ||
88 | *num_ret = key_count; | ||
89 | return keys; | ||
90 | } | ||
91 | |||
92 | Ecore_Config_Type | ||
93 | _ecore_config_db_key_type_get(Ecore_Config_DB_File *db, const char *key) | ||
94 | { | ||
95 | char *data; | ||
96 | int size; | ||
97 | |||
98 | data = eet_read(db->ef, (char*)key, &size); | ||
99 | if (data) | ||
100 | { | ||
101 | if (size <= 2) | ||
102 | { | ||
103 | free(data); | ||
104 | return ECORE_CONFIG_NIL; | ||
105 | } | ||
106 | if (data[size - 1] != 0) | ||
107 | { | ||
108 | free(data); | ||
109 | return ECORE_CONFIG_NIL; | ||
110 | } | ||
111 | return (Ecore_Config_Type) data[0]; | ||
112 | } | ||
113 | return ECORE_CONFIG_NIL; | ||
114 | } | ||
115 | |||
116 | int | ||
117 | _ecore_config_db_read(Ecore_Config_DB_File *db, const char *key) | ||
118 | { | ||
119 | char *data, *value; | ||
120 | int size; | ||
121 | Ecore_Config_Type type; | ||
122 | |||
123 | data = eet_read(db->ef, (char*)key, &size); | ||
124 | if (data) | ||
125 | { | ||
126 | int l; | ||
127 | char *prev_locale; | ||
128 | |||
129 | if (size <= 2) | ||
130 | { | ||
131 | free(data); | ||
132 | return 0; | ||
133 | } | ||
134 | if (data[size - 1] != 0) | ||
135 | { | ||
136 | free(data); | ||
137 | return 0; | ||
138 | } | ||
139 | /* "type" NIL 1242 NIL */ | ||
140 | l = strlen(data); | ||
141 | if (l >= (size - 1)) | ||
142 | { | ||
143 | free(data); | ||
144 | return 0; | ||
145 | } | ||
146 | |||
147 | type = data[0]; | ||
148 | value = data + l + 1; | ||
149 | |||
150 | switch (type) | ||
151 | { | ||
152 | case ECORE_CONFIG_INT: | ||
153 | case ECORE_CONFIG_BLN: | ||
154 | { | ||
155 | int tmp; | ||
156 | prev_locale = setlocale(LC_NUMERIC, "C"); | ||
157 | tmp = atoi(value); | ||
158 | if (prev_locale) setlocale(LC_NUMERIC, prev_locale); | ||
159 | |||
160 | ecore_config_typed_set(key, (void *)&tmp, type); | ||
161 | break; | ||
162 | } | ||
163 | case ECORE_CONFIG_FLT: | ||
164 | { | ||
165 | float tmp; | ||
166 | prev_locale = setlocale(LC_NUMERIC, "C"); | ||
167 | tmp = atof(value); | ||
168 | if (prev_locale) setlocale(LC_NUMERIC, prev_locale); | ||
169 | |||
170 | ecore_config_typed_set(key, (void *)&tmp, type); | ||
171 | break; | ||
172 | } | ||
173 | case ECORE_CONFIG_RGB: | ||
174 | ecore_config_argbstr_set(key, value); | ||
175 | break; | ||
176 | case ECORE_CONFIG_STR: | ||
177 | case ECORE_CONFIG_THM: | ||
178 | ecore_config_typed_set(key, (void *)value, type); | ||
179 | break; | ||
180 | case ECORE_CONFIG_SCT: | ||
181 | INF("loading struct %s", key); | ||
182 | break; | ||
183 | default: | ||
184 | WRN("Type %d not handled", type); | ||
185 | } | ||
186 | free(data); | ||
187 | return 1; | ||
188 | } | ||
189 | return 0; | ||
190 | } | ||
191 | |||
192 | /* | ||
193 | void * | ||
194 | _ecore_config_db_key_data_get(Ecore_Config_DB_File *db, const char *key, int *size_ret) | ||
195 | { | ||
196 | char *data; | ||
197 | int size; | ||
198 | |||
199 | data = eet_read(db->ef, (char*)key, &size); | ||
200 | if (data) | ||
201 | { | ||
202 | int l; | ||
203 | char *dat; | ||
204 | |||
205 | if (size <= 2) | ||
206 | { | ||
207 | free(data); | ||
208 | return NULL; | ||
209 | } | ||
210 | if (data[size - 1] != 0) | ||
211 | { | ||
212 | free(data); | ||
213 | return NULL; | ||
214 | } | ||
215 | * "type" NIL data_goes_here NIL * | ||
216 | l = strlen(data); | ||
217 | if (l >= (size - 1)) | ||
218 | { | ||
219 | free(data); | ||
220 | return NULL; | ||
221 | } | ||
222 | dat = malloc(size - (l + 2)); | ||
223 | memcpy(dat, data + l + 1, size - (l + 2)); | ||
224 | free(data); | ||
225 | *size_ret = size - (l + 2); | ||
226 | return dat; | ||
227 | } | ||
228 | return NULL; | ||
229 | }*/ | ||
230 | |||
231 | void | ||
232 | _ecore_config_db_write(Ecore_Config_DB_File *db, Ecore_Config_Prop *e) | ||
233 | { | ||
234 | char *prev_locale= NULL; | ||
235 | char *val = NULL; | ||
236 | char *r = NULL; | ||
237 | int num; | ||
238 | |||
239 | prev_locale = setlocale(LC_NUMERIC, "C"); | ||
240 | |||
241 | switch (e->type) | ||
242 | { | ||
243 | case ECORE_CONFIG_INT: | ||
244 | esprintf(&val, "%i", _ecore_config_int_get(e)); | ||
245 | break; | ||
246 | case ECORE_CONFIG_BLN: | ||
247 | esprintf(&val, "%i", _ecore_config_boolean_get(e)); | ||
248 | break; | ||
249 | case ECORE_CONFIG_FLT: | ||
250 | esprintf(&val, "%16.16f", _ecore_config_float_get(e)); | ||
251 | break; | ||
252 | case ECORE_CONFIG_STR: | ||
253 | val = _ecore_config_string_get(e); | ||
254 | break; | ||
255 | case ECORE_CONFIG_THM: | ||
256 | val = _ecore_config_theme_get(e); | ||
257 | break; | ||
258 | case ECORE_CONFIG_RGB: | ||
259 | val = _ecore_config_argbstr_get(e); | ||
260 | break; | ||
261 | default: | ||
262 | WRN("Type %d not handled", e->type); | ||
263 | } | ||
264 | |||
265 | if (prev_locale) | ||
266 | { | ||
267 | setlocale(LC_NUMERIC, prev_locale); | ||
268 | } | ||
269 | |||
270 | if(val) | ||
271 | { | ||
272 | num = esprintf(&r, "%c%c%s%c", (char) e->type, 0, val, 0); | ||
273 | if(num) | ||
274 | eet_write(db->ef, e->key, r, num, 1); | ||
275 | free(r); | ||
276 | } | ||
277 | |||
278 | free(val); | ||
279 | } | ||
280 | /* | ||
281 | void | ||
282 | _ecore_config_db_key_data_set(Ecore_Config_DB_File *db, const char *key, void *data, int data_size) | ||
283 | { | ||
284 | char *buf; | ||
285 | int num; | ||
286 | |||
287 | num = 1 + 1 + data_size + 1; | ||
288 | buf = malloc(num); | ||
289 | if (!buf) return; | ||
290 | buf[0] = (char) ECORE_CONFIG_BIN; | ||
291 | buf[1] = 0; | ||
292 | memcpy(buf + 2, data, data_size); | ||
293 | buf[num - 1] = 0; | ||
294 | eet_write(db->ef, (char*)key, buf, num, 1); | ||
295 | free(buf); | ||
296 | }*/ | ||