Mercurial > hg > th-libs
comparison th_config.c @ 457:85fa3d333556
Actually, revert the boolean changes .. meh.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 02 Jan 2018 23:09:29 +0200 |
parents | efd33accdc81 |
children | ff3ebe22f6c5 |
comparison
equal
deleted
inserted
replaced
456:1bf886fa9db5 | 457:85fa3d333556 |
---|---|
127 | 127 |
128 | 128 |
129 /* Add boolean type setting into given configuration | 129 /* Add boolean type setting into given configuration |
130 */ | 130 */ |
131 int th_cfg_add_bool(th_cfgitem_t **cfg, const char *name, | 131 int th_cfg_add_bool(th_cfgitem_t **cfg, const char *name, |
132 bool *itemData, bool defValue) | 132 BOOL *itemData, BOOL defValue) |
133 { | 133 { |
134 th_cfgitem_t *node; | 134 th_cfgitem_t *node; |
135 | 135 |
136 node = th_cfg_add(cfg, name, ITEM_bool, (void *) itemData); | 136 node = th_cfg_add(cfg, name, ITEM_BOOL, (void *) itemData); |
137 if (node == NULL) | 137 if (node == NULL) |
138 return -1; | 138 return -1; |
139 | 139 |
140 *itemData = defValue; | 140 *itemData = defValue; |
141 | 141 |
197 PM_NEXT, | 197 PM_NEXT, |
198 PM_KEYNAME, | 198 PM_KEYNAME, |
199 PM_KEYSET, | 199 PM_KEYSET, |
200 PM_STRING, | 200 PM_STRING, |
201 PM_NUMERIC, | 201 PM_NUMERIC, |
202 PM_bool, | 202 PM_BOOL, |
203 PM_SECTION, | 203 PM_SECTION, |
204 PM_ARRAY | 204 PM_ARRAY |
205 }; | 205 }; |
206 | 206 |
207 #define VADDCH(ch) if (strPos < SET_MAX_BUF) { tmpStr[strPos++] = ch; } | 207 #define VADDCH(ch) if (strPos < SET_MAX_BUF) { tmpStr[strPos++] = ch; } |
211 { | 211 { |
212 th_cfgitem_t *item = NULL; | 212 th_cfgitem_t *item = NULL; |
213 char *tmpStr = NULL; | 213 char *tmpStr = NULL; |
214 size_t strPos; | 214 size_t strPos; |
215 int c, parseMode, prevMode, nextMode, tmpCh; | 215 int c, parseMode, prevMode, nextMode, tmpCh; |
216 bool isFound, isStart, isError, validError, fpSet; | 216 BOOL isFound, isStart, isError, validError, fpSet; |
217 | 217 |
218 // Initialize values | 218 // Initialize values |
219 tmpCh = 0; | 219 tmpCh = 0; |
220 strPos = 0; | 220 strPos = 0; |
221 c = -1; | 221 c = -1; |
222 fpSet = isFound = isStart = isError = validError = false; | 222 fpSet = isFound = isStart = isError = validError = FALSE; |
223 nextMode = prevMode = parseMode = PM_IDLE; | 223 nextMode = prevMode = parseMode = PM_IDLE; |
224 | 224 |
225 if ((tmpStr = th_malloc(SET_MAX_BUF + 1)) == NULL) | 225 if ((tmpStr = th_malloc(SET_MAX_BUF + 1)) == NULL) |
226 goto out; | 226 goto out; |
227 | 227 |
343 case PM_KEYSET: | 343 case PM_KEYSET: |
344 if (c == '=') | 344 if (c == '=') |
345 { | 345 { |
346 // Find key from configuration | 346 // Find key from configuration |
347 tmpStr[strPos] = 0; | 347 tmpStr[strPos] = 0; |
348 isFound = false; | 348 isFound = FALSE; |
349 item = cfg; | 349 item = cfg; |
350 while (item != NULL && !isFound) | 350 while (item != NULL && !isFound) |
351 { | 351 { |
352 if (item->name != NULL && strcmp(item->name, tmpStr) == 0) | 352 if (item->name != NULL && strcmp(item->name, tmpStr) == 0) |
353 isFound = true; | 353 isFound = TRUE; |
354 else | 354 else |
355 item = (th_cfgitem_t *) item->node.next; | 355 item = (th_cfgitem_t *) item->node.next; |
356 } | 356 } |
357 | 357 |
358 // Check if key was found | 358 // Check if key was found |
374 case ITEM_UINT: | 374 case ITEM_UINT: |
375 case ITEM_FLOAT: | 375 case ITEM_FLOAT: |
376 nextMode = PM_NUMERIC; | 376 nextMode = PM_NUMERIC; |
377 break; | 377 break; |
378 | 378 |
379 case ITEM_bool: | 379 case ITEM_BOOL: |
380 nextMode = PM_bool; | 380 nextMode = PM_BOOL; |
381 break; | 381 break; |
382 | 382 |
383 case ITEM_SECTION: | 383 case ITEM_SECTION: |
384 nextMode = PM_SECTION; | 384 nextMode = PM_SECTION; |
385 break; | 385 break; |
386 } | 386 } |
387 | 387 |
388 prevMode = parseMode; | 388 prevMode = parseMode; |
389 parseMode = PM_NEXT; | 389 parseMode = PM_NEXT; |
390 isStart = true; | 390 isStart = TRUE; |
391 fpSet = false; | 391 fpSet = FALSE; |
392 strPos = 0; | 392 strPos = 0; |
393 } | 393 } |
394 else | 394 else |
395 { | 395 { |
396 // Error! No configuration key by this name found | 396 // Error! No configuration key by this name found |
448 { | 448 { |
449 switch (item->type) | 449 switch (item->type) |
450 { | 450 { |
451 case ITEM_STRING_LIST: | 451 case ITEM_STRING_LIST: |
452 c = -1; | 452 c = -1; |
453 isStart = true; | 453 isStart = TRUE; |
454 prevMode = parseMode; | 454 prevMode = parseMode; |
455 parseMode = PM_NEXT; | 455 parseMode = PM_NEXT; |
456 nextMode = PM_STRING; | 456 nextMode = PM_STRING; |
457 break; | 457 break; |
458 } | 458 } |
477 else | 477 else |
478 { | 478 { |
479 int res = th_cfg_read_sect(ctx, item->v.section, nesting + 1); | 479 int res = th_cfg_read_sect(ctx, item->v.section, nesting + 1); |
480 c = -1; | 480 c = -1; |
481 if (res > 0) | 481 if (res > 0) |
482 validError = true; | 482 validError = TRUE; |
483 else if (res < 0) | 483 else if (res < 0) |
484 parseMode = PM_ERROR; | 484 parseMode = PM_ERROR; |
485 else | 485 else |
486 { | 486 { |
487 prevMode = parseMode; | 487 prevMode = parseMode; |
494 // String parsing mode | 494 // String parsing mode |
495 if (isStart) | 495 if (isStart) |
496 { | 496 { |
497 // Start of string, get delimiter | 497 // Start of string, get delimiter |
498 tmpCh = c; | 498 tmpCh = c; |
499 isStart = false; | 499 isStart = FALSE; |
500 strPos = 0; | 500 strPos = 0; |
501 } | 501 } |
502 else if (c == tmpCh) | 502 else if (c == tmpCh) |
503 { | 503 { |
504 // End of string, set the value | 504 // End of string, set the value |
554 } | 554 } |
555 else if (isStart && (c == '-' || c == '+')) | 555 else if (isStart && (c == '-' || c == '+')) |
556 { | 556 { |
557 VADDCH(c) | 557 VADDCH(c) |
558 else | 558 else |
559 isError = true; | 559 isError = TRUE; |
560 } | 560 } |
561 else if (isStart && item->type == ITEM_FLOAT && c == '.') | 561 else if (isStart && item->type == ITEM_FLOAT && c == '.') |
562 { | 562 { |
563 fpSet = true; | 563 fpSet = TRUE; |
564 VADDCH('0') | 564 VADDCH('0') |
565 else | 565 else |
566 isError = true; | 566 isError = TRUE; |
567 | 567 |
568 VADDCH(c) | 568 VADDCH(c) |
569 else | 569 else |
570 isError = true; | 570 isError = TRUE; |
571 } | 571 } |
572 else if (item->type == ITEM_FLOAT && c == '.' && !fpSet) | 572 else if (item->type == ITEM_FLOAT && c == '.' && !fpSet) |
573 { | 573 { |
574 fpSet = true; | 574 fpSet = TRUE; |
575 VADDCH(c) | 575 VADDCH(c) |
576 else | 576 else |
577 isError = true; | 577 isError = TRUE; |
578 } | 578 } |
579 else if (th_isdigit(c)) | 579 else if (th_isdigit(c)) |
580 { | 580 { |
581 VADDCH(c) | 581 VADDCH(c) |
582 else | 582 else |
583 isError = true; | 583 isError = TRUE; |
584 } | 584 } |
585 else if (VISEND(c)) | 585 else if (VISEND(c)) |
586 { | 586 { |
587 // End of integer parsing mode | 587 // End of integer parsing mode |
588 tmpStr[strPos] = 0; | 588 tmpStr[strPos] = 0; |
620 "String too long! Maximum is %d characters.", | 620 "String too long! Maximum is %d characters.", |
621 SET_MAX_BUF); | 621 SET_MAX_BUF); |
622 parseMode = PM_ERROR; | 622 parseMode = PM_ERROR; |
623 } | 623 } |
624 | 624 |
625 isStart = false; | 625 isStart = FALSE; |
626 c = -1; | 626 c = -1; |
627 break; | 627 break; |
628 | 628 |
629 case PM_bool: | 629 case PM_BOOL: |
630 // Boolean parsing mode | 630 // Boolean parsing mode |
631 if (isStart) | 631 if (isStart) |
632 { | 632 { |
633 isStart = false; | 633 isStart = FALSE; |
634 strPos = 0; | 634 strPos = 0; |
635 } | 635 } |
636 | 636 |
637 if (th_isalnum(c)) | 637 if (th_isalnum(c)) |
638 { | 638 { |
639 VADDCH(c) | 639 VADDCH(c) |
640 else | 640 else |
641 isError = true; | 641 isError = TRUE; |
642 } | 642 } |
643 else | 643 else |
644 if (VISEND(c)) | 644 if (VISEND(c)) |
645 { | 645 { |
646 bool tmpBool; | 646 BOOL tmpBool; |
647 tmpStr[strPos] = 0; | 647 tmpStr[strPos] = 0; |
648 isError = !th_get_boolean(tmpStr, &tmpBool); | 648 isError = !th_get_boolean(tmpStr, &tmpBool); |
649 if (!isError) | 649 if (!isError) |
650 { | 650 { |
651 *(item->v.val_bool) = tmpBool; | 651 *(item->v.val_bool) = tmpBool; |
779 if (thfprintf(ctx, "%s = %1.5f\n", | 779 if (thfprintf(ctx, "%s = %1.5f\n", |
780 item->name, *(item->v.val_float)) < 0) | 780 item->name, *(item->v.val_float)) < 0) |
781 return -5; | 781 return -5; |
782 break; | 782 break; |
783 | 783 |
784 case ITEM_bool: | 784 case ITEM_BOOL: |
785 if (thfprintf(ctx, "%s = %s\n", item->name, | 785 if (thfprintf(ctx, "%s = %s\n", item->name, |
786 *(item->v.val_bool) ? "yes" : "no") < 0) | 786 *(item->v.val_bool) ? "yes" : "no") < 0) |
787 return -6; | 787 return -6; |
788 break; | 788 break; |
789 | 789 |
828 | 828 |
829 /* Find a configuration item based on section, name, type. | 829 /* Find a configuration item based on section, name, type. |
830 * Name MUST be defined. Section can be NULL and type -1, | 830 * Name MUST be defined. Section can be NULL and type -1, |
831 * first matching item will be returned. | 831 * first matching item will be returned. |
832 */ | 832 */ |
833 static th_cfgitem_t *th_cfg_find_do(th_cfgitem_t *item, bool *sect, const char *section, const char *name, const int type) | 833 static th_cfgitem_t *th_cfg_find_do(th_cfgitem_t *item, BOOL *sect, const char *section, const char *name, const int type) |
834 { | 834 { |
835 while (item != NULL) | 835 while (item != NULL) |
836 { | 836 { |
837 bool match = true; | 837 BOOL match = TRUE; |
838 | 838 |
839 if (item->type == ITEM_SECTION) | 839 if (item->type == ITEM_SECTION) |
840 { | 840 { |
841 // Check section name if set | 841 // Check section name if set |
842 if (section != NULL && strcmp(section, item->name) == 0) | 842 if (section != NULL && strcmp(section, item->name) == 0) |
843 *sect = true; | 843 *sect = TRUE; |
844 | 844 |
845 // Recurse to sub-section | 845 // Recurse to sub-section |
846 th_cfgitem_t *tmp = th_cfg_find_do(item->v.section, sect, section, name, type); | 846 th_cfgitem_t *tmp = th_cfg_find_do(item->v.section, sect, section, name, type); |
847 if (tmp != NULL) | 847 if (tmp != NULL) |
848 return tmp; | 848 return tmp; |
849 } | 849 } |
850 else | 850 else |
851 // Has type check been set, and does it match? | 851 // Has type check been set, and does it match? |
852 if (type != -1 && item->type != type) | 852 if (type != -1 && item->type != type) |
853 match = false; | 853 match = FALSE; |
854 else | 854 else |
855 // Check name (not section name, tho) | 855 // Check name (not section name, tho) |
856 if (strcmp(name, item->name) != 0) | 856 if (strcmp(name, item->name) != 0) |
857 match = false; | 857 match = FALSE; |
858 | 858 |
859 // Do we have a match? | 859 // Do we have a match? |
860 if (*sect && match) | 860 if (*sect && match) |
861 return item; | 861 return item; |
862 } | 862 } |
865 } | 865 } |
866 | 866 |
867 | 867 |
868 th_cfgitem_t *th_cfg_find(th_cfgitem_t *cfg, const char *section, const char *name, const int type) | 868 th_cfgitem_t *th_cfg_find(th_cfgitem_t *cfg, const char *section, const char *name, const int type) |
869 { | 869 { |
870 bool sect = false; | 870 BOOL sect = FALSE; |
871 | 871 |
872 if (section == NULL) | 872 if (section == NULL) |
873 sect = true; | 873 sect = TRUE; |
874 | 874 |
875 return th_cfg_find_do(cfg, §, section, name, type); | 875 return th_cfg_find_do(cfg, §, section, name, type); |
876 } | 876 } |