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