annotate th_config.c @ 138:3e221c16b087

Improvements in configuration file handing.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 02 Nov 2010 21:46:16 +0200
parents 26fe4dab7e78
children c6c3825376c9
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)
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 */
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;
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
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);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
230 fprintf(stderr, "%s: '%s', line #%d: ", th_prog_name, f->filename, f->line);
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:
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
429 prevMode = parseMode;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
430 parseMode = PM_STRING;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
431 break;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
432 }
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
433 } else {
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
434 prevMode = parseMode;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
435 parseMode = PM_NORMAL;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
436 }
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
437 break;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
438
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
439 case PM_SECTION:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
440 /* Section parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
441 if (c != '{') {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
442 /* Error! Section start '{' expected! */
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 "Unexpected character '%c', section start '{' was expected.\n", c);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
445 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
446 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
447 int res = th_cfg_read_sect(f, (cfgitem_t *) item->data, nesting + 1);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
448 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
449 if (res > 0)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
450 validError = TRUE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
451 else if (res < 0)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
452 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
453 else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
454 prevMode = parseMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
455 parseMode = PM_NORMAL;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
456 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
457 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
458 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
459
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
460 case PM_STRING:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
461 /* String parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
462 if (isStart) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
463 /* Start of string, get delimiter */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
464 tmpCh = c;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
465 isStart = FALSE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
466 strPos = 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
467 } else if (c == tmpCh) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
468 /* End of string, set the value */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
469 tmpStr[strPos] = 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
470
138
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
471 switch (item->type) {
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
472 case ITEM_HEX_TRIPLET:
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
473 *(int *) item->data = th_get_hex_triplet(tmpStr);
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
474 prevMode = parseMode;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
475 parseMode = PM_NORMAL;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
476 break;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
477 case ITEM_STRING:
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
478 th_pstrcpy((char **) item->data, tmpStr);
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
479 prevMode = parseMode;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
480 parseMode = PM_NORMAL;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
481 break;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
482 case ITEM_STRING_LIST:
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
483 // th_cfg_add_to_array(item, th_strdup(tmpStr));
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
484 prevMode = parseMode;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
485 parseMode = PM_NEXT;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
486 nextMode = PM_ARRAY;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
487 break;
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
488 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
489
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
490 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
491 /* Add character to string */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
492 VADDCH(c)
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 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
495 /* Error! String too long! */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
496 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
497 "String too long! Maximum is %d characters.",
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
498 SET_MAX_BUF);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
499 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
500 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
501 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
502
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
503 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
504 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
505
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
506 case PM_INT:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
507 /* Integer parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
508 if (isStart && item->type == ITEM_UINT && c == '-') {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
509 /* Error! Negative values not allowed for unsigned ints */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
510 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
511 "Negative value specified for %s, unsigned value expected.",
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
512 item->name);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
513 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
514 } else if (isStart && (c == '-' || c == '+')) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
515 VADDCH(c)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
516 else
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
517 isError = TRUE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
518 } else if (th_isdigit(c)) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
519 VADDCH(c)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
520 else
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
521 isError = TRUE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
522 } else if (VISEND(c)) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
523 /* End of integer parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
524 tmpStr[strPos] = 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
525 switch (item->type) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
526 case ITEM_INT:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
527 *((int *) item->data) = atoi(tmpStr);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
528 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
529
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
530 case ITEM_UINT:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
531 *((unsigned int *) item->data) = atol(tmpStr);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
532 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
533 }
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 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
538 /* Error! Unexpected character. */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
539 th_cfg_error(f,
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
540 "Unexpected character '%c' for integer setting '%s'.",
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
541 c, item->name);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
542 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
543 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
544
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
545 if (isError) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
546 /* Error! String too long! */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
547 th_cfg_error(f, "String too long! Maximum is %d characters.",
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
548 SET_MAX_BUF);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
549 parseMode = PM_ERROR;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
550 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
551
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
552 isStart = FALSE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
553 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
554 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
555
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
556 case PM_BOOL:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
557 /* Boolean parsing mode */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
558 if (isStart) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
559 tmpCh = c;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
560 isStart = FALSE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
561 } else if (VISEND(c)) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
562 BOOL tmpBool;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
563
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
564 /* End of boolean parsing */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
565 switch (tmpCh) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
566 case 'Y': case 'y':
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
567 case 'T': case 't':
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
568 case '1':
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
569 tmpBool = TRUE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
570 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
571
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
572 case 'N': case 'n':
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
573 case 'F': case 'f':
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
574 case '0':
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
575 tmpBool = FALSE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
576 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
577
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
578 default:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
579 isError = TRUE;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
580 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
581
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
582 if (isError) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
583 th_cfg_error(f, "Invalid boolean value for '%s'.\n", item->name);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
584 parseMode = PM_ERROR;
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 *((BOOL *) item->data) = tmpBool;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
587
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
588 prevMode = parseMode;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
589 parseMode = PM_NORMAL;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
590 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
591 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
592 c = -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
593 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
594 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
595 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
596
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
597 /* Check for validation errors */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
598 if (validError)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
599 return 1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
600
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
601 /* Return result */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
602 if (parseMode == PM_ERROR)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
603 return -2;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
604 else
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
605 return 0;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
606 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
607
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
608
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
609 int th_cfg_read(FILE *inFile, char *filename, cfgitem_t * cfg)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
610 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
611 conffile_t f;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
612
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
613 f.file = inFile;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
614 f.filename = filename;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
615 f.line = 1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
616
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
617 return th_cfg_read_sect(&f, cfg, 0);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
618 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
619
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
620
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
621 /* Write a configuration into file
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
622 */
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
623 static void th_print_indent(conffile_t *f, int nesting)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
624 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
625 int i;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
626 for (i = 0; i < nesting * 2; i++)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
627 fputc(' ', f->file);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
628 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
629
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
630
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
631 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
632 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
633 while (item != NULL) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
634 if (item->type == ITEM_COMMENT) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
635 th_print_indent(f, nesting);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
636 if (fprintf(f->file, "# %s\n", (item->name != NULL) ? item->name : "" ) < 0)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
637 return -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
638 } else
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
639 if (item->name != NULL) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
640 th_print_indent(f, nesting);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
641
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
642 switch (item->type) {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
643 case ITEM_STRING:
138
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
644 if (*(item->val_str) == NULL) {
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
645 if (fprintf(f->file, "#%s = \"\"\n", item->name) < 0)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
646 return -3;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
647 } else {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
648 if (fprintf(f->file, "%s = \"%s\"\n",
138
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
649 item->name, *(item->val_str)) < 0)
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
650 return -3;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
651 }
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
652 break;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
653
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
654 case ITEM_STRING_LIST:
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
655 if (*(item->list) == NULL) {
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
656 if (fprintf(f->file, "#%s = \"\", \"\"\n", item->name) < 0)
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
657 return -3;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
658 } else {
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
659 qlist_t *node = *(item->list);
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
660 size_t n = th_llist_length(node);
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
661 if (fprintf(f->file, "%s = ", item->name) < 0)
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
662 return -3;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
663
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
664 while (node != NULL) {
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
665 if (node->data != NULL)
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
666 fprintf(f->file, "\"%s\"", (char *) node->data);
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
667
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
668 if (--n > 0)
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
669 fprintf(f->file, ", ");
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 node = node->next;
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
672 }
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 if (fprintf(f->file, "\n") < 0)
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
675 return -3;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
676 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
677 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
678
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
679 case ITEM_INT:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
680 if (fprintf(f->file, "%s = %i\n",
138
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
681 item->name, *(item->val_int)) < 0)
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
682 return -4;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
683 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
684
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
685 case ITEM_UINT:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
686 if (fprintf(f->file, "%s = %d\n",
138
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
687 item->name, *(item->val_uint)) < 0)
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
688 return -5;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
689 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
690
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
691 case ITEM_BOOL:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
692 if (fprintf(f->file, "%s = %s\n",
138
3e221c16b087 Improvements in configuration file handing.
Matti Hamalainen <ccr@tnsp.org>
parents: 135
diff changeset
693 item->name, *(item->val_bool) ? "yes" : "no") < 0)
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
694 return -6;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
695 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
696
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
697 case ITEM_SECTION:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
698 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
699 int res;
135
26fe4dab7e78 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 133
diff changeset
700 if (fprintf(f->file, "%s = {\n", item->name) < 0)
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
701 return -7;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
702 res = th_cfg_write_sect(f, (cfgitem_t *) item->data, nesting + 1);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
703 if (res != 0) return res;
135
26fe4dab7e78 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 133
diff changeset
704 if (fprintf(f->file, "}\n\n") < 0)
133
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
705 return -8;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
706 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
707 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
708
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
709 case ITEM_HEX_TRIPLET:
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
710 if (fprintf(f->file, "%s = \"%06x\"\n",
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
711 item->name, *((int *) item->data)) < 0)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
712 return -6;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
713 break;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
714 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
715 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
716 item = item->next;
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 return 0;
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
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
723 int th_cfg_write(FILE *outFile, char *filename, cfgitem_t *cfg)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
724 {
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
725 conffile_t f;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
726
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
727 if (cfg == NULL)
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
728 return -1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
729
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
730 f.file = outFile;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
731 f.filename = filename;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
732 f.line = 1;
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
733
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
734 fprintf(outFile, "# Configuration written by %s %s\n\n",
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
735 th_prog_fullname, th_prog_version);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
736
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
737 return th_cfg_write_sect(&f, cfg, 0);
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
738 }
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
739
ffe8bbd429fa Config file.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
740