annotate th_config.c @ 499:836e16f27b34

Cleanup.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 25 Dec 2019 11:42:43 +0200
parents 1dbd9259c3b8
children 3a0864eb358f
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
144
51eec969b07a Update copyrights.
Matti Hamalainen <ccr@tnsp.org>
parents: 129
diff changeset
4 * (C) Copyright 2004-2015 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,
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
205 PM_ARRAY
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206 };
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208 #define VADDCH(ch) if (strPos < SET_MAX_BUF) { tmpStr[strPos++] = ch; }
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
209 #define VISEND(ch) (ch == '\r' || ch == '\n' || ch == ';' || th_isspace(c) || ch == '#')
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
211 static int th_cfg_read_sect(th_ioctx *fh, th_cfgitem_t *cfg, int nesting)
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
212 {
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
213 th_cfgitem_t *item = NULL;
110
d739df7efba7 Use dynamic allocation for one buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 83
diff changeset
214 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
215 size_t strPos;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
216 int c, parseMode, prevMode, nextMode, tmpCh;
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
217 BOOL 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
218
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
219 // Initialize values
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
220 tmpCh = 0;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
221 strPos = 0;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
222 c = -1;
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
223 fpSet = isFound = isStart = isError = validError = FALSE;
159
fc914ff7a9b8 Rename a constant.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
224 nextMode = prevMode = parseMode = PM_IDLE;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
225
110
d739df7efba7 Use dynamic allocation for one buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 83
diff changeset
226 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
227 goto out;
d739df7efba7 Use dynamic allocation for one buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 83
diff changeset
228
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
229 // Parse the configuration
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
230 while (parseMode != PM_EOF && parseMode != PM_ERROR)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
231 {
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
232 if (c == -1)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
233 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
234 // Get next character
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
235 switch (c = thfgetc(fh))
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
236 {
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
237 case EOF:
159
fc914ff7a9b8 Rename a constant.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
238 if (parseMode != PM_IDLE)
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
239 {
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
240 th_io_error(fh, THERR_OUT_OF_DATA, "Unexpected end of file.\n");
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
241 parseMode = PM_ERROR;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
242 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
243 else
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
244 parseMode = PM_EOF;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
245 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
246
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
247 case '\n':
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
248 fh->line++;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
249 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
250 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
251
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
252 switch (parseMode)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
253 {
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
254 case PM_COMMENT:
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
255 // Comment parsing mode
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
256 if (c == '\n')
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
257 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
258 // End of line, end of comment
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
259 parseMode = prevMode;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
260 prevMode = PM_COMMENT;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
261 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
262 c = -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
263 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
264
159
fc914ff7a9b8 Rename a constant.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
265 case PM_IDLE:
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
266 // Normal parsing mode
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
267 if (c == '#')
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
268 {
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
269 prevMode = parseMode;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
270 parseMode = PM_COMMENT;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
271 c = -1;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
272 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
273 else if (VISEND(c))
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
274 {
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
275 c = -1;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
276 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
277 else if (c == '}')
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
278 {
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
279 if (nesting > 0)
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
280 // Check for validation errors
110
d739df7efba7 Use dynamic allocation for one buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 83
diff changeset
281 goto out;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
282 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
283 {
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
284 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
285 "Invalid nesting sequence encountered.\n");
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
286 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
287 }
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
288 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
289 else if (th_isalpha(c))
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
290 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
291 // Start of key name found
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
292 prevMode = parseMode;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
293 parseMode = PM_KEYNAME;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
294 strPos = 0;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
295 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
296 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
297 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
298 // Error! Invalid character found
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
299 th_io_error(fh, THERR_INVALID_DATA,
205
c42a9c3f4d55 Improve error handling in th_config.
Matti Hamalainen <ccr@tnsp.org>
parents: 203
diff changeset
300 "Unexpected character '%c'.\n", c);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
301 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
302 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
303 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
304
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
305 case PM_KEYNAME:
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
306 // Configuration KEY name parsing mode
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
307 if (c == '#')
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
308 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
309 // Start of comment
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
310 prevMode = parseMode;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
311 parseMode = PM_COMMENT;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
312 c = -1;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
313 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
314 else if (th_iscrlf(c) || th_isspace(c) || c == '=')
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
315 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
316 // End of key name
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
317 prevMode = parseMode;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
318 parseMode = PM_NEXT;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
319 nextMode = PM_KEYSET;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
320 }
151
434c0eae87c9 Add '-' to allowed configuration key names.
Matti Hamalainen <ccr@tnsp.org>
parents: 144
diff changeset
321 else if (th_isalnum(c) || c == '_' || c == '-')
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
322 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
323 // Add to key name string
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
324 VADDCH(c)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
325 else
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
326 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
327 // Error! Key name string too long!
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
328 th_io_error(fh, THERR_INVALID_DATA, "Config key name too long!");
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
329 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
330 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
331 c = -1;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
332 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
333 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
334 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
335 // 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
336 tmpStr[strPos] = 0;
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
337 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
338 "Unexpected character '%c' in key name '%s'.\n",
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
339 c, tmpStr);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
340 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
341 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
342 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
343
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
344 case PM_KEYSET:
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
345 if (c == '=')
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
346 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
347 // 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
348 tmpStr[strPos] = 0;
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
349 isFound = FALSE;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
350 item = cfg;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
351 while (item != NULL && !isFound)
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 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
354 isFound = TRUE;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
355 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
356 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
357 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
358
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
359 // Check if key was found
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
360 if (isFound)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
361 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
362 // Okay, set next mode
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
363 switch (item->type)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
364 {
15
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
365 case ITEM_HEX_TRIPLET:
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
366 case ITEM_STRING:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
367 nextMode = PM_STRING;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
368 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
369
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
370 case ITEM_STRING_LIST:
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
371 nextMode = PM_ARRAY;
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
372 break;
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
373
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
374 case ITEM_INT:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
375 case ITEM_UINT:
170
8e69e07e5aac Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
376 case ITEM_FLOAT:
8e69e07e5aac Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
377 nextMode = PM_NUMERIC;
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
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
380 case ITEM_BOOL:
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
381 nextMode = PM_BOOL;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
382 break;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
383
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
384 case ITEM_SECTION:
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
385 nextMode = PM_SECTION;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
386 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
387 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
388
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
389 prevMode = parseMode;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
390 parseMode = PM_NEXT;
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
391 isStart = TRUE;
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
392 fpSet = FALSE;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
393 strPos = 0;
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 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
396 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
397 // Error! No configuration key by this name found
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
398 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
399 "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
400 tmpStr);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
401 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
402 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
403
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
404 c = -1;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
405 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
406 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
407 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
408 // Error! '=' expected!
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
409 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
410 "Unexpected character '%c', assignation '=' was expected.\n",
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
411 c);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
412 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
413 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
414 break;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
415
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
416 case PM_NEXT:
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
417 // Search next item parsing mode
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
418 if (c == '#')
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
419 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
420 // Start of comment
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
421 prevMode = parseMode;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
422 parseMode = PM_COMMENT;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
423 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
424 else if (th_isspace(c) || th_iscrlf(c))
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
425 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
426 // Ignore whitespaces and linechanges
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
427 c = -1;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
428 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
429 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
430 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
431 // Next item found
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
432 prevMode = parseMode;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
433 parseMode = nextMode;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
434 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
435 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
436
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
437 case PM_ARRAY:
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
438 if (isStart)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
439 {
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
440 switch (item->type)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
441 {
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
442 case ITEM_STRING_LIST:
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
443 prevMode = parseMode;
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
444 parseMode = PM_STRING;
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
445 break;
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
446 }
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
447 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
448 else if (c == ',')
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
449 {
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
450 switch (item->type)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
451 {
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
452 case ITEM_STRING_LIST:
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
453 c = -1;
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
454 isStart = TRUE;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
455 prevMode = parseMode;
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
456 parseMode = PM_NEXT;
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
457 nextMode = PM_STRING;
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
458 break;
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
459 }
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
460 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
461 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
462 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
463 prevMode = parseMode;
159
fc914ff7a9b8 Rename a constant.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
464 parseMode = PM_IDLE;
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
465 }
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
466 break;
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
467
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
468 case PM_SECTION:
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
469 // Section parsing mode
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
470 if (c != '{')
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
471 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
472 // Error! Section start '{' expected!
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
473 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
474 "Unexpected character '%c', section start '{' was expected.\n",
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
475 c);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
476 parseMode = PM_ERROR;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
477 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
478 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
479 {
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
480 int res = th_cfg_read_sect(fh, item->v.section, nesting + 1);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
481 c = -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
482 if (res > 0)
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
483 validError = TRUE;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
484 else if (res < 0)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
485 parseMode = PM_ERROR;
42
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 {
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
488 prevMode = parseMode;
159
fc914ff7a9b8 Rename a constant.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
489 parseMode = PM_IDLE;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
490 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
491 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
492 break;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
493
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
494 case PM_STRING:
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
495 // String parsing mode
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
496 if (isStart)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
497 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
498 // Start of string, get delimiter
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
499 tmpCh = c;
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
500 isStart = FALSE;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
501 strPos = 0;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
502 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
503 else if (c == tmpCh)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
504 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
505 // 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
506 tmpStr[strPos] = 0;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
507
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
508 switch (item->type)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
509 {
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
510 case ITEM_HEX_TRIPLET:
497
ff3ebe22f6c5 Change int th_get_hex_triplet(const char *str) API to
Matti Hamalainen <ccr@tnsp.org>
parents: 457
diff changeset
511 th_get_hex_triplet(tmpStr, item->v.val_uint);
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
512 prevMode = parseMode;
159
fc914ff7a9b8 Rename a constant.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
513 parseMode = PM_IDLE;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
514 break;
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
515 case ITEM_STRING:
444
1920a31927b2 Fix th_pstrcpy() call to th_pstr_cpy()
Matti Hamalainen <ccr@tnsp.org>
parents: 441
diff changeset
516 th_pstr_cpy(item->v.val_str, tmpStr);
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
517 prevMode = parseMode;
159
fc914ff7a9b8 Rename a constant.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
518 parseMode = PM_IDLE;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
519 break;
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
520 case ITEM_STRING_LIST:
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
521 th_llist_append(item->v.list, th_strdup(tmpStr));
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
522 prevMode = parseMode;
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
523 parseMode = PM_NEXT;
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
524 nextMode = PM_ARRAY;
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
525 break;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
526 }
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
527
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
528 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
529 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
530 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
531 // Add character to string
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
532 VADDCH(c)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
533 else
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
534 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
535 // Error! String too long!
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
536 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
537 "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
538 SET_MAX_BUF);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
539 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
540 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
541 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
542
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
543 c = -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
544 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
545
170
8e69e07e5aac Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
546 case PM_NUMERIC:
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
547 // Integer parsing mode
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
548 if (isStart && item->type == ITEM_UINT && c == '-')
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
549 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
550 // Error! Negative values not allowed for unsigned ints
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
551 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
552 "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
553 item->name);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
554 parseMode = PM_ERROR;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
555 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
556 else if (isStart && (c == '-' || c == '+'))
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
557 {
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
558 VADDCH(c)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
559 else
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
560 isError = TRUE;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
561 }
172
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
562 else if (isStart && item->type == ITEM_FLOAT && c == '.')
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
563 {
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
564 fpSet = TRUE;
172
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
565 VADDCH('0')
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
566 else
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
567 isError = TRUE;
172
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
568
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
569 VADDCH(c)
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
570 else
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
571 isError = TRUE;
172
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
572 }
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
573 else if (item->type == ITEM_FLOAT && c == '.' && !fpSet)
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
574 {
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
575 fpSet = TRUE;
172
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
576 VADDCH(c)
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
577 else
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
578 isError = TRUE;
172
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
579 }
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
580 else if (th_isdigit(c))
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
581 {
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
582 VADDCH(c)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
583 else
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
584 isError = TRUE;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
585 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
586 else if (VISEND(c))
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
587 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
588 // 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
589 tmpStr[strPos] = 0;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
590 switch (item->type)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
591 {
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
592 case ITEM_INT:
22
1ac2449c4df7 ISO C99 compatibility fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
593 *(item->v.val_int) = atoi(tmpStr);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
594 break;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
595
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
596 case ITEM_UINT:
22
1ac2449c4df7 ISO C99 compatibility fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
597 *(item->v.val_uint) = atol(tmpStr);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
598 break;
172
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
599
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
600 case ITEM_FLOAT:
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
601 *(item->v.val_float) = atof(tmpStr);
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
602 break;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
603 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
604
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
605 prevMode = parseMode;
159
fc914ff7a9b8 Rename a constant.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
606 parseMode = PM_IDLE;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
607 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
608 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
609 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
610 // Error! Unexpected character.
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
611 th_io_error(fh, THERR_INVALID_DATA,
170
8e69e07e5aac Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
612 "Unexpected character '%c' for numeric setting '%s'.",
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
613 c, item->name);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
614 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
615 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
616
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
617 if (isError)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
618 {
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
619 // Error! String too long!
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
620 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
621 "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
622 SET_MAX_BUF);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
623 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
624 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
625
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
626 isStart = FALSE;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
627 c = -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
628 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
629
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
630 case PM_BOOL:
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
631 // Boolean parsing mode
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
632 if (isStart)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
633 {
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
634 isStart = FALSE;
168
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
635 strPos = 0;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
636 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
637
168
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
638 if (th_isalnum(c))
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
639 {
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
640 VADDCH(c)
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
641 else
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
642 isError = TRUE;
168
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
643 }
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
644 else
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
645 if (VISEND(c))
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
646 {
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
647 BOOL tmpBool;
168
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
648 tmpStr[strPos] = 0;
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
649 isError = !th_get_boolean(tmpStr, &tmpBool);
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
650 if (!isError)
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
651 {
22
1ac2449c4df7 ISO C99 compatibility fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
652 *(item->v.val_bool) = tmpBool;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
653 prevMode = parseMode;
159
fc914ff7a9b8 Rename a constant.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
654 parseMode = PM_IDLE;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
655 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
656 }
168
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
657
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
658 if (isError)
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
659 {
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
660 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
661 "Invalid boolean value for '%s'.\n",
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
662 item->name);
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
663 parseMode = PM_ERROR;
a2cd862315c5 Use th_get_boolean() in configuration parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 160
diff changeset
664 }
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
665 c = -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
666 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
667 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
668 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
669
110
d739df7efba7 Use dynamic allocation for one buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 83
diff changeset
670 out:
d739df7efba7 Use dynamic allocation for one buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 83
diff changeset
671 th_free(tmpStr);
d739df7efba7 Use dynamic allocation for one buffer.
Matti Hamalainen <ccr@tnsp.org>
parents: 83
diff changeset
672
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
673 // 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
674 if (validError)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
675 return 1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
676
129
aa2d608fb3f3 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 125
diff changeset
677 // Return result
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
678 if (parseMode == PM_ERROR)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
679 return -2;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
680 else
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
681 return 0;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
682 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
683
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
684
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
685 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
686 {
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
687 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
688 return -1;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
689
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
690 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
691 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
692
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
693
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
694 /* Write a configuration into file
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
695 */
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
696 static void th_print_indent(th_ioctx *fh, int nesting)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
697 {
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
698 for (int i = 0; i < nesting * 2; i++)
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
699 thfputc(' ', fh);
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
700 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
701
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
702
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
703 static int th_cfg_write_sect(th_ioctx *fh, const th_cfgitem_t *item, int nesting)
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
704 {
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
705 while (item != NULL)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
706 {
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
707 if (item->type == ITEM_COMMENT)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
708 {
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
709 th_print_indent(fh, nesting);
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
710 if (thfprintf(fh, "# %s\n",
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
711 (item->name != NULL) ? item->name : "") < 0)
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
712 return -1;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
713 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
714 else if (item->name != NULL)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
715 {
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
716 th_print_indent(fh, nesting);
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
717
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
718 switch (item->type)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
719 {
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
720 case ITEM_STRING:
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
721 if (*(item->v.val_str) == NULL)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
722 {
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
723 if (thfprintf(fh, "#%s = \"\"\n",
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
724 item->name) < 0)
15
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
725 return -3;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
726 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
727 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
728 {
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
729 if (thfprintf(fh, "%s = \"%s\"\n",
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
730 item->name, *(item->v.val_str)) < 0)
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
731 return -3;
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
732 }
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
733 break;
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
734
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
735 case ITEM_STRING_LIST:
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
736 if (*(item->v.list) == NULL)
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 if (thfprintf(fh,
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
739 "#%s = \"\", \"\"\n", item->name) < 0)
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
740 return -3;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
741 }
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
742 else
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
743 {
152
b4e1b15a64e1 Rename qlist_t doubly linked list structure to th_llist_t.
Matti Hamalainen <ccr@tnsp.org>
parents: 151
diff changeset
744 th_llist_t *node = *(item->v.list);
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
745 size_t n = th_llist_length(node);
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
746 if (thfprintf(fh, "%s = ", item->name) < 0)
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
747 return -3;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
748
176
791eaeb4f412 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 172
diff changeset
749 for (; node != NULL; node = node->next)
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
750 {
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
751 if (node->data != NULL)
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
752 thfprintf(fh, "\"%s\"", (char *) node->data);
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
753
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
754 if (--n > 0)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
755 {
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
756 thfprintf(fh, ",\n");
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
757 th_print_indent(fh, nesting);
34
8dac45860fc7 Prettify writing of string lists in config files.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
758 }
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
759 }
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
760
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
761 if (thfprintf(fh, "\n") < 0)
15
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
762 return -3;
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
763 }
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
764 break;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
765
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
766 case ITEM_INT:
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
767 if (thfprintf(fh, "%s = %i\n",
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
768 item->name, *(item->v.val_int)) < 0)
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
769 return -4;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
770 break;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
771
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
772 case ITEM_UINT:
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
773 if (thfprintf(fh, "%s = %d\n",
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
774 item->name, *(item->v.val_uint)) < 0)
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
775 return -5;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
776 break;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
777
172
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
778 case ITEM_FLOAT:
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
779 if (thfprintf(fh, "%s = %1.5f\n",
172
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
780 item->name, *(item->v.val_float)) < 0)
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
781 return -5;
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
782 break;
eb2c073d93b3 Adding floating point configuration item support.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
783
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
784 case ITEM_BOOL:
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
785 if (thfprintf(fh, "%s = %s\n", item->name,
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
786 *(item->v.val_bool) ? "yes" : "no") < 0)
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
787 return -6;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
788 break;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
789
16
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
790 case ITEM_SECTION:
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
791 {
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
792 int res;
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
793 if (thfprintf(fh, "%s = {\n", item->name) < 0)
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
794 return -7;
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
795 res = th_cfg_write_sect(fh, item->v.section, nesting + 1);
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
796 if (res != 0)
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
797 return res;
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
798 if (thfprintf(fh, "}\n\n") < 0)
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
799 return -8;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
800 }
15
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
801 break;
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
802
15
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
803 case ITEM_HEX_TRIPLET:
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
804 if (thfprintf(fh, "%s = \"%06x\"\n",
70
a0e1b29be35d Refactor configuration file handling module rather thoroughly. The API is
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
805 item->name, *(item->v.val_int)) < 0)
15
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
806 return -6;
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
807 break;
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
808 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
809 }
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
810 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
811 }
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
812
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
813 return 0;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
814 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
815
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
816
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
817 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
818 {
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
819 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
820 return -1;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
821
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
822 thfprintf(fh, "# Configuration written by %s %s\n\n",
62
36286f2561e1 Oops, 10L.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
823 th_prog_desc, th_prog_version);
42
7851f704d499 Reindent and cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 34
diff changeset
824
499
836e16f27b34 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 498
diff changeset
825 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
826 }
83
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
827
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
828
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
829 /* Find a configuration item based on section, name, type.
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
830 * Name MUST be defined. Section can be NULL and type -1,
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
831 * first matching item will be returned.
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
832 */
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
833 static th_cfgitem_t *th_cfg_find_do(th_cfgitem_t *item, BOOL *sect, const char *section, const char *name, const int type)
83
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
834 {
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
835 while (item != NULL)
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
836 {
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
837 BOOL match = TRUE;
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 205
diff changeset
838
83
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
839 if (item->type == ITEM_SECTION)
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
840 {
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
841 // Check section name if set
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
842 if (section != NULL && strcmp(section, item->name) == 0)
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
843 *sect = TRUE;
83
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
844
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
845 // Recurse to sub-section
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
846 th_cfgitem_t *tmp = th_cfg_find_do(item->v.section, sect, section, name, type);
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
847 if (tmp != NULL)
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
848 return tmp;
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
849 }
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
850 else
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
851 // Has type check been set, and does it match?
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
852 if (type != -1 && item->type != type)
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
853 match = FALSE;
83
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
854 else
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
855 // Check name (not section name, tho)
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
856 if (strcmp(name, item->name) != 0)
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
857 match = FALSE;
83
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
858
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
859 // Do we have a match?
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
860 if (*sect && match)
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
861 return item;
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
862 }
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 205
diff changeset
863
83
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
864 return NULL;
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
865 }
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
866
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
867
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
868 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
869 {
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
870 BOOL sect = FALSE;
83
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
871
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
872 if (section == NULL)
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 453
diff changeset
873 sect = TRUE;
83
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
874
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
875 return th_cfg_find_do(cfg, &sect, section, name, type);
50006067bcd1 Add new function, th_cfg_find().
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
876 }