comparison th_config.c @ 138:3e221c16b087

Improvements in configuration file handing.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 02 Nov 2010 21:46:16 +0200
parents 26fe4dab7e78
children c6c3825376c9
comparison
equal deleted inserted replaced
137:fad8c31e41e6 138:3e221c16b087
1 /* 1 /*
2 * Very simple configuration handling functions 2 * Very simple configuration handling functions
3 * Programmed and designed by Matti 'ccr' Hamalainen 3 * Programmed and designed by Matti 'ccr' Hamalainen
4 * (C) Copyright 2004-2008 Tecnic Software productions (TNSP) 4 * (C) Copyright 2004-2010 Tecnic Software productions (TNSP)
5 * 5 *
6 * Please read file 'COPYING' for information on license and distribution. 6 * Please read file 'COPYING' for information on license and distribution.
7 */ 7 */
8 #ifdef HAVE_CONFIG_H 8 #ifdef HAVE_CONFIG_H
9 #include "config.h" 9 #include "config.h"
36 } 36 }
37 37
38 38
39 /* Allocate and add new item to configuration 39 /* Allocate and add new item to configuration
40 */ 40 */
41 static cfgitem_t *th_cfg_add(cfgitem_t **cfg, char *name, int type, void *data) 41 static cfgitem_t *th_cfg_add(cfgitem_t **cfg, const char *name, const int type, void *data)
42 { 42 {
43 cfgitem_t *node; 43 cfgitem_t *node;
44 44
45 if (cfg == NULL) 45 if (cfg == NULL)
46 return NULL; 46 return NULL;
172 int th_cfg_add_section(cfgitem_t ** cfg, char * name, cfgitem_t *data) 172 int th_cfg_add_section(cfgitem_t ** cfg, char * name, cfgitem_t *data)
173 { 173 {
174 cfgitem_t *node; 174 cfgitem_t *node;
175 175
176 node = th_cfg_add(cfg, name, ITEM_SECTION, (void *) data); 176 node = th_cfg_add(cfg, name, ITEM_SECTION, (void *) data);
177 if (node == NULL)
178 return -1;
179
180 return 0;
181 }
182
183
184 int th_cfg_add_string_list(cfgitem_t ** cfg, char * name, qlist_t **data)
185 {
186 cfgitem_t *node;
187
188 if (data == NULL)
189 return -5;
190
191 node = th_cfg_add(cfg, name, ITEM_STRING_LIST, (void *) data);
177 if (node == NULL) 192 if (node == NULL)
178 return -1; 193 return -1;
179 194
180 return 0; 195 return 0;
181 } 196 }
192 PM_KEYNAME, 207 PM_KEYNAME,
193 PM_KEYSET, 208 PM_KEYSET,
194 PM_STRING, 209 PM_STRING,
195 PM_INT, 210 PM_INT,
196 PM_BOOL, 211 PM_BOOL,
197 PM_SECTION 212 PM_SECTION,
213 PM_ARRAY
198 }; 214 };
199 215
200 #define VADDCH(ch) if (strPos < SET_MAX_BUF) { tmpStr[strPos++] = ch; } 216 #define VADDCH(ch) if (strPos < SET_MAX_BUF) { tmpStr[strPos++] = ch; }
201 #define VISEND(ch) (ch == '\r' || ch == '\n' || ch == ';' || th_isspace(c) || ch == '#') 217 #define VISEND(ch) (ch == '\r' || ch == '\n' || ch == ';' || th_isspace(c) || ch == '#')
202 218
343 case ITEM_HEX_TRIPLET: 359 case ITEM_HEX_TRIPLET:
344 case ITEM_STRING: 360 case ITEM_STRING:
345 nextMode = PM_STRING; 361 nextMode = PM_STRING;
346 break; 362 break;
347 363
364 case ITEM_STRING_LIST:
365 nextMode = PM_ARRAY;
366 break;
367
348 case ITEM_INT: 368 case ITEM_INT:
349 case ITEM_UINT: 369 case ITEM_UINT:
350 nextMode = PM_INT; 370 nextMode = PM_INT;
351 break; 371 break;
352 372
390 c = -1; 410 c = -1;
391 } else { 411 } else {
392 /* Next item found */ 412 /* Next item found */
393 prevMode = parseMode; 413 prevMode = parseMode;
394 parseMode = nextMode; 414 parseMode = nextMode;
415 }
416 break;
417
418 case PM_ARRAY:
419 if (isStart) {
420 switch (item->type) {
421 case ITEM_STRING_LIST:
422 prevMode = parseMode;
423 parseMode = PM_STRING;
424 break;
425 }
426 } else if (c == ',') {
427 switch (item->type) {
428 case ITEM_STRING_LIST:
429 prevMode = parseMode;
430 parseMode = PM_STRING;
431 break;
432 }
433 } else {
434 prevMode = parseMode;
435 parseMode = PM_NORMAL;
395 } 436 }
396 break; 437 break;
397 438
398 case PM_SECTION: 439 case PM_SECTION:
399 /* Section parsing mode */ 440 /* Section parsing mode */
425 strPos = 0; 466 strPos = 0;
426 } else if (c == tmpCh) { 467 } else if (c == tmpCh) {
427 /* End of string, set the value */ 468 /* End of string, set the value */
428 tmpStr[strPos] = 0; 469 tmpStr[strPos] = 0;
429 470
430 if (item->type == ITEM_HEX_TRIPLET) { 471 switch (item->type) {
431 } else if (item->type == ITEM_STRING) { 472 case ITEM_HEX_TRIPLET:
432 th_pstrcpy((char **) item->data, tmpStr); 473 *(int *) item->data = th_get_hex_triplet(tmpStr);
474 prevMode = parseMode;
475 parseMode = PM_NORMAL;
476 break;
477 case ITEM_STRING:
478 th_pstrcpy((char **) item->data, tmpStr);
479 prevMode = parseMode;
480 parseMode = PM_NORMAL;
481 break;
482 case ITEM_STRING_LIST:
483 // th_cfg_add_to_array(item, th_strdup(tmpStr));
484 prevMode = parseMode;
485 parseMode = PM_NEXT;
486 nextMode = PM_ARRAY;
487 break;
433 } 488 }
434 489
435 prevMode = parseMode;
436 parseMode = PM_NORMAL;
437 } else { 490 } else {
438 /* Add character to string */ 491 /* Add character to string */
439 VADDCH(c) 492 VADDCH(c)
440 else 493 else
441 { 494 {
586 if (item->name != NULL) { 639 if (item->name != NULL) {
587 th_print_indent(f, nesting); 640 th_print_indent(f, nesting);
588 641
589 switch (item->type) { 642 switch (item->type) {
590 case ITEM_STRING: 643 case ITEM_STRING:
591 if (*((char **) item->data) == NULL) { 644 if (*(item->val_str) == NULL) {
592 if (fprintf(f->file, "#%s = \"\"\n", item->name) < 0) 645 if (fprintf(f->file, "#%s = \"\"\n", item->name) < 0)
593 return -3; 646 return -3;
594 } else { 647 } else {
595 if (fprintf(f->file, "%s = \"%s\"\n", 648 if (fprintf(f->file, "%s = \"%s\"\n",
596 item->name, *((char **) item->data)) < 0) 649 item->name, *(item->val_str)) < 0)
650 return -3;
651 }
652 break;
653
654 case ITEM_STRING_LIST:
655 if (*(item->list) == NULL) {
656 if (fprintf(f->file, "#%s = \"\", \"\"\n", item->name) < 0)
657 return -3;
658 } else {
659 qlist_t *node = *(item->list);
660 size_t n = th_llist_length(node);
661 if (fprintf(f->file, "%s = ", item->name) < 0)
662 return -3;
663
664 while (node != NULL) {
665 if (node->data != NULL)
666 fprintf(f->file, "\"%s\"", (char *) node->data);
667
668 if (--n > 0)
669 fprintf(f->file, ", ");
670
671 node = node->next;
672 }
673
674 if (fprintf(f->file, "\n") < 0)
597 return -3; 675 return -3;
598 } 676 }
599 break; 677 break;
600 678
601 case ITEM_INT: 679 case ITEM_INT:
602 if (fprintf(f->file, "%s = %i\n", 680 if (fprintf(f->file, "%s = %i\n",
603 item->name, *((int *) item->data)) < 0) 681 item->name, *(item->val_int)) < 0)
604 return -4; 682 return -4;
605 break; 683 break;
606 684
607 case ITEM_UINT: 685 case ITEM_UINT:
608 if (fprintf(f->file, "%s = %d\n", 686 if (fprintf(f->file, "%s = %d\n",
609 item->name, *((unsigned int *) item->data)) < 0) 687 item->name, *(item->val_uint)) < 0)
610 return -5; 688 return -5;
611 break; 689 break;
612 690
613 case ITEM_BOOL: 691 case ITEM_BOOL:
614 if (fprintf(f->file, "%s = %s\n", 692 if (fprintf(f->file, "%s = %s\n",
615 item->name, *((BOOL *) item->data) ? "yes" : "no") < 0) 693 item->name, *(item->val_bool) ? "yes" : "no") < 0)
616 return -6; 694 return -6;
617 break; 695 break;
618 696
619 case ITEM_SECTION: 697 case ITEM_SECTION:
620 { 698 {