-
Notifications
You must be signed in to change notification settings - Fork 7
/
node.cpp
809 lines (645 loc) · 18.8 KB
/
node.cpp
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
#include "stdafx.h"
#include "node.h"
#define NODE_BUFFER_LEN 255
void fprintcx(FILE *file, const LPTSTR s, int count);
PNODE_ATT node_alloc_att(LPCTSTR key, LPCTSTR value, int flags);
PNODE_ATT node_alloc_att_multi(LPCTSTR key, LPCTSTR value, int flags);
int indent_depth = 0;
int xml_escape_content(LPCTSTR input, LPTSTR buffer, DWORD bufferSize);
int json_escape_content(LPCTSTR input, LPTSTR buffer, DWORD bufferSize);
PNODE node_alloc(LPCTSTR name, int flags)
{
PNODE node = NULL;
int size;
// Calculate required size
size = sizeof(NODE)
+ (sizeof(TCHAR) * (wcslen(name) + 1));
// Allocate
if (NULL == (node = (PNODE)calloc(1, size))) {
fprintf(stderr, "Failed to allocate memory for new node\n");
exit(ERROR_OUTOFMEMORY);
}
node->Children = (PNODE_LINK) calloc(1, sizeof(NODE_LINK));
node->Attributes = (PNODE_ATT_LINK) calloc(1, sizeof(NODE_ATT_LINK));
// Copy node name
node->Name = (LPTSTR)(node + 1);
wcscpy(node->Name, name);
// Set flags
node->Flags = flags;
return node;
}
void node_free(PNODE node, int deep)
{
PNODE_ATT_LINK att;
PNODE_LINK child;
// Free attributes
for(att = &node->Attributes[0]; NULL != att->LinkedAttribute; att++) {
free(att->LinkedAttribute);
}
// Free children
if(0 < deep) {
for(child = &node->Children[0]; NULL != child->LinkedNode; child++) {
node_free(child->LinkedNode, deep);
}
}
free(node->Attributes);
free(node->Children);
free(node);
}
int node_depth(PNODE node)
{
PNODE parent;
int count = 0;
for(parent = node; NULL != parent->Parent; parent = parent->Parent) {
count++;
}
return count;
}
int node_path(PNODE node, LPTSTR buffer, DWORD *bufferlen)
{
PNODE parent = NULL;
PNODE_LINK hierarchy = NULL;
PNODE_ATT_LINK att = NULL;
int i = 0;
int c = 0;
int len = 0;
int bufferused = 0;
int count = 1;
int ret = 0;
// Build an array of ordered nodes with [0]=root
count += node_depth(node);
hierarchy = (PNODE_LINK) calloc(count, sizeof(NODE_LINK));
parent = node;
for(i = 0; i < count; i++) {
hierarchy[count - i - 1].LinkedNode = parent;
parent = parent->Parent;
}
for(i = 0; i < count; i++) {
// Print the node name
len = wcslen(hierarchy[i].LinkedNode->Name);
for(c = 0; c < len; c++, bufferused++)
if(bufferused < *bufferlen)
buffer[bufferused] = hierarchy[i].LinkedNode->Name[c];
// Print node key
for(att = hierarchy[i].LinkedNode->Attributes; NULL != att->LinkedAttribute; att++) {
if(0 != (att->LinkedAttribute->Flags & NAFLG_KEY)) {
len = wcslen(NODE_DELIM_KEY_OPEN);
for(c = 0; c < len; c++, bufferused++)
if(bufferused < *bufferlen)
buffer[bufferused] = NODE_DELIM_KEY_OPEN[c];
len = wcslen(att->LinkedAttribute->Value);
for(c = 0; c < len; c++, bufferused++)
if(bufferused < *bufferlen)
buffer[bufferused] = att->LinkedAttribute->Value[c];
len = wcslen(NODE_DELIM_KEY_CLOSE);
for(c = 0; c < len; c++, bufferused++)
if(bufferused < *bufferlen)
buffer[bufferused] = NODE_DELIM_KEY_CLOSE[c];
break;
}
}
// Print path delimeter
if(i < count - 1) {
len = wcslen(NODE_DELIM_DS);
for(c = 0; c < len; c++, bufferused++) {
if(bufferused < *bufferlen) {
buffer[bufferused] = NODE_DELIM_DS[c];
}
}
}
}
free(hierarchy);
// Get index of last char
c = bufferused;
if(bufferused > *bufferlen)
c = *bufferlen - 1;
else
c = bufferused;
// Set to null char
if(c >= 0)
buffer[c] = '\0';
// Return zero if no more buffer required
ret = (bufferused > *bufferlen) ? bufferused : 0;
// Set buffer required
*bufferlen = bufferused + 1;
return ret;
}
int node_child_count(PNODE node)
{
int count = 0;
PNODE_LINK link = node->Children;
while(NULL != node->Children[count].LinkedNode) {
count++;
}
return count;
}
int node_append_child(PNODE parent, PNODE child)
{
int i, old_count, new_count;
PNODE_LINK new_links;
// Count old children
old_count = node_child_count(parent);
if (NULL == child)
return old_count;
new_count = old_count + 1;
// Allocate new link list
if (NULL == (new_links = (PNODE_LINK)calloc(new_count + 1, sizeof(NODE_LINK)))) {
fprintf(stderr, "Failed to allocate memory for appending node\n");
exit(ERROR_OUTOFMEMORY);
}
// Copy old child links
for(i = 0; i < old_count; i++)
new_links[i].LinkedNode = parent->Children[i].LinkedNode;
// Copy new children
new_links[new_count - 1].LinkedNode = child;
// Release old list
free(parent->Children);
parent->Children = new_links;
// Update parent pointer
child->Parent = parent;
return new_count;
}
PNODE node_append_new(PNODE parent, const LPCTSTR name, int flags)
{
PNODE node = node_alloc(name, flags);
node_append_child(parent, node);
return node;
}
PNODE_ATT node_alloc_att(const LPCTSTR key, const LPCTSTR value, int flags)
{
PNODE_ATT att = NULL;
LPTSTR nvalue = NULL;
int size;
if (NULL == key)
return att;
nvalue = (NULL == value) ? wcsdup(_T("")) : (LPTSTR) value;
size = sizeof(NODE_ATT)
+ (sizeof(TCHAR) * (wcslen(key) + 1))
+ (sizeof(TCHAR) * (wcslen(nvalue) + 1));
att = (PNODE_ATT) calloc(1, size);
att->Key = (LPTSTR)(att + 1);
wcscpy(att->Key, key);
att->Value = att->Key + wcslen(key) + 1;
wcscpy(att->Value, nvalue);
att->Flags = flags;
return att;
}
PNODE_ATT node_alloc_att_multi(LPCTSTR key, LPCTSTR value, int flags)
{
PNODE_ATT att = NULL;
int size;
int vallen = 0;
LPTSTR c;
// Calculate size of value
if (NULL == value) {
vallen = 0;
}
else {
for (c = (LPTSTR)&value[0]; '\0' != *c; c += wcslen(c) + 2)
{ }
vallen = c - value;
}
size = sizeof(NODE_ATT)
+ (sizeof(TCHAR) * (wcslen(key) + 1))
+ (sizeof(TCHAR) * ((vallen) + 1));
att = (PNODE_ATT) calloc(1, size);
att->Key = (LPTSTR)(att + 1);
wcscpy(att->Key, key);
att->Value = att->Key + wcslen(key) + 1;
if (NULL != value)
memcpy(att->Value, value, sizeof(TCHAR) * (vallen + 1));
att->Flags = flags | NAFLG_ARRAY;
return att;
}
int node_att_count(PNODE node)
{
int count = 0;
PNODE_ATT_LINK link = node->Attributes;
while(NULL != node->Attributes[count].LinkedAttribute) {
count++;
}
return count;
}
int node_att_indexof(PNODE node, const LPCTSTR key)
{
int i;
for(i = 0; NULL != node->Attributes[i].LinkedAttribute; i++) {
if(0 == wcscmp(node->Attributes[i].LinkedAttribute->Key, key)) {
return i;
}
}
return -1;
}
LPTSTR node_att_get(PNODE node, const LPCTSTR key)
{
int i = node_att_indexof(node, key);
return (i < 0) ? NULL : node->Attributes[i].LinkedAttribute->Value;
}
PNODE_ATT node_att_set(PNODE node, const LPCTSTR key, const LPCTSTR value, int flags)
{
int i, old_count, new_count, new_index;
PNODE_ATT att;
PNODE_ATT_LINK link = NULL;
PNODE_ATT_LINK new_link = NULL;
PNODE_ATT_LINK new_links = NULL;
// Count old attributes
old_count = node_att_count(node);
// Search for existing attribute
new_index = node_att_indexof(node, key);
if(new_index > -1)
new_link = &node->Attributes[new_index];
if(NULL != new_link) {
// Replace attribute link with new value if value differs
if(0 != wcscmp(new_link->LinkedAttribute->Value, value)) {
free(new_link->LinkedAttribute);
new_link->LinkedAttribute = node_alloc_att(key, value, flags);
}
else {
// Only update flags if value is identical
new_link->LinkedAttribute->Flags = flags;
}
att =new_link->LinkedAttribute;
}
else
{
// Reallocate link list and add new attribute
new_index = old_count;
new_count = old_count + 1;
// Allocate new link list
new_links = (PNODE_ATT_LINK) calloc(new_count + 1, sizeof(NODE_ATT_LINK));
// Copy old child links
for(i = 0; i < old_count; i++)
new_links[i].LinkedAttribute = node->Attributes[i].LinkedAttribute;
// Copy new attribute
new_links[new_index].LinkedAttribute = node_alloc_att(key, value, flags);
// Release old list
free(node->Attributes);
node->Attributes = new_links;
att = new_links[new_index].LinkedAttribute;
}
return att;
}
PNODE_ATT node_att_set_multi(PNODE node, LPCTSTR key, LPCTSTR value, int flags)
{
int i, old_count, new_count, new_index;
PNODE_ATT att;
PNODE_ATT_LINK link = NULL;
PNODE_ATT_LINK new_link = NULL;
PNODE_ATT_LINK new_links = NULL;
LPTSTR temp = NULL;
// Create new attribute
att = node_alloc_att_multi(key, value, flags);
// Count old attributes
old_count = node_att_count(node);
// Search for existing attribute
new_index = node_att_indexof(node, key);
if(new_index > -1)
new_link = &node->Attributes[new_index];
if(NULL != new_link) {
free(new_link->LinkedAttribute);
new_link->LinkedAttribute = att;
}
else
{
// Reallocate link list and add new attribute
new_index = old_count;
new_count = old_count + 1;
// Allocate new link list
new_links = (PNODE_ATT_LINK) calloc(new_count + 1, sizeof(NODE_ATT_LINK));
// Copy old child links
for(i = 0; i < old_count; i++)
new_links[i].LinkedAttribute = node->Attributes[i].LinkedAttribute;
// Copy new attribute
new_links[new_index].LinkedAttribute = att;
// Release old list
free(node->Attributes);
node->Attributes = new_links;
}
return att;
}
void fprintcx(FILE *file, const LPTSTR s, int count)
{
int i;
for(i = 0; i < count; i++)
fwprintf(file, s);
}
int node_to_list(PNODE node, FILE *file, int flags)
{
PNODE_ATT att = NULL;
DWORD count = 1;
DWORD i = 0;
DWORD children = node_child_count(node);
DWORD atts = node_att_count(node);
TCHAR *c = NULL;
// Print indent
fprintcx(file, _T("| "), indent_depth - 1);
fwprintf(file, _T("* %s \n"), node->Name);
// Print attributes
for (i = 0; i < atts; i++) {
att = node->Attributes[i].LinkedAttribute;
if (0 == wcslen(att->Value))
continue;
fprintcx(file, _T("| "), indent_depth - 1);
// Is attribute scalar or array?
if (0 == (node->Attributes[i].LinkedAttribute->Flags & NAFLG_ARRAY)) {
// Print scalar value
fwprintf(file, _T("|- %s = %s"), att->Key, att->Value);
}
else {
fwprintf(file, _T("|- %s = "), att->Key);
// Print remaining values as comman separated
for (c = att->Value; (*c) != '\0'; c += wcslen(c) + 1) {
if (c != att->Value)
fwprintf(file, _T(", "));
fwprintf(file, c);
}
}
fwprintf(file, _T("\n"));
}
// Print children
if (children) {
fprintcx(file, _T("| "), indent_depth - 1);
fwprintf(file, _T("|\\\n"));
}
indent_depth++;
for (i = 0; i < children; i++){
count += node_to_list(node->Children[i].LinkedNode, file, flags);
}
indent_depth--;
return count;
}
int node_to_walk(PNODE node, FILE *file, int flags)
{
PNODE_ATT_LINK att = NULL;
TCHAR buffer[1024];
DWORD bufferLen = sizeof(buffer);
DWORD i;
DWORD count = 1;
DWORD children = node_child_count(node);
if(NULL != node->Attributes->LinkedAttribute) {
// Get path of this node
node_path(node, buffer, &bufferLen);
// Print attributes
for(att = node->Attributes; NULL != att->LinkedAttribute; att++) {
fwprintf(file, L"%s%s%s%s%s\n", buffer, NODE_DELIM_ATT, att->LinkedAttribute->Key, NODE_DELIM_VAL, att->LinkedAttribute->Value);
}
}
// Print children
for(i = 0; i < children; i++) {
count += node_to_walk(node->Children[i].LinkedNode, file, flags);
}
return count;
}
int xml_escape_content(LPCTSTR input, LPTSTR buffer, DWORD bufferSize)
{
DWORD i = 0;
LPCTSTR cIn = input;
LPTSTR cOut = buffer;
DWORD newBufferSize = 0;
while ('\0' != (*cIn)) {
switch (*cIn) {
case '"':
memcpy(cOut, L""", sizeof(TCHAR) * 6);
cOut += 6;
break;
case '&':
memcpy(cOut, L"&", sizeof(TCHAR) * 5);
cOut += 5;
break;
case '<':
memcpy(cOut, L"<", sizeof(TCHAR) * 4);
cOut += 4;
break;
case '>':
memcpy(cOut, L">", sizeof(TCHAR) * 4);
cOut += 4;
break;
default:
memcpy(cOut, cIn, sizeof(TCHAR));
cOut += 1;
break;
}
cIn++;
}
*cOut = '\0';
return cOut - buffer;
}
int node_to_xml(PNODE node, FILE *file, int flags)
{
int i = 0, v = 0;
int nodes = 1;
int atts = node_att_count(node);
int children = node_child_count(node);
int hasChildren = 0;
int indent = (0 == (flags & NODE_XML_FLAG_NOWS)) ? indent_depth : 0;
LPTSTR nl = flags & NODE_XML_FLAG_NOWS ? L"" : NODE_XML_DELIM_NL;
LPTSTR tab = flags & NODE_XML_FLAG_NOWS ? L"" : NODE_XML_DELIM_INDENT;
LPTSTR key, val;
TCHAR strBuffer[NODE_BUFFER_LEN];
hasChildren = (children > 0) || ((flags & NODE_XML_FLAG_NOATTS) && atts > 0);
// Print xml declaration
if(0 == (flags & NODE_XML_FLAG_NODEC))
fwprintf(file, L"<?xml version=\"1.0\" encoding=\"Windows-1252\" standalone=\"yes\" ?>%s", nl);
// Indentation
fprintcx(file, tab, indent);
// Open element
fwprintf(file, L"<%s", node->Name);
// Print inline attributes
if(0 == (flags & NODE_XML_FLAG_NOATTS)) {
// Print as inline attributes
for (i = 0; i < atts; i++) {
xml_escape_content(node->Attributes[i].LinkedAttribute->Value, strBuffer, NODE_BUFFER_LEN);
fwprintf(file, L" %s=\"%s\"", node->Attributes[i].LinkedAttribute->Key, strBuffer);
}
}
if(0 != (node->Flags & NFLG_TABLE)) {
fwprintf(file, L" Count=\"%u\"", children);
}
// Close element header
if(0 == hasChildren)
fwprintf(file, L" />%s", nl);
else
fwprintf(file, L">%s", nl);
// Print attribute elements
if(0 != (flags & NODE_XML_FLAG_NOATTS)) {
// Print as child nodes
for(i = 0; i < atts; i++) {
// Expand multi-string array
v = 0;
key = node->Attributes[i].LinkedAttribute->Key;
val = node->Attributes[i].LinkedAttribute->Value;
// XML Escape value
xml_escape_content(val, strBuffer, NODE_BUFFER_LEN);
fprintcx(file, tab, indent + 1);
// Print non-array
if (0 == (NAFLG_ARRAY & node->Attributes[i].LinkedAttribute->Flags)) {
fwprintf(file, L"<%s>%s</%s>%s", key, strBuffer, key, nl);
}
else {
// Print array
fwprintf(file, L"<%s>%s", key, nl);
while ('\0' != *val) {
xml_escape_content(val, strBuffer, NODE_BUFFER_LEN);
fprintcx(file, tab, indent + 2);
fwprintf(file, L"<Item Id=\"%u\">%s</Item>%s", v, strBuffer, nl);
// Move cursor to next string
val += wcslen(val) + 1;
v++;
}
fprintcx(file, tab, indent + 1);
fwprintf(file, L"</%s>%s", key, nl);
}
}
}
// Print children
indent_depth++;
for(i = 0; i < children; i++) {
nodes += node_to_xml(node->Children[i].LinkedNode, file, flags | NODE_XML_FLAG_NODEC);
}
indent_depth--;
if(0 != hasChildren) {
// Indentation
fprintcx(file, tab, indent);
//Close element
fwprintf(file, L"</%s>%s", node->Name, nl);
}
return nodes;
}
int json_escape_content(LPCTSTR input, LPTSTR buffer, DWORD bufferSize)
{
DWORD i = 0;
LPCTSTR cIn = input;
LPTSTR cOut = buffer;
DWORD newBufferSize = 0;
while ('\0' != (*cIn)) {
switch (*cIn) {
case '"':
memcpy(cOut, L"\\\"", sizeof(TCHAR) * 2);
cOut += 2;
break;
case '\\':
memcpy(cOut, L"\\\\", sizeof(TCHAR) * 2);
cOut += 2;
break;
case '\r':
break;
case '\n':
memcpy(cOut, L"\\n", sizeof(TCHAR) * 2);
cOut += 2;
break;
default:
memcpy(cOut, cIn, sizeof(TCHAR));
cOut += 1;
break;
}
cIn++;
}
*cOut = '\0';
return cOut - buffer;
}
int node_to_json(PNODE node, FILE *file, int flags)
{
int i = 0;
int nodes = 1;
int atts = node_att_count(node);
int children = node_child_count(node);
int plural = 0;
int indent = (0 == (flags & NODE_JS_FLAG_NOWS)) ? indent_depth : 0;
LPTSTR nl = flags & NODE_JS_FLAG_NOWS ? L"" : NODE_JS_DELIM_NL;
LPTSTR space = flags & NODE_JS_FLAG_NOWS ? L"" : NODE_JS_DELIM_SPACE;
TCHAR strBuffer[NODE_BUFFER_LEN];
// Print header
fprintcx(file, NODE_JS_DELIM_INDENT, indent);
if (0 < indent_depth && 0 == (node->Flags & NFLG_TABLE_ROW))
fwprintf(file, L"\"%s\":%s", node->Name, space);
if (0 == (node->Flags & NFLG_TABLE))
fwprintf(file, L"{");
else
fwprintf(file, L"[");
// Print attributes
if(0 < atts && 0 == (node->Flags & NFLG_TABLE)) {
for(i = 0; i < atts; i++) {
if (*node->Attributes[i].LinkedAttribute->Value != '\0') {
if (plural)
fwprintf(file, L",");
// Print attribute name
fwprintf(file, L"%s", NODE_JS_DELIM_NL);
fprintcx(file, NODE_JS_DELIM_INDENT, indent + 1);
fwprintf(file, L"\"%s\":%s", node->Attributes[i].LinkedAttribute->Key, space);
// Print value
if (node->Attributes[i].LinkedAttribute->Flags & NAFLG_FMT_NUMERIC)
fwprintf(file, node->Attributes[i].LinkedAttribute->Value);
else {
json_escape_content(node->Attributes[i].LinkedAttribute->Value, strBuffer, NODE_BUFFER_LEN);
fwprintf(file, L"\"%s\"", strBuffer);
}
plural = 1;
}
}
}
// Print children
if(0 < children) {
indent_depth++;
for(i = 0; i < children; i++) {
if (plural)
fwprintf(file, L",");
fwprintf(file, NODE_JS_DELIM_NL);
nodes += node_to_json(node->Children[i].LinkedNode, file, flags);
plural = 1;
}
indent_depth--;
}
if (0 < atts || 0 < children) {
fwprintf(file, NODE_JS_DELIM_NL);
fprintcx(file, NODE_JS_DELIM_INDENT, indent);
}
if (0 == (node->Flags & NFLG_TABLE))
fwprintf(file, L"}");
else
fwprintf(file, L"]");
return nodes;
}
int node_to_yaml(PNODE node, FILE *file, int flags)
{
int i = 0;
int count = 1;
int atts = node_att_count(node);
int children = node_child_count(node);
PNODE_ATT att = NULL;
PNODE child = NULL;
wchar_t * attVal = NULL;
if (NULL == node->Parent)
fwprintf(file, _T("---%s"), NODE_YAML_DELIM_NL);
fprintcx(file, NODE_YAML_DELIM_INDENT, indent_depth);
if (NFLG_TABLE_ROW & node->Flags)
fwprintf(file, _T("- "));
fwprintf(file, _T("%s:"), node->Name);
// Print attributes
if (0 < atts) {
fwprintf(file, NODE_YAML_DELIM_NL);
for (i = 0; i < atts; i++) {
att = node->Attributes[i].LinkedAttribute;
attVal = (NULL != att->Value && '\0' != *att->Value) ? att->Value : L"~";
fprintcx(file, NODE_YAML_DELIM_INDENT, indent_depth + 1);
if (NAFLG_FMT_GUID & att->Flags)
fwprintf(file, _T("%s: '%s'%s"), att->Key, attVal, NODE_YAML_DELIM_NL);
else
fwprintf(file, _T("%s: %s%s"), att->Key, attVal, NODE_YAML_DELIM_NL);
}
}
// Print children
if (0 < children) {
if (0 == atts)
fwprintf(file, NODE_YAML_DELIM_NL);
indent_depth++;
for (i = 0; i < children; i++) {
child = node->Children[i].LinkedNode;
node_to_yaml(child, file, 0);
}
indent_depth--;
}
else if(0 == atts) {
fwprintf(file, _T(" ~%s"), NODE_YAML_DELIM_NL);
}
return count;
}