annotate th_config.c @ 511:934369fafd5d

Bump copyrights.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 26 Dec 2019 08:26:10 +0200
parents 7d4cdc0b1aa4
children db3fc3d4969e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * Very simple configuration handling functions
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Programmed and designed by Matti 'ccr' Hamalainen
511
934369fafd5d Bump copyrights.
Matti Hamalainen <ccr@tnsp.org>
parents: 510
diff changeset
4 * (C) Copyright 2004-2019 Tecnic Software productions (TNSP)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 *
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 * Please read file 'COPYING' for information on license and distribution.
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 */
49
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
8 #include "th_util.h"
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
9 #include "th_config.h"
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
10 #include "th_string.h"
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 #include <stdio.h>
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 #include <stdarg.h>
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
14
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
15 #define SET_MAX_BUF (8192)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 /* Free a given configuration (the values are not free'd)
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 */
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
20 void th_cfg_free(th_cfgitem_t *cfg)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 {
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
22 th_cfgitem_t *node = cfg;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
24 while (node != NULL)
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
25 {
155
23a79bd6c9d6 Use th_llist for th_config module as well instead of duped linked list
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
26 th_cfgitem_t *next = (th_cfgitem_t *) node->node.next;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
27
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
28 if (node->type == ITEM_SECTION)
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
29 th_cfg_free((th_cfgitem_t *) node->v.data);
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
30
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
31 th_free(node->name);
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
32 th_free(node);
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
33 node = next;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
34 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 /* Allocate and add new item to configuration
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 */
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
40 static th_cfgitem_t *th_cfg_add(th_cfgitem_t **cfg, const char *name,
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
41 const int type, void *data)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 {
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
43 th_cfgitem_t *node;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
45 if (cfg == NULL)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
46 return NULL;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
48 // Allocate new item
125
0ac59c798773 Use th_malloc0() where appropriate.
Matti Hamalainen <ccr@tnsp.org>
parents: 110
diff changeset
49 node = (th_cfgitem_t *) th_malloc0(sizeof(th_cfgitem_t));
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
50 if (node == NULL)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
51 return NULL;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
53 // Set values
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
54 node->type = type;
22
1ac2449c4df7 ISO C99 compatibility fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
55 node->v.data = data;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
56 node->name = th_strdup(name);
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
57
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
58 // Insert into linked list
155
23a79bd6c9d6 Use th_llist for th_config module as well instead of duped linked list
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
59 th_llist_append_node((th_llist_t **) cfg, (th_llist_t *) node);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
61 return node;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 /* Add integer type setting into give configuration
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 */
170
8e69e07e5aac Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
67 int th_cfg_add_int(th_cfgitem_t **cfg, const char *name, int *itemData, int defValue)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 {
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
69 th_cfgitem_t *node;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
71 node = th_cfg_add(cfg, name, ITEM_INT, (void *) itemData);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
72 if (node == NULL)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
73 return -1;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74
170
8e69e07e5aac Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
75 *itemData = defValue;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
77 return 0;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
81 int th_cfg_add_hexvalue(th_cfgitem_t **cfg, const char *name,
498
1dbd9259c3b8 Change hexvalue type to unsigned.
Matti Hamalainen <ccr@tnsp.org>
parents: 497
diff changeset
82 unsigned int *itemData, unsigned int defValue)
15
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
83 {
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
84 th_cfgitem_t *node;
15
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
85
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
86 node = th_cfg_add(cfg, name, ITEM_HEX_TRIPLET, (void *) itemData);
15
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
87 if (node == NULL)
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
88 return -1;
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
89
170
8e69e07e5aac Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
90 *itemData = defValue;
15
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
91
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
92 return 0;
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
93 }
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
94
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
95
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 /* Add unsigned integer type setting into give configuration
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 */
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
98 int th_cfg_add_uint(th_cfgitem_t **cfg, const char *name,
170
8e69e07e5aac Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
99 unsigned int *itemData, unsigned int defValue)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 {
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
101 th_cfgitem_t *node;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
103 node = th_cfg_add(cfg, name, ITEM_UINT, (void *) itemData);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
104 if (node == NULL)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
105 return -1;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106
170
8e69e07e5aac Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
107 *itemData = defValue;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
109 return 0;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 /* Add strint type setting into given configuration
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 */
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
115 int th_cfg_add_string(th_cfgitem_t **cfg, const char *name,
170
8e69e07e5aac Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
116 char **itemData, char *defValue)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 {
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
118 th_cfgitem_t *node;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
120 node = th_cfg_add(cfg, name, ITEM_STRING, (void *) itemData);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
121 if (node == NULL)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
122 return -1;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123
170
8e69e07e5aac Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
124 *itemData = th_strdup(defValue);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
126 return 0;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 /* Add boolean type setting into given configuration
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 */
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
132 int th_cfg_add_bool(th_cfgitem_t **cfg, const char *name,
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
133 BOOL *itemData, BOOL defValue)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 {
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
135 th_cfgitem_t *node;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
136
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
137 node = th_cfg_add(cfg, name, ITEM_BOOL, (void *) itemData);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
138 if (node == NULL)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
139 return -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
140
170
8e69e07e5aac Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
141 *itemData = defValue;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
142
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
143 return 0;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
144 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
145
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
147 /* Add implicit comment
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
148 */
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
149 int th_cfg_add_comment(th_cfgitem_t **cfg, const char *comment)
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
150 {
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
151 th_cfgitem_t *node;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
152
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
153 node = th_cfg_add(cfg, comment, ITEM_COMMENT, NULL);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
154 if (node == NULL)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
155 return -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
156
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
157 return 0;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
158 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
161 /* Add new section
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
162 */
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
163 int th_cfg_add_section(th_cfgitem_t **cfg, const char *name, th_cfgitem_t *sect)
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
164 {
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
165 th_cfgitem_t *node;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
166
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
167 node = th_cfg_add(cfg, name, ITEM_SECTION, (void *) sect);
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
168 if (node == NULL)
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
169 return -1;
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
170
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
171 return 0;
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
172 }
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
173
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
174
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 151
diff changeset
175 int th_cfg_add_string_list(th_cfgitem_t **cfg, const char *name, th_llist_t **data)
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
176 {
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
177 th_cfgitem_t *node;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
178
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
179 if (data == NULL)
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
180 return -5;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
181
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
182 node = th_cfg_add(cfg, name, ITEM_STRING_LIST, (void *) data);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
183 if (node == NULL)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
184 return -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
185
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
186 return 0;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
188
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190 /* Read a given file into configuration structure and variables
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191 */
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
192 enum
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
193 {
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
194 PM_EOF,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
195 PM_ERROR,
159
fc914ff7a9b8 Rename a constant.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
196 PM_IDLE,
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
197 PM_COMMENT,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
198 PM_NEXT,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
199 PM_KEYNAME,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
200 PM_KEYSET,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
201 PM_STRING,
170
8e69e07e5aac Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
202 PM_NUMERIC,
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
203 PM_BOOL,
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
204 PM_SECTION,
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
205 PM_LIST,
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
206
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
207 PM_LAST
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
208 };
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
209
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
210
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
211 typedef struct
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
212 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
213 int ch, strDelim,
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
214 prevMode, nextMode, parseMode;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
215 } th_cfgparserctx_t;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
216
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
217
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
218 static void th_cfg_set_parsemode(th_cfgparserctx_t *ctx, const int mode)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
219 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
220 ctx->prevMode = ctx->parseMode;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
221 ctx->parseMode = mode;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
222 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
223
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
224
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
225 static void th_cfg_set_next_parsemode(th_cfgparserctx_t *ctx, const int mode)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
226 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
227 th_cfg_set_parsemode(ctx, PM_NEXT);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
228 ctx->nextMode = mode;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
229 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
230
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
231
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
232 #define VADDCH(ch) if (strPos < SET_MAX_BUF) { tmpStr[strPos++] = ch; }
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
233
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
234
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
235 static BOOL th_cfg_is_end(const int ch)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
236 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
237 return
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
238 ch == '\r' ||
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
239 ch == '\n' ||
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
240 ch == ';' ||
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
241 ch == '#' ||
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
242 th_isspace(ch);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
243 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
244
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
245
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
246 static int th_cfg_get_parsemode(const int type)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
247 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
248 switch (type)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
249 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
250 case ITEM_HEX_TRIPLET:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
251 case ITEM_STRING:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
252 return PM_STRING;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
253
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
254 case ITEM_INT:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
255 case ITEM_UINT:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
256 case ITEM_FLOAT:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
257 return PM_NUMERIC;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
258
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
259 case ITEM_BOOL:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
260 return PM_BOOL;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
261
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
262 case ITEM_SECTION:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
263 return PM_SECTION;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
264
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
265 case ITEM_STRING_LIST:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
266 return PM_LIST;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
267
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
268 default:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
269 return PM_ERROR;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
270 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
271 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
272
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
273
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
274 static BOOL th_cfg_set_item(th_cfgparserctx_t *ctx, th_cfgitem_t *item, const char *str)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
275 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
276 BOOL ret = TRUE;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
277
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
278 switch (item->type)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
279 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
280 case ITEM_HEX_TRIPLET:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
281 ret = th_get_hex_triplet(str, item->v.val_uint);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
282 break;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
283
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
284 case ITEM_STRING:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
285 th_pstr_cpy(item->v.val_str, str);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
286 break;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
287
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
288 case ITEM_STRING_LIST:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
289 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
290 char *tmps;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
291 if ((tmps = th_strdup(str)) == NULL)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
292 ret = FALSE;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
293 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
294 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
295 th_llist_append(item->v.list, tmps);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
296 th_cfg_set_next_parsemode(ctx, PM_LIST);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
297 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
298 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
299 return ret;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
300
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
301 case ITEM_INT:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
302 *(item->v.val_int) = atoi(str);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
303 break;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
304
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
305 case ITEM_UINT:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
306 *(item->v.val_uint) = atol(str);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
307 break;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
308
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
309 case ITEM_FLOAT:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
310 *(item->v.val_float) = atof(str);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
311 break;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
312
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
313 case ITEM_BOOL:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
314 ret = th_get_boolean(str, item->v.val_bool);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
315 break;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
316
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
317 default:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
318 ret = FALSE;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
319 break;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
320 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
321
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
322 th_cfg_set_parsemode(ctx, ret ? PM_IDLE : PM_ERROR);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
323 return ret;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
324 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
325
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
326
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
327 static int th_cfg_read_sect(th_ioctx *fh, th_cfgitem_t *sect, int nesting)
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
328 {
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
329 th_cfgparserctx_t ctx;
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
330 th_cfgitem_t *item = NULL;
110
d739df7efba7 Use dynamic allocation for one buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 83
diff changeset
331 char *tmpStr = NULL;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
332 size_t strPos;
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
333 BOOL isEscaped, isFound, isStart, isError, validError, fpSet;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
334
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
335 // Initialize values
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
336 memset(&ctx, 0, sizeof(ctx));
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
337 ctx.ch = -1;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
338 strPos = 0;
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
339 ctx.nextMode = ctx.prevMode = ctx.parseMode = PM_IDLE;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
340 isEscaped = fpSet = isFound = isStart = isError = validError = FALSE;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
341
110
d739df7efba7 Use dynamic allocation for one buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 83
diff changeset
342 if ((tmpStr = th_malloc(SET_MAX_BUF + 1)) == NULL)
d739df7efba7 Use dynamic allocation for one buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 83
diff changeset
343 goto out;
d739df7efba7 Use dynamic allocation for one buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 83
diff changeset
344
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
345 // Parse the configuration
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
346 while (ctx.parseMode != PM_EOF && ctx.parseMode != PM_ERROR)
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
347 {
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
348 if (ctx.ch == -1)
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
349 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
350 // Get next character
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
351 switch (ctx.ch = thfgetc(fh))
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
352 {
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
353 case EOF:
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
354 if (ctx.parseMode != PM_IDLE)
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
355 {
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
356 th_io_error(fh, THERR_OUT_OF_DATA, "Unexpected end of file.\n");
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
357 ctx.parseMode = PM_ERROR;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
358 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
359 else
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
360 ctx.parseMode = PM_EOF;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
361 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
362
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
363 case '\n':
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
364 fh->line++;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
365 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
366 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
367
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
368 switch (ctx.parseMode)
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
369 {
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
370 case PM_COMMENT:
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
371 // Comment parsing mode
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
372 if (ctx.ch == '\n')
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
373 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
374 // End of line, end of comment
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
375 th_cfg_set_parsemode(&ctx, ctx.prevMode);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
376 }
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
377 ctx.ch = -1;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
378 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
379
159
fc914ff7a9b8 Rename a constant.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
380 case PM_IDLE:
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
381 // Normal parsing mode
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
382 if (ctx.ch == '#')
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
383 {
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
384 th_cfg_set_parsemode(&ctx, PM_COMMENT);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
385 ctx.ch = -1;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
386 }
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
387 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
388 if (th_cfg_is_end(ctx.ch))
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
389 {
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
390 ctx.ch = -1;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
391 }
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
392 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
393 if (ctx.ch == '}')
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
394 {
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
395 if (nesting > 0)
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
396 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
397 // Check for validation errors
110
d739df7efba7 Use dynamic allocation for one buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 83
diff changeset
398 goto out;
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
399 }
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
400 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
401 {
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
402 th_io_error(fh, THERR_INVALID_DATA,
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
403 "Invalid nesting sequence encountered.\n");
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
404 ctx.parseMode = PM_ERROR;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
405 }
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
406 }
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
407 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
408 if (th_isalpha(ctx.ch))
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
409 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
410 // Start of key name found
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
411 th_cfg_set_parsemode(&ctx, PM_KEYNAME);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
412 strPos = 0;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
413 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
414 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
415 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
416 // Error! Invalid character found
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
417 th_io_error(fh, THERR_INVALID_DATA,
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
418 "Unexpected character '%c'.\n", ctx.ch);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
419 ctx.parseMode = PM_ERROR;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
420 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
421 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
422
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
423 case PM_KEYNAME:
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
424 // Configuration KEY name parsing mode
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
425 if (ctx.ch == '#')
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
426 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
427 // Start of comment
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
428 th_cfg_set_parsemode(&ctx, PM_COMMENT);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
429 ctx.ch = -1;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
430 }
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
431 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
432 if (th_iscrlf(ctx.ch) || th_isspace(ctx.ch) || ctx.ch == '=')
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
433 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
434 // End of key name
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
435 th_cfg_set_next_parsemode(&ctx, PM_KEYSET);
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
436 }
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
437 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
438 if (th_isalnum(ctx.ch) || ctx.ch == '_' || ctx.ch == '-')
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
439 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
440 // Add to key name string
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
441 VADDCH(ctx.ch)
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
442 else
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
443 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
444 // Error! Key name string too long!
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
445 th_io_error(fh, THERR_INVALID_DATA, "Config key name too long!");
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
446 ctx.parseMode = PM_ERROR;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
447 }
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
448 ctx.ch = -1;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
449 tmpStr[strPos] = 0;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
450 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
451 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
452 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
453 // Error! Invalid character found
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
454 tmpStr[strPos] = 0;
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
455 th_io_error(fh, THERR_INVALID_DATA,
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
456 "Unexpected character '%c' in key name '%s'.\n",
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
457 ctx.ch, tmpStr);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
458 ctx.parseMode = PM_ERROR;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
459 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
460 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
461
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
462 case PM_KEYSET:
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
463 if (ctx.ch == '=')
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
464 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
465 // Find key from configuration
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
466 tmpStr[strPos] = 0;
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
467 isFound = FALSE;
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
468 item = sect;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
469 while (item != NULL && !isFound)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
470 {
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
471 if (item->name != NULL && strcmp(item->name, tmpStr) == 0)
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
472 isFound = TRUE;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
473 else
155
23a79bd6c9d6 Use th_llist for th_config module as well instead of duped linked list
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
474 item = (th_cfgitem_t *) item->node.next;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
475 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
476
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
477 // Check if key was found
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
478 if (isFound)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
479 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
480 // Okay, set next mode
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
481 th_cfg_set_next_parsemode(&ctx, th_cfg_get_parsemode(item->type));
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
482 isStart = TRUE;
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
483 fpSet = FALSE;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
484 strPos = 0;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
485 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
486 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
487 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
488 // Error! No configuration key by this name found
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
489 th_io_error(fh, THERR_INVALID_DATA,
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
490 "No such configuration setting ('%s')\n",
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
491 tmpStr);
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
492 ctx.parseMode = PM_ERROR;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
493 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
494
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
495 ctx.ch = -1;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
496 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
497 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
498 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
499 // Error! '=' expected!
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
500 th_io_error(fh, THERR_INVALID_DATA,
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
501 "Unexpected character '%c', assignation '=' was expected.\n",
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
502 ctx.ch);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
503 ctx.parseMode = PM_ERROR;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
504 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
505 break;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
506
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
507 case PM_NEXT:
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
508 // Search next item parsing mode
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
509 if (ctx.ch == '#')
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
510 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
511 // Start of comment
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
512 th_cfg_set_parsemode(&ctx, PM_COMMENT);
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
513 }
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
514 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
515 if (th_isspace(ctx.ch) || th_iscrlf(ctx.ch))
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
516 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
517 // Ignore whitespaces and linechanges
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
518 ctx.ch = -1;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
519 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
520 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
521 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
522 // Next item found
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
523 th_cfg_set_parsemode(&ctx, ctx.nextMode);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
524 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
525 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
526
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
527 case PM_LIST:
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
528 if (isStart)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
529 {
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
530 th_cfg_set_parsemode(&ctx, PM_STRING);
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
531 }
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
532 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
533 if (ctx.ch == ',')
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
534 {
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
535 th_cfg_set_next_parsemode(&ctx, PM_STRING);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
536 ctx.ch = -1;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
537 isStart = TRUE;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
538 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
539 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
540 {
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
541 th_cfg_set_parsemode(&ctx, PM_IDLE);
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
542 }
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
543 break;
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
544
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
545 case PM_SECTION:
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
546 // Section parsing mode
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
547 if (ctx.ch != '{')
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
548 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
549 // Error! Section start '{' expected!
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
550 th_io_error(fh, THERR_INVALID_DATA,
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
551 "Unexpected character '%c', section start '{' was expected.\n",
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
552 ctx.ch);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
553 ctx.parseMode = PM_ERROR;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
554 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
555 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
556 {
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
557 int res = th_cfg_read_sect(fh, item->v.section, nesting + 1);
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
558 ctx.ch = -1;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
559 if (res > 0)
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
560 validError = TRUE;
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
561 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
562 if (res < 0)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
563 ctx.parseMode = PM_ERROR;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
564 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
565 {
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
566 th_cfg_set_parsemode(&ctx, PM_IDLE);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
567 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
568 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
569 break;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
570
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
571 case PM_STRING:
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
572 // String parsing mode
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
573 if (isStart)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
574 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
575 // Start of string, get delimiter
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
576 ctx.strDelim = ctx.ch;
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
577 isStart = FALSE;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
578 strPos = 0;
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
579 isEscaped = FALSE;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
580 }
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
581 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
582 if (!isEscaped && ctx.ch == ctx.strDelim)
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
583 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
584 // End of string, set the value
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
585 tmpStr[strPos] = 0;
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
586 th_cfg_set_item(&ctx, item, tmpStr);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
587 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
588 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
589 if (!isEscaped && ctx.ch == '\\')
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
590 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
591 // Escape sequence
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
592 isEscaped = TRUE;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
593 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
594 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
595 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
596 // Add character to string
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
597 VADDCH(ctx.ch)
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
598 else
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
599 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
600 // Error! String too long!
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
601 th_io_error(fh, THERR_INVALID_DATA,
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
602 "String too long! Maximum is %d characters.",
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
603 SET_MAX_BUF);
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
604 ctx.parseMode = PM_ERROR;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
605 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
606 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
607
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
608 ctx.ch = -1;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
609 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
610
170
8e69e07e5aac Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
611 case PM_NUMERIC:
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
612 // Integer parsing mode
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
613 if (isStart && item->type == ITEM_UINT && ctx.ch == '-')
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
614 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
615 // Error! Negative values not allowed for unsigned ints
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
616 th_io_error(fh, THERR_INVALID_DATA,
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
617 "Negative value specified for %s, unsigned value expected.",
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
618 item->name);
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
619 ctx.parseMode = PM_ERROR;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
620 }
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
621 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
622 if (isStart && (ctx.ch == '-' || ctx.ch == '+'))
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
623 {
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
624 VADDCH(ctx.ch)
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
625 else
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
626 isError = TRUE;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
627 }
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
628 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
629 if (isStart && item->type == ITEM_FLOAT && ctx.ch == '.')
172
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
630 {
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
631 fpSet = TRUE;
172
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
632 VADDCH('0')
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
633 else
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
634 isError = TRUE;
172
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
635
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
636 VADDCH(ctx.ch)
172
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
637 else
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
638 isError = TRUE;
172
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
639 }
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
640 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
641 if (item->type == ITEM_FLOAT && ctx.ch == '.' && !fpSet)
172
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
642 {
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
643 fpSet = TRUE;
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
644 VADDCH(ctx.ch)
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
645 else
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
646 isError = TRUE;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
647 }
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
648 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
649 if (th_isdigit(ctx.ch))
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
650 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
651 VADDCH(ctx.ch)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
652 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
653 isError = TRUE;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
654 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
655 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
656 if (th_cfg_is_end(ctx.ch))
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
657 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
658 // End of integer parsing mode
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
659 tmpStr[strPos] = 0;
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
660 th_cfg_set_item(&ctx, item, tmpStr);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
661 th_cfg_set_parsemode(&ctx, PM_IDLE);
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
662 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
663 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
664 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
665 // Error! Unexpected character.
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
666 th_io_error(fh, THERR_INVALID_DATA,
170
8e69e07e5aac Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
667 "Unexpected character '%c' for numeric setting '%s'.",
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
668 ctx.ch, item->name);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
669 ctx.parseMode = PM_ERROR;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
670 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
671
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
672 if (isError)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
673 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
674 // Error! String too long!
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
675 th_io_error(fh, THERR_INVALID_DATA,
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
676 "String too long! Maximum is %d characters.",
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
677 SET_MAX_BUF);
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
678 ctx.parseMode = PM_ERROR;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
679 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
680
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
681 isStart = FALSE;
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
682 ctx.ch = -1;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
683 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
684
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
685 case PM_BOOL:
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
686 // Boolean parsing mode
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
687 if (isStart)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
688 {
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
689 isStart = FALSE;
168
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
690 strPos = 0;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
691 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
692
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
693 if (th_isalnum(ctx.ch))
168
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
694 {
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
695 VADDCH(ctx.ch)
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
696 else
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
697 isError = TRUE;
168
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
698 }
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
699 else
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
700 if (th_cfg_is_end(ctx.ch))
168
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
701 {
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
702 tmpStr[strPos] = 0;
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
703 isError = !th_cfg_set_item(&ctx, item, tmpStr);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
704 }
168
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
705
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
706 if (isError)
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
707 {
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
708 th_io_error(fh, THERR_INVALID_DATA,
168
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
709 "Invalid boolean value for '%s'.\n",
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
710 item->name);
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
711 ctx.parseMode = PM_ERROR;
168
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
712 }
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
713 ctx.ch = -1;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
714 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
715 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
716 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
717
110
d739df7efba7 Use dynamic allocation for one buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 83
diff changeset
718 out:
d739df7efba7 Use dynamic allocation for one buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 83
diff changeset
719 th_free(tmpStr);
d739df7efba7 Use dynamic allocation for one buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 83
diff changeset
720
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
721 // Check for validation errors
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
722 if (validError)
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
723 return -91;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
724
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
725 // Return result
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
726 if (ctx.parseMode == PM_ERROR)
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
727 return -2;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
728 else
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
729 return 0;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
730 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
731
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
732
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
733 int th_cfg_read(th_ioctx *fh, th_cfgitem_t *cfg)
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
734 {
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
735 if (fh == NULL || cfg == NULL)
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
736 return -1;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
737
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
738 return th_cfg_read_sect(fh, cfg, 0);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
739 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
740
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
741
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
742 /* Write a configuration into file
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
743 */
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
744 static BOOL th_print_indent_a(th_ioctx *fh, const int nesting, const char *fmt, va_list ap)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
745 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
746 for (int i = 0; i < nesting * 4; i++)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
747 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
748 if (thfputc(' ', fh) == EOF)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
749 return FALSE;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
750 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
751
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
752 return thvfprintf(fh, fmt, ap) >= 0;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
753 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
754
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
755
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
756 static BOOL th_print_indent(th_ioctx *fh, const int nesting, const char *fmt, ...)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
757 {
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
758 BOOL ret;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
759 va_list ap;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
760 va_start(ap, fmt);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
761 ret = th_print_indent_a(fh, nesting, fmt, ap);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
762 va_end(ap);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
763 return ret;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
764 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
765
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
766
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
767 static BOOL th_cfg_is_item_valid(const th_cfgitem_t *item)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
768 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
769 switch (item->type)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
770 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
771 case ITEM_STRING:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
772 return (*(item->v.val_str) != NULL);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
773
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
774 case ITEM_STRING_LIST:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
775 return (*(item->v.list) != NULL);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
776
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
777 case ITEM_SECTION:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
778 return TRUE;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
779
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
780 case ITEM_INT:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
781 case ITEM_UINT:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
782 case ITEM_FLOAT:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
783 case ITEM_BOOL:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
784 case ITEM_HEX_TRIPLET:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
785 return TRUE;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
786
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
787 default:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
788 return FALSE;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
789 }
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
790 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
791
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
792
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
793 static BOOL th_cfg_write_string_escaped(th_ioctx *fh, const char *str, const char delim)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
794 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
795 for (const char *ptr = str; *ptr; ptr++)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
796 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
797 if (*ptr == delim)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
798 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
799 if (thfputc('\\', fh) == EOF ||
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
800 thfputc(*ptr, fh) == EOF)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
801 return FALSE;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
802 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
803 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
804 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
805 if (thfputc(*ptr, fh) == EOF)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
806 return FALSE;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
807 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
808 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
809
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
810 return TRUE;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
811 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
812
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
813
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
814 static int th_cfg_write_item(th_ioctx *fh, const th_cfgitem_t *item)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
815 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
816 switch (item->type)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
817 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
818 case ITEM_STRING:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
819 if (thfputc('"', fh) != EOF &&
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
820 th_cfg_write_string_escaped(fh, *(item->v.val_str), '"') &&
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
821 thfputc('"', fh) != EOF)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
822 return 1;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
823 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
824 return -1;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
825
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
826 case ITEM_INT:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
827 return thfprintf(fh, "%d", *(item->v.val_int));
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
828
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
829 case ITEM_UINT:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
830 return thfprintf(fh, "%u", *(item->v.val_uint));
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
831
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
832 case ITEM_FLOAT:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
833 return thfprintf(fh, "%1.5f", *(item->v.val_float));
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
834
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
835 case ITEM_BOOL:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
836 return thfprintf(fh, "%s", *(item->v.val_bool) ? "yes" : "no");
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
837
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
838 case ITEM_HEX_TRIPLET:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
839 return thfprintf(fh, "\"%06x\"", *(item->v.val_int));
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
840
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
841 default:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
842 return -1;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
843 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
844 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
845
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
846
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
847 static int th_cfg_write_sect(th_ioctx *fh, const th_cfgitem_t *item, const int nesting)
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
848 {
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
849 while (item != NULL)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
850 {
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
851 if (item->name == NULL)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
852 return -1;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
853
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
854 if (item->type == ITEM_COMMENT)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
855 {
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
856 BOOL lineStart = TRUE, lineFeed = FALSE;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
857
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
858 for (const char *ptr = item->name; *ptr; ptr++)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
859 switch (*ptr)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
860 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
861 case '\r':
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
862 case '\n':
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
863 lineStart = lineFeed = TRUE;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
864 break;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
865
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
866 default:
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
867 if (lineFeed)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
868 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
869 thfprintf(fh, "\n");
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
870 lineFeed = FALSE;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
871 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
872 if (lineStart)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
873 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
874 if (!th_print_indent(fh, nesting, "# "))
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
875 return -3;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
876
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
877 lineStart = FALSE;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
878 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
879
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
880 if (thfputc(*ptr, fh) == EOF)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
881 return -9;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
882 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
883
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
884 if (!lineFeed)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
885 thfprintf(fh, "\n");
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
886 }
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
887 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
888 if (item->type == ITEM_SECTION)
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
889 {
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
890 int res;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
891
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
892 if (!th_print_indent(fh, nesting, "%s = {\n", item->name))
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
893 return -3;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
894
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
895 if ((res = th_cfg_write_sect(fh, item->v.section, nesting + 1)) != 0)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
896 return res;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
897
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
898 if (!th_print_indent(fh, nesting, "}\n\n"))
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
899 return -3;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
900 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
901 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
902 if (item->type == ITEM_STRING_LIST)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
903 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
904 if (!th_cfg_is_item_valid(item))
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
905 {
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
906 if (!th_print_indent(fh, nesting, "#%s = \"\", \"\"", item->name))
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
907 return -3;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
908 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
909 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
910 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
911 th_llist_t *node = *(item->v.list);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
912 size_t n = th_llist_length(node);
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
913
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
914 if (!th_print_indent(fh, nesting, "%s = \n", item->name))
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
915 return -3;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
916
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
917 for (; node != NULL; node = node->next)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
918 if (node->data != NULL)
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
919 {
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
920 if (!th_print_indent(fh, nesting, "\"%s\"%s\n",
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
921 (char *) node->data,
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
922 --n > 0 ? "," : ""))
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
923 return -3;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
924 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
925
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
926 if (!th_print_indent(fh, nesting, "\n"))
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
927 return -3;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
928 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
929 }
509
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
930 else
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
931 {
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
932 if (!th_print_indent(fh, nesting, "%s%s = ",
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
933 th_cfg_is_item_valid(item) ? "" : "#",
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
934 item->name))
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
935 return -3;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
936
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
937 if (th_cfg_write_item(fh, item) < 0)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
938 return -4;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
939
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
940 if (thfprintf(fh, "\n") < 0)
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
941 return -3;
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
942 }
b506bff0a7ab Some cleanup work and refactoring on the configuration file parser and writer.
Matti Hamalainen <ccr@tnsp.org>
parents: 504
diff changeset
943
155
23a79bd6c9d6 Use th_llist for th_config module as well instead of duped linked list
Matti Hamalainen <ccr@tnsp.org>
parents: 152
diff changeset
944 item = (th_cfgitem_t *) item->node.next;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
945 }
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
946
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
947 return 0;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
948 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
949
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
950
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
951 int th_cfg_write(th_ioctx *fh, const th_cfgitem_t *cfg)
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
952 {
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
953 if (fh == NULL || cfg == NULL)
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
954 return -1;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
955
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
956 thfprintf(fh, "# Configuration written by %s %s\n\n",
62
36286f2561e1 Oops, 10L.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
957 th_prog_desc, th_prog_version);
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
958
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
959 return th_cfg_write_sect(fh, cfg, 0);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
960 }
83
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
961
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
962
504
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
963 /* Find a configuration item based on section and/or name (and/or) type.
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
964 * The first matching item will be returned.
83
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
965 */
504
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
966 static th_cfgitem_t *th_cfg_find_do(th_cfgitem_t *item, const char *name, const int type)
83
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
967 {
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
968 while (item != NULL)
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
969 {
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
970 BOOL match = TRUE;
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 205
diff changeset
971
504
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
972 // Has type check been set, and does it match?
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
973 if (type != -1 && item->type != type)
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
974 match = FALSE;
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
975
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
976 // Check item name
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
977 if (name != NULL && item->name != NULL &&
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
978 strcmp(name, item->name) != 0)
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
979 match = FALSE;
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
980
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
981 // Recurse to section
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
982 if (!match && item->type == ITEM_SECTION)
83
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
983 {
504
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
984 th_cfgitem_t *tmp = th_cfg_find_do(item->v.section, name, type);
83
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
985 if (tmp != NULL)
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
986 return tmp;
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
987 }
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
988
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
989 // Do we have a match?
504
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
990 if (match)
83
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
991 return item;
504
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
992
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
993 item = (th_cfgitem_t *) item->node.next;
83
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
994 }
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 205
diff changeset
995
83
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
996 return NULL;
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
997 }
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
998
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
999
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
1000 th_cfgitem_t *th_cfg_find(th_cfgitem_t *cfg, const char *section, const char *name, const int type)
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
1001 {
504
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
1002 th_cfgitem_t *node;
83
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
1003
504
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
1004 if (section != NULL)
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
1005 node = th_cfg_find_do(cfg, section, ITEM_SECTION);
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
1006 else
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
1007 node = cfg;
83
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
1008
504
3a0864eb358f Fix the functionality of th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 499
diff changeset
1009 return th_cfg_find_do(node, name, type);
83
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
1010 }