-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpret.c
More file actions
443 lines (435 loc) · 13.8 KB
/
interpret.c
File metadata and controls
443 lines (435 loc) · 13.8 KB
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/* BOLDIȘOR Dragoș-Alexandru - 312CB */
#include "interpret.h"
char *itoa(int x) // functie de transformare int in char*
{
int aux = x, no = 0;
x = 0;
while(aux)
{
x *= 10;
x += aux % 10;
no++;
aux /= 10;
}
char *number = calloc(no + 1, sizeof(char));
if(!number)
{
fprintf(stderr, "ERROR! Allocation\n");
return NULL;
}
while(x)
{
char c = (x % 10) + '0';
strncat(number, &c, 1);
x /= 10;
}
return number;
}
TArb TagNode(void *st_parinti) /* creaza tag nou si il leaga in arbore prin
intermediul unei stive de stramosi (varful fiind parintele noului nod) */
{
TArb arb = (TArb)malloc(sizeof(TNodArb));
if(!arb)
return NULL;
arb->firstChild = NULL;
arb->nextSibling = NULL;
arb->info = (TInfo)malloc(sizeof(TNodInfo));
if(!arb->info)
{
free(arb);
return NULL;
}
arb->info->contents = calloc(MAX_LENGTH_TAG_ELEMENTS, sizeof(char));
if(!arb->info->contents)
{
free(arb->info);
free(arb);
return NULL;
}
arb->info->type = calloc(MAX_LENGTH_TAG_ELEMENTS, sizeof(char));
if(!arb->info->type)
{
free(arb->info->contents);
free(arb->info);
free(arb);
return NULL;
}
arb->info->isSelfClosing = 0;
arb->info->otherAttributes = NULL;
arb->info->style = NULL;
void *parinte = malloc(sizeof(TArb));
if(!parinte)
{
free(arb->info->type); free(arb->info->contents); free(arb->info);
free(arb);
fprintf(stderr, "ERROR! Allocation\n");
return NULL;
}
if(VIDAS(st_parinti))
{
free(parinte);
arb->info->id = NULL;
return arb;
}
arb->info->id = calloc(MAX_LENGTH_TAG_ELEMENTS, sizeof(char));
if(!arb->info->id)
{
free(parinte); free(arb->info->type); free(arb->info->contents);
free(arb->info); free(arb);
fprintf(stderr, "ERROR! Allocation\n");
return NULL;
}
Pop(st_parinti, parinte); // cautare loc in lista de copii
int poz, i;
if( (*((TArb *) parinte))->firstChild == NULL )
{
(*((TArb *) parinte))->firstChild = arb; // legare copil de parinte
poz = 1;
}
else
{
TArb aux;
for(i = 0, aux = (*((TArb *) parinte))->firstChild;
aux->nextSibling != NULL; ++i, aux = aux->nextSibling)
{ }
aux->nextSibling = arb; // legare copil de ultimul frate
poz = i + 2;
}
if( (*((TArb *) parinte))->info->id == NULL)
{
char *num = itoa(poz); // creare id pt copiii radacinii arborelui
if(!num)
{
free(parinte); free(arb->info->type); free(arb->info->contents);
free(arb->info); free(arb);
return NULL;
}
strcpy(arb->info->id, num);
free(num);
}
else
{
strcpy(arb->info->id, (*((TArb *) parinte))->info->id );
strncat(arb->info->id, ".", 1);
char *num = itoa(poz); // creare id pt copiii unui nod
if(!num)
{
free(parinte); free(arb->info->type); free(arb->info->contents);
free(arb->info); free(arb);
return NULL;
}
strcat(arb->info->id, num);
free(num);
}
Push(st_parinti, parinte); // punem parintele inapoi in stiva
free(parinte);
return arb;
}
void endTag(TArb *a, void *st_parinti) /* functie apelata in momentul in care
se incheie informatia dintr-un tag. Se va repara contents astfel incat sa nu
contina spatii la final si (*a) se face NULL, iar in cazul in care (*a) este
deja NULL (adica se iese din 2 sau mai multe tag-uri consecutiv), scoatem
primul parinte din stiva de stramosi. In momentul in care stiva este vida,
inseamna ca am ajuns la final. */
{
if((*a))
{
if((*a)->info->contents[0] != '\0')
if((*a)->info->contents[strlen((*a)->info->contents) - 1] == ' ')
(*a)->info->contents[strlen((*a)->info->contents) - 1] = '\0';
}
if(VIDAS(st_parinti))
return;
void *aux = malloc(sizeof(TArb));
if(!aux)
{
fprintf(stderr, "ERROR! Allocation\n");
return;
}
if((*a) == NULL)
Pop(st_parinti, aux);
else if((*a)->info->isSelfClosing)
{
(*a) = NULL;
Pop(st_parinti, aux);
}
else
(*a) = NULL;
if(VIDAS(st_parinti))
*a = *((TArb *) aux);
free(aux);
}
void insertAttr(TArb *a, char *name, char *value) /* insereaza un nou atribut
in (*a) (name este numele si value, valoarea). */
{
TAttr curr = (*a)->info->otherAttributes;
if(!curr) // daca este primul atribut
{
(*a)->info->otherAttributes = malloc(sizeof(TNodAttr));
if(!(*a)->info->otherAttributes)
{
fprintf(stderr, "ERROR! Allocation\n");
return;
}
curr = (*a)->info->otherAttributes;
curr->name = malloc(MAX_LENGTH_TAG_ELEMENTS * sizeof(char));
if(!curr->name)
{
free((*a)->info->otherAttributes);
fprintf(stderr, "ERROR! Allocation\n");
return;
}
curr->value = malloc(MAX_LENGTH_TAG_ELEMENTS * sizeof(char));
if(!curr->value)
{
free(curr->name);
free((*a)->info->otherAttributes);
fprintf(stderr, "ERROR! Allocation\n");
return;
}
strcpy(curr->name, name);
strcpy(curr->value, value);
curr->next = NULL;
}
else // daca mai exista atribute, il cautam pe ultimul
{
while(curr->next)
curr = curr->next;
curr->next = malloc(sizeof(TNodAttr));
if(!curr->next)
{
fprintf(stderr, "ERROR! Allocation\n");
return;
}
curr->next->name = malloc(MAX_LENGTH_TAG_ELEMENTS * sizeof(char));
if(!curr->next->name)
{
free(curr->next);
fprintf(stderr, "ERROR! Allocation\n");
return;
}
curr->next->value = malloc(MAX_LENGTH_TAG_ELEMENTS * sizeof(char));
if(!curr->value)
{
free(curr->next->name);
free(curr->next);
fprintf(stderr, "ERROR! Allocation\n");
return;
}
strcpy(curr->next->name, name);
strcpy(curr->next->value, value);
curr->next->next = NULL;
}
name[0] = '\0'; value[0] = '\0';
}
void insertStyle(TArb *a, char *name, char *value) /* insereaza un nou atribut
de tip style in (*a) (name este numele si value, valoarea). */
{
TAttr curr = (*a)->info->style;
if(!curr) // daca este primul atribut
{
(*a)->info->style = malloc(sizeof(TNodAttr));
if(!(*a)->info->style)
{
fprintf(stderr, "ERROR! Allocation\n");
return;
}
curr = (*a)->info->style;
curr->name = malloc(MAX_LENGTH_TAG_ELEMENTS * sizeof(char));
if(!curr->name)
{
free((*a)->info->style);
fprintf(stderr, "ERROR! Allocation\n");
return;
}
curr->value = malloc(MAX_LENGTH_TAG_ELEMENTS * sizeof(char));
if(!curr->value)
{
free(curr->name);
free((*a)->info->style);
fprintf(stderr, "ERROR! Allocation\n");
return;
}
strcpy(curr->name, name);
strcpy(curr->value, value);
curr->next = NULL;
}
else // daca mai exista atribute, il cautam pe ultimul
{
while(curr->next)
curr = curr->next;
curr->next = malloc(sizeof(TNodAttr));
if(!curr->next)
{
fprintf(stderr, "ERROR! Allocation\n");
return;
}
curr->next->name = malloc(MAX_LENGTH_TAG_ELEMENTS * sizeof(char));
if(!curr->next->name)
{
free(curr->next);
fprintf(stderr, "ERROR! Allocation\n");
return;
}
curr->next->value = malloc(MAX_LENGTH_TAG_ELEMENTS * sizeof(char));
if(!curr->value)
{
free(curr->next->name);
free(curr->next);
fprintf(stderr, "ERROR! Allocation\n");
return;
}
strcpy(curr->next->name, name);
strcpy(curr->next->value, value);
curr->next->next = NULL;
}
}
void createStyle(TArb *a, char *name, char *value) /* face modificarile
necesare pentru a apela functia insertStyle. La inceput, name va fi style si
value va retine totalitatea atributelor de tip style care urmeaza sa fie
inserate. */
{
char *buffer = calloc(MAX_LENGTH_TAG_ELEMENTS, sizeof(char));
if(!buffer)
{
fprintf(stderr, "ERROR! Allocation\n");
return;
}
strcpy(buffer, value); // salvare in buffer
name[0] = '\0'; value[0] = '\0';
unsigned int i;
int state = 0; // adica este nume
for(i = 0; i < strlen(buffer); ++i)
if(!isspace(buffer[i]))
{
if(buffer[i] == ':')
state = 1; // adica este valoare
else if(buffer[i] == ';')
{
state = 0; // urmeaza un nume
insertStyle(a, name, value); // inserare
name[0] = '\0'; value[0] = '\0';
}
else
{
if(state == 0) // facem verificarea
strncat(name, &buffer[i], 1);
else
strncat(value, &buffer[i], 1);
}
}
free(buffer);
}
TParseState Interpret(TParseState currentState, char c, TArb *a,
void *st_parinti, char *name, char *value) /* prezinta argumentele initiale
(currentState - statusul curent in care se afla "automatul", c - caracterul
care urmeaza sa fie verificat, precum si arborele *a). De asemenea, am introdus
si alte argumente (stiva de stramosi, precum si stringurile name si value in
care se retin temporar informatiile unui atribut). */
{
TParseState nextState;
switch (currentState){
case PARSE_CONTENTS:
if (c == '<')
nextState = PARSE_OPENING_BRACKET;
else {
nextState = PARSE_CONTENTS;
if(c != '\n' && c != 13 && (*a) != NULL){ // caracter valid
if((*a)->info->contents[0] == '\0' && c == ' ')
break; //nu punem spatii inainte sa avem alte caractere
if((*a)->info->contents[0] != '\0')
if((*a)->info->contents
[strlen((*a)->info->contents) - 1] == ' ' && c == ' ')
break; // nu punem mai mult de un spatiu consecutiv
strncat((*a)->info->contents, &c, 1);
}
}
break;
case PARSE_OPENING_BRACKET:
if (isspace(c))
nextState = PARSE_OPENING_BRACKET;
else if (c == '>')
nextState = PARSE_ERROR;
else if (c == '/')
nextState = PARSE_CLOSING_TAG;
else {
nextState = PARSE_TAG_TYPE;
if((*a) != NULL && (*a)->info->isSelfClosing == 0)
Push(st_parinti, a); // introducem in stiva de stramosi
*a = TagNode(st_parinti); // creem un nou nod
if(!(*a)){
fprintf(stderr,"ERROR! Allocation\n");
return PARSE_ERROR;
}
strncat((*a)->info->type, &c, 1); /* introducem primul caracter
in type */
}
break;
case PARSE_TAG_TYPE:
if (isspace(c))
nextState = PARSE_REST_OF_TAG;
else if (c == '>')
nextState = PARSE_CONTENTS;
else {
nextState = PARSE_TAG_TYPE;
strncat((*a)->info->type, &c, 1); // introducere in type
}
break;
case PARSE_CLOSING_TAG:
if (c == '>') {
nextState = PARSE_CONTENTS;
endTag(a, st_parinti);
}
else
nextState = PARSE_CLOSING_TAG;
break;
case PARSE_REST_OF_TAG:
if (isspace(c))
nextState = PARSE_REST_OF_TAG;
else if (c == '>')
nextState = PARSE_CONTENTS;
else if (c == '/')
nextState = PARSE_SELF_CLOSING;
else{
nextState = PARSE_ATTRIBUTE_NAME;
strncat(name, &c, 1); // introducere in nume atribut temporar
}
break;
case PARSE_ATTRIBUTE_NAME:
if (c == '=')
nextState = PARSE_ATTRIBUTE_EQ;
else {
nextState = PARSE_ATTRIBUTE_NAME;
strncat(name, &c, 1); // introducere in nume atribut temporar
}
break;
case PARSE_ATTRIBUTE_EQ:
if (c == '\"')
nextState = PARSE_ATTRIBUTE_VALUE;
break;
case PARSE_ATTRIBUTE_VALUE:
if (c == '\"') {
nextState = PARSE_REST_OF_TAG;
if(!strcmp(name, "style"))
createStyle(a, name, value); // creare atribute de tip style
else
insertAttr(a, name, value); // creare atribute (otherAtributes)
}
else {
nextState = PARSE_ATTRIBUTE_VALUE;
strncat(value, &c, 1); // introducere in value atribut temporar
}
break;
case PARSE_SELF_CLOSING:
if (c == '>') {
nextState = PARSE_CONTENTS;
(*a)->info->isSelfClosing = 1; // tag-ul este self-closing
}
else
nextState = PARSE_ERROR;
break;
default:
break;
}
return nextState;
}