forked from Unidata/netcdf-c
/
d4parser.c
1515 lines (1404 loc) · 43.4 KB
/
d4parser.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 2016, UCAR/Unidata
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*********************************************************************/
#include "d4includes.h"
#include <stdarg.h>
#include <assert.h>
#include "ezxml.h"
/**
* Implement the Dap4 Parser Using a DOM Parser
*
* This code creates in internal representation of the netcdf-4 metadata
* to avoid having to make so many calls into the netcdf library.
*/
/***************************************************/
/*
Map an xml node name to an interpretation.
We use NCD4_NULL when we directly interpret
the tag and do not need to search for it.
E.g Dataset, Dim, econst, etc.
If the sort is NCD4_NULL, then that means it is
irrelevant because that keyword will never be
searched for in this table.
*/
struct KEYWORDINFO {
char* tag; /* The xml tag e.g. <tag...> */
NCD4sort sort; /* What kind of node are we building */
nc_type subsort; /* discriminator */
char* aliasfor; /* Some names are aliases for others */
} keywordmap[] = {
{"Attribute", NCD4_ATTR,NC_NAT,NULL},
{"Byte", NCD4_VAR,NC_BYTE,"Int8"},
{"Char", NCD4_VAR,NC_CHAR,NULL},
{"Dataset", NCD4_NULL,NC_NAT,NULL},
{"Dim", NCD4_NULL,NC_NAT,NULL},
{"Dimension", NCD4_DIM,NC_NAT,NULL},
{"Enum", NCD4_VAR,NC_ENUM,NULL},
{"Enumconst", NCD4_NULL,NC_NAT,NULL},
{"Enumeration", NCD4_TYPE,NC_ENUM,NULL},
{"Float32", NCD4_VAR,NC_FLOAT,NULL},
{"Float64", NCD4_VAR,NC_DOUBLE,NULL},
{"Group", NCD4_GROUP,NC_NAT,NULL},
{"Int16", NCD4_VAR,NC_SHORT,NULL},
{"Int32", NCD4_VAR,NC_INT,NULL},
{"Int64", NCD4_VAR,NC_INT64,NULL},
{"Int8", NCD4_VAR,NC_BYTE,NULL},
{"Map", NCD4_NULL,NC_NAT,NULL},
{"Opaque", NCD4_VAR,NC_OPAQUE,NULL},
{"OtherXML", NCD4_XML,NC_NAT,NULL},
{"Sequence", NCD4_VAR,NC_SEQ,NULL},
{"String", NCD4_VAR,NC_STRING,NULL},
{"Structure", NCD4_VAR,NC_STRUCT,NULL},
{"UByte", NCD4_VAR,NC_UBYTE,"UInt8"},
{"UInt16", NCD4_VAR,NC_USHORT,NULL},
{"UInt32", NCD4_VAR,NC_UINT,NULL},
{"UInt64", NCD4_VAR,NC_UINT64,NULL},
{"UInt8", NCD4_VAR,NC_UBYTE,NULL},
{"URL", NCD4_VAR,NC_STRING,"String"},
};
typedef struct KEYWORDINFO KEYWORDINFO;
static struct ATOMICTYPEINFO {
char* name; nc_type type; size_t size;
} atomictypeinfo[] = {
/* Keep in sorted order for binary search */
{"Byte",NC_BYTE,sizeof(char)},
{"Char",NC_CHAR,sizeof(char)},
{"Float32",NC_FLOAT,sizeof(float)},
{"Float64",NC_DOUBLE,sizeof(double)},
{"Int16",NC_SHORT,sizeof(short)},
{"Int32",NC_INT,sizeof(int)},
{"Int64",NC_INT64,sizeof(long long)},
{"Int8",NC_BYTE,sizeof(char)},
{"String",NC_STRING,sizeof(char*)},
{"UByte",NC_UBYTE,sizeof(unsigned char)},
{"UInt16",NC_USHORT,sizeof(unsigned short)},
{"UInt32",NC_UINT,sizeof(unsigned int)},
{"UInt64",NC_UINT64,sizeof(unsigned long long)},
{"UInt8",NC_UBYTE,sizeof(unsigned char)},
{NULL,NC_NAT,0}
};
/***************************************************/
#ifdef D4DEBUG
static void setname(NCD4node* node, const char* name)
{
nullfree(node->name);
(node)->name = strdup(name); \
fprintf(stderr,"setname: node=%lx name=%s\n",(unsigned long)(node),(node)->name); \
}
#define SETNAME(node,src) setname((node),src)
#else
#define SETNAME(node,src) do {nullfree((node)->name); (node)->name = strdup(src);} while(0);
#endif
/***************************************************/
extern const char** ezxml_all_attr(ezxml_t xml, int* countp);
/* Forwards */
static int addOrigType(NCD4parser*, NCD4node* src, NCD4node* dst, const char* tag);
static int defineAtomicTypes(NCD4parser*);
static int defineBytestringType(NCD4parser*);
static void classify(NCD4node* container, NCD4node* node);
static int convertString(union ATOMICS*, NCD4node* type, const char* s);
static int downConvert(union ATOMICS*, NCD4node* type);
static int fillgroup(NCD4parser*, NCD4node* group, ezxml_t xml);
static NCD4node* getOpaque(NCD4parser*, ezxml_t varxml, NCD4node* group);
static int getValueStrings(NCD4parser*, NCD4node*, ezxml_t xattr, NClist*);
static int isReserved(const char* name);
static KEYWORDINFO* keyword(const char* name);
static NCD4node* lookupAtomictype(NCD4parser*, const char* name);
static NCD4node* lookFor(NClist* elems, const char* name, NCD4sort sort);
static NCD4node* lookupFQN(NCD4parser*, const char* sfqn, NCD4sort);
static int lookupFQNList(NCD4parser*, NClist* fqn, NCD4sort sort, NCD4node** result);
static NCD4node* makeAnonDim(NCD4parser*, const char* sizestr);
static int makeNode(NCD4parser*, NCD4node* parent, ezxml_t, NCD4sort, nc_type, NCD4node**);
static int parseAtomicVar(NCD4parser*, NCD4node* container, ezxml_t xml, NCD4node**);
static int parseAttributes(NCD4parser*, NCD4node* container, ezxml_t xml);
static int parseDimensions(NCD4parser*, NCD4node* group, ezxml_t xml);
static int parseDimRefs(NCD4parser*, NCD4node* var, ezxml_t xml);
static int parseEconsts(NCD4parser*, NCD4node* en, ezxml_t xml);
static int parseEnumerations(NCD4parser*, NCD4node* group, ezxml_t dom);
static int parseFields(NCD4parser*, NCD4node* container, ezxml_t xml);
static int parseError(NCD4parser*, ezxml_t errxml);
static int parseGroups(NCD4parser*, NCD4node* group, ezxml_t dom);
static int parseMaps(NCD4parser*, NCD4node* var, ezxml_t xml);
static int parseMetaData(NCD4parser*, NCD4node* node, ezxml_t xml);
static int parseStructure(NCD4parser*, NCD4node* container, ezxml_t dom, NCD4node**);
static int parseSequence(NCD4parser*, NCD4node* container, ezxml_t dom,NCD4node**);
static int parseLL(const char* text, long long*);
static int parseULL(const char* text, unsigned long long*);
static int parseVariables(NCD4parser*, NCD4node* group, ezxml_t xml);
static int parseVariable(NCD4parser*, NCD4node* group, ezxml_t xml, NCD4node**);
static void reclaimParser(NCD4parser* parser);
static void record(NCD4parser*, NCD4node* node);
static int splitOrigType(NCD4parser*, const char* fqn, NCD4node* var);
static void track(NCD4parser*, NCD4node* node);
static int traverse(NCD4parser*, ezxml_t dom);
/***************************************************/
/* API */
int
NCD4_parse(NCD4meta* metadata)
{
int ret = NC_NOERR;
NCD4parser* parser = NULL;
int ilen;
ezxml_t dom = NULL;
/* Create and fill in the parser state */
parser = (NCD4parser*)calloc(1,sizeof(NCD4parser));
if(parser == NULL) {ret=NC_ENOMEM; goto done;}
parser->metadata = metadata;
ilen = strlen(parser->metadata->serial.dmr);
dom = ezxml_parse_str(parser->metadata->serial.dmr,ilen);
if(dom == NULL) {ret=NC_ENOMEM; goto done;}
parser->types = nclistnew();
parser->dims = nclistnew();
parser->vars = nclistnew();
#ifdef D4DEBUG
parser->debuglevel = 1;
#endif
/*Walk the DOM tree */
ret = traverse(parser,dom);
done:
if(dom != NULL)
ezxml_free(dom);
reclaimParser(parser);
return THROW(ret);
}
static void
reclaimParser(NCD4parser* parser)
{
if(parser == NULL) return;
nclistfree(parser->atomictypes);
nclistfree(parser->types);
nclistfree(parser->dims);
nclistfree(parser->vars);
free (parser);
}
/**************************************************/
/* Recursively walk the DOM tree to create the metadata */
static int
traverse(NCD4parser* parser, ezxml_t dom)
{
int ret = NC_NOERR;
/* See if we have an <Error> or <Dataset> */
if(strcmp(dom->name,"Error")==0) {
ret=parseError(parser,dom);
ret=NC_EDMR;
goto done;
} else if(strcmp(dom->name,"Dataset")==0) {
const char* xattr = NULL;
if((ret=makeNode(parser,NULL,NULL,NCD4_GROUP,NC_NULL,&parser->metadata->root))) goto done;
parser->metadata->root->group.isdataset = 1;
parser->metadata->root->meta.id = parser->metadata->ncid;
parser->metadata->groupbyid = nclistnew();
SETNAME(parser->metadata->root,"/");
xattr = ezxml_attr(dom,"name");
if(xattr != NULL) parser->metadata->root->group.datasetname = strdup(xattr);
xattr = ezxml_attr(dom,"dapVersion");
if(xattr != NULL) parser->metadata->root->group.dapversion = strdup(xattr);
xattr = ezxml_attr(dom,"dmrVersion");
if(xattr != NULL) parser->metadata->root->group.dmrversion = strdup(xattr);
/* fill in the atomic types */
if((ret=defineAtomicTypes(parser))) goto done;
/* Recursively walk the tree */
if((ret = fillgroup(parser,parser->metadata->root,dom))) goto done;
} else
FAIL(NC_EINVAL,"Unexpected dom root name: %s",dom->name);
done:
return THROW(ret);
}
static int
fillgroup(NCD4parser* parser, NCD4node* group, ezxml_t xml)
{
int ret = NC_NOERR;
/* Extract Dimensions */
if((ret = parseDimensions(parser,group,xml))) goto done;
/* Extract Enum types */
if((ret = parseEnumerations(parser,group,xml))) goto done;
/* Extract variables */
if((ret = parseVariables(parser,group,xml))) goto done;
/* Extract subgroups*/
if((ret = parseGroups(parser,group,xml))) goto done;
/* Parse group level attributes */
if((ret = parseAttributes(parser,group,xml))) goto done;
done:
return THROW(ret);
}
static int
parseDimensions(NCD4parser* parser, NCD4node* group, ezxml_t xml)
{
int ret = NC_NOERR;
ezxml_t x;
for(x=ezxml_child(xml, "Dimension");x != NULL;x = ezxml_next(x)) {
NCD4node* dimnode = NULL;
unsigned long long size;
const char* sizestr;
const char* unlimstr;
sizestr = ezxml_attr(x,"size");
if(sizestr == NULL)
FAIL(NC_EDIMSIZE,"Dimension has no size");
unlimstr = ezxml_attr(x,UCARTAGUNLIM);
if((ret = parseULL(sizestr,&size))) goto done;
if((ret=makeNode(parser,group,x,NCD4_DIM,NC_NULL,&dimnode))) goto done;
dimnode->dim.size = (long long)size;
dimnode->dim.isunlimited = (unlimstr != NULL);
/* Process attributes */
if((ret = parseAttributes(parser,dimnode,x))) goto done;
classify(group,dimnode);
}
done:
return THROW(ret);
}
static int
parseEnumerations(NCD4parser* parser, NCD4node* group, ezxml_t xml)
{
int ret = NC_NOERR;
ezxml_t x;
for(x=ezxml_child(xml, "Enumeration");x != NULL;x = ezxml_next(x)) {
NCD4node* node = NULL;
NCD4node* basetype = NULL;
const char* fqn = ezxml_attr(x,"basetype");
basetype = lookupFQN(parser,fqn,NCD4_TYPE);
if(basetype == NULL) {
FAIL(NC_EBADTYPE,"Enumeration has unknown type: ",fqn);
}
if((ret=makeNode(parser,group,x,NCD4_TYPE,NC_ENUM,&node))) goto done;
node->basetype = basetype;
if((ret=parseEconsts(parser,node,x))) goto done;
if(nclistlength(node->en.econsts) == 0)
FAIL(NC_EINVAL,"Enumeration has no values");
classify(group,node);
/* Finally, see if this type has UCARTAGORIGTYPE xml attribute */
if(parser->metadata->controller->controls.translation == NCD4_TRANSNC4) {
const char* typetag = ezxml_attr(x,UCARTAGORIGTYPE);
if(typetag != NULL) {
}
}
}
done:
return THROW(ret);
}
static int
parseEconsts(NCD4parser* parser, NCD4node* en, ezxml_t xml)
{
int ret = NC_NOERR;
ezxml_t x;
NClist* econsts = nclistnew();
for(x=ezxml_child(xml, "EnumConst");x != NULL;x = ezxml_next(x)) {
NCD4node* ec = NULL;
const char* name;
const char* svalue;
name = ezxml_attr(x,"name");
if(name == NULL) FAIL(NC_EBADNAME,"Enum const with no name");
if((ret=makeNode(parser,en,x,NCD4_ECONST,NC_NULL,&ec))) goto done ;
svalue = ezxml_attr(x,"value");
if(svalue == NULL)
FAIL(NC_EINVAL,"Enumeration Constant has no value");
if((ret=convertString(&ec->en.ecvalue,en->basetype,svalue)))
FAIL(NC_EINVAL,"Non-numeric Enumeration Constant: %s->%s",ec->name,svalue);
PUSH(econsts,ec);
}
en->en.econsts = econsts;
done:
return THROW(ret);
}
static int
parseVariables(NCD4parser* parser, NCD4node* group, ezxml_t xml)
{
int ret = NC_NOERR;
ezxml_t x;
for(x=xml->child;x != NULL;x=x->ordered) {
NCD4node* node = NULL;
KEYWORDINFO* info = keyword(x->name);
if(info == NULL)
FAIL(NC_ETRANSLATION,"Unexpected node type: %s",x->name);
/* Check if we need to process this node */
if(!ISVAR(info->sort)) continue; /* Handle elsewhere */
node = NULL;
ret = parseVariable(parser,group,x,&node);
if(ret != NC_NOERR || node == NULL) break;
}
done:
return THROW(ret);
}
static int
parseVariable(NCD4parser* parser, NCD4node* container, ezxml_t xml, NCD4node** nodep)
{
int ret = NC_NOERR;
NCD4node* node = NULL;
KEYWORDINFO* info = keyword(xml->name);
switch (info->subsort) {
case NC_STRUCT:
ret = parseStructure(parser,container,xml,&node);
break;
case NC_SEQ:
ret = parseSequence(parser,container,xml,&node);
break;
default:
ret = parseAtomicVar(parser,container,xml,&node);
}
*nodep = node;
return THROW(ret);
}
static int
parseMetaData(NCD4parser* parser, NCD4node* container, ezxml_t xml)
{
int ret = NC_NOERR;
/* Process dimrefs */
if((ret=parseDimRefs(parser,container,xml))) goto done;
/* Process attributes */
if((ret = parseAttributes(parser,container,xml))) goto done;
/* Process maps */
if((ret = parseMaps(parser,container,xml))) goto done;
done:
return THROW(ret);
}
static int
parseStructure(NCD4parser* parser, NCD4node* container, ezxml_t xml, NCD4node** nodep)
{
int ret = NC_NOERR;
NCD4node* var = NULL;
NCD4node* type = NULL;
NCD4node* group = NULL;
char* fqnname = NULL;
group = NCD4_groupFor(container); /* default: put type in the same group as var */
/* Make the structure as a variable with same name as structure; will be fixed later */
if((ret=makeNode(parser,container,xml,NCD4_VAR,NC_STRUCT,&var))) goto done;
classify(container,var);
/* Make the structure as a type with (for now) partial fqn name from the variable */
if((ret=makeNode(parser,group,xml,NCD4_TYPE,NC_STRUCT,&type))) goto done;
classify(group,type);
/* Set the basetype */
var->basetype = type;
/* Now change the struct typename */
fqnname = NCD4_makeName(var,"_");
if(fqnname == NULL)
FAIL(NC_ENOMEM,"Out of memory");
SETNAME(type,fqnname);
/* Parse Fields into the type */
if((ret = parseFields(parser,type,xml))) goto done;
/* Parse attributes, dims, and maps into the var */
if((ret = parseMetaData(parser,var,xml))) goto done;
record(parser,var);
/* See if this var has UCARTAGORIGTYPE attribute */
if(parser->metadata->controller->controls.translation == NCD4_TRANSNC4) {
const char* typetag = ezxml_attr(xml,UCARTAGORIGTYPE);
if(typetag != NULL) {
/* yes, place it on the type */
if((ret=addOrigType(parser,var,type,typetag))) goto done;
}
}
if(nodep) *nodep = var;
done:
nullfree(fqnname);
return THROW(ret);
}
static int
parseFields(NCD4parser* parser, NCD4node* container, ezxml_t xml)
{
int ret = NC_NOERR;
ezxml_t x;
for(x=xml->child;x != NULL;x=x->ordered) {
NCD4node* node = NULL;
KEYWORDINFO* info = keyword(x->name);
if(!ISVAR(info->sort)) continue; /* not a field */
ret = parseVariable(parser,container,x,&node);
if(ret) goto done;
}
done:
return THROW(ret);
}
/*
Specialized version of parseFields that is used
to attach a singleton field to a vlentype
*/
static int
parseVlenField(NCD4parser* parser, NCD4node* container, ezxml_t xml, NCD4node** fieldp)
{
int ret = NC_NOERR;
NCD4node* field = NULL;
ezxml_t x;
for(x=xml->child;x != NULL;x=x->ordered) {
KEYWORDINFO* info = keyword(x->name);
if(!ISVAR(info->sort)) continue; /* not a field */
if(field != NULL)
{ret = NC_EBADTYPE; goto done;}
if((ret = parseVariable(parser,container,x,&field)))
goto done;
}
if(fieldp) *fieldp = field;
done:
return THROW(ret);
}
static int
parseSequence(NCD4parser* parser, NCD4node* container, ezxml_t xml, NCD4node** nodep)
{
int ret = NC_NOERR;
NCD4node* var = NULL;
NCD4node* structtype = NULL;
NCD4node* vlentype = NULL;
NCD4node* group = NULL;
char name[NC_MAX_NAME];
char* fqnname = NULL;
int usevlen = 0;
group = NCD4_groupFor(container);
/* Convert a sequence variable into two or three things:
1. a compound type representing the fields of the sequence.
2. a vlen type whose basetype is #1
3. a variable whose basetype is #2.
If we can infer that the sequence was riginally produced
from a netcdf-4 vlen, then we can avoid createing #1.
Naming is as follows. Assume the var name is V
and the NCD4_makeName of the var is V1..._Vn.
1. var name is V.
2. vlen type is V1..._VN_t
3. compound type (if any) is V1..._VN_cmpd (Note, d4meta will append _t to this)
*/
/* Determine if we need to build a structure type or can go straight to a vlen
Test: UCARTAGVLEN xml attribute is set
*/
if(parser->metadata->controller->controls.translation == NCD4_TRANSNC4) {
const char* vlentag = ezxml_attr(xml,UCARTAGVLEN);
if(vlentag != NULL)
usevlen = 1;
} else
usevlen = 0;
/* make secondary names from the var fqn name */
if(usevlen) {
/* Parse the singleton field and then use it to fix up the var */
if((ret=parseVlenField(parser,container,xml,&var)))
goto done;
/* compute a partial fqn */
fqnname = NCD4_makeName(var,"_");
if(fqnname == NULL)
{ret = NC_ENOMEM; goto done;}
/* Now, create the vlen type using the field's basetype */
if((ret=makeNode(parser,group,xml,NCD4_TYPE,NC_SEQ,&vlentype))) goto done;
classify(group,vlentype);
vlentype->basetype = var->basetype;
/* Use name <fqnname>_t */
strncpy(name,fqnname,sizeof(name));
strncat(name,"_t",sizeof(name)-strlen(name));
SETNAME(vlentype,name);
/* Set the basetype */
var->basetype = vlentype;
} else {
/* Start by creating the var node; will be fixed up later */
if((ret=makeNode(parser,container,xml,NCD4_VAR,NC_SEQ,&var))) goto done;
classify(container,var);
fqnname = NCD4_makeName(var,"_");
if(fqnname == NULL)
{ret = NC_ENOMEM; goto done;}
if((ret=makeNode(parser,group,xml,NCD4_TYPE,NC_STRUCT,&structtype))) goto done;
classify(group,structtype);
/* Use name <fqnname>_base */
strncpy(name,fqnname,sizeof(name));
strncat(name,"_base",sizeof(name)-strlen(name));
SETNAME(structtype,name);
/* Parse Fields into type */
if((ret = parseFields(parser,structtype,xml))) goto done;
/* Create a seq type whose basetype is the compound type */
if((ret=makeNode(parser,group,xml,NCD4_TYPE,NC_SEQ,&vlentype))) goto done;
classify(group,vlentype);
/* Use name <xname>_t */
strncpy(name,fqnname,sizeof(name));
strncat(name,"_t",sizeof(name)-strlen(name));
SETNAME(vlentype,name);
vlentype->basetype = structtype;
/* Set the basetype */
var->basetype = vlentype;
}
/* Parse attributes, dims, and maps into var*/
if((ret = parseMetaData(parser,var,xml))) goto done;
record(parser,var);
/* See if this var has UCARTAGORIGTYPE attribute */
if(parser->metadata->controller->controls.translation == NCD4_TRANSNC4) {
const char* typetag = ezxml_attr(xml,UCARTAGORIGTYPE);
if(typetag != NULL) {
/* yes, place it on the type */
if((ret=addOrigType(parser,var,vlentype,typetag))) goto done;
}
}
if(nodep) *nodep = var;
done:
if(fqnname) free(fqnname);
return THROW(ret);
}
static int
parseGroups(NCD4parser* parser, NCD4node* parent, ezxml_t xml)
{
int ret = NC_NOERR;
ezxml_t x;
for(x=ezxml_child(xml, "Group");x != NULL;x = ezxml_next(x)) {
NCD4node* group = NULL;
const char* name = ezxml_attr(x,"name");
if(name == NULL) FAIL(NC_EBADNAME,"Group has no name");
if((ret=makeNode(parser,parent,x,NCD4_GROUP,NC_NULL,&group))) goto done;
group->group.varbyid = nclistnew();
if((ret = fillgroup(parser,group,x))) goto done;
/* Parse group attributes */
if((ret = parseAttributes(parser,group,x))) goto done;
PUSH(parent->groups,group);
}
done:
return THROW(ret);
}
static int
parseAtomicVar(NCD4parser* parser, NCD4node* container, ezxml_t xml, NCD4node** nodep)
{
int ret = NC_NOERR;
NCD4node* node = NULL;
NCD4node* base = NULL;
const char* typename;
KEYWORDINFO* info;
NCD4node* group;
/* Check for aliases */
for(typename=xml->name;;) {
info = keyword(typename);
if(info->aliasfor == NULL) break;
typename = info->aliasfor;
}
group = NCD4_groupFor(container);
/* Locate its basetype; handle opaque and enum separately */
if(info->subsort == NC_ENUM) {
const char* enumfqn = ezxml_attr(xml,"enum");
if(enumfqn == NULL)
base = NULL;
else
base = lookupFQN(parser,enumfqn,NCD4_TYPE);
} else if(info->subsort == NC_OPAQUE) {
/* See if the xml references an opaque type name */
base = getOpaque(parser,xml,group);
} else {
base = lookupFQN(parser,info->tag,NCD4_TYPE);
}
if(base == NULL || !ISTYPE(base->sort)) {
FAIL(NC_EBADTYPE,"Unexpected variable type: %s",info->tag);
}
if((ret=makeNode(parser,container,xml,NCD4_VAR,base->subsort,&node))) goto done;
classify(container,node);
node->basetype = base;
/* Parse attributes, dims, and maps */
if((ret = parseMetaData(parser,node,xml))) goto done;
/* See if this var has UCARTAGORIGTYPE attribute */
if(parser->metadata->controller->controls.translation == NCD4_TRANSNC4) {
const char* typetag = ezxml_attr(xml,UCARTAGORIGTYPE);
if(typetag != NULL) {
/* yes, place it on the type */
if((ret=addOrigType(parser,node,node,typetag))) goto done;
}
}
if(nodep) *nodep = node;
done:
return THROW(ret);
}
static int
parseDimRefs(NCD4parser* parser, NCD4node* var, ezxml_t xml)
{
int ret = NC_NOERR;
ezxml_t x;
for(x=ezxml_child(xml, "Dim");x!= NULL;x=ezxml_next(x)) {
NCD4node* dim = NULL;
const char* fqn;
fqn = ezxml_attr(x,"name");
if(fqn != NULL) {
dim = lookupFQN(parser,fqn,NCD4_DIM);
if(dim == NULL) {
FAIL(NC_EBADDIM,"Cannot locate dim with name: %s",fqn);
}
} else {
const char* sizestr = ezxml_attr(x,"size");
if(sizestr == NULL) {
FAIL(NC_EBADDIM,"Dimension reference has no name and no size");
}
/* Make or reuse anonymous dimension in root group */
dim = makeAnonDim(parser,sizestr);
if(dim == NULL)
FAIL(NC_EBADDIM,"Cannot create anonymous dimension for size: %s",sizestr);
}
PUSH(var->dims,dim);
}
done:
return THROW(ret);
}
static int
parseMaps(NCD4parser* parser, NCD4node* var, ezxml_t xml)
{
int ret = NC_NOERR;
ezxml_t x;
for(x=ezxml_child(xml, "Map");x!= NULL;x=ezxml_next(x)) {
NCD4node* mapref = NULL;
const char* fqn;
fqn = ezxml_attr(x,"name");
if(fqn == NULL)
FAIL(NC_ENOTVAR,"<Map> has no name attribute");
mapref = lookupFQN(parser,fqn,NCD4_VAR);
if(mapref == NULL)
FAIL(NC_ENOTVAR,"<Map> name does not refer to a variable: %s",fqn);
PUSH(var->maps,mapref);
}
done:
return THROW(ret);
}
static int
parseAttributes(NCD4parser* parser, NCD4node* container, ezxml_t xml)
{
int ret = NC_NOERR;
ezxml_t x;
NClist* values = NULL;
/* First, transfer any reserved xml attributes */
{
int count = 0;
const char** all = NULL;
all = ezxml_all_attr(xml,&count);
if(all != NULL && count > 0) {
const char** p;
container->xmlattributes = nclistnew();
for(p=all;*p;p+=2) {
if(isReserved(*p)) {
nclistpush(container->xmlattributes,strdup(p[0]));
nclistpush(container->xmlattributes,strdup(p[1]));
}
}
}
}
for(x=ezxml_child(xml, "Attribute");x!= NULL;x=ezxml_next(x)) {
const char* name = ezxml_attr(x,"name");
const char* type = ezxml_attr(x,"type");
NCD4node* attr = NULL;
NCD4node* basetype;
if(name == NULL) FAIL(NC_EBADNAME,"Missing <Attribute> name");
#ifdef HYRAXHACK
/* Hyrax specifies type="container" for container types */
if(strcmp(type,"container")==0
|| strcmp(type,"Container")==0)
type = NULL;
#endif
if(type == NULL) {
/* <Attribute> containers not supported; ignore */
continue;
}
if((ret=makeNode(parser,container,x,NCD4_ATTR,NC_NULL,&attr))) goto done;
/* HACK: If the attribute is _FillValue, then force the use of the
container's type as the attribute type */
if(strcmp(attr->name,"_FillValue") == 0)
basetype = container->basetype;
else
basetype = lookupFQN(parser,type,NCD4_TYPE);
if(basetype == NULL)
FAIL(NC_EBADTYPE,"Unknown <Attribute> type: ",type);
if(basetype->subsort == NC_NAT && basetype->subsort != NC_ENUM)
FAIL(NC_EBADTYPE,"<Attribute> type must be atomic or enum: ",type);
attr->basetype = basetype;
values = nclistnew();
if((ret=getValueStrings(parser,basetype,x,values))) {
FAIL(NC_EINVAL,"Malformed attribute: %s",name);
}
attr->attr.values = values; values = NULL;
PUSH(container->attributes,attr);
}
done:
if(ret != NC_NOERR) {
nclistfreeall(values);
}
return THROW(ret);
}
static int
parseError(NCD4parser* parser, ezxml_t errxml)
{
const char* shttpcode = ezxml_attr(errxml,"httpcode");
ezxml_t x;
if(shttpcode == NULL) shttpcode = "400";
if(sscanf(shttpcode,"%d",&parser->metadata->error.httpcode) != 1)
nclog(NCLOGERR,"Malformed <ERROR> response");
x=ezxml_child(errxml, "Message");
if(x != NULL) {
const char* txt = ezxml_txt(x);
parser->metadata->error.message = (txt == NULL ? NULL : strdup(txt));
}
x=ezxml_child(errxml, "Context");
if(x != NULL) {
const char* txt = ezxml_txt(x);
parser->metadata->error.context = (txt == NULL ? NULL : strdup(txt));
}
x=ezxml_child(errxml, "OtherInformation");
if(x != NULL) {
const char* txt = ezxml_txt(x);
parser->metadata->error.otherinfo = (txt == NULL ? NULL : strdup(txt));
}
return THROW(NC_NOERR);
}
/*
Find or create an opaque type
*/
static NCD4node*
getOpaque(NCD4parser* parser, ezxml_t varxml, NCD4node* group)
{
int i, ret = NC_NOERR;
long long len;
NCD4node* opaquetype = NULL;
const char* xattr;
if(parser->metadata->controller->controls.translation == NCD4_TRANSNC4) {
/* See if this var has UCARTAGOPAQUE attribute */
xattr = ezxml_attr(varxml,UCARTAGOPAQUE);
if(xattr != NULL) {
if((ret = parseLL(xattr,&len)) || (len < 0))
FAIL(NC_EINVAL,"Illegal opaque len: %s",xattr);
} else
len = 0;
} else
len = 0;
if(len == 0) { /* Need to use _bytestring */
if((ret=defineBytestringType(parser)))
goto done;
assert(parser->metadata->_bytestring != NULL);
opaquetype = parser->metadata->_bytestring;
} else {//(len > 0)
/* Try to locate existing opaque type with this length */
for(i=0;i<nclistlength(parser->types); i++) {
NCD4node* op = (NCD4node*)nclistget(parser->types,i);
if(op->subsort != NC_OPAQUE) continue;
if(op->opaque.size == len) {opaquetype = op; break;}
}
if(opaquetype == NULL) {/* create it */
char name[NC_MAX_NAME+1];
/* Make name be "opaqueN" */
snprintf(name,NC_MAX_NAME,"opaque%lld_t",len);
/* Opaque types are always created in the current group */
if((ret=makeNode(parser,group,NULL,NCD4_TYPE,NC_OPAQUE,&opaquetype)))
goto done;
SETNAME(opaquetype,name);
opaquetype->opaque.size = len;
if(opaquetype != NULL)
record(parser,opaquetype);
}
}
done:
return opaquetype;
}
/* get all value strings */
static int
getValueStrings(NCD4parser* parser, NCD4node* type, ezxml_t xattr, NClist* svalues)
{
const char* s;
/* See first if we have a "value" xml attribute */
s = ezxml_attr(xattr,"value");
if(s != NULL)
PUSH(svalues,strdup(s));
else {/* look for <Value> subnodes */
ezxml_t x;
for(x=ezxml_child(xattr, "Value");x != NULL;x = ezxml_next(x)) {
char* es;
char* ds;
/* We assume that either their is a single xml attribute called "value",
or there is a single chunk of text containing possibly multiple values.
*/
s = ezxml_attr(x,"value");
if(s == NULL) {/* See if there is a text part. */
s = x->txt;
if(s == NULL) s = "";
}
/* Need to de-escape the string */
es = NCD4_entityescape(s);
ds = NCD4_deescape(es);
nclistpush(svalues,ds);
nullfree(es);
}
}
return THROW(NC_NOERR);
}
/***************************************************/
/* Utilities */
NCD4node*
NCD4_groupFor(NCD4node* node)
{
while(node->sort != NCD4_GROUP) node = node->container;
return node;
}
/* Determine is a name is reserved */
static int
isReserved(const char* name)
{
if(name == NULL) return 0;
return (name[0] == RESERVECHAR);
}
/* If a node has the UCARTAGORIGTYPE attribute,
then capture that annotation. */
static int
addOrigType(NCD4parser* parser, NCD4node* src, NCD4node* dst, const char* oldname)
{
int ret = NC_NOERR;
if(dst == NULL) dst = src;
/* Record the original type in the destination*/
if((ret=splitOrigType(parser,oldname,dst))) goto done;
done:
return THROW(ret);
}
static int
splitOrigType(NCD4parser* parser, const char* fqn, NCD4node* type)
{
int ret = NC_NOERR;
NClist* pieces = nclistnew();
NCD4node* group = NULL;
char* name = NULL;
if((ret=NCD4_parseFQN(fqn,pieces))) goto done;
/* It should be the case that the pieces are {/group}+/name */
name = (char*)nclistpop(pieces);
if((ret = lookupFQNList(parser,pieces,NCD4_GROUP,&group))) goto done;
if(group == NULL) {
FAIL(NC_ENOGRP,"Non-existent group in FQN: ",fqn);
}
type->nc4.orig.name = strdup(name+1); /* plus 1 to skip the leading separator */
type->nc4.orig.group = group;
done:
return THROW(ret);
}
/* Locate an attribute.
If not found, then *attrp will be null
*/
NCD4node*
NCD4_findAttr(NCD4node* container, const char* attrname)
{
int i;
/* Look directly under this xml for <Attribute> */
for(i=0;i<nclistlength(container->attributes);i++) {
NCD4node* attr = (NCD4node*)nclistget(container->attributes,i);
if(strcmp(attr->name,attrname)!=0) continue;
return attr;
}
return NULL;
}
/*
Parse a simple string of digits into an unsigned long long
Return the value.
*/
static int
parseULL(const char* text, unsigned long long* ullp)
{
extern int errno;
char* endptr;
unsigned long long uint64 = 0;
errno = 0; endptr = NULL;
#ifdef HAVE_STRTOULL
uint64 = strtoull(text,&endptr,10);
if(errno == ERANGE)
return THROW(NC_ERANGE);
#else /*!(defined HAVE_STRTOLL && defined HAVE_STRTOULL)*/
sscanf((char*)text, "%llu", &uint64);
/* Have no useful way to detect out of range */
#endif /*!(defined HAVE_STRTOLL && defined HAVE_STRTOULL)*/
if(ullp) *ullp = uint64;
return THROW(NC_NOERR);
}
/*
Parse a simple string of digits into an signed long long
Return the value.
*/
static int
parseLL(const char* text, long long* llp)
{
extern int errno;
char* endptr;
long long int64 = 0;
errno = 0; endptr = NULL;
#ifdef HAVE_STRTOLL
int64 = strtoll(text,&endptr,10);
if(errno == ERANGE)
return THROW(NC_ERANGE);
#else /*!(defined HAVE_STRTOLL && defined HAVE_STRTOLL)*/
sscanf((char*)text, "%lld", &int64);
/* Have no useful way to detect out of range */
#endif /*!(defined HAVE_STRTOLL && defined HAVE_STRTOLL)*/
if(llp) *llp = int64;
return THROW(NC_NOERR);
}
/*
Convert a sequence of fqn names into a specific node.
WARNING: This is highly specialized in that it assumes