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