1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
|
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#undef alloca
#ifdef HAVE_ALLOCA_H
# include <alloca.h>
#elif defined __GNUC__
# define alloca __builtin_alloca
#elif defined _AIX
# define alloca __alloca
#elif defined _MSC_VER
# include <malloc.h>
# define alloca _alloca
#else
# include <stddef.h>
# ifdef __cplusplus
extern "C"
# endif
void *alloca (size_t);
#endif
#include <ctype.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/mman.h>
#include <Ecore_File.h>
/* define macros and variable for using the eina logging system */
#define EFREET_MODULE_LOG_DOM _efreet_ini_log_dom
static int _efreet_ini_log_dom = -1;
#include "Efreet.h"
#include "efreet_private.h"
static Eina_Hash *efreet_ini_parse(const char *file);
static const char *efreet_ini_unescape(const char *str) EINA_ARG_NONNULL(1);
static Eina_Bool
efreet_ini_section_save(const Eina_Hash *hash, const void *key, void *data, void *fdata);
static Eina_Bool
efreet_ini_value_save(const Eina_Hash *hash, const void *key, void *data, void *fdata);
/**
* @internal
* @return Returns > 0 on success or 0 on failure
* @brief Initialize the Ini parser subsystem
*/
int
efreet_ini_init(void)
{
_efreet_ini_log_dom = eina_log_domain_register
("efreet_ini", EFREET_DEFAULT_LOG_COLOR);
if (_efreet_ini_log_dom < 0)
{
EINA_LOG_ERR("Efreet: Could not create a log domain for efreet_ini");
return 0;
}
return 1;
}
/**
* @internal
* @returns the number of initializations left for this system
* @brief Attempts to shut down the subsystem if nothing else is using it
*/
void
efreet_ini_shutdown(void)
{
eina_log_domain_unregister(_efreet_ini_log_dom);
_efreet_ini_log_dom = -1;
}
EAPI Efreet_Ini *
efreet_ini_new(const char *file)
{
Efreet_Ini *ini;
ini = NEW(Efreet_Ini, 1);
if (!ini) return NULL;
/* This can validly be NULL at the moment as _parse() will return NULL
* if the input file doesn't exist. Should we change _parse() to create
* the hash and only return NULL on failed parse? */
ini->data = efreet_ini_parse(file);
return ini;
}
/**
* @internal
* @param file The file to parse
* @return Returns an Eina_Hash with the contents of @a file, or NULL if the
* file fails to parse or if the file doesn't exist
* @brief Parses the ini file @a file into an Eina_Hash
*/
static Eina_Hash *
efreet_ini_parse(const char *file)
{
const char *buffer, *line_start;
FILE *f;
Eina_Hash *data, *section = NULL;
struct stat file_stat;
int line_length, left;
if (!file) return NULL;
f = fopen(file, "rb");
if (!f) return NULL;
if (fstat(fileno(f), &file_stat) || (file_stat.st_size < 1))
{
fclose(f);
return NULL;
}
if (!S_ISREG(file_stat.st_mode)) /* if not a regular file - close */
{
fclose(f);
return NULL;
}
left = file_stat.st_size;
/* let's make mmap safe and just get 0 pages for IO erro */
eina_mmap_safety_enabled_set(EINA_TRUE);
buffer = mmap(NULL, left, PROT_READ, MAP_SHARED, fileno(f), 0);
if (buffer == MAP_FAILED)
{
fclose(f);
return NULL;
}
data = eina_hash_string_small_new(EINA_FREE_CB(eina_hash_free));
line_start = buffer;
while (left > 0)
{
int sep;
/* find the end of line */
for (line_length = 0;
(line_length < left) &&
(line_start[line_length] != '\n'); line_length++)
;
/* check for all white space */
while (isspace(line_start[0]) && (line_length > 0))
{
line_start++;
line_length--;
}
/* skip empty lines and comments */
if ((line_length == 0) || (line_start[0] == '\r') ||
(line_start[0] == '\n') || (line_start[0] == '#') ||
(line_start[0] == '\0'))
goto next_line;
/* new section */
if (line_start[0] == '[')
{
int header_length;
/* find the ']' */
for (header_length = 1;
(header_length < line_length) &&
(line_start[header_length] != ']'); ++header_length)
;
if (line_start[header_length] == ']')
{
const char *header;
header = alloca(header_length * sizeof(unsigned char));
if (!header) goto next_line;
memcpy((char*)header, line_start + 1, header_length - 1);
((char*)header)[header_length - 1] = '\0';
section = eina_hash_string_small_new(EINA_FREE_CB(eina_stringshare_del));
eina_hash_del_by_key(data, header);
// if (old) INF("[efreet] Warning: duplicate section '%s' "
// "in file '%s'", header, file);
eina_hash_add(data, header, section);
}
else
{
/* invalid file - skip line? or refuse to parse file? */
/* just printf for now till we figure out what to do */
// ERR("Invalid file (%s) (missing ] on group name)", file);
}
goto next_line;
}
if (!section)
{
INF("Invalid file (%s) (missing section)", file);
goto error;
}
/* find for '=' */
for (sep = 0; (sep < line_length) && (line_start[sep] != '='); ++sep)
;
if (sep < line_length && line_start[sep] == '=')
{
char *key, *value;
int key_end, value_start, value_end;
/* trim whitespace from end of key */
for (key_end = sep - 1;
(key_end > 0) && isspace(line_start[key_end]); --key_end)
;
if (!isspace(line_start[key_end])) key_end++;
/* trim whitespace from start of value */
for (value_start = sep + 1;
(value_start < line_length) &&
isspace(line_start[value_start]); ++value_start)
;
/* trim \n off of end of value */
for (value_end = line_length;
(value_end > value_start) &&
((line_start[value_end] == '\n') ||
(line_start[value_end] == '\r')); --value_end)
;
if (line_start[value_end] != '\n'
&& line_start[value_end] != '\r'
&& value_end < line_length)
value_end++;
/* make sure we have a key. blank values are allowed */
if (key_end <= 0)
{
/* invalid file... */
// INF("Invalid file (%s) (invalid key=value pair)", file);
goto next_line;
}
key = alloca(key_end + 1);
value = alloca(value_end - value_start + 1);
if (!key || !value) goto next_line;
memcpy(key, line_start, key_end);
key[key_end] = '\0';
memcpy(value, line_start + value_start,
value_end - value_start);
value[value_end - value_start] = '\0';
eina_hash_del_by_key(section, key);
eina_hash_add(section, key, efreet_ini_unescape(value));
}
else
{
/* invalid file... */
INF("Invalid file (%s) (missing = from key=value pair)", file);
goto error;
}
next_line:
left -= line_length + 1;
line_start += line_length + 1;
}
munmap((char*) buffer, file_stat.st_size);
fclose(f);
#if 0
if (!eina_hash_population(data))
{
eina_hash_free(data);
return NULL;
}
#endif
return data;
error:
if (data) eina_hash_free(data);
return NULL;
}
EAPI void
efreet_ini_free(Efreet_Ini *ini)
{
if (!ini) return;
IF_FREE_HASH(ini->data);
FREE(ini);
}
EAPI int
efreet_ini_save(Efreet_Ini *ini, const char *file)
{
char *dir;
FILE *f;
EINA_SAFETY_ON_NULL_RETURN_VAL(ini, 0);
EINA_SAFETY_ON_NULL_RETURN_VAL(ini->data, 0);
EINA_SAFETY_ON_NULL_RETURN_VAL(file, 0);
dir = ecore_file_dir_get(file);
if (!ecore_file_mkpath(dir))
{
free(dir);
return 0;
}
free(dir);
f = fopen(file, "wb");
if (!f) return 0;
eina_hash_foreach(ini->data, efreet_ini_section_save, f);
fclose(f);
return 1;
}
EAPI int
efreet_ini_section_set(Efreet_Ini *ini, const char *section)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(ini, 0);
EINA_SAFETY_ON_NULL_RETURN_VAL(ini->data, 0);
EINA_SAFETY_ON_NULL_RETURN_VAL(section, 0);
ini->section = eina_hash_find(ini->data, section);
return (ini->section ? 1 : 0);
}
EAPI void
efreet_ini_section_add(Efreet_Ini *ini, const char *section)
{
Eina_Hash *hash;
EINA_SAFETY_ON_NULL_RETURN(ini);
EINA_SAFETY_ON_NULL_RETURN(section);
if (!ini->data)
ini->data = eina_hash_string_small_new(EINA_FREE_CB(eina_hash_free));
if (eina_hash_find(ini->data, section)) return;
hash = eina_hash_string_small_new(EINA_FREE_CB(eina_stringshare_del));
eina_hash_add(ini->data, section, hash);
}
EAPI const char *
efreet_ini_string_get(Efreet_Ini *ini, const char *key)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(ini, NULL);
EINA_SAFETY_ON_NULL_RETURN_VAL(ini->section, NULL);
EINA_SAFETY_ON_NULL_RETURN_VAL(key, NULL);
return eina_hash_find(ini->section, key);
}
EAPI void
efreet_ini_string_set(Efreet_Ini *ini, const char *key, const char *value)
{
EINA_SAFETY_ON_NULL_RETURN(ini);
EINA_SAFETY_ON_NULL_RETURN(ini->section);
EINA_SAFETY_ON_NULL_RETURN(key);
eina_hash_del_by_key(ini->section, key);
eina_hash_add(ini->section, key, eina_stringshare_add(value));
}
EAPI int
efreet_ini_int_get(Efreet_Ini *ini, const char *key)
{
const char *str;
EINA_SAFETY_ON_NULL_RETURN_VAL(ini, -1);
EINA_SAFETY_ON_NULL_RETURN_VAL(ini->section, -1);
EINA_SAFETY_ON_NULL_RETURN_VAL(key, -1);
str = efreet_ini_string_get(ini, key);
if (str) return atoi(str);
return -1;
}
EAPI void
efreet_ini_int_set(Efreet_Ini *ini, const char *key, int value)
{
char str[12];
EINA_SAFETY_ON_NULL_RETURN(ini);
EINA_SAFETY_ON_NULL_RETURN(ini->section);
EINA_SAFETY_ON_NULL_RETURN(key);
snprintf(str, 12, "%d", value);
efreet_ini_string_set(ini, key, str);
}
EAPI double
efreet_ini_double_get(Efreet_Ini *ini, const char *key)
{
const char *str;
EINA_SAFETY_ON_NULL_RETURN_VAL(ini, -1);
EINA_SAFETY_ON_NULL_RETURN_VAL(ini->section, -1);
EINA_SAFETY_ON_NULL_RETURN_VAL(key, -1);
str = efreet_ini_string_get(ini, key);
if (str) return atof(str);
return -1;
}
EAPI void
efreet_ini_double_set(Efreet_Ini *ini, const char *key, double value)
{
char str[512];
size_t len;
EINA_SAFETY_ON_NULL_RETURN(ini);
EINA_SAFETY_ON_NULL_RETURN(ini->section);
EINA_SAFETY_ON_NULL_RETURN(key);
snprintf(str, 512, "%.6f", value);
len = strlen(str) - 1;
/* Strip trailing zero's */
while (str[len] == '0' && str[len - 1] != '.') str[len--] = '\0';
efreet_ini_string_set(ini, key, str);
}
EAPI unsigned int
efreet_ini_boolean_get(Efreet_Ini *ini, const char *key)
{
const char *str;
EINA_SAFETY_ON_NULL_RETURN_VAL(ini, 0);
EINA_SAFETY_ON_NULL_RETURN_VAL(ini->section, 0);
EINA_SAFETY_ON_NULL_RETURN_VAL(key, 0);
str = efreet_ini_string_get(ini, key);
if (str && !strcmp("true", str)) return 1;
return 0;
}
EAPI void
efreet_ini_boolean_set(Efreet_Ini *ini, const char *key, unsigned int value)
{
EINA_SAFETY_ON_NULL_RETURN(ini);
EINA_SAFETY_ON_NULL_RETURN(ini->section);
EINA_SAFETY_ON_NULL_RETURN(key);
if (value) efreet_ini_string_set(ini, key, "true");
else efreet_ini_string_set(ini, key, "false");
}
EAPI const char *
efreet_ini_localestring_get(Efreet_Ini *ini, const char *key)
{
const char *lang, *country, *modifier;
const char *val = NULL;
char *buf;
int maxlen = 5; /* _, @, [, ] and \0 */
int found = 0;
EINA_SAFETY_ON_NULL_RETURN_VAL(ini, NULL);
EINA_SAFETY_ON_NULL_RETURN_VAL(ini->section, NULL);
EINA_SAFETY_ON_NULL_RETURN_VAL(key, NULL);
lang = efreet_lang_get();
country = efreet_lang_country_get();
modifier = efreet_lang_modifier_get();
maxlen += strlen(key);
if (lang) maxlen += strlen(lang);
if (country) maxlen += strlen(country);
if (modifier) maxlen += strlen(modifier);
buf = alloca(maxlen);
if (lang && modifier && country)
{
snprintf(buf, maxlen, "%s[%s_%s@%s]", key, lang, country, modifier);
val = efreet_ini_string_get(ini, buf);
if (val && (*val != '\0')) found = 1;
}
if (!found && lang && country)
{
snprintf(buf, maxlen, "%s[%s_%s]", key, lang, country);
val = efreet_ini_string_get(ini, buf);
if (val && (*val != '\0')) found = 1;
}
if (!found && lang && modifier)
{
snprintf(buf, maxlen, "%s[%s@%s]", key, lang, modifier);
val = efreet_ini_string_get(ini, buf);
if (val && (*val != '\0')) found = 1;
}
if (!found && lang)
{
snprintf(buf, maxlen, "%s[%s]", key, lang);
val = efreet_ini_string_get(ini, buf);
if (val && (*val != '\0')) found = 1;
}
if (!found)
val = efreet_ini_string_get(ini, key);
return val;
}
EAPI void
efreet_ini_localestring_set(Efreet_Ini *ini, const char *key, const char *value)
{
const char *lang, *country, *modifier;
char *buf;
int maxlen = 5; /* _, @, [, ] and \0 */
EINA_SAFETY_ON_NULL_RETURN(ini);
EINA_SAFETY_ON_NULL_RETURN(ini->section);
EINA_SAFETY_ON_NULL_RETURN(key);
lang = efreet_lang_get();
country = efreet_lang_country_get();
modifier = efreet_lang_modifier_get();
maxlen += strlen(key);
if (lang) maxlen += strlen(lang);
if (country) maxlen += strlen(country);
if (modifier) maxlen += strlen(modifier);
buf = alloca(maxlen);
if (lang && modifier && country)
snprintf(buf, maxlen, "%s[%s_%s@%s]", key, lang, country, modifier);
else if (lang && country)
snprintf(buf, maxlen, "%s[%s_%s]", key, lang, country);
else if (lang && modifier)
snprintf(buf, maxlen, "%s[%s@%s]", key, lang, modifier);
else if (lang)
snprintf(buf, maxlen, "%s[%s]", key, lang);
else
return;
efreet_ini_string_set(ini, buf, value);
}
EAPI void
efreet_ini_key_unset(Efreet_Ini *ini, const char *key)
{
EINA_SAFETY_ON_NULL_RETURN(ini);
EINA_SAFETY_ON_NULL_RETURN(ini->section);
EINA_SAFETY_ON_NULL_RETURN(key);
eina_hash_del_by_key(ini->section, key);
}
/**
* @param str The string to unescape
* @return An allocated unescaped string
* @brief Unescapes backslash escapes in a string
*/
static const char *
efreet_ini_unescape(const char *str)
{
char *buf, *dest;
const char *p;
if (!strchr(str, '\\')) return eina_stringshare_add(str);
buf = alloca(strlen(str) + 1);
p = str;
dest = buf;
while (*p)
{
if ((*p == '\\') && (p[1] != '\0'))
{
p++;
switch (*p)
{
case 's':
*(dest++) = ' ';
break;
case 'n':
*(dest++) = '\n';
break;
case 't':
*(dest++) = '\t';
break;
case 'r':
*(dest++) = '\r';
break;
case '\\':
*(dest++) = '\\';
break;
default:
(*dest++) = '\\';
(*dest++) = *p;
}
}
else
*(dest++) = *p;
p++;
}
*(dest) = '\0';
return eina_stringshare_add(buf);
}
static Eina_Bool
efreet_ini_section_save(const Eina_Hash *hash __UNUSED__, const void *key, void *value, void *fdata)
{
FILE *f = fdata;
fprintf(f, "[%s]\n", (char *)key);
eina_hash_foreach(value, efreet_ini_value_save, f);
return EINA_TRUE;
}
static Eina_Bool
efreet_ini_value_save(const Eina_Hash *hash __UNUSED__, const void *key, void *value, void *fdata)
{
FILE *f = fdata;
fprintf(f, "%s=%s\n", (char *)key, (char *)value);
return EINA_TRUE;
}
|