-
Notifications
You must be signed in to change notification settings - Fork 0
/
attrib.c
412 lines (352 loc) · 8.45 KB
/
attrib.c
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
*
* attrib.c
*
*
*
*
* 08/10/2001
*/
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include "strtree.h"
#include "mem.h"
#include "attrib.h"
#include "debug.h"
/*
* Add a name to Nink list
*/
static void addNameToFixed (Fixed* fixed, char* name)
{
Nink* new_nink = (Nink*) getmem (sizeof (Nink));
new_nink->name = name;
new_nink->next = NULL;
if (fixed->fixed_candidates != NULL)
fixed->last_candidate->next = new_nink;
else
fixed->fixed_candidates = new_nink;
fixed->last_candidate = new_nink;
fixed->n_fixed++;
}
/*
* free Nink list
*/
static void releaseNinks (Fixed* fixed)
{
if (fixed->fixed_candidates != NULL) {
Nink* nink = fixed->fixed_candidates;
while (nink) {
Nink* tnink = nink->next;
memfree ((void*)nink);
nink = tnink;
}
}
}
/*
* Allocate Fixed struct
*/
Fixed* newFixedStruct (void)
{
Fixed* fixed = (Fixed*) getmem (sizeof (Fixed)); /* yuck! */
fixed->n_fixed = 0;
fixed->fixed_candidates = NULL;
fixed->last_candidate = NULL;
return fixed;
}
/*
* Fixed構造体を解放
*/
void releaseFixedStruct (Fixed* fixed)
{
releaseNinks (fixed);
memfree ((void*) fixed);
}
/*
* 新しいAttribute構造体を作成
*/
Attribute* newAttributeStruct (char* name)
{
Attribute* attr = (Attribute*) getmem (sizeof (Attribute));
attr->type = 0;
attr->name = name; /* attribute name */
attr->fixed = NULL;
attr->next = NULL; /* next link */
return attr;
}
/*
* type = AT_CDATA|AT_ID|AT_IDREF...
* attrib = (AX_REQUIRED|AX_IMPLIED|AX_FIXED)
*/
Attribute* setAttributeType (Attribute* attr, int type, int attrib)
{
attr->type = type | attrib;
return attr;
}
/*
* Attribute構造体を解放
*/
void releaseAttribute (Attribute* attr)
{
if (attr != NULL) {
while (attr) {
Attribute* tattr = attr->next;
if (attr->fixed)
releaseFixedStruct (attr->fixed);
memfree ((void*) attr);
attr = tattr;
}
}
}
/*
* enumerationタイプのアトリビュートにenum文字列を追加する
*/
Attribute* addEnumNameToAttribute (Attribute* attr, char* name)
{
if (attr->fixed == NULL)
attr->fixed = newFixedStruct ();
addNameToFixed (attr->fixed, name);
return attr;
}
/*
* Attributeのデフォルト文字列をセットする
*/
Attribute* setDefaultToAttribute (Attribute* attr, char* string)
{
attr->default_value = string;
return attr;
}
/*
* 新Elementを割り付ける
*/
Element* newElementStruct (char* elem_name)
{
Element* element = (Element*) getmem (sizeof (Element));
element->element = elem_name;
element->elemtype = 0; /* no type yet */
element->dfa = NULL;
element->n_attributes = 0;
element->attrib_list = NULL;
return element;
}
/*
* Elementを解放
*/
void releaseElementStruct (Element* sa)
{
releaseDFA (sa->dfa);
releaseAttribute (sa->attrib_list);
memfree ((void*) sa);
}
/*
* ElementにDFAをセット
*/
Element* setDfaToElement (Element* element, Dfa* dfa)
{
element->elemtype = EL_DFA;
/*
* DTDに同じエレメント名の行があると古いDFAに上書きされるので
* 古いDFAを解放してから入れる。
*/
if (element->dfa != NULL)
releaseDFA (element->dfa);
element->dfa = dfa;
return element;
}
/*
* Elementのタイプ設定します
* type = {EL_DFA|EL_PCDATA|EL_EMPTY|EL_ANY}
*/
Element* setTypeToElement (Element* element, unsigned type)
{
element->elemtype = type;
return element;
}
/*
* ElementにAttributeを追加します
*/
Element* addAttributeToElement (Element* element, Attribute* attrib)
{
if (element->attrib_list != NULL) {
element->attrib_last->next = attrib;
element->attrib_last = attrib;
} else {
element->attrib_last = attrib;
element->attrib_list = attrib;
}
element->n_attributes++;
return element;
}
/*
* 属性名から、対応するAttribute構造体を得る
*/
Attribute* findAttributeFromElement (Element* element, char* name)
{
if (element) {
if (element->attrib_list != NULL) {
Attribute* ap;
ap = element->attrib_list;
while (ap) {
if (strcmp (name, ap->name) == 0)
return ap;
ap = ap->next;
}
}
}
return NULL; /* not found */
}
/*
* 属性に関してまとめてチェック。今のところREQUIREDの属性がそろっているかどうかの
* チェックだけ。時間がないので。
* 戻り値:REQUIREDなのに、ない属性の数
* vecにない属性名のリストを返す。
*/
int checkAttributesForRequired (Element* element, StrTreeNode attribRoot, char* vec[])
{
int required_nonexistent = 0;
if (element) {
if (element->attrib_list != NULL) {
Attribute* ap = element->attrib_list;
while (ap) {
if (TypeOfAttribute (ap) & AX_REQUIRED) {
/* 省略不可属性 */
StrTreeNode node = STreeSearchNode (attribRoot, ap->name);
if (node == NULL)
vec[required_nonexistent++] = ap->name; /* ない属性名追加 */
} /* if (TypeOfAttribute (ap) & AX_REQUIRED) */
ap = ap->next;
} /* while */
}
}
return required_nonexistent;
}
/*
* ENUMタイプの属性値が正しいかどうか調べる。
* 戻り値: エラー数。エラーがなければ0。-1ならば、属性自体がない。
* vec[error*2]: 属性名
* vec[error*2+1]: 間違った属性値
*
*/
int checkAttributesForFixed (Element* element, StrTreeNode attribRoot, char* vec[])
{
int error_fixed =0;
int k = 0;
if (element) {
if (element->attrib_list != NULL) {
Attribute* ap = element->attrib_list;
char* value;
Fixed* fixed;
int found_enum = 0;
while (ap) {
if ((TypeOfAttribute (ap) & AX_REQUIRED)
&& (ATTypeOfAttribute (ap) == AT_ENUM)) {
/* 省略不可属性 & FIXED */
StrTreeNode node = STreeSearchNode (attribRoot, ap->name);
if (node == NULL)
return -1; /* ない! */
value = (char*) Closure (node);
fixed = FixedOfAttribute (ap);
found_enum = 0;
if (fixed->fixed_candidates != NULL) {
Nink* nink = fixed->fixed_candidates;
while (nink) {
if (strcmp (NameOfNink (nink), value) == 0)
found_enum++;
nink = NextOfNink (nink);
}
if (found_enum == 0) {
vec[k++] = NameOfAttribute (ap);
vec[k++] = value;
error_fixed++;
}
}
}
ap = ap->next;
} /* while */
}
}
return error_fixed;
}
/*
* state transition.
* returns accept=1, not accept=0
*/
int getNextStateOnSymbol (Element* parentElement, char* elem)
{
int accept, i, k=-1, toState=-1;
Dfa* dfa;
accept = 0; /* initiallize to not accepted yet */
if (parentElement && (dfa = parentElement->dfa)) {
for (i = 0; i < dfa->n_gotos; i++) {
if (dfa->gotos[i].fromState == parentElement->stateWork) {
k = i;
break;
}
}
if (-1 < k) {
while (dfa->gotos[k].fromState == parentElement->stateWork) {
if (strcmp (dfa->gotos[k].input_symbol, elem)==0) {
toState = dfa->gotos[k].toState;
break;
}
k++;
}
}
parentElement->stateWork = toState;
accept = dfa->gotos[k].accept;
}
return accept;
}
/*
* DFA状態遷移初期化
*/
void initStateWorkOfElement (Element* element)
{
if (element)
element->stateWork = 0;
}
#ifdef DEBUG
/*=============================================================================
* DEBUG..DEBUG..DEBUG..DEBUG..DEBUG..DEBUG..DEBUG..DEBUG..DEBUG..DEBUG..DEBUG
=============================================================================*/
void printFixed (Fixed* fixed)
{
Nink* nink;
printf ("\tFixed{");
nink = fixed->fixed_candidates;
while (nink) {
printf("%s ", nink->name);
nink = nink->next;
}
printf ("}\n");
}
void printAttribute (Attribute* attr)
{
while (attr) {
int type = attr->type & 0xff;
int att = attr->type & 0xff00;
printf ("\tname=%s type=%d attrib=%03x\n", attr->name, type, att);
if (attr->fixed)
printFixed (attr->fixed);
attr = attr->next;
}
}
void printElement (Element* element)
{
if (element) {
printf ("Element {\n");
printf ("\telement=%s type=%04x\n", element->element, element->elemtype);
if (element->dfa)
printDfa (element->dfa);
printf ("\tnumber of attributes=%d\n", element->n_attributes);
printAttribute (element->attrib_list);
printf ("}\n");
}
}
void printAllElementAttributes ()
{
extern int attribTab;
extern StrTreeNode getTree(int);
STreeApply (getTree (attribTab), printElement);
}
#endif