-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinypds_dom.h
849 lines (783 loc) · 25.7 KB
/
tinypds_dom.h
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
/*
* Tiny PDS3 parser
*
* This file provides both the interface and the implementation.
* To instantiate the implementation add the following in *ONE* source file before including this file
* #define TINY_PDS_DOM_IMPL
*
* Licensed under the MIT License
* (c) 2016-2020 Vincent Cruz
*/
#ifndef TINY_PDS_DOM_H
#define TINY_PDS_DOM_H
#include <tinypds.h>
#ifdef __cplusplus
extern "C" {
#endif
/** PDS item type **/
typedef enum {
/**
* PDS attribute.
* An attribute can contain a scalar, a set, a one dimensional or
* 2 dimensional sequence.
*/
PDS_ATTRIBUTE = 0,
/**
* PDS pointer.
* An attribute can contain a scalar, a set, a one dimensional or
* 2 dimensional sequence.
*/
PDS_POINTER,
/**
* PDS object.
* An object can contains attributes, pointers or objects.
*/
PDS_OBJECT,
/**
* PDS group.
* A group contains only attributes or pointers.
*/
PDS_GROUP
} PDS_type;
/** PDS scalar type **/
typedef enum {
/**
* No scalar is associated to current item. This is generally the
* case for objects and groups.
*/
PDS_NONE,
/** Single scalar. **/
PDS_SINGLE,
/** Scalar set. **/
PDS_SET,
/** One dimensional scalar sequence. **/
PDS_SEQUENCE_1D,
/** Two dimensional scalar sequence. **/
PDS_SEQUENCE_2D
} PDS_scalar_type;
/** PDS item **/
typedef struct {
/** Name **/
PDS_string name;
/** Item type. **/
PDS_type type;
} PDS_item;
#define PDS_DOM_typeof(pds) ((pds)->type)
#define PDS_DOM_is_attribute(pds) ((NULL != pds) && (PDS_ATTRIBUTE == (pds)->type))
#define PDS_DOM_is_pointer(pds) ((NULL != pds) && (PDS_POINTER == (pds)->type))
#define PDS_DOM_is_object(pds) ((NULL != pds) && (PDS_OBJECT == (pds)->type))
#define PDS_DOM_is_group(pds) ((NULL != pds) && (PDS_GROUP == (pds)->type))
#define PDS_DOM_has_scalar(pds) ((NULL != pds) && ((PDS_ATTRIBUTE == (pds)->type) || (PDS_POINTER == (pds)->type)))
/**
* Returns the type of the scalar attribute of current item.
* The item must be an attribute or a pointer.
* @param [in] pds PDS item.
* @return scalar type or PDS_NONE if the item is neither an attribute
* nor a pointer.
*/
PDS_scalar_type PDS_DOM_scalar_typeof(PDS_item *pds);
/**
* Returns the total number of scalars.
* @param [in] pds PDS item.
* @return total number of scalars or 0 if the item is neither an attribute
* nor a pointer.
*/
int PDS_DOM_scalar_count(PDS_item *pds);
/**
* Gets the scalar associated to current item.
* The item must be an attribute or a pointer containing a single scalar
* value.
* @param [in] pds PDS item.
* @param [out] scalar Scalar value.
* @return 1 if the item contains a single scalar, 0 otherwise.
*/
int PDS_DOM_scalar_get(PDS_item *pds, PDS_scalar *scalar);
/**
* Gets the scalar at specified position in the set associated to the
* current item, with bounds checking.
* The item must be an attribute or a pointer containing a set of scalar
* values.
* @param [in] pds PDS item.
* @param [out] scalar Scalar value.
* @param [in] i Position of the scalar in the set.
* @return 1 upon success, 0 otherwise.
*/
int PDS_DOM_set_get(PDS_item *pds, PDS_scalar *scalar, int i);
/**
* Gets the scalar at specified position in the 1 dimensional sequence
* associated to the current item, with bounds checking.
* The item must be an attribute or a pointer containing a 1 dimensional
* sequence of scalar values.
* @param [in] pds PDS item.
* @param [out] scalar Scalar value.
* @param [in] i Position of the scalar in the 1D sequence.
* @return 1 upon success, 0 otherwise.
*/
int PDS_DOM_sequence1d_get(PDS_item *pds, PDS_scalar *scalar, int i);
/**
* Gets the scalar at specified position in the 2 dimensional sequence
* associated to the current item, with bounds checking.
* The item must be an attribute or a pointer containing a 2 dimensional
* sequence of scalar values.
* @param [in] pds PDS item.
* @param [out] scalar Scalar value.
* @param [in] i Row index.
* @param [in] j Column index.
* @return 1 upon success, 0 otherwise.
*/
int PDS_DOM_sequence2d_get(PDS_item *pds, PDS_scalar *scalar, int i, int j);
/**
* Returns the number of rows.
* The item must be an attribute or a pointer, and the associated scalar
* must be a 2d sequence.
* @param [in] pds PDS item.
* @return number of rows for current 2d sequence or -1 if the item is not
* an attribute or a pointer and if the associated scalar is not
* a 2d sequence.
*/
int PDS_DOM_sequence2d_rows(PDS_item *pds);
/**
* Returns the number of columns for the specified row.
* The item must be an attribute or a pointer, and the associated scalar
* must be a 2d sequence.
* @param [in] pds PDS item.
* @param [in] i Row index.
* @return number of columns for the ith row of current 2d sequence or -1 if the item
* is not an attribute or a pointer and if the associated scalar
* is not a 2d sequence.
*/
int PDS_DOM_sequence2d_cols(PDS_item *pds, int i);
/**
* Get the first sibbling of current PDS item.
* @param [in] pds PDS item.
* @return pointer to sibling entity or NULL if the current PDS item
* does not have any sibling.
*/
PDS_item* PDS_DOM_sibling(PDS_item* pds);
/**
* Get the parent of current PDS item.
* @param [in] pds PDS item.
* @return pointer to parent PDS item or NULL if the current PDS item
* does not have any parent.
*/
PDS_item* PDS_DOM_parent(PDS_item* pds);
/**
* Returns a pointer to the first item of current object.
* @param [in] pds PDS item
* @return pointer to the first item of the current object.
*/
PDS_item* PDS_DOM_object_begin(PDS_item* pds);
/**
* Returns a pointer to one past the last item of current object.
* @param [in] pds PDS item
* @return pointer to one past the last item of the current object.
*/
PDS_item* PDS_DOM_object_end(PDS_item* pds);
#define PDS_DOM_object_foreach(obj, element) \
for(element=PDS_DOM_object_begin(obj); NULL!=element; element=PDS_DOM_sibling(element))
/**
* Returns a pointer to the first item of current group.
* @param [in] pds PDS item
* @return pointer to the first item of the current group.
*/
PDS_item* PDS_DOM_group_begin(PDS_item* pds);
/**
* Returns a pointer to one past the last item of current group.
* @param [in] pds PDS item
* @return pointer to one past the last item of the current group.
*/
PDS_item* PDS_DOM_group_end(PDS_item* pds);
#define PDS_DOM_group_foreach(grp, element) \
for(element=PDS_DOM_group_begin(grp); NULL!=element; element=PDS_DOM_sibling(element))
/**
* Parse string buffer.
* @param [in] buffer Input string buffer.
* @param [in] len Length of the input string buffer.
* @param [out] pds Pointer to the first element of the PDS tree.
* @param [out] error Description of the last encountered error.
* @return 1 if the PDS file was succesfully parser, 0 if an error occured.
*/
int PDS_DOM_parse(const char *buffer, size_t len, PDS_item **pds, PDS_error_description *error);
/**
* Release object resources.
* Recursively release any resources used by the current item and its
* siblings and children.
* @param [in] pds PDS item.
*/
void PDS_DOM_delete(PDS_item *pds);
/**
* Defines the way the PDS tree is traversed.
*/
typedef enum {
/** Only the siblings of the current item are checked. **/
PDS_ONLY_SIBLINGS = 0,
/** Only the children of the current item are checked.
* The traversal is not recursive. **/
PDS_ONLY_CHILDREN,
/** Recursively checks all children **/
PDS_CHILDREN_RECURSIVE,
/** Recursively checks current item and its siblings. **/
PDS_SIBLINGS_RECURSIVE
} PDS_search_type;
/**
* Finds the first item which name matches.
* @param [in] name Name we are looking for.
* @param [in] current Current item.
* @param [in] search Search type.
* @param [in] check_current Check current item if not 0.
* @return A pointer to the first matching item or NULL if no match was found.
*/
PDS_item* PDS_DOM_find(const char *name, PDS_item *current, PDS_search_type search, int check_current);
#ifdef __cplusplus
}
#endif
#endif /* TINY_PDS_DOM_H */
/*****************************************************************************/
#ifdef TINY_PDS_DOM_IMPL
#if defined(PDS_DOM_MALLOC) && defined(PDS_DOM_REALLOC) && defined(PDS_DOM_FREE)
// ok
#elif !defined(PDS_DOM_MALLOC) && !defined(PDS_DOM_REALLOC) && !defined(PDS_DOM_FREE)
// ok
#else
#error "Must define all or none of PDS_DOM_MALLOC, PDS_DOM_REALLOC and PDS_DOM_FREE"
#endif
#if !defined(PDS_DOM_MALLOC)
#include <stdlib.h>
#define PDS_DOM_MALLOC(sz) malloc(sz)
#define PDS_DOM_REALLOC(p, new_sz) realloc(p, new_sz)
#define PDS_DOM_FREE(p) free(p)
#endif
#if defined(PDS_DOM_MEMCPY) && defined(PDS_DOM_MEMSET)
// ok
#elif !defined(PDS_DOM_MEMCPY) && !defined(PDS_DOM_MEMSET)
// ok
#else
#error "Must define all or none of PDS_DOM_MEMCPY and PDS_DOM_MEMSET"
#endif
#if !defined(PDS_DOM_MEMCPY)
#include <string.h>
#define PDS_DOM_MEMCPY(dst, src, sz) memcpy(dst, src, sz)
#define PDS_DOM_MEMSET(dst, c, sz) memset(dst,c, sz)
#endif
typedef struct PDS_item_impl_t {
PDS_item info;
PDS_scalar_type scalar_type;
PDS_scalar *scalar;
int *last;
int count;
struct PDS_item_impl_t *parent;
struct PDS_item_impl_t *child;
struct PDS_item_impl_t *sibling;
struct PDS_item_impl_t *next;
} PDS_item_impl;
typedef struct {
PDS_item_impl *root;
PDS_item_impl *parent;
PDS_item_impl *current;
PDS_item_impl *previous;
int dimension;
PDS_error_description error;
} PDS_DOM_payload;
/* Create new PDS item */
static PDS_item_impl* PDS_DOM_create(PDS_type type, const char *first, const char *last) {
PDS_item_impl *item;
item = PDS_DOM_MALLOC(sizeof(PDS_item_impl));
if(NULL == item) {
return NULL;
}
item->info.name.type = PDS_TEXT_STRING_VALUE;
item->info.name.first = first;
item->info.name.last = last;
item->info.type = type;
item->scalar_type = PDS_NONE;
item->scalar = NULL;
item->last = NULL;
item->count = 0;
item->child = NULL;
item->sibling = NULL;
item->next = NULL;
item->parent = NULL;
return item;
}
/* Create a new PDS_item and add it to the hierarchy. */
static int PDS_DOM_add(PDS_DOM_payload *payload, PDS_type type, const char *first, const char *last)
{
PDS_item_impl *item = PDS_DOM_create(type, first, last);
if(NULL == item)
{
return PDS_INVALID_VALUE;
}
item->parent = payload->parent;
if( (PDS_GROUP == type) || (PDS_OBJECT == type) ) {
payload->parent = item;
}
if(payload->current) {
payload->current->sibling = item;
}
payload->current = item;
if(NULL == payload->root) {
payload->root = item;
}
if(payload->previous) {
payload->previous->next = item;
}
payload->previous = item;
return PDS_OK;
}
/* Scalar callback */
static int PDS_DOM_parse_scalar(const PDS_scalar *scalar, void *user_data) {
int index;
int last;
PDS_scalar *tmp;
PDS_DOM_payload *payload = (PDS_DOM_payload*)user_data;
/* Sanity check */
if(NULL == payload) {
return 0;
}
if(NULL == payload->current) {
return 0;
}
if((PDS_ATTRIBUTE != payload->current->info.type) && (PDS_POINTER != payload->current->info.type)) {
return 0;
}
/* Copy scalar */
index = payload->current->count-1;
last = payload->current->last[index];
tmp = PDS_DOM_REALLOC(payload->current->scalar, (last+1)*sizeof(PDS_scalar));
if(NULL == tmp) {
return 0;
}
payload->current->scalar = tmp;
payload->current->last[index]++;
PDS_DOM_MEMCPY(&payload->current->scalar[last], scalar, sizeof(PDS_scalar));
return 1;
}
/* Set start callback. */
static int PDS_DOM_parse_set_begin(void *user_data) {
PDS_DOM_payload *payload = (PDS_DOM_payload*)user_data;
/* Sanity check */
if(NULL == payload) {
return 0;
}
if(NULL == payload->current) {
return 0;
}
if((PDS_ATTRIBUTE != payload->current->info.type) && (PDS_POINTER != payload->current->info.type)) {
return 0;
}
if(payload->dimension) {
return 0;
}
payload->current->scalar_type = PDS_SET;
payload->dimension = 1;
return 1;
}
/* Set end callback. */
static int PDS_DOM_parse_set_end(void *user_data) {
PDS_DOM_payload *payload = (PDS_DOM_payload*)user_data;
/* Sanity check */
if(NULL == payload) {
return 0;
}
if(NULL == payload->current) {
return 0;
}
payload->dimension = 0;
return 1;
}
/* Sequence start callback. */
static int PDS_DOM_parse_sequence_begin(void *user_data) {
PDS_DOM_payload *payload = (PDS_DOM_payload*)user_data;
/* Sanity check */
if(NULL == payload) {
return 0;
}
if(NULL == payload->current) {
return 0;
}
if((PDS_ATTRIBUTE != payload->current->info.type) && (PDS_POINTER != payload->current->info.type)) {
return 0;
}
/* Check that the sequence is either 1D or 2D */
if(payload->dimension >= 2) {
return 0;
}
if(payload->dimension == 1) {
int *tmp;
int index = payload->current->count;
tmp = PDS_DOM_REALLOC(payload->current->last, (index+1)*sizeof(int));
if(NULL == tmp) {
return 0;
}
payload->current->last[index] = index ? payload->current->last[index-1] : 0;
payload->current->count++;
payload->current->scalar_type = PDS_SEQUENCE_2D;
}
else if(PDS_SINGLE == payload->current->scalar_type) {
payload->current->scalar_type = PDS_SEQUENCE_1D;
}
payload->dimension++;
return 1;
}
/* Sequence end callback. */
static int PDS_DOM_parse_sequence_end(void *user_data) {
PDS_DOM_payload *payload = (PDS_DOM_payload*)user_data;
/* Sanity check */
if(NULL == payload) {
return 0;
}
if(NULL == payload->current) {
return 0;
}
payload->dimension--;
return 1;
}
/* Attribute callback */
static int PDS_DOM_parse_attribute_begin(const char *first, const char *last, void *user_data) {
int ret;
PDS_DOM_payload *payload = (PDS_DOM_payload*)user_data;
if(NULL == payload) {
return 0;
}
ret = PDS_DOM_add(payload, PDS_ATTRIBUTE, first, last);
if(PDS_OK != ret) {
return 0;
}
payload->current->count = 1;
payload->current->last = PDS_DOM_MALLOC(sizeof(int));
if(NULL == payload->current->last) {
return 0;
}
payload->current->last[0] = 0;
payload->current->scalar_type = PDS_SINGLE;
return 1;
}
static int PDS_DOM_parse_attribute_end(const char *first, const char *last, void *user_data) {
(void)first;
(void)last;
(void)user_data;
return 1;
}
/* Pointer callback */
static int PDS_DOM_parse_pointer_begin(const char *first, const char *last, void *user_data) {
int ret;
PDS_DOM_payload *payload = (PDS_DOM_payload*)user_data;
if(NULL == payload) {
return 0;
}
ret = PDS_DOM_add(payload, PDS_POINTER, first, last);
if(PDS_OK != ret) {
return 0;
}
payload->current->count = 1;
payload->current->last = PDS_DOM_MALLOC(sizeof(int));
if(NULL == payload->current->last) {
return 0;
}
payload->current->last[0] = 0;
payload->current->scalar_type = PDS_SINGLE;
return 1;
}
static int PDS_DOM_parse_pointer_end(const char *first, const char *last, void *user_data) {
(void)user_data;
(void)first;
(void)last;
return 1;
}
/* Group start callback. */
static int PDS_DOM_parse_group_begin(const char *first, const char *last, void *user_data) {
int ret;
PDS_DOM_payload *payload = (PDS_DOM_payload*)user_data;
if(NULL == payload) {
return 0;
}
ret = PDS_DOM_add(payload, PDS_GROUP, first, last);
return (PDS_OK == ret);
}
/* Group end callback. */
static int PDS_DOM_parse_group_end(const char *first, const char *last, void *user_data) {
PDS_DOM_payload *payload = (PDS_DOM_payload*)user_data;
if(NULL == payload) {
return 0;
}
payload->current->sibling = NULL;
payload->current = payload->parent;
if(payload->parent) {
payload->parent->child = payload->parent->sibling;
payload->parent->sibling = NULL;
payload->parent = payload->parent->parent;
}
(void)first;
(void)last;
return 1;
}
/* Object start callback. */
static int PDS_DOM_parse_object_begin(const char *first, const char *last, void *user_data)
{
int ret;
PDS_DOM_payload *payload = (PDS_DOM_payload*)user_data;
if(NULL == payload)
{
return 0;
}
ret = PDS_DOM_add(payload, PDS_OBJECT, first, last);
return (PDS_OK == ret);
}
/* Object end callback. */
static int PDS_DOM_parse_object_end(const char *first, const char *last, void *user_data) {
PDS_DOM_payload *payload = (PDS_DOM_payload*)user_data;
if(NULL == payload) {
return 0;
}
payload->current->sibling = NULL;
payload->current = payload->parent;
if(payload->parent) {
payload->parent->child = payload->parent->sibling;
payload->parent->sibling = NULL;
payload->parent = payload->parent->parent;
}
(void)first;
(void)last;
return 1;
}
/* Error callback. */
static void PDS_DOM_error(const PDS_error_description* desc, void *user_data) {
PDS_DOM_payload *payload = (PDS_DOM_payload*)user_data;
if(NULL == payload) {
return;
}
payload->error = *desc;
}
/* Parse string buffer. */
int PDS_DOM_parse(const char *buffer, size_t len, PDS_item **item, PDS_error_description *error)
{
PDS_callbacks callbacks;
PDS_DOM_payload payload;
int ret;
/* Sanity check */
if( NULL == error ) {
return 0;
}
if( (NULL == buffer) || (0 == len) || (NULL == item) ) {
error->line_first = error->line_last = 0;
error->msg = "invalid parameters";
return 0;
}
/* Set tinypds callbacks */
PDS_set_attribute_callbacks(&callbacks, PDS_DOM_parse_attribute_begin,
PDS_DOM_parse_attribute_end);
PDS_set_pointer_callbacks(&callbacks, PDS_DOM_parse_pointer_begin,
PDS_DOM_parse_pointer_end);
PDS_set_set_callbacks(&callbacks, PDS_DOM_parse_set_begin,
PDS_DOM_parse_set_end);
PDS_set_sequence_callbacks(&callbacks, PDS_DOM_parse_sequence_begin,
PDS_DOM_parse_sequence_end);
PDS_set_group_callbacks(&callbacks, PDS_DOM_parse_group_begin,
PDS_DOM_parse_group_end);
PDS_set_object_callbacks(&callbacks, PDS_DOM_parse_object_begin,
PDS_DOM_parse_object_end);
PDS_set_scalar_callback(&callbacks, PDS_DOM_parse_scalar);
PDS_set_error_callback(&callbacks, PDS_DOM_error);
payload.root = NULL;
payload.parent = PDS_DOM_create(PDS_OBJECT, NULL, NULL);
payload.current = NULL;
payload.previous = payload.parent;
payload.dimension = 0;
*item = NULL;
payload.error.line_first = 0;
payload.error.line_last = 0;
payload.error.number = 0;
payload.error.position = 0;
payload.error.status = PDS_OK;
payload.error.msg = "ok";
/* Parse buffer */
ret = PDS_parse(&callbacks, buffer, len, &payload);
*error = payload.error;
if(!ret) {
PDS_DOM_delete((PDS_item*)payload.root);
return ret;
}
*item = (PDS_item*)payload.root;
return ret;
}
/* Release object resources. */
void PDS_DOM_delete(PDS_item *pds) {
PDS_item_impl *it;
PDS_item_impl *end;
if(NULL == pds) {
return;
}
it = (PDS_item_impl*)pds;
if(it->parent) {
end = it->parent->sibling;
it->parent->child = NULL;
it->parent->next = end;
}
else {
end = NULL;
}
while(it != end) {
PDS_item_impl *obj = it;
it = it->next;
if(obj->scalar) {
PDS_DOM_FREE(obj->scalar);
}
if(obj->last) {
PDS_DOM_FREE(obj->last);
}
PDS_DOM_FREE(obj);
}
}
/* Get the type of the scalar attribute of current item. */
PDS_scalar_type PDS_DOM_scalar_typeof(PDS_item *pds)
{
return PDS_DOM_has_scalar(pds) ? ((PDS_item_impl*)pds)->scalar_type : PDS_NONE;
}
/* Returns the total number of scalars. */
int PDS_DOM_scalar_count(PDS_item *pds)
{
if(PDS_DOM_has_scalar(pds))
{
PDS_item_impl *item = (PDS_item_impl*)pds;
return (item->count) ? item->last[item->count-1] : 0;
}
return 0;
}
/* Gets the scalar associated to current item at the given row and column. */
static int PDS_DOM_scalar_get_internal(PDS_item *pds, PDS_scalar *scalar, PDS_scalar_type type, int i, int j)
{
PDS_item_impl *item = (PDS_item_impl*)pds;
int count = PDS_DOM_scalar_count(pds);
int offset;
scalar->type = PDS_UNKNOWN_VALUE;
if( (type != PDS_DOM_scalar_typeof(pds)) || (0 == count) )
{
return 0;
}
if(i >= item->count)
{
return 0;
}
offset = (i ? item->last[i-1] : 0) + j;
if(offset > item->last[i])
{
return 0;
}
PDS_DOM_MEMCPY(scalar, item->scalar + offset, sizeof(PDS_scalar));
return 1;
}
/* Gets the scalar associated to current item. */
int PDS_DOM_scalar_get(PDS_item *pds, PDS_scalar *scalar) {
return PDS_DOM_scalar_get_internal(pds, scalar, PDS_SINGLE, 0, 0);
}
/* Gets the scalar at specified position in the set associated to the
* current item, with bounds checking. */
int PDS_DOM_set_get(PDS_item *pds, PDS_scalar *scalar, int i) {
return PDS_DOM_scalar_get_internal(pds, scalar, PDS_SET, 0, i);
}
/* Gets the scalar at specified position in the 1 dimensional sequence
* associated to the current item, with bounds checking. */
int PDS_DOM_sequence1d_get(PDS_item *pds, PDS_scalar *scalar, int i) {
return PDS_DOM_scalar_get_internal(pds, scalar, PDS_SEQUENCE_1D, 0, i);
}
/* Gets the scalar at specified position in the 2 dimensional sequence
* associated to the current item, with bounds checking. */
int PDS_DOM_sequence2d_get(PDS_item *pds, PDS_scalar *scalar, int i, int j) {
return PDS_DOM_scalar_get_internal(pds, scalar, PDS_SEQUENCE_2D, i, j);
}
/* Returns the number of rows. */
int PDS_DOM_sequence2d_rows(PDS_item *pds) {
if(PDS_SEQUENCE_2D != PDS_DOM_scalar_typeof(pds)) {
return -1;
}
return ((PDS_item_impl*)pds)->count;
}
/* Returns the number of columns for the specified row. */
int PDS_DOM_sequence2d_cols(PDS_item *pds, int i) {
PDS_item_impl *item = (PDS_item_impl*)pds;
if(PDS_SEQUENCE_2D != PDS_DOM_scalar_typeof(pds)) {
return -1;
}
if((i >= item->count) || (0 == item->count)) {
return 0;
}
return item->last[i] - ((i > 0) ? item->last[i-1] : 0);
}
/* Get the sibbling of current PDS object. */
PDS_item* PDS_DOM_sibling(PDS_item* pds) {
PDS_item_impl *current = (PDS_item_impl*)pds;
return (NULL != current) ? (PDS_item*)(current->sibling) : NULL;
}
/* Get the parent of current PDS object. */
PDS_item* PDS_DOM_parent(PDS_item* pds) {
PDS_item_impl *current = (PDS_item_impl*)pds;
return (NULL != current) ? (PDS_item*)(current->parent) : NULL;
}
/* Returns a pointer to the first item of current object. */
PDS_item* PDS_DOM_object_begin(PDS_item* pds) {
return (PDS_item*)(PDS_DOM_is_object(pds) ? ((PDS_item_impl*)pds)->child : NULL);
}
/* Returns a pointer to one past the last item of current object. */
PDS_item* PDS_DOM_object_end(PDS_item* pds) {
if(PDS_DOM_is_object(pds))
{
PDS_item_impl* item = (PDS_item_impl*)pds;
if(NULL != item->child)
{
return (PDS_item*)item->sibling;
}
}
return NULL;
}
/* Returns a pointer to the first item of current group. */
PDS_item* PDS_DOM_group_begin(PDS_item* pds) {
return (PDS_item*)(PDS_DOM_is_group(pds) ? ((PDS_item_impl*)pds)->child : NULL);
}
/* Returns a pointer to one past the last item of current group. */
PDS_item* PDS_DOM_group_end(PDS_item* pds) {
if(PDS_DOM_is_group(pds)) {
PDS_item_impl* item = (PDS_item_impl*)pds;
if(NULL != item->child) {
return (PDS_item*)item->sibling;
}
}
return NULL;
}
/* Finds the first item which name matches. */
PDS_item* PDS_DOM_find(const char *name, PDS_item *current, PDS_search_type search, int check_current) {
const char *first;
const char *last;
PDS_item_impl *begin = (PDS_item_impl*)current;
PDS_item_impl *it;
PDS_item_impl *end;
/* sanity check */
if((NULL == name) || (NULL == current)) {
return NULL;
}
first = name;
last = name + strlen(name) - 1;
if(check_current && PDS_string_compare(current->name.first, current->name.last, first, last)) {
return current;
}
switch(search) {
case PDS_ONLY_SIBLINGS:
for(it=begin->sibling, end=NULL; (it!=end) && !PDS_string_compare(it->info.name.first, it->info.name.last, first, last); it=it->sibling)
{}
break;
case PDS_ONLY_CHILDREN:
for(it=begin->child, end=NULL; (it!=end) && !PDS_string_compare(it->info.name.first, it->info.name.last, first, last); it=it->sibling)
{}
break;
case PDS_CHILDREN_RECURSIVE:
for(it=begin->child, end=begin->sibling; (it!=end) && !PDS_string_compare(it->info.name.first, it->info.name.last, first, last); it=it->next)
{}
break;
case PDS_SIBLINGS_RECURSIVE:
end = (begin->parent) ? (begin->parent->sibling) : NULL;
for(it=begin->next; (it!=end) && !PDS_string_compare(it->info.name.first, it->info.name.last, first, last); it=it->next)
{}
break;
}
return (PDS_item*)((it != end) ? it : NULL);
}
#endif /* TINY_PDS_DOM_IMPL */