forked from MiSTer-devel/Main_MiSTer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sxmlc.c
2291 lines (1910 loc) · 62.6 KB
/
sxmlc.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
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2010, Matthieu Labas
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of the FreeBSD Project.
*/
#if defined(WIN32) || defined(WIN64)
#pragma warning(disable : 4996)
#else
#ifndef strdup
#define _GNU_SOURCE
#endif
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "sxmlc.h"
/*
Struct defining "special" tags such as "<? ?>" or "<![CDATA[ ]]/>".
These tags are considered having a start and an end with some data in between that will
be stored in the 'tag' member of an XMLNode.
The 'tag_type' member is a constant that is associated to such tag.
All 'len_*' members are basically the "sx_strlen()" of 'start' and 'end' members.
*/
typedef struct _Tag {
TagType tag_type;
SXML_CHAR* start;
int len_start;
SXML_CHAR* end;
int len_end;
} _TAG;
typedef struct _SpecialTag {
_TAG *tags;
int n_tags;
} SPECIAL_TAG;
/*
List of "special" tags handled by sxmlc.
NB the "<!DOCTYPE" tag has a special handling because its 'end' changes according
to its content ('>' or ']>').
*/
static _TAG _spec[] = {
{ TAG_INSTR, C2SX("<?"), 2, C2SX("?>"), 2 },
{ TAG_COMMENT, C2SX("<!--"), 4, C2SX("-->"), 3 },
{ TAG_CDATA, C2SX("<![CDATA["), 9, C2SX("]]>"), 3 }
};
static int NB_SPECIAL_TAGS = (int)(sizeof(_spec) / sizeof(_TAG)); /* Auto computation of number of special tags */
/*
User-registered tags.
*/
static SPECIAL_TAG _user_tags = { NULL, 0 };
int XML_register_user_tag(TagType tag_type, SXML_CHAR* start, SXML_CHAR* end)
{
_TAG* p;
int i, n, le;
if (tag_type < TAG_USER)
return -1;
if (start == NULL || end == NULL || *start != C2SX('<'))
return -1;
le = sx_strlen(end);
if (end[le-1] != C2SX('>'))
return -1;
i = _user_tags.n_tags;
n = i + 1;
p = (_TAG*)__realloc(_user_tags.tags, n * sizeof(_TAG));
if (p == NULL)
return -1;
p[i].tag_type = tag_type;
p[i].start = start;
p[i].end = end;
p[i].len_start = sx_strlen(start);
p[i].len_end = le;
_user_tags.tags = p;
_user_tags.n_tags = n;
return i;
}
int XML_unregister_user_tag(int i_tag)
{
_TAG* pt;
if (i_tag < 0 || i_tag >= _user_tags.n_tags)
return -1;
if (_user_tags.n_tags == 1)
pt = NULL;
else {
pt = (_TAG*)__malloc((_user_tags.n_tags - 1) * sizeof(_TAG));
if (pt == NULL)
return -1;
}
if (pt != NULL) {
memcpy(pt, _user_tags.tags, i_tag * sizeof(_TAG));
memcpy(&pt[i_tag], &_user_tags.tags[i_tag + 1], (_user_tags.n_tags - i_tag - 1) * sizeof(_TAG));
}
if (_user_tags.tags != NULL)
__free(_user_tags.tags);
_user_tags.tags = pt;
_user_tags.n_tags--;
return _user_tags.n_tags;
}
int XML_get_nb_registered_user_tags(void)
{
return _user_tags.n_tags;
}
int XML_get_registered_user_tag(TagType tag_type)
{
int i;
for (i = 0; i < _user_tags.n_tags; i++)
if (_user_tags.tags[i].tag_type == tag_type)
return i;
return -1;
}
/* --- XMLNode methods --- */
/*
Add 'node' to given '*children_array' of '*len_array' elements.
'*len_array' is overwritten with the number of elements in '*children_array' after its reallocation.
Return the index of the newly added 'node' in '*children_array', or '-1' for memory error.
*/
static int _add_node(XMLNode*** children_array, int* len_array, XMLNode* node)
{
XMLNode** pt = (XMLNode**)__realloc(*children_array, (*len_array+1) * sizeof(XMLNode*));
if (pt == NULL)
return -1;
pt[*len_array] = node;
*children_array = pt;
return (*len_array)++;
}
int XMLNode_init(XMLNode* node)
{
if (node == NULL)
return false;
if (node->init_value == XML_INIT_DONE)
return true; /*(void)XMLNode_free(node);*/
node->tag = NULL;
node->text = NULL;
node->attributes = NULL;
node->n_attributes = 0;
node->father = NULL;
node->children = NULL;
node->n_children = 0;
node->tag_type = TAG_NONE;
node->active = true;
node->init_value = XML_INIT_DONE;
return true;
}
XMLNode* XMLNode_allocN(int n)
{
int i;
XMLNode* p;
if (n <= 0)
return NULL;
p = (XMLNode*)__calloc(n, sizeof(XMLNode));
if (p == NULL)
return NULL;
for (i = 0; i < n; i++)
(void)XMLNode_init(&p[i]);
return p;
}
XMLNode* XMLNode_dup(const XMLNode* node, int copy_children)
{
XMLNode* n;
if (node == NULL)
return NULL;
n = (XMLNode*)__calloc(1, sizeof(XMLNode));
if (n == NULL)
return NULL;
XMLNode_init(n);
if (!XMLNode_copy(n, node, copy_children)) {
XMLNode_free(n);
return NULL;
}
return n;
}
int XMLNode_free(XMLNode* node)
{
if (node == NULL || node->init_value != XML_INIT_DONE)
return false;
if (node->tag != NULL) {
__free(node->tag);
node->tag = NULL;
}
XMLNode_remove_text(node);
XMLNode_remove_all_attributes(node);
XMLNode_remove_children(node);
node->tag_type = TAG_NONE;
return true;
}
int XMLNode_copy(XMLNode* dst, const XMLNode* src, int copy_children)
{
int i;
if (dst == NULL || (src != NULL && src->init_value != XML_INIT_DONE))
return false;
(void)XMLNode_free(dst); /* 'dst' is freed first */
/* NULL 'src' resets 'dst' */
if (src == NULL)
return true;
/* Tag */
if (src->tag != NULL) {
dst->tag = sx_strdup(src->tag);
if (dst->tag == NULL) goto copy_err;
}
/* Text */
if (dst->text != NULL) {
dst->text = sx_strdup(src->text);
if (dst->text == NULL) goto copy_err;
}
/* Attributes */
if (src->n_attributes > 0) {
dst->attributes = (XMLAttribute*)__calloc(src->n_attributes, sizeof(XMLAttribute));
if (dst->attributes== NULL) goto copy_err;
dst->n_attributes = src->n_attributes;
for (i = 0; i < src->n_attributes; i++) {
dst->attributes[i].name = sx_strdup(src->attributes[i].name);
dst->attributes[i].value = sx_strdup(src->attributes[i].value);
if (dst->attributes[i].name == NULL || dst->attributes[i].value == NULL) goto copy_err;
dst->attributes[i].active = src->attributes[i].active;
}
}
dst->tag_type = src->tag_type;
dst->father = src->father;
dst->user = src->user;
dst->active = src->active;
/* Copy children if required (and there are any) */
if (copy_children && src->n_children > 0) {
dst->children = (XMLNode**)__calloc(src->n_children, sizeof(XMLNode*));
if (dst->children == NULL) goto copy_err;
dst->n_children = src->n_children;
for (i = 0; i < src->n_children; i++) {
if (!XMLNode_copy(dst->children[i], src->children[i], true)) goto copy_err;
}
}
return true;
copy_err:
(void)XMLNode_free(dst);
return false;
}
int XMLNode_set_active(XMLNode* node, int active)
{
if (node == NULL || node->init_value != XML_INIT_DONE)
return false;
node->active = active;
return true;
}
int XMLNode_set_tag(XMLNode* node, const SXML_CHAR* tag)
{
SXML_CHAR* newtag;
if (node == NULL || tag == NULL || node->init_value != XML_INIT_DONE)
return false;
newtag = sx_strdup(tag);
if (newtag == NULL)
return false;
if (node->tag != NULL) __free(node->tag);
node->tag = newtag;
return true;
}
int XMLNode_set_type(XMLNode* node, const TagType tag_type)
{
if (node == NULL || node->init_value != XML_INIT_DONE)
return false;
switch (tag_type) {
case TAG_ERROR:
case TAG_END:
case TAG_PARTIAL:
case TAG_NONE:
return false;
default:
node->tag_type = tag_type;
return true;
}
}
int XMLNode_set_attribute(XMLNode* node, const SXML_CHAR* attr_name, const SXML_CHAR* attr_value)
{
XMLAttribute* pt;
int i;
if (node == NULL || attr_name == NULL || attr_name[0] == NULC || node->init_value != XML_INIT_DONE)
return -1;
i = XMLNode_search_attribute(node, attr_name, 0);
if (i >= 0) { /* Attribute found: update it */
SXML_CHAR* value = NULL;
if (attr_value != NULL && (value = sx_strdup(attr_value)) == NULL)
return -1;
pt = node->attributes;
if (pt[i].value != NULL)
__free(pt[i].value);
pt[i].value = value;
} else { /* Attribute not found: add it */
SXML_CHAR* name = sx_strdup(attr_name);
SXML_CHAR* value = (attr_value == NULL ? NULL : sx_strdup(attr_value));
if (name == NULL || (value == NULL && attr_value != NULL)) {
if (value != NULL)
__free(value);
if (name != NULL)
__free(name);
return -1;
}
i = node->n_attributes;
pt = (XMLAttribute*)__realloc(node->attributes, (i+1) * sizeof(XMLAttribute));
if (pt == NULL) {
if (value != NULL)
__free(value);
__free(name);
return -1;
}
pt[i].name = name;
pt[i].value = value;
pt[i].active = true;
node->attributes = pt;
node->n_attributes = i + 1;
}
return node->n_attributes;
}
int XMLNode_get_attribute_with_default(XMLNode* node, const SXML_CHAR* attr_name, const SXML_CHAR** attr_value, const SXML_CHAR* default_attr_value)
{
XMLAttribute* pt;
int i;
if (node == NULL || attr_name == NULL || attr_name[0] == NULC || attr_value == NULL || node->init_value != XML_INIT_DONE)
return false;
i = XMLNode_search_attribute(node, attr_name, 0);
if (i >= 0) {
pt = node->attributes;
if (pt[i].value != NULL) {
*attr_value = sx_strdup(pt[i].value);
if (*attr_value == NULL)
return false;
} else
*attr_value = NULL; /* NULL but returns 'true' as 'NULL' is the actual attribute value */
} else if (default_attr_value != NULL) {
*attr_value = sx_strdup(default_attr_value);
if (*attr_value == NULL)
return false;
} else
*attr_value = NULL;
return true;
}
int XMLNode_get_attribute_count(const XMLNode* node)
{
int i, n;
if (node == NULL || node->init_value != XML_INIT_DONE)
return -1;
for (i = n = 0; i < node->n_attributes; i++)
if (node->attributes[i].active) n++;
return n;
}
int XMLNode_search_attribute(const XMLNode* node, const SXML_CHAR* attr_name, int i_search)
{
int i;
if (node == NULL || attr_name == NULL || attr_name[0] == NULC || i_search < 0 || i_search >= node->n_attributes)
return -1;
for (i = i_search; i < node->n_attributes; i++)
if (node->attributes[i].active && !sx_strcmp(node->attributes[i].name, attr_name))
return i;
return -1;
}
int XMLNode_remove_attribute(XMLNode* node, int i_attr)
{
XMLAttribute* pt;
if (node == NULL || node->init_value != XML_INIT_DONE || i_attr < 0 || i_attr >= node->n_attributes)
return -1;
/* Before modifying first see if we run out of memory */
if (node->n_attributes == 1)
pt = NULL;
else {
pt = (XMLAttribute*)__malloc((node->n_attributes - 1) * sizeof(XMLAttribute));
if (pt == NULL)
return -1;
}
/* Can't fail anymore, free item */
if (node->attributes[i_attr].name != NULL) __free(node->attributes[i_attr].name);
if (node->attributes[i_attr].value != NULL) __free(node->attributes[i_attr].value);
if (pt != NULL) {
memcpy(pt, node->attributes, i_attr * sizeof(XMLAttribute));
memcpy(&pt[i_attr], &node->attributes[i_attr + 1], (node->n_attributes - i_attr - 1) * sizeof(XMLAttribute));
}
if (node->attributes != NULL)
__free(node->attributes);
node->attributes = pt;
node->n_attributes--;
return node->n_attributes;
}
int XMLNode_remove_all_attributes(XMLNode* node)
{
int i;
if (node == NULL || node->init_value != XML_INIT_DONE)
return false;
if (node->attributes != NULL) {
for (i = 0; i < node->n_attributes; i++) {
if (node->attributes[i].name != NULL)
__free(node->attributes[i].name);
if (node->attributes[i].value != NULL)
__free(node->attributes[i].value);
}
__free(node->attributes);
node->attributes = NULL;
}
node->n_attributes = 0;
return true;
}
int XMLNode_set_text(XMLNode* node, const SXML_CHAR* text)
{
SXML_CHAR* p;
if (node == NULL || node->init_value != XML_INIT_DONE)
return false;
if (text == NULL) { /* We want to remove it => free node text */
if (node->text != NULL) {
__free(node->text);
node->text = NULL;
}
return true;
}
p = (SXML_CHAR*)__realloc(node->text, (sx_strlen(text) + 1)*sizeof(SXML_CHAR)); /* +1 for '\0' */
if (p == NULL)
return false;
node->text = p;
sx_strcpy(node->text, text);
return true;
}
int XMLNode_add_child(XMLNode* node, XMLNode* child)
{
if (node == NULL || child == NULL || node->init_value != XML_INIT_DONE || child->init_value != XML_INIT_DONE)
return false;
if (_add_node(&node->children, &node->n_children, child) >= 0) {
node->tag_type = TAG_FATHER;
child->father = node;
return true;
} else
return false;
}
int XMLNode_get_children_count(const XMLNode* node)
{
int i, n;
if (node == NULL || node->init_value != XML_INIT_DONE)
return -1;
for (i = n = 0; i < node->n_children; i++)
if (node->children[i]->active) n++;
return n;
}
XMLNode* XMLNode_get_child(const XMLNode* node, int i_child)
{
int i;
if (node == NULL || node->init_value != XML_INIT_DONE || i_child < 0 || i_child >= node->n_children)
return NULL;
for (i = 0; i < node->n_children; i++) {
if (!node->children[i]->active)
i_child++;
else if (i == i_child)
return node->children[i];
}
return NULL;
}
int XMLNode_remove_child(XMLNode* node, int i_child, int free_child)
{
int i;
XMLNode** pt;
if (node == NULL || node->init_value != XML_INIT_DONE || i_child < 0 || i_child >= node->n_children)
return -1;
/* Lookup 'i_child'th active child */
for (i = 0; i < node->n_children; i++) {
if (!node->children[i]->active)
i_child++;
else if (i == i_child)
break;
}
if (i >= node->n_children)
return -1; /* Children is not found */
/* Before modifying first see if we run out of memory */
if (node->n_children == 1)
pt = NULL;
else {
pt = (XMLNode**)__malloc((node->n_children - 1) * sizeof(XMLNode*));
if (pt == NULL)
return -1;
}
/* Can't fail anymore, free item */
(void)XMLNode_free(node->children[i_child]);
if (free_child)
__free(node->children[i_child]);
if (pt != NULL) {
memcpy(pt, node->children, i_child * sizeof(XMLNode*));
memcpy(&pt[i_child], &node->children[i_child + 1], (node->n_children - i_child - 1) * sizeof(XMLNode*));
}
if (node->children != NULL)
__free(node->children);
node->children = pt;
node->n_children--;
if (node->n_children == 0)
node->tag_type = TAG_SELF;
return node->n_children;
}
int XMLNode_remove_children(XMLNode* node)
{
int i;
if (node == NULL || node->init_value != XML_INIT_DONE)
return false;
if (node->children != NULL) {
for (i = 0; i < node->n_children; i++)
if (node->children[i] != NULL) {
(void)XMLNode_free(node->children[i]);
__free(node->children[i]);
}
__free(node->children);
node->children = NULL;
}
node->n_children = 0;
return true;
}
int XMLNode_equal(const XMLNode* node1, const XMLNode* node2)
{
int i, j;
if (node1 == node2)
return true;
if (node1 == NULL || node2 == NULL || node1->init_value != XML_INIT_DONE || node2->init_value != XML_INIT_DONE)
return false;
if (sx_strcmp(node1->tag, node2->tag))
return false;
/* Test all attributes from 'node1' */
for (i = 0; i < node1->n_attributes; i++) {
if (!node1->attributes[i].active)
continue;
j = XMLNode_search_attribute(node2, node1->attributes[i].name, 0);
if (j < 0)
return false;
if (sx_strcmp(node1->attributes[i].value, node2->attributes[j].value))
return false;
}
/* Test other attributes from 'node2' that might not be in 'node1' */
for (i = 0; i < node2->n_attributes; i++) {
if (!node2->attributes[i].active)
continue;
j = XMLNode_search_attribute(node1, node2->attributes[i].name, 0);
if (j < 0)
return false;
if (sx_strcmp(node2->attributes[i].name, node1->attributes[j].name))
return false;
}
return true;
}
XMLNode* XMLNode_next_sibling(const XMLNode* node)
{
int i;
XMLNode* father;
if (node == NULL || node->init_value != XML_INIT_DONE || node->father == NULL)
return NULL;
father = node->father;
for (i = 0; i < father->n_children && father->children[i] != node; i++) ;
i++; /* father->children[i] is now 'node' next sibling */
return i < father->n_children ? father->children[i] : NULL;
}
static XMLNode* _XMLNode_next(const XMLNode* node, int in_children)
{
XMLNode* node2;
if (node == NULL || node->init_value != XML_INIT_DONE)
return NULL;
/* Check first child */
if (in_children && node->n_children > 0)
return node->children[0];
/* Check next sibling */
if ((node2 = XMLNode_next_sibling(node)) != NULL)
return node2;
/* Check next uncle */
return _XMLNode_next(node->father, false);
}
XMLNode* XMLNode_next(const XMLNode* node)
{
return _XMLNode_next(node, true);
}
/* --- XMLDoc methods --- */
int XMLDoc_init(XMLDoc* doc)
{
if (doc == NULL)
return false;
doc->filename[0] = NULC;
#ifdef SXMLC_UNICODE
memset(&doc->bom, 0, sizeof(doc->bom));
#endif
doc->nodes = NULL;
doc->n_nodes = 0;
doc->i_root = -1;
doc->init_value = XML_INIT_DONE;
return true;
}
int XMLDoc_free(XMLDoc* doc)
{
int i;
if (doc == NULL || doc->init_value != XML_INIT_DONE)
return false;
for (i = 0; i < doc->n_nodes; i++) {
(void)XMLNode_free(doc->nodes[i]);
__free(doc->nodes[i]);
}
__free(doc->nodes);
doc->nodes = NULL;
doc->n_nodes = 0;
doc->i_root = -1;
return true;
}
int XMLDoc_set_root(XMLDoc* doc, int i_root)
{
if (doc == NULL || doc->init_value != XML_INIT_DONE || i_root < 0 || i_root >= doc->n_nodes)
return false;
doc->i_root = i_root;
return true;
}
int XMLDoc_add_node(XMLDoc* doc, XMLNode* node)
{
if (doc == NULL || node == NULL || doc->init_value != XML_INIT_DONE)
return -1;
if (_add_node(&doc->nodes, &doc->n_nodes, node) < 0)
return -1;
if (node->tag_type == TAG_FATHER)
doc->i_root = doc->n_nodes - 1; /* Main root node is the last father node */
return doc->n_nodes;
}
int XMLDoc_remove_node(XMLDoc* doc, int i_node, int free_node)
{
XMLNode** pt;
if (doc == NULL || doc->init_value != XML_INIT_DONE || i_node < 0 || i_node > doc->n_nodes)
return false;
/* Before modifying first see if we run out of memory */
if (doc->n_nodes == 1)
pt = NULL;
else {
pt = (XMLNode**)__malloc((doc->n_nodes - 1) * sizeof(XMLNode*));
if (pt == NULL)
return false;
}
/* Can't fail anymore, free item */
(void)XMLNode_free(doc->nodes[i_node]);
if (free_node) __free(doc->nodes[i_node]);
if (pt != NULL) {
memcpy(pt, &doc->nodes[i_node], i_node * sizeof(XMLNode*));
memcpy(&pt[i_node], &doc->nodes[i_node + 1], (doc->n_nodes - i_node - 1) * sizeof(XMLNode*));
}
if (doc->nodes != NULL)
__free(doc->nodes);
doc->nodes = pt;
doc->n_nodes--;
return true;
}
/*
Helper functions to print formatting before a new tag.
Returns the new number of characters in the line.
*/
static int _count_new_char_line(const SXML_CHAR* str, int nb_char_tab, int cur_sz_line)
{
for (; *str; str++) {
if (*str == C2SX('\n'))
cur_sz_line = 0;
else if (*str == C2SX('\t'))
cur_sz_line += nb_char_tab;
else
cur_sz_line++;
}
return cur_sz_line;
}
static int _print_formatting(const XMLNode* node, FILE* f, const SXML_CHAR* tag_sep, const SXML_CHAR* child_sep, int nb_char_tab, int cur_sz_line)
{
if (tag_sep != NULL) {
sx_fprintf(f, tag_sep);
cur_sz_line = _count_new_char_line(tag_sep, nb_char_tab, cur_sz_line);
}
if (child_sep != NULL) {
for (node = node->father; node != NULL; node = node->father) {
sx_fprintf(f, child_sep);
cur_sz_line = _count_new_char_line(child_sep, nb_char_tab, cur_sz_line);
}
}
return cur_sz_line;
}
static int _XMLNode_print_header(const XMLNode* node, FILE* f, const SXML_CHAR* tag_sep, const SXML_CHAR* child_sep, const SXML_CHAR* attr_sep, int sz_line, int cur_sz_line, int nb_char_tab)
{
int i;
SXML_CHAR* p;
if (node == NULL || f == NULL || !node->active || node->tag == NULL || node->tag[0] == NULC)
return -1;
/* Special handling of DOCTYPE */
if (node->tag_type == TAG_DOCTYPE) {
/* Search for an unescaped '[' in the DOCTYPE definition, in which case the end delimiter should be ']>' instead of '>' */
for (p = sx_strchr(node->tag, C2SX('[')); p != NULL && *(p-1) == C2SX('\\'); p = sx_strchr(p+1, C2SX('['))) ;
cur_sz_line += sx_fprintf(f, C2SX("<!DOCTYPE%s%s>"), node->tag, p != NULL ? C2SX("]") : C2SX(""));
return cur_sz_line;
}
/* Check for special tags first */
for (i = 0; i < NB_SPECIAL_TAGS; i++) {
if (node->tag_type == _spec[i].tag_type) {
sx_fprintf(f, C2SX("%s%s%s"), _spec[i].start, node->tag, _spec[i].end);
cur_sz_line += sx_strlen(_spec[i].start) + sx_strlen(node->tag) + sx_strlen(_spec[i].end);
return cur_sz_line;
}
}
/* Check for user tags */
for (i = 0; i < _user_tags.n_tags; i++) {
if (node->tag_type == _user_tags.tags[i].tag_type) {
sx_fprintf(f, C2SX("%s%s%s"), _user_tags.tags[i].start, node->tag, _user_tags.tags[i].end);
cur_sz_line += sx_strlen(_user_tags.tags[i].start) + sx_strlen(node->tag) + sx_strlen(_user_tags.tags[i].end);
return cur_sz_line;
}
}
/* Print tag name */
cur_sz_line += sx_fprintf(f, C2SX("<%s"), node->tag);
/* Print attributes */
for (i = 0; i < node->n_attributes; i++) {
if (!node->attributes[i].active)
continue;
cur_sz_line += sx_strlen(node->attributes[i].name) + sx_strlen(node->attributes[i].value) + 3;
if (sz_line > 0 && cur_sz_line > sz_line) {
cur_sz_line = _print_formatting(node, f, tag_sep, child_sep, nb_char_tab, cur_sz_line);
/* Add extra separator, as if new line was a child of the previous one */
if (child_sep != NULL) {
sx_fprintf(f, child_sep);
cur_sz_line = _count_new_char_line(child_sep, nb_char_tab, cur_sz_line);
}
}
/* Attribute name */
if (attr_sep != NULL) {
sx_fprintf(f, attr_sep);
cur_sz_line = _count_new_char_line(attr_sep, nb_char_tab, cur_sz_line);
sx_fprintf(f, C2SX("%s="), node->attributes[i].name);
} else
sx_fprintf(f, C2SX(" %s="), node->attributes[i].name);
/* Attribute value */
(void)sx_fputc(XML_DEFAULT_QUOTE, f);
cur_sz_line += fprintHTML(f, node->attributes[i].value) + 2;
(void)sx_fputc(XML_DEFAULT_QUOTE, f);
}
/* End the tag if there are no children and no text */
if (node->n_children == 0 && (node->text == NULL || node->text[0] == NULC)) {
cur_sz_line += sx_fprintf(f, C2SX("/>"));
} else {
(void)sx_fputc(C2SX('>'), f);
cur_sz_line++;
}
return cur_sz_line;
}
int XMLNode_print_header(const XMLNode* node, FILE* f, int sz_line, int nb_char_tab)
{
return _XMLNode_print_header(node, f, NULL, NULL, NULL, sz_line, 0, nb_char_tab) < 0 ? false : true;
}
static int _XMLNode_print(const XMLNode* node, FILE* f, const SXML_CHAR* tag_sep, const SXML_CHAR* child_sep, const SXML_CHAR* attr_sep, int keep_text_spaces, int sz_line, int cur_sz_line, int nb_char_tab, int depth)
{
int i;
SXML_CHAR* p;
if (node != NULL && node->tag_type==TAG_TEXT) { /* Text has to be printed: check if it is only spaces */
if (!keep_text_spaces) {
for (p = node->text; *p != NULC && sx_isspace(*p); p++) ; /* 'p' points to first non-space character, or to '\0' if only spaces */
} else
p = node->text; /* '*p' won't be '\0' */
if (*p != NULC)
cur_sz_line += fprintHTML(f, node->text);
return cur_sz_line;
}
if (node == NULL || f == NULL || !node->active || node->tag == NULL || node->tag[0] == NULC)
return -1;
if (nb_char_tab <= 0)
nb_char_tab = 1;
/* Print formatting */
if (depth < 0) /* UGLY HACK: 'depth' forced negative on very first line so we don't print an extra 'tag_sep' (usually "\n" when pretty-printing) */
depth = 0;
else
cur_sz_line = _print_formatting(node, f, tag_sep, child_sep, nb_char_tab, cur_sz_line);
_XMLNode_print_header(node, f, tag_sep, child_sep, attr_sep, sz_line, cur_sz_line, nb_char_tab);
if (node->text != NULL && node->text[0] != NULC) {
/* Text has to be printed: check if it is only spaces */
if (!keep_text_spaces) {
for (p = node->text; *p != NULC && sx_isspace(*p); p++) ; /* 'p' points to first non-space character, or to '\0' if only spaces */
} else
p = node->text; /* '*p' won't be '\0' */
if (*p != NULC) cur_sz_line += fprintHTML(f, node->text);
} else if (node->n_children <= 0) /* Everything has already been printed */
return cur_sz_line;
/* Recursively print children */
for (i = 0; i < node->n_children; i++)
(void)_XMLNode_print(node->children[i], f, tag_sep, child_sep, attr_sep, keep_text_spaces, sz_line, cur_sz_line, nb_char_tab, depth+1);
/* Print tag end after children */
/* Print formatting */
if (node->n_children > 0)
cur_sz_line = _print_formatting(node, f, tag_sep, child_sep, nb_char_tab, cur_sz_line);
cur_sz_line += sx_fprintf(f, C2SX("</%s>"), node->tag);
return cur_sz_line;
}
int XMLNode_print_attr_sep(const XMLNode* node, FILE* f, const SXML_CHAR* tag_sep, const SXML_CHAR* child_sep, const SXML_CHAR* attr_sep, int keep_text_spaces, int sz_line, int nb_char_tab)
{
return _XMLNode_print(node, f, tag_sep, child_sep, attr_sep, keep_text_spaces, sz_line, 0, nb_char_tab, 0);
}
int XMLDoc_print_attr_sep(const XMLDoc* doc, FILE* f, const SXML_CHAR* tag_sep, const SXML_CHAR* child_sep, const SXML_CHAR* attr_sep, int keep_text_spaces, int sz_line, int nb_char_tab)
{
int i, depth, cur_sz_line;
if (doc == NULL || f == NULL || doc->init_value != XML_INIT_DONE)
return false;
#ifdef SXMLC_UNICODE