File: namespaces.c

package info (click to toggle)
rxp 1.1-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 416 kB
  • ctags: 899
  • sloc: ansic: 8,784; makefile: 102; sh: 3
file content (289 lines) | stat: -rw-r--r-- 6,111 bytes parent folder | download | duplicates (3)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <string.h>

#ifdef FOR_LT

#include "lt-memory.h"
#include "nsllib.h"

#define Malloc salloc
#define Realloc srealloc
#define Free sfree

#else

#include "system.h"

#endif

#include "charset.h"
#include "string16.h"
#include "namespaces.h"
#include "rxputil.h"

static void FreeNamespace(Namespace ns);
static void FreeNSElementDefinition(NSElementDefinition element);
static void FreeNSAttributeDefinition(NSAttributeDefinition attribute);

NamespaceUniverse global_universe = 0;

int init_namespaces(void)
{
    if(global_universe)
	return 0;

    if(!(global_universe = NewNamespaceUniverse()))
	return -1;

    return 0;
}

void deinit_namespaces(void)
{
    if(global_universe)
	FreeNamespaceUniverse(global_universe);
    global_universe = 0;
}

int reinit_namespaces(void)
{
    deinit_namespaces();
    return init_namespaces();
}

NamespaceUniverse NewNamespaceUniverse(void)
{
    NamespaceUniverse u;

    if(!(u = Malloc(sizeof(*global_universe))))
	return 0;
    VectorInit(u->namespaces);

    return u;
}

void FreeNamespaceUniverse(NamespaceUniverse universe)
{
    int i;

    if(!universe)
	universe = global_universe;

    for(i=VectorCount(universe->namespaces)-1; i>=0; --i)
	FreeNamespace(universe->namespaces[i]);

    Free(universe->namespaces);
    Free(universe);
}

Namespace NewNamespace(NamespaceUniverse universe, const char8 *uri)
{
    Namespace ns;

    if(!universe)
	universe = global_universe;

    if(!(ns = Malloc(sizeof(*ns))))
	return 0;
    if(!(ns->uri = strdup8(uri)))
	return 0;
    ns->nsnum = VectorCount(universe->namespaces);
    if(!VectorPush(universe->namespaces, ns))
	return 0;
    ns->universe = universe;
    VectorInit(ns->elements);
    VectorInit(ns->attributes);

    return ns;
}

static void FreeNamespace(Namespace ns)
{
    int i;

    for(i=VectorCount(ns->elements)-1; i>=0; --i)
	FreeNSElementDefinition(ns->elements[i]);

    for(i=VectorCount(ns->attributes)-1; i>=0; --i)
	FreeNSAttributeDefinition(ns->attributes[i]);

    Free(ns->uri);
    Free(ns->elements);
    Free(ns->attributes);
    Free(ns);
}

NSElementDefinition DefineNSElement(Namespace ns, const Char *name)
{
    NSElementDefinition e;

    if(!(e = Malloc(sizeof(*e))))
	return 0;
    if(!(e->name = Strdup(name)))
	return 0;
    e->eltnum = VectorCount(ns->elements);
    if(!VectorPush(ns->elements, e))
	return 0;
    e->namespace = ns;
    VectorInit(e->attributes);
    
    return e;
}

static void FreeNSElementDefinition(NSElementDefinition element)
{
    int i;


    for(i=VectorCount(element->attributes)-1; i>=0; --i)
	FreeNSAttributeDefinition(element->attributes[i]);

    Free(element->attributes);
    Free((Char *)element->name); /* cast to get rid of const */
    Free(element);
}

NSAttributeDefinition 
    DefineNSGlobalAttribute(Namespace ns, const Char *name)
{
    NSAttributeDefinition a;

    if(!(a = Malloc(sizeof(*a))))
	return 0;
    if(!(a->name = Strdup(name)))
	return 0;
    a->attrnum = VectorCount(ns->attributes);
    if(!VectorPush(ns->attributes, a))
	return 0;
    a->namespace = ns;
    a->element = 0;
    
    return a;
}

NSAttributeDefinition
     DefineNSElementAttribute(NSElementDefinition element, const Char *name)
{
    NSAttributeDefinition a;
    Namespace ns = element->namespace;

    if(!(a = Malloc(sizeof(*a))))
	return 0;
    if(!(a->name = Strdup(name)))
	return 0;
    a->attrnum = VectorCount(element->attributes);
    if(!VectorPush(element->attributes, a))
	return 0;
    a->namespace = ns;
    a->element = element;
    
    return a;
}

static void FreeNSAttributeDefinition(NSAttributeDefinition attribute)
{
    Free((Char *)attribute->name); /* cast to get rid of const */
    Free(attribute);
}

Namespace
    FindNamespace(NamespaceUniverse universe, const char8 *uri, int create)
{
    int i;

    if(!universe)
	universe = global_universe;

    for(i=VectorCount(universe->namespaces)-1; i>=0; --i)
	if(strcmp8(uri, universe->namespaces[i]->uri) == 0)
	    return universe->namespaces[i];

    if(create)
	return NewNamespace(universe, uri);

    return 0;
}

NSElementDefinition
    FindNSElementDefinition(Namespace ns, const Char *name, int create)
{
    int i;

    for(i=VectorCount(ns->elements)-1; i>=0; --i)
	if(Strcmp(name, ns->elements[i]->name) == 0)
	    return ns->elements[i];

    if(create)
	return DefineNSElement(ns, name);

    return 0;
}

NSAttributeDefinition
    FindNSGlobalAttributeDefinition(Namespace ns, const Char *name, int create)
{
    int i;

    for(i=VectorCount(ns->attributes)-1; i>=0; --i)
	if(Strcmp(name, ns->attributes[i]->name) == 0)
	    return ns->attributes[i];

    if(create)
	return DefineNSGlobalAttribute(ns, name);

    return 0;
}

NSAttributeDefinition
    FindNSElementAttributeDefinition(NSElementDefinition element,
				     const Char *name, int create)
{
    int i;

    for(i=VectorCount(element->attributes)-1; i>=0; --i)
	if(Strcmp(name, element->attributes[i]->name) == 0)
	    return element->attributes[i];

    if(create)
	return DefineNSElementAttribute(element, name);

    return 0;
}

Namespace 
    NextNamespace(NamespaceUniverse universe, Namespace previous)
{
    int next;
    
    if(!universe)
	universe = global_universe;
    next = previous ? previous->nsnum + 1 : 0;
    return next < VectorCount(universe->namespaces) ?
	        universe->namespaces[next] : 0;
}

NSElementDefinition
    NextNSElementDefinition(Namespace ns, NSElementDefinition previous)
{
    int next = previous ? previous->eltnum + 1: 0;
    
    return next < VectorCount(ns->elements) ? ns->elements[next] : 0;
}

NSAttributeDefinition
    NextNSGlobalAttributeDefinition(Namespace ns, 
				    NSAttributeDefinition previous)
{
    int next = previous ? previous->attrnum + 1: 0;
    
    return next < VectorCount(ns->attributes) ? ns->attributes[next] : 0;
}

NSAttributeDefinition
    NextNSElementAttributeDefinition(NSElementDefinition element,
				     NSAttributeDefinition previous)
{
    int next = previous ? previous->attrnum + 1: 0;
    
    return next < VectorCount(element->attributes) ? element->attributes[next] : 0;
}