annotate th_config.c @ 133:ffe8bbd429fa

Config file.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 30 Oct 2010 20:37:09 +0300
parents
children 26fe4dab7e78
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * Very simple configuration handling functions
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Programmed and designed by Matti 'ccr' Hamalainen
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 * (C) Copyright 2004-2008 Tecnic Software productions (TNSP)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 *
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 * Please read file 'COPYING' for information on license and distribution.
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 #ifdef HAVE_CONFIG_H
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include "config.h"
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #endif
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 #include <stdio.h>
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 #include <stdarg.h>
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 #include "th_config.h"
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 #include "th_util.h"
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 #include "th_string.h"
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 #define SET_MAX_BUF (8192)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 /* Free a given configuration (the values are not free'd)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 void th_cfg_free(cfgitem_t *cfg)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 cfgitem_t *curr = cfg;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 while (curr != NULL) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 cfgitem_t *next = curr->next;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 if (curr->type == ITEM_SECTION)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 th_cfg_free((cfgitem_t *) curr->data);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 th_free(curr->name);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 th_free(curr);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 curr = next;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 /* Allocate and add new item to configuration
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 static cfgitem_t *th_cfg_add(cfgitem_t **cfg, char *name, int type, void *data)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 cfgitem_t *node;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 if (cfg == NULL)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 return NULL;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 /* Allocate new item */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 node = (cfgitem_t *) th_calloc(1, sizeof(cfgitem_t));
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 if (node == NULL)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 return NULL;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 /* Set values */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 node->type = type;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 node->data = data;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 node->name = th_strdup(name);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 /* Insert into linked list */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 if (*cfg != NULL) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 node->prev = (*cfg)->prev;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 (*cfg)->prev->next = node;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 (*cfg)->prev = node;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 *cfg = node;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 node->prev = node;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 node->next = NULL;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 return node;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 /* Add integer type setting into give configuration
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 int th_cfg_add_int(cfgitem_t ** cfg, char * name,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 int *itemData, int itemDef)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 cfgitem_t *node;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 node = th_cfg_add(cfg, name, ITEM_INT, (void *) itemData);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 if (node == NULL)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 return -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 *itemData = itemDef;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 return 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 int th_cfg_add_hexvalue(cfgitem_t ** cfg, char * name,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 int *itemData, int itemDef)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 cfgitem_t *node;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 node = th_cfg_add(cfg, name, ITEM_HEX_TRIPLET, (void *) itemData);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 if (node == NULL)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 return -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 *itemData = itemDef;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 return 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 /* Add unsigned integer type setting into give configuration
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 int th_cfg_add_uint(cfgitem_t ** cfg, char * name,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 unsigned int * itemData, unsigned int itemDef)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 cfgitem_t *node;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 node = th_cfg_add(cfg, name, ITEM_UINT, (void *) itemData);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 if (node == NULL)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 return -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 *itemData = itemDef;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 return 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 /* Add strint type setting into given configuration
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 int th_cfg_add_string(cfgitem_t ** cfg, char * name,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 char ** itemData, char * itemDef)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 cfgitem_t *node;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 node = th_cfg_add(cfg, name, ITEM_STRING, (void *) itemData);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 if (node == NULL)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 return -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 *itemData = th_strdup(itemDef);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 return 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 /* Add boolean type setting into given configuration
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141 int th_cfg_add_bool(cfgitem_t ** cfg, char * name,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 BOOL * itemData, BOOL itemDef)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 cfgitem_t *node;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146 node = th_cfg_add(cfg, name, ITEM_BOOL, (void *) itemData);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147 if (node == NULL)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 return -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150 *itemData = itemDef;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 return 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156 /* Add implicit comment
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157 */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158 int th_cfg_add_comment(cfgitem_t ** cfg, char * comment)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160 cfgitem_t *node;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162 node = th_cfg_add(cfg, comment, ITEM_COMMENT, NULL);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163 if (node == NULL)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 return -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166 return 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
170 /* Add new section
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171 */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172 int th_cfg_add_section(cfgitem_t ** cfg, char * name, cfgitem_t *data)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174 cfgitem_t *node;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176 node = th_cfg_add(cfg, name, ITEM_SECTION, (void *) data);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177 if (node == NULL)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178 return -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180 return 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
182
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
183
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184 /* Read a given file into configuration structure and variables
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
185 */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186 enum {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187 PM_EOF,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
188 PM_ERROR,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189 PM_NORMAL,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190 PM_COMMENT,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191 PM_NEXT,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192 PM_KEYNAME,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
193 PM_KEYSET,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
194 PM_STRING,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195 PM_INT,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196 PM_BOOL,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
197 PM_SECTION
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
198 };
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
199
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200 #define VADDCH(ch) if (strPos < SET_MAX_BUF) { tmpStr[strPos++] = ch; }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201 #define VISEND(ch) (ch == '\r' || ch == '\n' || ch == ';' || th_isspace(c) || ch == '#')
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
202
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203 typedef struct {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
204 FILE *file;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
205 char *filename;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206 size_t line;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207 } conffile_t;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210 static void th_cfg_error(conffile_t *f, const char *fmt, ...)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
211 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
212 va_list ap;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
213 va_start(ap, fmt);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
214 fprintf(stderr, "%s: '%s', line #%d: ", th_prog_name, f->filename, f->line);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
215 vfprintf(stderr, fmt, ap);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
216 va_end(ap);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
217 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
220 static int th_cfg_read_sect(conffile_t *f, cfgitem_t * cfg, int nesting)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
221 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
222 cfgitem_t *item = NULL;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
223 char tmpStr[SET_MAX_BUF + 1];
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
224 size_t strPos;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
225 int c, parseMode, prevMode, nextMode, tmpCh;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
226 BOOL isFound, isStart, isError, validError;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228 /* Initialize values */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
229 tmpCh = 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
230 strPos = 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
231 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
232 isFound = isStart = isError = validError = FALSE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
233 nextMode = prevMode = parseMode = PM_NORMAL;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
234
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
235 /* Parse the configuration */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
236 while (parseMode != PM_EOF && parseMode != PM_ERROR) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237 if (c == -1) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
238 /* Get next character */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239 switch (c = fgetc(f->file)) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
240 case EOF:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241 if (parseMode != PM_NORMAL) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
243 "Unexpected end of file.\n");
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
244 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245 } else
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 parseMode = PM_EOF;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249 case '\n':
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
250 f->line++;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
251 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
253
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
254 switch (parseMode) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
255 case PM_COMMENT:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256 /* Comment parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
257 if (c == '\n') {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258 /* End of line, end of comment */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259 parseMode = prevMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260 prevMode = PM_COMMENT;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
262 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
263 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
264
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265 case PM_NORMAL:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
266 /* Normal parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
267 if (c == '#') {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
268 prevMode = parseMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
269 parseMode = PM_COMMENT;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
270 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
271 } else if (VISEND(c)) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
272 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
273 } else if (c == '}') {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
274 if (nesting > 0) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
275 /* Check for validation errors */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
276 return (validError) ? 1 : 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
277 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
278 th_cfg_error(f, "Invalid nesting sequence encountered.\n");
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
279 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
280 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
281 } else if (th_isalpha(c)) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
282 /* Start of key name found */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
283 prevMode = parseMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
284 parseMode = PM_KEYNAME;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
285 strPos = 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
286 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
287 /* Error! Invalid character found */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
288 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
289 "Unexpected character '%c'.\n", c);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
290 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
291 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
292 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
293
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
294 case PM_KEYNAME:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
295 /* Configuration KEY name parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
296 if (c == '#') {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
297 /* Start of comment */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
298 prevMode = parseMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
299 parseMode = PM_COMMENT;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
300 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
301 } else if (th_iscrlf(c) || th_isspace(c) || c == '=') {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
302 /* End of key name */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
303 prevMode = parseMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
304 parseMode = PM_NEXT;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
305 nextMode = PM_KEYSET;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
306 } else if (th_isalnum(c) || c == '_') {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
307 /* Add to key name string */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
308 VADDCH(c)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
309 else
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
310 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
311 /* Error! Key name string too long! */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
312 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
313 "Config key name too long!");
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
314 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
315 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
316 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
317 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
318 /* Error! Invalid character found */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
319 tmpStr[strPos] = 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
320 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
321 "Unexpected character '%c' in key name '%s'.\n", c, tmpStr);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
322 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
323 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
324 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
325
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
326 case PM_KEYSET:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
327 if (c == '=') {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
328 /* Find key from configuration */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
329 tmpStr[strPos] = 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
330 isFound = FALSE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
331 item = cfg;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
332 while (item != NULL && !isFound) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
333 if (item->name != NULL && strcmp(item->name, tmpStr) == 0)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
334 isFound = TRUE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
335 else
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
336 item = item->next;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
337 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
338
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
339 /* Check if key was found */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
340 if (isFound) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
341 /* Okay, set next mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
342 switch (item->type) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
343 case ITEM_HEX_TRIPLET:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
344 case ITEM_STRING:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
345 nextMode = PM_STRING;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
346 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
347
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
348 case ITEM_INT:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
349 case ITEM_UINT:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
350 nextMode = PM_INT;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
351 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
352
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
353 case ITEM_BOOL:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
354 nextMode = PM_BOOL;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
355 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
356
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
357 case ITEM_SECTION:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
358 nextMode = PM_SECTION;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
359 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
360 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
361
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
362 prevMode = parseMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
363 parseMode = PM_NEXT;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
364 isStart = TRUE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
365 strPos = 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
366 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
367 /* Error! No configuration key by this name found */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
368 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
369 "No such configuration setting ('%s')\n", tmpStr);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
370 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
371 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
372
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
373 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
374 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
375 /* Error! '=' expected! */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
376 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
377 "Unexpected character '%c', assignation '=' was expected.\n", c);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
378 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
379 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
380 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
381
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
382 case PM_NEXT:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
383 /* Search next item parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
384 if (c == '#') {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
385 /* Start of comment */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
386 prevMode = parseMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
387 parseMode = PM_COMMENT;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
388 } else if (th_isspace(c) || th_iscrlf(c)) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
389 /* Ignore whitespaces and linechanges */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
390 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
391 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
392 /* Next item found */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
393 prevMode = parseMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
394 parseMode = nextMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
395 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
396 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
397
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
398 case PM_SECTION:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
399 /* Section parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
400 if (c != '{') {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
401 /* Error! Section start '{' expected! */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
402 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
403 "Unexpected character '%c', section start '{' was expected.\n", c);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
404 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
405 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
406 int res = th_cfg_read_sect(f, (cfgitem_t *) item->data, nesting + 1);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
407 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
408 if (res > 0)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
409 validError = TRUE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
410 else if (res < 0)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
411 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
412 else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
413 prevMode = parseMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
414 parseMode = PM_NORMAL;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
415 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
416 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
417 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
418
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
419 case PM_STRING:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
420 /* String parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
421 if (isStart) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
422 /* Start of string, get delimiter */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
423 tmpCh = c;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
424 isStart = FALSE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
425 strPos = 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
426 } else if (c == tmpCh) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
427 /* End of string, set the value */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
428 tmpStr[strPos] = 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
429
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
430 if (item->type == ITEM_HEX_TRIPLET) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
431 } else if (item->type == ITEM_STRING) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
432 th_pstrcpy((char **) item->data, tmpStr);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
433 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
434
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
435 prevMode = parseMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
436 parseMode = PM_NORMAL;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
437 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
438 /* Add character to string */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
439 VADDCH(c)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
440 else
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
441 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
442 /* Error! String too long! */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
443 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
444 "String too long! Maximum is %d characters.",
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
445 SET_MAX_BUF);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
446 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
447 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
448 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
449
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
450 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
451 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
452
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
453 case PM_INT:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
454 /* Integer parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
455 if (isStart && item->type == ITEM_UINT && c == '-') {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
456 /* Error! Negative values not allowed for unsigned ints */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
457 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
458 "Negative value specified for %s, unsigned value expected.",
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
459 item->name);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
460 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
461 } else if (isStart && (c == '-' || c == '+')) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
462 VADDCH(c)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
463 else
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
464 isError = TRUE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
465 } else if (th_isdigit(c)) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
466 VADDCH(c)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
467 else
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
468 isError = TRUE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
469 } else if (VISEND(c)) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
470 /* End of integer parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
471 tmpStr[strPos] = 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
472 switch (item->type) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
473 case ITEM_INT:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
474 *((int *) item->data) = atoi(tmpStr);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
475 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
476
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
477 case ITEM_UINT:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
478 *((unsigned int *) item->data) = atol(tmpStr);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
479 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
480 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
481
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
482 prevMode = parseMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
483 parseMode = PM_NORMAL;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
484 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
485 /* Error! Unexpected character. */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
486 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
487 "Unexpected character '%c' for integer setting '%s'.",
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
488 c, item->name);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
489 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
490 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
491
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
492 if (isError) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
493 /* Error! String too long! */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
494 th_cfg_error(f, "String too long! Maximum is %d characters.",
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
495 SET_MAX_BUF);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
496 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
497 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
498
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
499 isStart = FALSE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
500 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
501 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
502
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
503 case PM_BOOL:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
504 /* Boolean parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
505 if (isStart) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
506 tmpCh = c;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
507 isStart = FALSE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
508 } else if (VISEND(c)) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
509 BOOL tmpBool;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
510
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
511 /* End of boolean parsing */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
512 switch (tmpCh) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
513 case 'Y': case 'y':
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
514 case 'T': case 't':
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
515 case '1':
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
516 tmpBool = TRUE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
517 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
518
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
519 case 'N': case 'n':
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
520 case 'F': case 'f':
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
521 case '0':
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
522 tmpBool = FALSE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
523 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
524
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
525 default:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
526 isError = TRUE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
527 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
528
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
529 if (isError) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
530 th_cfg_error(f, "Invalid boolean value for '%s'.\n", item->name);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
531 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
532 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
533 *((BOOL *) item->data) = tmpBool;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
534
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
535 prevMode = parseMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
536 parseMode = PM_NORMAL;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
537 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
538 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
539 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
540 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
541 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
542 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
543
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
544 /* Check for validation errors */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
545 if (validError)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
546 return 1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
547
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
548 /* Return result */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
549 if (parseMode == PM_ERROR)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
550 return -2;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
551 else
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
552 return 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
553 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
554
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
555
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
556 int th_cfg_read(FILE *inFile, char *filename, cfgitem_t * cfg)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
557 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
558 conffile_t f;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
559
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
560 f.file = inFile;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
561 f.filename = filename;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
562 f.line = 1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
563
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
564 return th_cfg_read_sect(&f, cfg, 0);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
565 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
566
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
567
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
568 /* Write a configuration into file
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
569 */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
570 static void th_print_indent(conffile_t *f, int nesting)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
571 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
572 int i;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
573 for (i = 0; i < nesting * 2; i++)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
574 fputc(' ', f->file);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
575 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
576
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
577
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
578 static int th_cfg_write_sect(conffile_t *f, cfgitem_t *item, int nesting)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
579 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
580 while (item != NULL) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
581 if (item->type == ITEM_COMMENT) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
582 th_print_indent(f, nesting);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
583 if (fprintf(f->file, "# %s\n", (item->name != NULL) ? item->name : "" ) < 0)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
584 return -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
585 } else
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
586 if (item->name != NULL) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
587 th_print_indent(f, nesting);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
588
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
589 switch (item->type) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
590 case ITEM_STRING:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
591 if (*((char **) item->data) == NULL) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
592 if (fprintf(f->file, "#%s = \"\"\n", item->name) < 0)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
593 return -3;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
594 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
595 if (fprintf(f->file, "%s = \"%s\"\n",
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
596 item->name, *((char **) item->data)) < 0)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
597 return -3;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
598 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
599 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
600
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
601 case ITEM_INT:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
602 if (fprintf(f->file, "%s = %i\n",
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
603 item->name, *((int *) item->data)) < 0)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
604 return -4;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
605 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
606
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
607 case ITEM_UINT:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
608 if (fprintf(f->file, "%s = %d\n",
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
609 item->name, *((unsigned int *) item->data)) < 0)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
610 return -5;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
611 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
612
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
613 case ITEM_BOOL:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
614 if (fprintf(f->file, "%s = %s\n",
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
615 item->name, *((BOOL *) item->data) ? "yes" : "no") < 0)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
616 return -6;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
617 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
618
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
619 case ITEM_SECTION:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
620 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
621 int res;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
622 if (fprintf(f->file, "\n%s = {\n", item->name) < 0)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
623 return -7;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
624 res = th_cfg_write_sect(f, (cfgitem_t *) item->data, nesting + 1);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
625 if (res != 0) return res;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
626 if (fprintf(f->file, "} # End of '%s'\n\n", item->name) < 0)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
627 return -8;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
628 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
629 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
630
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
631 case ITEM_HEX_TRIPLET:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
632 if (fprintf(f->file, "%s = \"%06x\"\n",
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
633 item->name, *((int *) item->data)) < 0)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
634 return -6;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
635 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
636 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
637 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
638 item = item->next;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
639 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
640
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
641 return 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
642 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
643
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
644
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
645 int th_cfg_write(FILE *outFile, char *filename, cfgitem_t *cfg)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
646 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
647 conffile_t f;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
648
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
649 if (cfg == NULL)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
650 return -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
651
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
652 f.file = outFile;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
653 f.filename = filename;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
654 f.line = 1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
655
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
656 fprintf(outFile, "# Configuration written by %s %s\n\n",
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
657 th_prog_fullname, th_prog_version);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
658
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
659 return th_cfg_write_sect(&f, cfg, 0);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
660 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
661
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
662