annotate th_config.c @ 261:0db02b8d2d11

ISO C99 compatibility fixes.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 22 May 2011 02:10:42 +0300
parents 659b8229d015
children e694c02d6982
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
138
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
4 * (C) Copyright 2004-2010 Tecnic Software productions (TNSP)
133
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)
261
0db02b8d2d11 ISO C99 compatibility fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 215
diff changeset
30 th_cfg_free((cfgitem_t *) curr->v.data);
133
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 */
138
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
41 static cfgitem_t *th_cfg_add(cfgitem_t **cfg, const char *name, const int type, void *data)
133
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;
261
0db02b8d2d11 ISO C99 compatibility fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 215
diff changeset
55 node->v.data = data;
133
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
138
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
184 int th_cfg_add_string_list(cfgitem_t ** cfg, char * name, qlist_t **data)
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
185 {
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
186 cfgitem_t *node;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
187
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
188 if (data == NULL)
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
189 return -5;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
190
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
191 node = th_cfg_add(cfg, name, ITEM_STRING_LIST, (void *) data);
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
192 if (node == NULL)
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
193 return -1;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
194
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
195 return 0;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
196 }
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
197
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
198
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
199 /* Read a given file into configuration structure and variables
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200 */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201 enum {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
202 PM_EOF,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203 PM_ERROR,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
204 PM_NORMAL,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
205 PM_COMMENT,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206 PM_NEXT,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207 PM_KEYNAME,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208 PM_KEYSET,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209 PM_STRING,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210 PM_INT,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
211 PM_BOOL,
138
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
212 PM_SECTION,
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
213 PM_ARRAY
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
214 };
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
215
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
216 #define VADDCH(ch) if (strPos < SET_MAX_BUF) { tmpStr[strPos++] = ch; }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
217 #define VISEND(ch) (ch == '\r' || ch == '\n' || ch == ';' || th_isspace(c) || ch == '#')
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219 typedef struct {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
220 FILE *file;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
221 char *filename;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
222 size_t line;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
223 } conffile_t;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
224
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
225
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
226 static void th_cfg_error(conffile_t *f, const char *fmt, ...)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228 va_list ap;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
229 va_start(ap, fmt);
215
659b8229d015 Silence some warnings on OpenBSD.
Matti Hamalainen <ccr@tnsp.org>
parents: 201
diff changeset
230 fprintf(stderr, "%s: '%s', line #%d: ", th_prog_name, f->filename, (unsigned int) f->line);
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
231 vfprintf(stderr, fmt, ap);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
232 va_end(ap);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
233 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
234
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
235
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
236 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
237 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
238 cfgitem_t *item = NULL;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239 char tmpStr[SET_MAX_BUF + 1];
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
240 size_t strPos;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241 int c, parseMode, prevMode, nextMode, tmpCh;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242 BOOL isFound, isStart, isError, validError;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
243
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
244 /* Initialize values */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245 tmpCh = 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 strPos = 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248 isFound = isStart = isError = validError = FALSE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249 nextMode = prevMode = parseMode = PM_NORMAL;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
250
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
251 /* Parse the configuration */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252 while (parseMode != PM_EOF && parseMode != PM_ERROR) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
253 if (c == -1) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
254 /* Get next character */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
255 switch (c = fgetc(f->file)) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256 case EOF:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
257 if (parseMode != PM_NORMAL) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259 "Unexpected end of file.\n");
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261 } else
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
262 parseMode = PM_EOF;
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 '\n':
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
266 f->line++;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
267 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
268 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
269
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
270 switch (parseMode) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
271 case PM_COMMENT:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
272 /* Comment parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
273 if (c == '\n') {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
274 /* End of line, end of comment */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
275 parseMode = prevMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
276 prevMode = PM_COMMENT;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
277 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
278 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
279 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
280
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
281 case PM_NORMAL:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
282 /* Normal parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
283 if (c == '#') {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
284 prevMode = parseMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
285 parseMode = PM_COMMENT;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
286 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
287 } else if (VISEND(c)) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
288 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
289 } else if (c == '}') {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
290 if (nesting > 0) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
291 /* Check for validation errors */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
292 return (validError) ? 1 : 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
293 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
294 th_cfg_error(f, "Invalid nesting sequence encountered.\n");
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
295 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
296 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
297 } else if (th_isalpha(c)) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
298 /* Start of key name found */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
299 prevMode = parseMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
300 parseMode = PM_KEYNAME;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
301 strPos = 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
302 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
303 /* Error! Invalid character found */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
304 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
305 "Unexpected character '%c'.\n", c);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
306 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
307 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
308 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
309
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
310 case PM_KEYNAME:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
311 /* Configuration KEY name parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
312 if (c == '#') {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
313 /* Start of comment */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
314 prevMode = parseMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
315 parseMode = PM_COMMENT;
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 if (th_iscrlf(c) || th_isspace(c) || c == '=') {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
318 /* End of key name */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
319 prevMode = parseMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
320 parseMode = PM_NEXT;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
321 nextMode = PM_KEYSET;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
322 } else if (th_isalnum(c) || c == '_') {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
323 /* Add to key name string */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
324 VADDCH(c)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
325 else
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
326 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
327 /* Error! Key name string too long! */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
328 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
329 "Config key name too long!");
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
330 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
331 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
332 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
333 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
334 /* Error! Invalid character found */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
335 tmpStr[strPos] = 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
336 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
337 "Unexpected character '%c' in key name '%s'.\n", c, tmpStr);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
338 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
339 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
340 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
341
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
342 case PM_KEYSET:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
343 if (c == '=') {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
344 /* Find key from configuration */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
345 tmpStr[strPos] = 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
346 isFound = FALSE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
347 item = cfg;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
348 while (item != NULL && !isFound) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
349 if (item->name != NULL && strcmp(item->name, tmpStr) == 0)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
350 isFound = TRUE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
351 else
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
352 item = item->next;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
353 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
354
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
355 /* Check if key was found */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
356 if (isFound) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
357 /* Okay, set next mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
358 switch (item->type) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
359 case ITEM_HEX_TRIPLET:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
360 case ITEM_STRING:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
361 nextMode = PM_STRING;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
362 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
363
138
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
364 case ITEM_STRING_LIST:
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
365 nextMode = PM_ARRAY;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
366 break;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
367
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
368 case ITEM_INT:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
369 case ITEM_UINT:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
370 nextMode = PM_INT;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
371 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
372
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
373 case ITEM_BOOL:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
374 nextMode = PM_BOOL;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
375 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
376
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
377 case ITEM_SECTION:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
378 nextMode = PM_SECTION;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
379 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
380 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
381
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
382 prevMode = parseMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
383 parseMode = PM_NEXT;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
384 isStart = TRUE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
385 strPos = 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
386 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
387 /* Error! No configuration key by this name found */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
388 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
389 "No such configuration setting ('%s')\n", tmpStr);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
390 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
391 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
392
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
393 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
394 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
395 /* Error! '=' expected! */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
396 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
397 "Unexpected character '%c', assignation '=' was expected.\n", c);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
398 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
399 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
400 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
401
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
402 case PM_NEXT:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
403 /* Search next item parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
404 if (c == '#') {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
405 /* Start of comment */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
406 prevMode = parseMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
407 parseMode = PM_COMMENT;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
408 } else if (th_isspace(c) || th_iscrlf(c)) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
409 /* Ignore whitespaces and linechanges */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
410 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
411 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
412 /* Next item found */
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 = nextMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
415 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
416 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
417
138
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
418 case PM_ARRAY:
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
419 if (isStart) {
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
420 switch (item->type) {
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
421 case ITEM_STRING_LIST:
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
422 prevMode = parseMode;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
423 parseMode = PM_STRING;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
424 break;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
425 }
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
426 } else if (c == ',') {
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
427 switch (item->type) {
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
428 case ITEM_STRING_LIST:
146
c6c3825376c9 Fix string list parsing and handling in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
429 c = -1;
c6c3825376c9 Fix string list parsing and handling in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
430 isStart = TRUE;
138
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
431 prevMode = parseMode;
146
c6c3825376c9 Fix string list parsing and handling in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
432 parseMode = PM_NEXT;
c6c3825376c9 Fix string list parsing and handling in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
433 nextMode = PM_STRING;
138
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
434 break;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
435 }
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
436 } else {
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
437 prevMode = parseMode;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
438 parseMode = PM_NORMAL;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
439 }
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
440 break;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
441
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
442 case PM_SECTION:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
443 /* Section parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
444 if (c != '{') {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
445 /* Error! Section start '{' expected! */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
446 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
447 "Unexpected character '%c', section start '{' was expected.\n", c);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
448 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
449 } else {
261
0db02b8d2d11 ISO C99 compatibility fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 215
diff changeset
450 int res = th_cfg_read_sect(f, item->v.section, nesting + 1);
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
451 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
452 if (res > 0)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
453 validError = TRUE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
454 else if (res < 0)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
455 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
456 else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
457 prevMode = parseMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
458 parseMode = PM_NORMAL;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
459 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
460 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
461 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
462
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
463 case PM_STRING:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
464 /* String parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
465 if (isStart) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
466 /* Start of string, get delimiter */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
467 tmpCh = c;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
468 isStart = FALSE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
469 strPos = 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
470 } else if (c == tmpCh) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
471 /* End of string, set the value */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
472 tmpStr[strPos] = 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
473
138
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
474 switch (item->type) {
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
475 case ITEM_HEX_TRIPLET:
261
0db02b8d2d11 ISO C99 compatibility fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 215
diff changeset
476 *(item->v.val_int) = th_get_hex_triplet(tmpStr);
138
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
477 prevMode = parseMode;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
478 parseMode = PM_NORMAL;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
479 break;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
480 case ITEM_STRING:
261
0db02b8d2d11 ISO C99 compatibility fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 215
diff changeset
481 th_pstrcpy(item->v.val_str, tmpStr);
138
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
482 prevMode = parseMode;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
483 parseMode = PM_NORMAL;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
484 break;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
485 case ITEM_STRING_LIST:
261
0db02b8d2d11 ISO C99 compatibility fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 215
diff changeset
486 th_llist_append(item->v.list, th_strdup(tmpStr));
138
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
487 prevMode = parseMode;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
488 parseMode = PM_NEXT;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
489 nextMode = PM_ARRAY;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
490 break;
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
491 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
492
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
493 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
494 /* Add character to string */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
495 VADDCH(c)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
496 else
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
497 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
498 /* Error! String too long! */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
499 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
500 "String too long! Maximum is %d characters.",
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
501 SET_MAX_BUF);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
502 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
503 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
504 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
505
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
506 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
507 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
508
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
509 case PM_INT:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
510 /* Integer parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
511 if (isStart && item->type == ITEM_UINT && c == '-') {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
512 /* Error! Negative values not allowed for unsigned ints */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
513 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
514 "Negative value specified for %s, unsigned value expected.",
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
515 item->name);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
516 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
517 } else if (isStart && (c == '-' || c == '+')) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
518 VADDCH(c)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
519 else
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
520 isError = TRUE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
521 } else if (th_isdigit(c)) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
522 VADDCH(c)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
523 else
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
524 isError = TRUE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
525 } else if (VISEND(c)) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
526 /* End of integer parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
527 tmpStr[strPos] = 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
528 switch (item->type) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
529 case ITEM_INT:
261
0db02b8d2d11 ISO C99 compatibility fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 215
diff changeset
530 *(item->v.val_int) = atoi(tmpStr);
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
531 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
532
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
533 case ITEM_UINT:
261
0db02b8d2d11 ISO C99 compatibility fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 215
diff changeset
534 *(item->v.val_uint) = atol(tmpStr);
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
535 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
536 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
537
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
538 prevMode = parseMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
539 parseMode = PM_NORMAL;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
540 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
541 /* Error! Unexpected character. */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
542 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
543 "Unexpected character '%c' for integer setting '%s'.",
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
544 c, item->name);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
545 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
546 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
547
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
548 if (isError) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
549 /* Error! String too long! */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
550 th_cfg_error(f, "String too long! Maximum is %d characters.",
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
551 SET_MAX_BUF);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
552 parseMode = PM_ERROR;
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 isStart = FALSE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
556 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
557 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
558
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
559 case PM_BOOL:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
560 /* Boolean parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
561 if (isStart) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
562 tmpCh = c;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
563 isStart = FALSE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
564 } else if (VISEND(c)) {
201
f7b571debd81 Silence a warning when compiling with older versions of GCC.
Matti Hamalainen <ccr@tnsp.org>
parents: 146
diff changeset
565 BOOL tmpBool = FALSE;
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
566
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
567 /* End of boolean parsing */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
568 switch (tmpCh) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
569 case 'Y': case 'y':
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
570 case 'T': case 't':
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
571 case '1':
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
572 tmpBool = TRUE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
573 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
574
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
575 case 'N': case 'n':
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
576 case 'F': case 'f':
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
577 case '0':
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
578 tmpBool = FALSE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
579 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
580
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
581 default:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
582 isError = TRUE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
583 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
584
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
585 if (isError) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
586 th_cfg_error(f, "Invalid boolean value for '%s'.\n", item->name);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
587 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
588 } else {
261
0db02b8d2d11 ISO C99 compatibility fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 215
diff changeset
589 *(item->v.val_bool) = tmpBool;
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
590
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
591 prevMode = parseMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
592 parseMode = PM_NORMAL;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
593 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
594 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
595 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
596 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
597 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
598 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
599
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
600 /* Check for validation errors */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
601 if (validError)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
602 return 1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
603
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
604 /* Return result */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
605 if (parseMode == PM_ERROR)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
606 return -2;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
607 else
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
608 return 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
609 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
610
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
611
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
612 int th_cfg_read(FILE *inFile, char *filename, cfgitem_t * cfg)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
613 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
614 conffile_t f;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
615
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
616 f.file = inFile;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
617 f.filename = filename;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
618 f.line = 1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
619
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
620 return th_cfg_read_sect(&f, cfg, 0);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
621 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
622
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
623
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
624 /* Write a configuration into file
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
625 */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
626 static void th_print_indent(conffile_t *f, int nesting)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
627 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
628 int i;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
629 for (i = 0; i < nesting * 2; i++)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
630 fputc(' ', f->file);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
631 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
632
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
633
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
634 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
635 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
636 while (item != NULL) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
637 if (item->type == ITEM_COMMENT) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
638 th_print_indent(f, nesting);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
639 if (fprintf(f->file, "# %s\n", (item->name != NULL) ? item->name : "" ) < 0)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
640 return -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
641 } else
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
642 if (item->name != NULL) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
643 th_print_indent(f, nesting);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
644
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
645 switch (item->type) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
646 case ITEM_STRING:
261
0db02b8d2d11 ISO C99 compatibility fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 215
diff changeset
647 if (*(item->v.val_str) == NULL) {
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
648 if (fprintf(f->file, "#%s = \"\"\n", item->name) < 0)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
649 return -3;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
650 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
651 if (fprintf(f->file, "%s = \"%s\"\n",
261
0db02b8d2d11 ISO C99 compatibility fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 215
diff changeset
652 item->name, *(item->v.val_str)) < 0)
138
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
653 return -3;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
654 }
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
655 break;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
656
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
657 case ITEM_STRING_LIST:
261
0db02b8d2d11 ISO C99 compatibility fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 215
diff changeset
658 if (*(item->v.list) == NULL) {
138
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
659 if (fprintf(f->file, "#%s = \"\", \"\"\n", item->name) < 0)
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
660 return -3;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
661 } else {
261
0db02b8d2d11 ISO C99 compatibility fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 215
diff changeset
662 qlist_t *node = *(item->v.list);
138
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
663 size_t n = th_llist_length(node);
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
664 if (fprintf(f->file, "%s = ", item->name) < 0)
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
665 return -3;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
666
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
667 while (node != NULL) {
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
668 if (node->data != NULL)
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
669 fprintf(f->file, "\"%s\"", (char *) node->data);
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
670
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
671 if (--n > 0)
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
672 fprintf(f->file, ", ");
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
673
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
674 node = node->next;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
675 }
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
676
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
677 if (fprintf(f->file, "\n") < 0)
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
678 return -3;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
679 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
680 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
681
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
682 case ITEM_INT:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
683 if (fprintf(f->file, "%s = %i\n",
261
0db02b8d2d11 ISO C99 compatibility fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 215
diff changeset
684 item->name, *(item->v.val_int)) < 0)
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
685 return -4;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
686 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
687
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
688 case ITEM_UINT:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
689 if (fprintf(f->file, "%s = %d\n",
261
0db02b8d2d11 ISO C99 compatibility fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 215
diff changeset
690 item->name, *(item->v.val_uint)) < 0)
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
691 return -5;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
692 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
693
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
694 case ITEM_BOOL:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
695 if (fprintf(f->file, "%s = %s\n",
261
0db02b8d2d11 ISO C99 compatibility fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 215
diff changeset
696 item->name, *(item->v.val_bool) ? "yes" : "no") < 0)
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
697 return -6;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
698 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
699
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
700 case ITEM_SECTION:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
701 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
702 int res;
135
26fe4dab7e78 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 133
diff changeset
703 if (fprintf(f->file, "%s = {\n", item->name) < 0)
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
704 return -7;
261
0db02b8d2d11 ISO C99 compatibility fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 215
diff changeset
705 res = th_cfg_write_sect(f, item->v.section, nesting + 1);
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
706 if (res != 0) return res;
135
26fe4dab7e78 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 133
diff changeset
707 if (fprintf(f->file, "}\n\n") < 0)
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
708 return -8;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
709 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
710 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
711
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
712 case ITEM_HEX_TRIPLET:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
713 if (fprintf(f->file, "%s = \"%06x\"\n",
261
0db02b8d2d11 ISO C99 compatibility fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 215
diff changeset
714 item->name, *(item->v.val_int)) < 0)
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
715 return -6;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
716 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
717 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
718 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
719 item = item->next;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
720 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
721
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
722 return 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
723 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
724
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
725
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
726 int th_cfg_write(FILE *outFile, char *filename, cfgitem_t *cfg)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
727 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
728 conffile_t f;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
729
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
730 if (cfg == NULL)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
731 return -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
732
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
733 f.file = outFile;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
734 f.filename = filename;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
735 f.line = 1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
736
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
737 fprintf(outFile, "# Configuration written by %s %s\n\n",
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
738 th_prog_fullname, th_prog_version);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
739
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
740 return th_cfg_write_sect(&f, cfg, 0);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
741 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
742
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
743