annotate th_config.c @ 13:adcbcac66125

Import improved config code from chat client fork of th-libs.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 29 Oct 2010 13:48:31 +0300
parents 41885619fc79
children 4adf7093060c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * Very simple configuration handling functions
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Programmed and designed by Matti 'ccr' Hamalainen
3
5a327a2988fa Breaking the API a bit, cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
4 * (C) Copyright 2004-2008 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 */
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 #ifdef HAVE_CONFIG_H
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include "config.h"
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #endif
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 #include "th_config.h"
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 #include "th_util.h"
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 #include "th_string.h"
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
17 #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
18
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 /* 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
21 */
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
22 void th_config_free(cfgitem_t *cfg)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 {
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
24 cfgitem_t *curr = cfg;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
26 while (curr != NULL) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
27 cfgitem_t *next = curr->next;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
28
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
29 if (curr->type == ITEM_BLOCK)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
30 th_config_free((cfgitem_t *) curr->data);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
31
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
32 th_free(curr->name);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
33 th_free(curr);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
34 curr = next;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
35 }
0
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
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 /* 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
40 */
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
41 static cfgitem_t *th_config_add(cfgitem_t **cfg, char *name, int type,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
42 BOOL (*validate)(cfgitem_t *), void *data)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 {
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
44 cfgitem_t *node;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
46 if (cfg == NULL)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
47 return NULL;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
49 /* Allocate new item */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
50 node = (cfgitem_t *) th_calloc(1, sizeof(cfgitem_t));
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
51 if (node == NULL)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
52 return NULL;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
54 /* Set values */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
55 node->type = type;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
56 node->data = data;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
57 node->validate = validate;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
58 node->name = th_strdup(name);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
59
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
60 /* Insert into linked list */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
61 if (*cfg != NULL) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
62 node->prev = (*cfg)->prev;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
63 (*cfg)->prev->next = node;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
64 (*cfg)->prev = node;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
65 } else {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
66 *cfg = node;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
67 node->prev = node;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
68 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
69 node->next = NULL;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
71 return node;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 /* 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
76 */
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
77 int th_config_add_int(cfgitem_t ** cfg, char * name, BOOL(*itemValidate) (cfgitem_t *),
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
78 int *itemData, int itemDef)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 {
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
80 cfgitem_t *node;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
82 node = th_config_add(cfg, name, ITEM_INT, itemValidate, (void *) itemData);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
83 if (node == NULL)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
84 return -1;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
86 *itemData = itemDef;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
88 return 0;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 /* 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
93 */
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
94 int th_config_add_uint(cfgitem_t ** cfg, char * name, BOOL(*itemValidate) (cfgitem_t *),
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
95 unsigned int * itemData, unsigned int itemDef)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 {
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
97 cfgitem_t *node;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
99 node = th_config_add(cfg, name, ITEM_UINT, itemValidate, (void *) itemData);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
100 if (node == NULL)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
101 return -1;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
103 *itemData = itemDef;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
105 return 0;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 /* 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
110 */
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
111 int th_config_add_str(cfgitem_t ** cfg, char * name, BOOL(*itemValidate) (cfgitem_t *),
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
112 char ** itemData, char * itemDef)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 {
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
114 cfgitem_t *node;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
116 node = th_config_add(cfg, name, ITEM_STRING, itemValidate, (void *) itemData);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
117 if (node == NULL)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
118 return -1;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
120 *itemData = th_strdup(itemDef);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
122 return 0;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 /* 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
127 */
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
128 int th_config_add_bool(cfgitem_t ** cfg, char * name, BOOL(*itemValidate) (cfgitem_t *),
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
129 BOOL * itemData, BOOL itemDef)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 {
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
131 cfgitem_t *node;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
132
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
133 node = th_config_add(cfg, name, ITEM_BOOL, itemValidate, (void *) itemData);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
134 if (node == NULL)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
135 return -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
136
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
137 *itemData = itemDef;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
138
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
139 return 0;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
140 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
141
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
143 /* Add implicit comment
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
144 */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
145 int th_config_add_comment(cfgitem_t ** cfg, char * comment)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
146 {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
147 cfgitem_t *node;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
148
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
149 node = th_config_add(cfg, comment, ITEM_COMMENT, NULL, NULL);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
150 if (node == NULL)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
151 return -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
152
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
153 return 0;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
154 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
157 /* Add new block
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
158 */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
159 int th_config_add_section(cfgitem_t ** cfg, char * name, cfgitem_t *data)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
160 {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
161 cfgitem_t *node;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
162
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
163 node = th_config_add(cfg, name, ITEM_BLOCK, NULL, (void *) data);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
164 if (node == NULL)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
165 return -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
166
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
167 return 0;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
170
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171 /* 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
172 */
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
173 enum {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
174 PM_EOF,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
175 PM_ERROR,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
176 PM_NORMAL,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
177 PM_COMMENT,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
178 PM_NEXT,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
179 PM_KEYNAME,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
180 PM_KEYSET,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
181 PM_STRING,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
182 PM_INT,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
183 PM_BOOL,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
184 PM_BLOCK
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
185 };
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187 #define VADDCH(ch) if (strPos < SET_MAX_BUF) { tmpStr[strPos++] = ch; }
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
188 #define VISEND(ch) (ch == '\r' || ch == '\n' || ch == ';' || th_isspace(c) || ch == '#')
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
190 typedef struct {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
191 FILE *file;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
192 char *filename;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
193 size_t line;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
194 } conffile_t;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
197 static void th_config_error(conffile_t *f, const char *fmt, ...)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
198 {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
199 va_list ap;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
200 va_start(ap, fmt);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
201 fprintf(stderr, "%s: '%s', line #%d: ", th_prog_name, f->filename, f->line);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
202 vfprintf(stderr, fmt, ap);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
203 va_end(ap);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
204 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
205
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
207 static int th_config_read_sect(conffile_t *f, cfgitem_t * cfg, int nesting)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
208 {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
209 cfgitem_t *item = NULL;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
210 char tmpStr[SET_MAX_BUF + 1];
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
211 size_t strPos;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
212 int c, parseMode, prevMode, nextMode, tmpCh;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
213 BOOL isFound, isStart, isError, validError;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
214
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
215 /* Initialize values */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
216 tmpCh = 0;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
217 strPos = 0;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
218 c = -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
219 isFound = isStart = isError = validError = FALSE;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
220 nextMode = prevMode = parseMode = PM_NORMAL;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
221
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
222 /* Parse the configuration */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
223 while (parseMode != PM_EOF && parseMode != PM_ERROR) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
224 if (c == -1) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
225 /* Get next character */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
226 switch (c = fgetc(f->file)) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
227 case EOF:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
228 if (parseMode != PM_NORMAL) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
229 th_config_error(f,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
230 "Unexpected end of file.\n");
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
231 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
232 } else
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
233 parseMode = PM_EOF;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
234 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
235
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
236 case '\n':
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
237 f->line++;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
238 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
239 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
240
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
241 switch (parseMode) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
242 case PM_COMMENT:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
243 /* Comment parsing mode */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
244 if (c == '\n') {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
245 /* End of line, end of comment */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
246 parseMode = prevMode;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
247 prevMode = PM_COMMENT;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
248 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
249 c = -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
250 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
251
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
252 case PM_NORMAL:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
253 /* Normal parsing mode */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
254 if (c == '#') {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
255 prevMode = parseMode;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
256 parseMode = PM_COMMENT;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
257 c = -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
258 } else if (VISEND(c)) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
259 c = -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
260 } else if (c == '}') {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
261 if (nesting > 0) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
262 /* Check for validation errors */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
263 return (validError) ? 1 : 0;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
264 } else {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
265 th_config_error(f,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
266 "HMMM!\n");
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
267 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
268 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
269 } else if (th_isalpha(c)) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
270 /* Start of key name found */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
271 prevMode = parseMode;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
272 parseMode = PM_KEYNAME;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
273 strPos = 0;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
274 } else {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
275 /* Error! Invalid character found */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
276 th_config_error(f,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
277 "Unexpected character '%c'.\n", c);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
278 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
279 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
280 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
281
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
282 case PM_KEYNAME:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
283 /* Configuration KEY name parsing mode */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
284 if (c == '#') {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
285 /* Start of comment */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
286 prevMode = parseMode;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
287 parseMode = PM_COMMENT;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
288 c = -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
289 } else if (th_iscrlf(c) || th_isspace(c) || c == '=') {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
290 /* End of key name */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
291 prevMode = parseMode;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
292 parseMode = PM_NEXT;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
293 nextMode = PM_KEYSET;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
294 } else if (th_isalnum(c) || c == '_') {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
295 /* Add to key name string */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
296 VADDCH(c)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
297 else
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
298 {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
299 /* Error! Key name string too long! */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
300 th_config_error(f,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
301 "Config key name too long!");
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
302 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
303 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
304 c = -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
305 } else {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
306 /* Error! Invalid character found */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
307 tmpStr[strPos] = 0;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
308 th_config_error(f,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
309 "Unexpected character '%c' in key name '%s'.\n", c, tmpStr);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
310 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
311 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
312 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
313
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
314 case PM_KEYSET:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
315 if (c == '=') {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
316 /* Find key from configuration */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
317 tmpStr[strPos] = 0;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
318 isFound = FALSE;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
319 item = cfg;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
320 while (item != NULL && !isFound) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
321 if (item->name != NULL && strcmp(item->name, tmpStr) == 0)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
322 isFound = TRUE;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
323 else
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
324 item = item->next;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
325 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
326
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
327 /* Check if key was found */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
328 if (isFound) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
329 /* Okay, set next mode */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
330 switch (item->type) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
331 case ITEM_STRING:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
332 nextMode = PM_STRING;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
333 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
334
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
335 case ITEM_INT:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
336 case ITEM_UINT:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
337 nextMode = PM_INT;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
338 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
339
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
340 case ITEM_BOOL:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
341 nextMode = PM_BOOL;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
342 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
343
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
344 case ITEM_BLOCK:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
345 nextMode = PM_BLOCK;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
346 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
347 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
348
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
349 prevMode = parseMode;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
350 parseMode = PM_NEXT;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
351 isStart = TRUE;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
352 strPos = 0;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
353 } else {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
354 /* Error! No configuration key by this name found */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
355 th_config_error(f,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
356 "No such configuration setting ('%s')\n", tmpStr);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
357 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
358 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
359
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
360 c = -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
361 } else {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
362 /* Error! '=' expected! */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
363 th_config_error(f,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
364 "Unexpected character '%c', assignation '=' was expected.\n", c);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
365 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
366 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
367 break;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
368
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
369 case PM_NEXT:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
370 /* Search next item parsing mode */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
371 if (c == '#') {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
372 /* Start of comment */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
373 prevMode = parseMode;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
374 parseMode = PM_COMMENT;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
375 } else if (th_isspace(c) || th_iscrlf(c)) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
376 /* Ignore whitespaces and linechanges */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
377 c = -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
378 } else {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
379 /* Next item found */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
380 prevMode = parseMode;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
381 parseMode = nextMode;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
382 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
383 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
384
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
385 case PM_BLOCK:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
386 /* Block parsing mode */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
387 if (c != '{') {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
388 /* Error! Block start '{' expected! */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
389 th_config_error(f,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
390 "Unexpected character '%c', block start '{' was expected.\n", c);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
391 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
392 } else {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
393 int res = th_config_read_sect(f, (cfgitem_t *) item->data, nesting + 1);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
394 c = -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
395 if (res > 0)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
396 validError = TRUE;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
397 else if (res < 0)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
398 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
399 else {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
400 prevMode = parseMode;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
401 parseMode = PM_NORMAL;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
402 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
403 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
404 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
405
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
406 case PM_STRING:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
407 /* String parsing mode */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
408 if (isStart) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
409 /* Start of string, get delimiter */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
410 tmpCh = c;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
411 isStart = FALSE;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
412 strPos = 0;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
413 } else if (c == tmpCh) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
414 /* End of string, set the value */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
415 tmpStr[strPos] = 0;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
416 th_pstrcpy((char **) item->data, tmpStr);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
417 if (item->validate != NULL) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
418 if (!item->validate(item))
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
419 validError = TRUE;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
420 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
421
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
422 prevMode = parseMode;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
423 parseMode = PM_NORMAL;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
424 } else {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
425 /* Add character to string */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
426 VADDCH(c)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
427 else
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
428 {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
429 /* Error! String too long! */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
430 th_config_error(f,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
431 "String too long! Maximum is %d characters.",
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
432 SET_MAX_BUF);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
433 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
434 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
435 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
436
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
437 c = -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
438 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
439
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
440 case PM_INT:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
441 /* Integer parsing mode */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
442 if (isStart && item->type == ITEM_UINT && c == '-') {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
443 /* Error! Negative values not allowed for unsigned ints */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
444 th_config_error(f,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
445 "Negative value specified for %s, unsigned value expected.",
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
446 item->name);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
447 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
448 } else if (isStart && (c == '-' || c == '+')) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
449 VADDCH(c)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
450 else
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
451 isError = TRUE;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
452 } else if (th_isdigit(c)) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
453 VADDCH(c)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
454 else
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
455 isError = TRUE;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
456 } else if (VISEND(c)) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
457 /* End of integer parsing mode */
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;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
459 switch (item->type) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
460 case ITEM_INT:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
461 *((int *) item->data) = atoi(tmpStr);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
462 break;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
463
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
464 case ITEM_UINT:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
465 *((unsigned int *) item->data) = atol(tmpStr);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
466 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
467 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
468 if (item->validate != NULL) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
469 if (!item->validate(item))
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
470 validError = TRUE;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
471 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
472
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
473 prevMode = parseMode;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
474 parseMode = PM_NORMAL;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
475 } else {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
476 /* Error! Unexpected character. */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
477 th_config_error(f,
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
478 "Unexpected character '%c' for integer setting '%s'.",
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
479 c, item->name);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
480 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
481 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
482
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
483 if (isError) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
484 /* Error! String too long! */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
485 th_config_error(f, "String too long! Maximum is %d characters.",
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
486 SET_MAX_BUF);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
487 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
488 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
489
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
490 isStart = FALSE;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
491 c = -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
492 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
493
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
494 case PM_BOOL:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
495 /* Boolean parsing mode */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
496 if (isStart) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
497 tmpCh = c;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
498 isStart = FALSE;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
499 } else if (VISEND(c)) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
500 BOOL tmpBool;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
501
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
502 /* End of boolean parsing */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
503 switch (tmpCh) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
504 case 'Y': case 'y':
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
505 case 'T': case 't':
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
506 case '1':
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
507 tmpBool = TRUE;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
508 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
509
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
510 case 'N': case 'n':
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
511 case 'F': case 'f':
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
512 case '0':
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
513 tmpBool = FALSE;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
514 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
515
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
516 default:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
517 isError = TRUE;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
518 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
519
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
520 if (isError) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
521 th_config_error(f, "Invalid boolean value for '%s'.\n", item->name);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
522 parseMode = PM_ERROR;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
523 } else {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
524 *((BOOL *) item->data) = tmpBool;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
525 if (item->validate != NULL) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
526 if (!item->validate(item))
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
527 validError = TRUE;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
528 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
529
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
530 prevMode = parseMode;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
531 parseMode = PM_NORMAL;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
532 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
533 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
534 c = -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
535 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
536 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
537 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
538
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
539 /* Check for validation errors */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
540 if (validError)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
541 return 1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
542
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
543 /* Return result */
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
544 if (parseMode == PM_ERROR)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
545 return -2;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
546 else
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
547 return 0;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
548 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
549
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
550
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
551 int th_config_read(FILE *inFile, char *filename, cfgitem_t * cfg)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
552 {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
553 conffile_t f;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
554
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
555 f.file = inFile;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
556 f.filename = filename;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
557 f.line = 1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
558
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
559 return th_config_read_sect(&f, cfg, 0);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
560 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
561
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
562
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
563 /* Write a configuration into file
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
564 */
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
565 static void th_print_indent(conffile_t *f, int nesting)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
566 {
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
567 int i;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
568 for (i = 0; i < nesting * 2; i++)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
569 fputc(' ', f->file);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
570 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
571
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
572
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
573 static int th_config_write_sect(conffile_t *f, cfgitem_t *item, int nesting)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
574 {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
575 while (item != NULL) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
576 if (item->type == ITEM_COMMENT) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
577 th_print_indent(f, nesting);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
578 if (fprintf(f->file, "# %s\n", (item->name != NULL) ? item->name : "" ) < 0)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
579 return -1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
580 } else
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
581 if (item->name != NULL) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
582 th_print_indent(f, nesting);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
583
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
584 switch (item->type) {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
585 case ITEM_STRING:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
586 if (fprintf(f->file, "%s = \"%s\"\n",
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
587 item->name, *((char **) item->data)) < 0)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
588 return -3;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
589 break;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
590
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
591 case ITEM_INT:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
592 if (fprintf(f->file, "%s = %i\n",
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
593 item->name, *((int *) item->data)) < 0)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
594 return -4;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
595 break;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
596
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
597 case ITEM_UINT:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
598 if (fprintf(f->file, "%s = %d\n",
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
599 item->name, *((unsigned int *) item->data)) < 0)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
600 return -5;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
601 break;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
602
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
603 case ITEM_BOOL:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
604 if (fprintf(f->file, "%s = %s\n",
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
605 item->name, *((BOOL *) item->data) ? "yes" : "no") < 0)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
606 return -6;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
607 break;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
608
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
609 case ITEM_BLOCK:
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
610 {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
611 int res;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
612 if (fprintf(f->file, "\n%s = {\n", item->name) < 0)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
613 return -7;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
614 res = th_config_write_sect(f, (cfgitem_t *) item->data, nesting + 1);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
615 if (res != 0) return res;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
616 if (fprintf(f->file, "} # End of '%s'\n\n", item->name) < 0)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
617 return -8;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
618 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
619 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
620 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
621 item = item->next;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
622 }
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
623
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
624 return 0;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
625 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
626
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
627
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
628 int th_config_write(FILE *outFile, char *filename, cfgitem_t *cfg)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
629 {
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
630 conffile_t f;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
631
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
632 if (cfg == NULL)
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
633 return -1;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
634
13
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
635 f.file = outFile;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
636 f.filename = filename;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
637 f.line = 1;
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
638
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
639 fprintf(outFile, "# Configuration written by %s %s\n\n",
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
640 th_prog_fullname, th_prog_version);
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
641
adcbcac66125 Import improved config code from chat client fork of th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
642 return th_config_write_sect(&f, cfg, 0);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
643 }