Mercurial > hg > th-libs
comparison th_config.c @ 735:31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 07 Dec 2022 12:14:39 +0200 |
parents | 29e44a58bc73 |
children | c17eadc60c3d |
comparison
equal
deleted
inserted
replaced
734:2ae1045f6c18 | 735:31bc1ed07cf5 |
---|---|
123 | 123 |
124 | 124 |
125 /* Add boolean type setting into given configuration | 125 /* Add boolean type setting into given configuration |
126 */ | 126 */ |
127 int th_cfg_add_bool(th_cfgitem_t **cfg, const char *name, | 127 int th_cfg_add_bool(th_cfgitem_t **cfg, const char *name, |
128 BOOL *itemData, BOOL defValue) | 128 bool *itemData, bool defValue) |
129 { | 129 { |
130 th_cfgitem_t *node = th_cfg_add(cfg, name, ITEM_BOOL, (void *) itemData); | 130 th_cfgitem_t *node = th_cfg_add(cfg, name, ITEM_bool, (void *) itemData); |
131 if (node == NULL) | 131 if (node == NULL) |
132 return THERR_MALLOC; | 132 return THERR_MALLOC; |
133 | 133 |
134 *itemData = defValue; | 134 *itemData = defValue; |
135 | 135 |
187 PM_NEXT, | 187 PM_NEXT, |
188 PM_KEYNAME, | 188 PM_KEYNAME, |
189 PM_KEYSET, | 189 PM_KEYSET, |
190 PM_STRING, | 190 PM_STRING, |
191 PM_NUMERIC, | 191 PM_NUMERIC, |
192 PM_BOOL, | 192 PM_bool, |
193 PM_SECTION, | 193 PM_SECTION, |
194 PM_LIST, | 194 PM_LIST, |
195 | 195 |
196 PM_LAST | 196 PM_LAST |
197 }; | 197 }; |
221 | 221 |
222 | 222 |
223 #define VADDCH(ch) if (strPos < SET_MAX_BUF) { tmpStr[strPos++] = ch; } | 223 #define VADDCH(ch) if (strPos < SET_MAX_BUF) { tmpStr[strPos++] = ch; } |
224 | 224 |
225 | 225 |
226 static BOOL th_cfg_is_end(const int ch) | 226 static bool th_cfg_is_end(const int ch) |
227 { | 227 { |
228 return | 228 return |
229 ch == '\r' || | 229 ch == '\r' || |
230 ch == '\n' || | 230 ch == '\n' || |
231 ch == ';' || | 231 ch == ';' || |
245 case ITEM_INT: | 245 case ITEM_INT: |
246 case ITEM_UINT: | 246 case ITEM_UINT: |
247 case ITEM_FLOAT: | 247 case ITEM_FLOAT: |
248 return PM_NUMERIC; | 248 return PM_NUMERIC; |
249 | 249 |
250 case ITEM_BOOL: | 250 case ITEM_bool: |
251 return PM_BOOL; | 251 return PM_bool; |
252 | 252 |
253 case ITEM_SECTION: | 253 case ITEM_SECTION: |
254 return PM_SECTION; | 254 return PM_SECTION; |
255 | 255 |
256 case ITEM_STRING_LIST: | 256 case ITEM_STRING_LIST: |
262 } | 262 } |
263 | 263 |
264 | 264 |
265 static int th_cfg_set_item(th_cfgparserctx_t *ctx, th_cfgitem_t *item, const char *str) | 265 static int th_cfg_set_item(th_cfgparserctx_t *ctx, th_cfgitem_t *item, const char *str) |
266 { | 266 { |
267 BOOL res = TRUE; | 267 bool res = true; |
268 | 268 |
269 switch (item->type) | 269 switch (item->type) |
270 { | 270 { |
271 case ITEM_HEX_TRIPLET: | 271 case ITEM_HEX_TRIPLET: |
272 res = th_get_hex_triplet(str, item->v.val_uint); | 272 res = th_get_hex_triplet(str, item->v.val_uint); |
286 | 286 |
287 // Early exit as we set the parsemode here | 287 // Early exit as we set the parsemode here |
288 return THERR_OK; | 288 return THERR_OK; |
289 } | 289 } |
290 else | 290 else |
291 res = FALSE; | 291 res = false; |
292 } | 292 } |
293 break; | 293 break; |
294 | 294 |
295 case ITEM_INT: | 295 case ITEM_INT: |
296 *(item->v.val_int) = atoi(str); | 296 *(item->v.val_int) = atoi(str); |
302 | 302 |
303 case ITEM_FLOAT: | 303 case ITEM_FLOAT: |
304 *(item->v.val_float) = atof(str); | 304 *(item->v.val_float) = atof(str); |
305 break; | 305 break; |
306 | 306 |
307 case ITEM_BOOL: | 307 case ITEM_bool: |
308 res = th_get_boolean(str, item->v.val_bool); | 308 res = th_get_boolean(str, item->v.val_bool); |
309 break; | 309 break; |
310 | 310 |
311 default: | 311 default: |
312 res = FALSE; | 312 res = false; |
313 break; | 313 break; |
314 } | 314 } |
315 | 315 |
316 th_cfg_set_parsemode(ctx, PM_IDLE); | 316 th_cfg_set_parsemode(ctx, PM_IDLE); |
317 | 317 |
323 { | 323 { |
324 th_cfgparserctx_t ctx; | 324 th_cfgparserctx_t ctx; |
325 th_cfgitem_t *item = NULL; | 325 th_cfgitem_t *item = NULL; |
326 char *tmpStr = NULL; | 326 char *tmpStr = NULL; |
327 size_t strPos; | 327 size_t strPos; |
328 BOOL isEscaped, isStart, fpSet; | 328 bool isEscaped, isStart, fpSet; |
329 int ret = THERR_OK; | 329 int ret = THERR_OK; |
330 | 330 |
331 // Initialize values | 331 // Initialize values |
332 memset(&ctx, 0, sizeof(ctx)); | 332 memset(&ctx, 0, sizeof(ctx)); |
333 ctx.ch = -1; | 333 ctx.ch = -1; |
334 ctx.nextMode = ctx.prevMode = ctx.parseMode = PM_IDLE; | 334 ctx.nextMode = ctx.prevMode = ctx.parseMode = PM_IDLE; |
335 isEscaped = fpSet = isStart = FALSE; | 335 isEscaped = fpSet = isStart = false; |
336 strPos = 0; | 336 strPos = 0; |
337 | 337 |
338 if ((tmpStr = th_malloc(SET_MAX_BUF + 1)) == NULL) | 338 if ((tmpStr = th_malloc(SET_MAX_BUF + 1)) == NULL) |
339 goto out; | 339 goto out; |
340 | 340 |
459 | 459 |
460 case PM_KEYSET: | 460 case PM_KEYSET: |
461 if (ctx.ch == '=') | 461 if (ctx.ch == '=') |
462 { | 462 { |
463 // Find key from configuration | 463 // Find key from configuration |
464 BOOL found; | 464 bool found; |
465 tmpStr[strPos] = 0; | 465 tmpStr[strPos] = 0; |
466 | 466 |
467 for (item = sect, found = FALSE; item != NULL && !found; ) | 467 for (item = sect, found = false; item != NULL && !found; ) |
468 { | 468 { |
469 if (item->type != ITEM_COMMENT && | 469 if (item->type != ITEM_COMMENT && |
470 item->name != NULL && | 470 item->name != NULL && |
471 strcmp(item->name, tmpStr) == 0) | 471 strcmp(item->name, tmpStr) == 0) |
472 found = TRUE; | 472 found = true; |
473 else | 473 else |
474 item = (th_cfgitem_t *) item->node.next; | 474 item = (th_cfgitem_t *) item->node.next; |
475 } | 475 } |
476 | 476 |
477 // Check if key was found | 477 // Check if key was found |
478 if (found) | 478 if (found) |
479 { | 479 { |
480 // Okay, set next mode | 480 // Okay, set next mode |
481 th_cfg_set_next_parsemode(&ctx, th_cfg_get_parsemode(item->type)); | 481 th_cfg_set_next_parsemode(&ctx, th_cfg_get_parsemode(item->type)); |
482 isStart = TRUE; | 482 isStart = true; |
483 fpSet = FALSE; | 483 fpSet = false; |
484 strPos = 0; | 484 strPos = 0; |
485 } | 485 } |
486 else | 486 else |
487 { | 487 { |
488 // Error! No configuration key by this name found | 488 // Error! No configuration key by this name found |
532 else | 532 else |
533 if (ctx.ch == ',') | 533 if (ctx.ch == ',') |
534 { | 534 { |
535 th_cfg_set_next_parsemode(&ctx, PM_STRING); | 535 th_cfg_set_next_parsemode(&ctx, PM_STRING); |
536 ctx.ch = -1; | 536 ctx.ch = -1; |
537 isStart = TRUE; | 537 isStart = true; |
538 } | 538 } |
539 else | 539 else |
540 { | 540 { |
541 th_cfg_set_parsemode(&ctx, PM_IDLE); | 541 th_cfg_set_parsemode(&ctx, PM_IDLE); |
542 } | 542 } |
566 // String parsing mode | 566 // String parsing mode |
567 if (isStart) | 567 if (isStart) |
568 { | 568 { |
569 // Start of string, get delimiter | 569 // Start of string, get delimiter |
570 ctx.strDelim = ctx.ch; | 570 ctx.strDelim = ctx.ch; |
571 isStart = FALSE; | 571 isStart = false; |
572 strPos = 0; | 572 strPos = 0; |
573 isEscaped = FALSE; | 573 isEscaped = false; |
574 } | 574 } |
575 else | 575 else |
576 if (!isEscaped && ctx.ch == ctx.strDelim) | 576 if (!isEscaped && ctx.ch == ctx.strDelim) |
577 { | 577 { |
578 // End of string, set the value | 578 // End of string, set the value |
582 } | 582 } |
583 else | 583 else |
584 if (!isEscaped && ctx.ch == '\\') | 584 if (!isEscaped && ctx.ch == '\\') |
585 { | 585 { |
586 // Escape sequence | 586 // Escape sequence |
587 isEscaped = TRUE; | 587 isEscaped = true; |
588 } | 588 } |
589 else | 589 else |
590 { | 590 { |
591 // Add character to string | 591 // Add character to string |
592 VADDCH(ctx.ch) | 592 VADDCH(ctx.ch) |
596 ret = th_io_error(fh, THERR_INVALID_DATA, | 596 ret = th_io_error(fh, THERR_INVALID_DATA, |
597 "String too long! Maximum is %d characters.", | 597 "String too long! Maximum is %d characters.", |
598 SET_MAX_BUF); | 598 SET_MAX_BUF); |
599 goto out; | 599 goto out; |
600 } | 600 } |
601 isEscaped = FALSE; | 601 isEscaped = false; |
602 } | 602 } |
603 | 603 |
604 ctx.ch = -1; | 604 ctx.ch = -1; |
605 break; | 605 break; |
606 | 606 |
622 ret = THERR_INVALID_DATA; | 622 ret = THERR_INVALID_DATA; |
623 } | 623 } |
624 else | 624 else |
625 if (isStart && item->type == ITEM_FLOAT && ctx.ch == '.') | 625 if (isStart && item->type == ITEM_FLOAT && ctx.ch == '.') |
626 { | 626 { |
627 fpSet = TRUE; | 627 fpSet = true; |
628 VADDCH('0') | 628 VADDCH('0') |
629 else | 629 else |
630 ret = THERR_INVALID_DATA; | 630 ret = THERR_INVALID_DATA; |
631 | 631 |
632 VADDCH(ctx.ch) | 632 VADDCH(ctx.ch) |
634 ret = THERR_INVALID_DATA; | 634 ret = THERR_INVALID_DATA; |
635 } | 635 } |
636 else | 636 else |
637 if (item->type == ITEM_FLOAT && ctx.ch == '.' && !fpSet) | 637 if (item->type == ITEM_FLOAT && ctx.ch == '.' && !fpSet) |
638 { | 638 { |
639 fpSet = TRUE; | 639 fpSet = true; |
640 VADDCH(ctx.ch) | 640 VADDCH(ctx.ch) |
641 else | 641 else |
642 ret = THERR_INVALID_DATA; | 642 ret = THERR_INVALID_DATA; |
643 } | 643 } |
644 else | 644 else |
675 "String too long! Maximum is %d characters.", | 675 "String too long! Maximum is %d characters.", |
676 SET_MAX_BUF); | 676 SET_MAX_BUF); |
677 goto out; | 677 goto out; |
678 } | 678 } |
679 | 679 |
680 isStart = FALSE; | 680 isStart = false; |
681 ctx.ch = -1; | 681 ctx.ch = -1; |
682 break; | 682 break; |
683 | 683 |
684 case PM_BOOL: | 684 case PM_bool: |
685 // Boolean parsing mode | 685 // Boolean parsing mode |
686 if (isStart) | 686 if (isStart) |
687 { | 687 { |
688 isStart = FALSE; | 688 isStart = false; |
689 strPos = 0; | 689 strPos = 0; |
690 } | 690 } |
691 | 691 |
692 if (th_isalnum(ctx.ch)) | 692 if (th_isalnum(ctx.ch)) |
693 { | 693 { |
734 } | 734 } |
735 | 735 |
736 | 736 |
737 /* Write a configuration into file | 737 /* Write a configuration into file |
738 */ | 738 */ |
739 static BOOL th_print_indent_a(th_ioctx *fh, const int nesting, const char *fmt, va_list ap) | 739 static bool th_print_indent_a(th_ioctx *fh, const int nesting, const char *fmt, va_list ap) |
740 { | 740 { |
741 for (int i = 0; i < nesting * 4; i++) | 741 for (int i = 0; i < nesting * 4; i++) |
742 { | 742 { |
743 if (thfputc(' ', fh) == EOF) | 743 if (thfputc(' ', fh) == EOF) |
744 return FALSE; | 744 return false; |
745 } | 745 } |
746 | 746 |
747 return thvfprintf(fh, fmt, ap) >= 0; | 747 return thvfprintf(fh, fmt, ap) >= 0; |
748 } | 748 } |
749 | 749 |
750 | 750 |
751 static BOOL th_print_indent(th_ioctx *fh, const int nesting, const char *fmt, ...) | 751 static bool th_print_indent(th_ioctx *fh, const int nesting, const char *fmt, ...) |
752 { | 752 { |
753 BOOL ret; | 753 bool ret; |
754 va_list ap; | 754 va_list ap; |
755 va_start(ap, fmt); | 755 va_start(ap, fmt); |
756 ret = th_print_indent_a(fh, nesting, fmt, ap); | 756 ret = th_print_indent_a(fh, nesting, fmt, ap); |
757 va_end(ap); | 757 va_end(ap); |
758 return ret; | 758 return ret; |
759 } | 759 } |
760 | 760 |
761 | 761 |
762 static BOOL th_cfg_is_item_valid(const th_cfgitem_t *item) | 762 static bool th_cfg_is_item_valid(const th_cfgitem_t *item) |
763 { | 763 { |
764 switch (item->type) | 764 switch (item->type) |
765 { | 765 { |
766 case ITEM_STRING: | 766 case ITEM_STRING: |
767 return (*(item->v.val_str) != NULL); | 767 return (*(item->v.val_str) != NULL); |
768 | 768 |
769 case ITEM_STRING_LIST: | 769 case ITEM_STRING_LIST: |
770 return (*(item->v.list) != NULL); | 770 return (*(item->v.list) != NULL); |
771 | 771 |
772 case ITEM_SECTION: | 772 case ITEM_SECTION: |
773 return TRUE; | 773 return true; |
774 | 774 |
775 case ITEM_INT: | 775 case ITEM_INT: |
776 case ITEM_UINT: | 776 case ITEM_UINT: |
777 case ITEM_FLOAT: | 777 case ITEM_FLOAT: |
778 case ITEM_BOOL: | 778 case ITEM_bool: |
779 case ITEM_HEX_TRIPLET: | 779 case ITEM_HEX_TRIPLET: |
780 return TRUE; | 780 return true; |
781 | 781 |
782 default: | 782 default: |
783 return FALSE; | 783 return false; |
784 } | 784 } |
785 } | 785 } |
786 | 786 |
787 | 787 |
788 static BOOL th_cfg_write_string_escaped(th_ioctx *fh, const char *str, const char delim) | 788 static bool th_cfg_write_string_escaped(th_ioctx *fh, const char *str, const char delim) |
789 { | 789 { |
790 for (const char *ptr = str; *ptr; ptr++) | 790 for (const char *ptr = str; *ptr; ptr++) |
791 { | 791 { |
792 if (*ptr == delim) | 792 if (*ptr == delim) |
793 { | 793 { |
794 if (thfputc('\\', fh) == EOF || | 794 if (thfputc('\\', fh) == EOF || |
795 thfputc(*ptr, fh) == EOF) | 795 thfputc(*ptr, fh) == EOF) |
796 return FALSE; | 796 return false; |
797 } | 797 } |
798 else | 798 else |
799 { | 799 { |
800 if (thfputc(*ptr, fh) == EOF) | 800 if (thfputc(*ptr, fh) == EOF) |
801 return FALSE; | 801 return false; |
802 } | 802 } |
803 } | 803 } |
804 | 804 |
805 return TRUE; | 805 return true; |
806 } | 806 } |
807 | 807 |
808 | 808 |
809 static int th_cfg_write_item(th_ioctx *fh, const th_cfgitem_t *item) | 809 static int th_cfg_write_item(th_ioctx *fh, const th_cfgitem_t *item) |
810 { | 810 { |
825 return thfprintf(fh, "%u", *(item->v.val_uint)); | 825 return thfprintf(fh, "%u", *(item->v.val_uint)); |
826 | 826 |
827 case ITEM_FLOAT: | 827 case ITEM_FLOAT: |
828 return thfprintf(fh, "%1.5f", *(item->v.val_float)); | 828 return thfprintf(fh, "%1.5f", *(item->v.val_float)); |
829 | 829 |
830 case ITEM_BOOL: | 830 case ITEM_bool: |
831 return thfprintf(fh, "%s", *(item->v.val_bool) ? "yes" : "no"); | 831 return thfprintf(fh, "%s", *(item->v.val_bool) ? "yes" : "no"); |
832 | 832 |
833 case ITEM_HEX_TRIPLET: | 833 case ITEM_HEX_TRIPLET: |
834 return thfprintf(fh, "\"%06x\"", *(item->v.val_int)); | 834 return thfprintf(fh, "\"%06x\"", *(item->v.val_int)); |
835 | 835 |
846 if (item->name == NULL) | 846 if (item->name == NULL) |
847 return THERR_NULLPTR; | 847 return THERR_NULLPTR; |
848 | 848 |
849 if (item->type == ITEM_COMMENT) | 849 if (item->type == ITEM_COMMENT) |
850 { | 850 { |
851 BOOL lineStart = TRUE, lineFeed = FALSE; | 851 bool lineStart = true, lineFeed = false; |
852 | 852 |
853 for (const char *ptr = item->name; *ptr; ptr++) | 853 for (const char *ptr = item->name; *ptr; ptr++) |
854 switch (*ptr) | 854 switch (*ptr) |
855 { | 855 { |
856 case '\r': | 856 case '\r': |
857 case '\n': | 857 case '\n': |
858 lineStart = lineFeed = TRUE; | 858 lineStart = lineFeed = true; |
859 break; | 859 break; |
860 | 860 |
861 default: | 861 default: |
862 if (lineFeed) | 862 if (lineFeed) |
863 { | 863 { |
864 thfprintf(fh, "\n"); | 864 thfprintf(fh, "\n"); |
865 lineFeed = FALSE; | 865 lineFeed = false; |
866 } | 866 } |
867 if (lineStart) | 867 if (lineStart) |
868 { | 868 { |
869 if (!th_print_indent(fh, nesting, "# ")) | 869 if (!th_print_indent(fh, nesting, "# ")) |
870 return THERR_FWRITE; | 870 return THERR_FWRITE; |
871 | 871 |
872 lineStart = FALSE; | 872 lineStart = false; |
873 } | 873 } |
874 | 874 |
875 if (thfputc(*ptr, fh) == EOF) | 875 if (thfputc(*ptr, fh) == EOF) |
876 return THERR_FWRITE; | 876 return THERR_FWRITE; |
877 } | 877 } |
956 */ | 956 */ |
957 static th_cfgitem_t *th_cfg_find_do(th_cfgitem_t *item, const char *name, const int type) | 957 static th_cfgitem_t *th_cfg_find_do(th_cfgitem_t *item, const char *name, const int type) |
958 { | 958 { |
959 while (item != NULL) | 959 while (item != NULL) |
960 { | 960 { |
961 BOOL match = TRUE; | 961 bool match = true; |
962 | 962 |
963 // Has type check been set, and does it match? | 963 // Has type check been set, and does it match? |
964 if (type != -1 && item->type != type) | 964 if (type != -1 && item->type != type) |
965 match = FALSE; | 965 match = false; |
966 | 966 |
967 // Check item name | 967 // Check item name |
968 if (name != NULL && item->name != NULL && | 968 if (name != NULL && item->name != NULL && |
969 strcmp(name, item->name) != 0) | 969 strcmp(name, item->name) != 0) |
970 match = FALSE; | 970 match = false; |
971 | 971 |
972 // Recurse to section | 972 // Recurse to section |
973 if (!match && item->type == ITEM_SECTION) | 973 if (!match && item->type == ITEM_SECTION) |
974 { | 974 { |
975 th_cfgitem_t *tmp = th_cfg_find_do(item->v.section, name, type); | 975 th_cfgitem_t *tmp = th_cfg_find_do(item->v.section, name, type); |