Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/util/catalog.c
blob: 76cfa69fca5ede456d8075f5cde19a2924673aa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <config.h>
#ifndef _plan9_
#include <string.h>
#ifndef NO_MALLOC_H
#include <malloc.h>
#endif
#include <string.h>
#else
#include <u.h>
#include <libc.h>
#endif
#include <catalog.h>
#include <misc-f.h>
/* Well, just simple implementation of unbalanced trees in combination
 * of small hash table. I am lazy :) but should be OK for my purposes
 *
 * This function is used for both-lookups and adds into table (since most of
 * code is the same, depends whether newvalue is NULL. If newvalue is nonNULL,
 * new variable is added into table, if name is not present here or value is
 * changed othewise.
 */
static char *find_variable(catalog_t * context, CONST char *name,
			   CONST char *newvalue)
{
    int r = 0;
    int hash = (int) strlen(name);
    struct varnames *current, *last, *newp;
    hash =
	((unsigned char) (name[0]) + (unsigned char) (name[hash - 1]) +
	 hash) % (unsigned int) CHASHMAX;
    current = last = context->root[hash];
    while (current != NULL) {
	last = current;
	r = strcmp(current->name, name);
	if (!r) {
	    if (newvalue != NULL) {	/*overwrite value */
		free(current->value);
		current->value = mystrdup(newvalue);
	    }
	    return (current->value);
	}
	if (r > 0)
	    current = current->left;
	else
	    current = current->right;
    }
    /*Entry is new */
    if (newvalue == NULL)
	return (NULL);
    newp = (struct varnames *) calloc(1, sizeof(struct varnames));
    newp->name = mystrdup(name);
    newp->value = mystrdup(newvalue);
    /*FIXME. Should take a care to full memory */
    newp->left = NULL;
    newp->right = NULL;
    if (last == NULL) {
	context->root[hash] = newp;
    } else {
	if (r > 0)
	    last->left = newp;
	else
	    last->right = newp;
    }
    return (newp->value);
}

/*
 * free memory used by node and its sons
 */
static void free_node(struct varnames *node)
{
    while (node != NULL) {
	struct varnames *nextnode;
	free_node(node->left);
	nextnode = node->right;
	free(node->name);
	free(node->value);
	free(node);
	node = nextnode;
    }
}

/*
 * free catalog
 */
void free_catalog(catalog_t * context)
{
    int i;
    for (i = 0; i < CHASHMAX; i++) {
	free_node(context->root[i]);
	context->root[i] = NULL;
    }
    free(context);
}

static catalog_t *alloc_catalog(void)
{
    int i;
    catalog_t *c;
    c = (catalog_t *) calloc(1, sizeof(catalog_t));
    if (c == NULL)
	return NULL;
    for (i = 0; i < CHASHMAX; i++)
	c->root[i] = NULL;
    return c;
}

/*
 * Parse an catalog file and save values into memory
 */
// FIXME: this macro gives a segfault if \" is used in text. kovzol, 2009-06-29
#define seterror(text) sprintf(errort,"line %i:%s",line,text),*error=errort
catalog_t *load_catalog(xio_file f, CONST char **error)
{
    int i;
    int line = 1;
    int size;
    int c;
    catalog_t *catalog = alloc_catalog();
    static char errort[40];
    char name[1024];
    char value[1024];
    if (catalog == NULL) {
	*error = "Out of memory";
    }
    if (f == NULL) {
	*error = "File could not be opened";
	free_catalog(catalog);
	return NULL;
    }
    /* Just very simple parsing loop of format 
     * [blanks]name[blanks]"value"[blanks]
     * Blanks should be comments using # or space, newline, \r and tabulator
     * Value shoud contain and \ seqences where \\ means \ and
     * \[something] means something. Should be used for character "
     */
    while (!xio_feof(f)) {

	do {
	    c = xio_getc(f);
	    if (c == '\n')
		line++;
	    if (c == '#') {
		while ((c = xio_getc(f)) != '\n' && c != XIO_EOF);
		line++;
	    }
	}
	while (c == ' ' || c == '\n' || c == '\r' || c == '\t');

	/*Skip blanks */
	if (c == XIO_EOF) {
	    if (xio_feof(f))
		break;
	    free_catalog(catalog);
	    seterror("read error");
	    xio_close(f);
	    return NULL;
	}
	i = 0;

	/*read name */
	do {
	    name[i] = c;
	    i++;
	    c = xio_getc(f);
	    if (c == '\n')
		line++;
	    if (i == 1024) {
		seterror("Name is too long (>=1024 characters)");
		free_catalog(catalog);
		xio_close(f);
		return NULL;
	    }
	}
	
	while (c != '\n' && c != ' ' && c != '\t' && c != XIO_EOF);

	/*Skip blanks */
	while (c == ' ' || c == '\n' || c == '\r' || c == '\t') {
	    c = xio_getc(f);
	    if (c == '\n')
		line++;
	    if (c == '#') {
		while ((c = xio_getc(f)) != '\n' && c != XIO_EOF);
		line++;
	    }
	}

	/*Skip blanks */
	if (c == XIO_EOF) {
	    if (xio_feof(f))
		seterror("Unexpected end of file after name field");
	    else
		seterror("read error");
	    free_catalog(catalog);
	    xio_close(f);
	    return NULL;
	}
	
	name[i] = 0;
	if (c != '"') {
	    seterror("Begin of value field expected"); // Text modified due to segfault problem, see above (kovzol)
	    free_catalog(catalog);
	    xio_close(f);
	    return NULL;
	}
	c = xio_getc(f);
	if (c == '\n')
	    line++;
	i = 0;

	size = 0;
	do {
	    if (c == '\\')
		value[i] = xio_getc(f);
	    else
		value[i] = c;
	    i++;
	    c = xio_getc(f);
	    if (c == '\n')
		line++, size = 0;
	    if (size == 40 && c != '"') {
		fprintf(stderr, "Warning - too long text at line %i\n",
			line);
	    }
	    size++;
	    if (i == 1024) {
		seterror("Value is too long (>=1024 characters)");
		free_catalog(catalog);
		xio_close(f);
		return NULL;
	    }
	}
	while (c != '"' && c != XIO_EOF);

	if (c == XIO_EOF) {
	    seterror("Unexpected EOF in value field");
	    free_catalog(catalog);
	    xio_close(f);
	    return NULL;
	}
	
	value[i] = 0;
	find_variable(catalog, name, value);
    }				/*while */
    xio_close(f);
    return (catalog);
}				/*load_catalog */

char *find_text(catalog_t * catalog, CONST char *name)
{
    return (find_variable(catalog, name, NULL));
}