Mercurial > code
comparison ini.c @ 85:8cea08140a71
Fix comment issue and switch ini_value_dispatch to allow NULL values
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 19 Nov 2011 20:51:18 +0100 |
parents | 89e4d8d63bfa |
children | ff1fcb6e385f |
comparison
equal
deleted
inserted
replaced
84:89e4d8d63bfa | 85:8cea08140a71 |
---|---|
81 #define SKIP_SPACES(lp) while (isspace(*lp) && *lp != '\0') ++lp | 81 #define SKIP_SPACES(lp) while (isspace(*lp) && *lp != '\0') ++lp |
82 #define WARN(file, fmt, ...) \ | 82 #define WARN(file, fmt, ...) \ |
83 if (I_VERBOSE((file))) \ | 83 if (I_VERBOSE((file))) \ |
84 fprintf(stderr, fmt, __VA_ARGS__) | 84 fprintf(stderr, fmt, __VA_ARGS__) |
85 | 85 |
86 /* -------------------------------------------------------- */ | 86 /* -------------------------------------------------------- |
87 /* public functions */ | 87 * public functions |
88 /* -------------------------------------------------------- */ | 88 * -------------------------------------------------------- */ |
89 | 89 |
90 struct ini_config * | 90 struct ini_config * |
91 ini_load(const char *path, int flags) | 91 ini_load(const char *path, int flags) |
92 { | 92 { |
93 FILE *fp; | 93 FILE *fp; |
300 | 300 |
301 /* Skip the section if does not exists in the config */ | 301 /* Skip the section if does not exists in the config */ |
302 if (section == NULL) | 302 if (section == NULL) |
303 continue; | 303 continue; |
304 | 304 |
305 if ((value = ini_get_option(section, hdrs[i].option)) != NULL) | 305 value = ini_get_option(section, hdrs[i].option); |
306 hdrs[i].handler(hdrs[i].dst, value, hdrs[i].userdata); | 306 hdrs[i].handler(hdrs[i].dst, value, hdrs[i].userdata); |
307 } | 307 } |
308 } | 308 } |
309 | 309 |
310 void | 310 void |
311 ini_convert_bool(void *dst, const char *value, void *dummy) | 311 ini_convert_bool(void *dst, const char *value, void *dummy) |
312 { | 312 { |
313 char *p = dst; | 313 char *p = dst; |
314 | |
315 if (value == NULL) | |
316 return ; | |
314 | 317 |
315 if (strcmp(value, "yes") == 0 || | 318 if (strcmp(value, "yes") == 0 || |
316 strcmp(value, "true") == 0 || | 319 strcmp(value, "true") == 0 || |
317 strcmp(value, "1") == 0) | 320 strcmp(value, "1") == 0) |
318 *p = 1; | 321 *p = 1; |
325 void | 328 void |
326 ini_convert_int(void *dst, const char *value, void *dummy) | 329 ini_convert_int(void *dst, const char *value, void *dummy) |
327 { | 330 { |
328 int *p = dst; | 331 int *p = dst; |
329 | 332 |
333 if (value == NULL) | |
334 return ; | |
335 | |
330 *p = (int) strtol(value, NULL, 10); | 336 *p = (int) strtol(value, NULL, 10); |
331 (void)dummy; | 337 (void)dummy; |
332 } | 338 } |
333 | 339 |
334 void | 340 void |
335 ini_convert_short(void *dst, const char *value, void *dummy) | 341 ini_convert_short(void *dst, const char *value, void *dummy) |
336 { | 342 { |
337 short *p = dst; | 343 short *p = dst; |
338 | 344 |
345 if (value == NULL) | |
346 return ; | |
347 | |
339 *p = (short) strtol(value, NULL, 10); | 348 *p = (short) strtol(value, NULL, 10); |
340 (void)dummy; | 349 (void)dummy; |
341 } | 350 } |
342 | 351 |
343 void | 352 void |
344 ini_convert_string(void *dst, const char *value, void *dummy) | 353 ini_convert_string(void *dst, const char *value, void *dummy) |
345 { | 354 { |
346 char **p = dst; | 355 char **p = dst; |
347 | 356 |
348 *p = strdup(value); | 357 if (value == NULL) |
358 *p = NULL; | |
359 else | |
360 *p = strdup(value); | |
361 | |
349 (void)dummy; | 362 (void)dummy; |
350 } | 363 } |
351 | 364 |
352 /* | 365 /* |
353 * Return the last error or "No error" if there is not error at all. | 366 * Return the last error or "No error" if there is not error at all. |
600 SKIP_SPACES(endValue); | 613 SKIP_SPACES(endValue); |
601 | 614 |
602 /* Find end of option value */ | 615 /* Find end of option value */ |
603 token = *endValue; | 616 token = *endValue; |
604 if (token == '\'' || token == '"') { | 617 if (token == '\'' || token == '"') { |
605 for (*lp = ++endValue; *endValue != token && *endValue != '\0'; ++endValue) | 618 for (*lp = ++endValue; *endValue != token && |
619 *endValue != '\0'; ++endValue) | |
606 continue; | 620 continue; |
607 | 621 |
608 length = endValue - *lp; | 622 length = endValue - *lp; |
609 | 623 |
610 /* Correctly closed */ | 624 /* Correctly closed */ |
611 if (token != '\0' && *endValue == token) | 625 if (token != '\0' && *endValue == token) |
612 ++ endValue; | 626 ++ endValue; |
613 else | 627 else |
614 WARN(conf, "line %d: missing '%c'\n", conf->lineno, token); | 628 WARN(conf, "line %d: missing '%c'\n", conf->lineno, token); |
615 } else { | 629 } else { |
616 for (*lp = endValue; !isspace(*endValue) && *endValue != '\0'; ++endValue) | 630 for (*lp = endValue; !isspace(*endValue) && *endValue != '\0' && |
631 *endValue != '#' && *endValue != ';'; ++endValue) | |
617 continue; | 632 continue; |
618 | 633 |
619 length = endValue - *lp; | 634 length = endValue - *lp; |
620 } | 635 } |
621 | 636 |