-
Notifications
You must be signed in to change notification settings - Fork 262
/
oc.c
2068 lines (1710 loc) · 60.4 KB
/
oc.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 2009, UCAR/Unidata and OPeNDAP, Inc.
See the COPYRIGHT file for more information. */
#include "config.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "netcdf.h"
#include "ocinternal.h"
#include "ocdebug.h"
#include "ocdump.h"
#include "nclog.h"
#include "ncrc.h"
#include "occurlfunctions.h"
#include "ochttp.h"
#include "ncwinpath.h"
#undef TRACK
/**************************************************/
/* Track legal ids */
#define ocverify(o) ((o) != NULL && (((OCheader*)(o))->magic == OCMAGIC)?1:0)
#define ocverifyclass(o,cl) ((o) != NULL && (((OCheader*)(o))->occlass == cl)?1:0)
#define OCVERIFYX(k,x,r) if(!ocverify(x)||!ocverifyclass(x,k)) {return (r);}
#define OCVERIFY(k,x) OCVERIFYX(k,x,OCTHROW(OC_EINVAL))
#define OCDEREF(T,s,x) (s)=(T)(x)
/**************************************************/
/*!\file oc.c
*/
/*!\defgroup Link Link Management
@{*/
/*!
This procedure opens a link to some OPeNDAP
data server to request a specific url, possibly with constraints.
It returns an <i>OClink</i> object.
\param[in] url The url for the OPeNDAP server to which a connection
is created and the request is made.
\param[out] linkp A pointer to a location into which the link
object is to be returned.
\retval OC_NOERR The link was successfully created.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_open(const char* url, OCobject* linkp)
{
OCerror ocerr = OC_NOERR;
OCstate* state = NULL;
ocerr = ocopen(&state,url);
if(ocerr == OC_NOERR && linkp) {
*linkp = (OCobject)(state);
} else {
if(state) free(state);
}
return OCTHROW(ocerr);
}
/*!
This procedure closes a previously opened
link and releases all resources associated with
that link.
\param[in] link The link object to be closed.
\retval OC_NOERR The link was successfully closed.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_close(OCobject link)
{
OCstate* state;
OCVERIFY(OC_State,link);
OCDEREF(OCstate*,state,link);
occlose(state);
return OCTHROW(OC_NOERR);
}
/** @} */
/*!\defgroup Tree Tree Management
@{*/
/*!
This procedure is used to send requests to the server
to obtain either a DAS, DDS, or DATADDS response
and produce a corresponding tree.
It fetchs and parses a given class of DXD the server specified
at open time, and using a specified set of constraints
and flags.
\param[in] link The link through which the server is accessed.
\param[in] constraint The constraint to be applied to the request.
\param[in] dxdkind The OCdxd value indicating what to fetch (i.e.
DAS, DDS, or DataDDS).
\param[in] flags The 'OR' of OCflags to control the fetch:
The OCONDISK flag is defined to cause the fetched
xdr data to be stored on disk instead of in memory.
\param[out] rootp A pointer a location to store
the root node of the tree associated with the the request.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_fetch(OCobject link, const char* constraint,
OCdxd dxdkind, OCflags flags, OCobject* rootp)
{
OCstate* state;
OCerror ocerr = OC_NOERR;
OCnode* root;
OCVERIFY(OC_State,link);
OCDEREF(OCstate*,state,link);
ocerr = ocfetch(state,constraint,dxdkind,flags,&root);
if(ocerr) return OCTHROW(ocerr);
if(rootp) *rootp = (OCobject)(root);
return OCTHROW(ocerr);
}
/*!
This procedure reclaims all resources
associated with a given tree of objects
associated with a given root.
If the root is that of a DataDDS, then the associated data tree
will be reclaimed as well.
\param[in] link The link through which the server is accessed.
\param[in] ddsroot The root of the tree to be reclaimed.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_root_free(OCobject link, OCobject ddsroot)
{
OCnode* root;
OCVERIFY(OC_Node,ddsroot);
OCDEREF(OCnode*,root,ddsroot);
ocroot_free(root);
return OCTHROW(OC_NOERR);
}
/*!
This procedure returns the textual part of
a DAS, DDS, or DATADDS request exactly as sent by the server.
\param[in] link The link through which the server is accessed.
\param[in] ddsroot The root of the tree whose text is to be returned.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
const char*
oc_tree_text(OCobject link, OCobject ddsroot)
{
OCnode* root = NULL;
OCVERIFYX(OC_Node,ddsroot,NULL);
OCDEREF(OCnode*,root,ddsroot);
if(root == NULL) return NULL;
root = root->root;
if(root->tree == NULL) return NULL;
return root->tree->text;
}
/**@}*/
/*!\defgroup Node Node Management
@{*/
/*!
This procedure returns a variety of properties
associated with a specific node.
Any of the pointers may be NULL in the following procedure call;
If the node is of type Dataset, then return # of global attributes
If the node is of type Attribute, then return the # of values in nattrp.
\param[in] link The link through which the server is accessed.
\param[in] ddsnode The node whose properties are of interest.
\param[out] namep Pointer for storing the node's associated name.
The caller must free the returned name.
\param[out] octypep Pointer for storing the node's octype.
\param[out] atomtypep Pointer for storing the object's
atomic type (i.e. OC_NAT .. OC_URL);only defined when
the object's octype is OC_Atomic
\param[out] containerp Pointer for storing the
OCnode for which this object is a subnode. The value OCNULL
is stored if the object is a root object.
\param[out] rankp Pointer for storing the rank (i.e. the number
of dimensions) for this object; zero implies a scalar.
\param[out] nsubnodesp Pointer for storing the number
of subnodes of this object.
\param[out] nattrp Pointer for storing the number
of attributes associated with this object.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_dds_properties(OCobject link,
OCobject ddsnode,
char** namep,
OCtype* octypep,
OCtype* atomtypep, /* if objecttype == OC_Atomic */
OCobject* containerp,
size_t* rankp,
size_t* nsubnodesp,
size_t* nattrp)
{
OCnode* node;
OCVERIFY(OC_Node,ddsnode);
OCDEREF(OCnode*,node,ddsnode);
if(namep) *namep = nulldup(node->name);
if(octypep) *octypep = node->octype;
if(atomtypep) *atomtypep = node->etype;
if(rankp) *rankp = node->array.rank;
if(containerp) *containerp = (OCobject)node->container;
if(nsubnodesp) *nsubnodesp = nclistlength(node->subnodes);
if(nattrp) {
if(node->octype == OC_Attribute) {
*nattrp = nclistlength(node->att.values);
} else {
*nattrp = nclistlength(node->attributes);
}
}
return OCTHROW(OC_NOERR);
}
/*!
Specialized accessor function as an alternative to oc_dds_properties.
\param[in] link The link through which the server is accessed.
\param[in] ddsnode The node whose properties are of interest.
\param[out] namep A pointer into which the node name is stored
as a null terminated string. The caller must free this value
when no longer needed.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_dds_name(OCobject link, OCobject ddsnode, char** namep)
{
OCstate* state;
OCnode* node;
OCVERIFY(OC_State,link);
OCDEREF(OCstate*,state,link);
OCVERIFY(OC_Node,ddsnode);
OCDEREF(OCnode*,node,ddsnode);
if(state == NULL || node == NULL) return OCTHROW(OCTHROW(OC_EINVAL));
if(namep) *namep = nulldup(node->name);
return OCTHROW(OC_NOERR);
}
/*!
Specialized accessor function as an alternative to oc_dds_properties.
\param[in] link The link through which the server is accessed.
\param[in] ddsnode The node whose properties are of interest.
\param[out] nsubnodesp A pointer into which the number of subnodes
is stored.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_dds_nsubnodes(OCobject link, OCobject ddsnode, size_t* nsubnodesp)
{
OCnode* node;
OCVERIFY(OC_Node,ddsnode);
OCDEREF(OCnode*,node,ddsnode);
if(nsubnodesp) *nsubnodesp = nclistlength(node->subnodes);
return OCTHROW(OC_NOERR);
}
/*!
Specialized accessor function as an alternative to oc_dds_properties.
\param[in] link The link through which the server is accessed.
\param[in] ddsnode The node whose properties are of interest.
\param[out] typep A pointer into which the atomictype is stored.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_dds_atomictype(OCobject link, OCobject ddsnode, OCtype* typep)
{
OCnode* node;
OCVERIFY(OC_Node,ddsnode);
OCDEREF(OCnode*,node,ddsnode);
if(typep) *typep = node->etype;
return OCTHROW(OC_NOERR);
}
/*!
Specialized accessor function as an alternative to oc_dds_properties.
\param[in] link The link through which the server is accessed.
\param[in] ddsnode The node whose properties are of interest.
\param[out] typep A pointer into which the octype is stored.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_dds_class(OCobject link, OCobject ddsnode, OCtype* typep)
{
OCnode* node;
OCVERIFY(OC_Node,ddsnode);
OCDEREF(OCnode*,node,ddsnode);
if(typep) *typep = node->octype;
return OCTHROW(OC_NOERR);
}
/*!
Specialized accessor function as an alternative to oc_dds_properties.
\param[in] link The link through which the server is accessed.
\param[in] ddsnode The node whose properties are of interest.
\param[out] rankp A pointer into which the rank is stored.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_dds_rank(OCobject link, OCobject ddsnode, size_t* rankp)
{
OCnode* node;
OCVERIFY(OC_Node,ddsnode);
OCDEREF(OCnode*,node,ddsnode);
if(rankp) *rankp = node->array.rank;
return OCTHROW(OC_NOERR);
}
/*!
Specialized accessor function as an alternative to oc_dds_properties.
\param[in] link The link through which the server is accessed.
\param[in] ddsnode The node whose properties are of interest.
\param[out] nattrp A pointer into which the number of attributes is stored.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_dds_attr_count(OCobject link, OCobject ddsnode, size_t* nattrp)
{
OCnode* node;
OCVERIFY(OC_Node,ddsnode);
OCDEREF(OCnode*,node,ddsnode);
if(nattrp) {
if(node->octype == OC_Attribute) {
*nattrp = nclistlength(node->att.values);
} else {
*nattrp = nclistlength(node->attributes);
}
}
return OCTHROW(OC_NOERR);
}
/*!
Specialized accessor function as an alternative to oc_dds_properties.
\param[in] link The link through which the server is accessed.
\param[in] ddsnode The node whose properties are of interest.
\param[out] rootp A pointer into which the the root of the tree containing
the node is stored.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_dds_root(OCobject link, OCobject ddsnode, OCobject* rootp)
{
OCnode* node;
OCVERIFY(OC_Node,ddsnode);
OCDEREF(OCnode*,node,ddsnode);
if(rootp) *rootp = (OCobject)node->root;
return OCTHROW(OC_NOERR);
}
/*!
Specialized accessor function as an alternative to oc_dds_properties.
\param[in] link The link through which the server is accessed.
\param[in] ddsnode The node whose properties are of interest.
\param[out] containerp A pointer into which the the immediate
container ddsnode is stored.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_dds_container(OCobject link, OCobject ddsnode, OCobject* containerp)
{
OCnode* node;
OCVERIFY(OC_Node,ddsnode);
OCDEREF(OCnode*,node,ddsnode);
if(containerp) *containerp = (OCobject)node->container;
return OCTHROW(OC_NOERR);
}
/*!
Obtain the DDS node corresponding to the i'th field
of a node that itself is a container (Dataset, Structure, Sequence, or Grid)
\param[in] link The link through which the server is accessed.
\param[in] ddsnode The container node of interest.
\param[in] index The index (starting at zero) of the field to return.
\param[out] fieldnodep A pointer into which the i'th field node is stored.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINDEX The index was greater than the number of fields.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
\retval OC_EBADTYPE The dds node is not a container node.
*/
OCerror
oc_dds_ithfield(OCobject link, OCobject ddsnode, size_t index, OCobject* fieldnodep)
{
OCnode* node;
OCnode* field;
OCVERIFY(OC_Node,ddsnode);
OCDEREF(OCnode*,node,ddsnode);
if(!ociscontainer(node->octype))
return OCTHROW(OC_EBADTYPE);
if(index >= nclistlength(node->subnodes))
return OCTHROW(OC_EINDEX);
field = (OCnode*)nclistget(node->subnodes,index);
if(fieldnodep) *fieldnodep = (OCobject)field;
return OCTHROW(OC_NOERR);
}
/*!
Alias for oc_dds_ithfield.
\param[in] link The link through which the server is accessed.
\param[in] ddsnode The container node of interest.
\param[in] index The index (starting at zero) of the field to return.
\param[out] fieldnodep A pointer into which the i'th field node is stored.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINDEX The index was greater than the number of fields.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
\retval OC_EBADTYPE The dds node is not a container node.
*/
OCerror
oc_dds_ithsubnode(OCobject link, OCobject ddsnode, size_t index, OCobject* fieldnodep)
{
return OCTHROW(oc_dds_ithfield(link,ddsnode,index,fieldnodep));
}
/*!
Obtain the DDS node corresponding to the array of a Grid container.
Equivalent to oc_dds_ithfield(link,grid-container,0,arraynode).
\param[in] link The link through which the server is accessed.
\param[in] grid The grid container node of interest.
\param[out] arraynodep A pointer into which the grid array node is stored.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_dds_gridarray(OCobject link, OCobject grid, OCobject* arraynodep)
{
return OCTHROW(oc_dds_ithfield(link,grid,0,arraynodep));
}
/*!
Obtain the DDS node corresponding to the i'th map of a Grid container.
Equivalent to oc_dds_ithfield(link,grid-container,index+1,arraynode).
Note the map index starts at zero.
\param[in] link The link through which the server is accessed.
\param[in] grid The grid container node of interest.
\param[in] index The (zero-based) index of the map node to return.
\param[out] mapnodep A pointer into which the grid map node is stored.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINDEX The map index is illegal.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_dds_gridmap(OCobject link, OCobject grid, size_t index, OCobject* mapnodep)
{
return OCTHROW(oc_dds_ithfield(link,grid,index+1,mapnodep));
}
/*!
Obtain a dds node by name from a dds structure or dataset node.
\param[in] link The link through which the server is accessed.
\param[in] ddsnode The container node of interest.
\param[in] name The name of the field to return.
\param[out] fieldp A pointer into which the name'th field node is stored.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINDEX No field with the given name was found.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_dds_fieldbyname(OCobject link, OCobject ddsnode, const char* name, OCobject* fieldp)
{
OCerror err = OC_NOERR;
OCnode* node;
size_t count,i;
OCVERIFY(OC_Node,ddsnode);
OCDEREF(OCnode*,node,ddsnode);
if(!ociscontainer(node->octype))
return OCTHROW(OC_EBADTYPE);
/* Search the fields to find a name match */
err = oc_dds_nsubnodes(link,ddsnode,&count);
if(err != OC_NOERR) return err;
for(i=0;i<count;i++) {
OCobject field;
char* fieldname = NULL;
int match = 1;
err = oc_dds_ithfield(link,ddsnode,i,&field);
if(err != OC_NOERR) return err;
/* Get the field's name */
err = oc_dds_name(link,field,&fieldname);
if(err != OC_NOERR) return err;
if(fieldname != NULL) {
match = strcmp(name,fieldname);
free(fieldname);
}
if(match == 0) {
if(fieldp) *fieldp = field;
return OCTHROW(OC_NOERR);
}
}
return OCTHROW(OC_EINDEX); /* name was not found */
}
/*!
Obtain the dimension nodes (of octype OC_Dimension)
associated with the node of interest.
\param[in] link The link through which the server is accessed.
\param[in] ddsnode The dds node of interest.
\param[out] dims A vector into which the dimension nodes
are stored. The caller must allocate based on the rank of the node.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_dds_dimensions(OCobject link, OCobject ddsnode, OCobject* dims)
{
OCnode* node;
size_t i;
OCVERIFY(OC_Node,ddsnode);
OCDEREF(OCnode*,node,ddsnode);
if(node->array.rank == 0) return OCTHROW(OCTHROW(OC_ESCALAR));
if(dims != NULL) {
for(i=0;i<node->array.rank;i++) {
OCnode* dim = (OCnode*)nclistget(node->array.dimensions,i);
dims[i] = (OCobject)dim;
}
}
return OCTHROW(OC_NOERR);
}
/*!
Obtain the i'th dimension node (of octype OC_Dimension)
associated with the node of interest.
\param[in] link The link through which the server is accessed.
\param[in] ddsnode The dds node of interest.
\param[in] index The index of the dimension to be returned.
\param[out] dimidp A pointer into which the index'th dimension is stored.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINDEX The index is greater than the node's rank.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_dds_ithdimension(OCobject link, OCobject ddsnode, size_t index, OCobject* dimidp)
{
OCnode* node;
OCobject dimid = NULL;
OCVERIFY(OC_Node,ddsnode);
OCDEREF(OCnode*,node,ddsnode);
if(node->array.rank == 0) return OCTHROW(OCTHROW(OC_ESCALAR));
if(index >= node->array.rank) return OCTHROW(OCTHROW(OC_EINDEX));
dimid = (OCobject)nclistget(node->array.dimensions,index);
if(dimidp) *dimidp = dimid;
return OCTHROW(OC_NOERR);
}
/*!
Obtain the properties of a dimension node.
\param[in] link The link through which the server is accessed.
\param[in] ddsnode The dimension node.
\param[out] sizep A pointer into which to store the size of the dimension.
\param[out] namep A pointer into which to store the name of the dimension.
If the dimension is anonymous, then the value NULL is returned as the name.
The caller must free the returned name.
\retval OC_NOERR The procedure executed normally.
\retval OC_BADTYPE If the node is not of type OC_Dimension.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_dimension_properties(OCobject link, OCobject ddsnode, size_t* sizep, char** namep)
{
OCnode* dim;
OCVERIFY(OC_Node,ddsnode);
OCDEREF(OCnode*,dim,ddsnode);
if(dim->octype != OC_Dimension) return OCTHROW(OCTHROW(OC_EBADTYPE));
if(sizep) *sizep = dim->dim.declsize;
if(namep) *namep = nulldup(dim->name);
return OCTHROW(OC_NOERR);
}
/*!
Obtain just the set of sizes of the dimensions
associated with a dds node.
\param[in] link The link through which the server is accessed.
\param[in] ddsnode The node of interest
\param[out] dimsizes A vector into which the sizes of all
the dimensions of a node are stored. Its size is determined
by the rank of the node and must be allocated and free'd by the caller.
\retval OC_NOERR The procedure executed normally.
\retval OC_ESCALAR If the node is a scalar.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_dds_dimensionsizes(OCobject link, OCobject ddsnode, size_t* dimsizes)
{
OCnode* node;
OCVERIFY(OC_Node,ddsnode);
OCDEREF(OCnode*,node,ddsnode);
if(node->array.rank == 0) return OCTHROW(OCTHROW(OC_ESCALAR));
if(dimsizes != NULL) {
size_t i;
for(i=0;i<node->array.rank;i++) {
OCnode* dim = (OCnode*)nclistget(node->array.dimensions,i);
dimsizes[i] = dim->dim.declsize;
}
}
return OCTHROW(OC_NOERR);
}
/*!
Return the name, type, length, and values associated with
the i'th attribute of a specified node. The actual attribute
strings are returned and the user must do any required
conversion based on the octype. The strings argument must
be allocated and freed by caller. Standard practice is to
call twice, once with the strings argument == NULL so we get
the number of values, then the second time with an allocated
char** vector. The caller should reclaim the contents of
the returned string vector using <i>oc_reclaim_strings</i>.
\param[in] link The link through which the server is accessed.
\param[in] ddsnode The node of interest
\param[in] index Return the information of the index'th attribute.
\param[out] namep A pointer into which the attribute's name is stored.
It must be freed by the caller.
\param[out] octypep A pointer into which the attribute's atomic type is stored.
\param[out] nvaluesp A pointer into which the number
of attribute values is stored.
\param[out] strings A vector into which the values of the attribute
are stored. It must be allocated and free'd by the caller.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINDEX If the index is more than the number of attributes.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_dds_attr(OCobject link, OCobject ddsnode, size_t index,
char** namep, OCtype* octypep,
size_t* nvaluesp, char** strings)
{
int i;
OCnode* node;
OCattribute* attr;
size_t nattrs;
OCVERIFY(OC_Node,ddsnode);
OCDEREF(OCnode*,node,ddsnode);
nattrs = nclistlength(node->attributes);
if(index >= nattrs) return OCTHROW(OCTHROW(OC_EINDEX));
attr = (OCattribute*)nclistget(node->attributes,index);
if(namep) *namep = strdup(attr->name);
if(octypep) *octypep = attr->etype;
if(nvaluesp) *nvaluesp = attr->nvalues;
if(strings) {
if(attr->nvalues > 0) {
for(i=0;i<attr->nvalues;i++)
strings[i] = nulldup(attr->values[i]);
}
}
return OCTHROW(OC_NOERR);
}
/*!
Given a counted vector of strings, free up all of the strings,
BUT NOT THE VECTOR since that was allocated by the caller.
\param[in] n The link through which the server is accessed.
\param[in] svec The node of interest.
*/
void
oc_reclaim_strings(size_t n, char** svec)
{
int i;
for(i=0;i<n;i++) if(svec[i] != NULL) free(svec[i]);
}
/*!
Return the count of DAS attribute values.
\param[in] link The link through which the server is accessed.
\param[in] dasnode The node of interest
\param[out] nvaluesp A pointer into which the number of attributes
is stored.
\retval OC_NOERR The procedure executed normally.
\retval OC_EBADTPE If the node is not of type OC_Attribute.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_das_attr_count(OCobject link, OCobject dasnode, size_t* nvaluesp)
{
OCnode* attr;
OCVERIFY(OC_Node,dasnode);
OCDEREF(OCnode*,attr,dasnode);
if(attr->octype != OC_Attribute) return OCTHROW(OCTHROW(OC_EBADTYPE));
if(nvaluesp) *nvaluesp = nclistlength(attr->att.values);
return OCTHROW(OC_NOERR);
}
/*!
The procedure oc_das_attr returns the i'th string value
associated with a DAS object of type <i>OC_Attribute</i>.
Note carefully that this operation applies to DAS nodes
and not to DDS or DATADDS nodes.
Note also that the returned value is always a string
and it is the caller;'s responsibility to free the returned string.
\param[in] link The link through which the server is accessed.
\param[in] dasnode The DAS node of interest.
\param[in] index The index of the das value to return.
\param[in] atomtypep A pointer into which is stored the atomic
type of the attribute.
\param[out] valuep A vector into which the attribute's string values
are stored. Caller must allocate and free.
\retval OC_NOERR The procedure executed normally.
\retval OC_EBADTPE If the node is not of type OC_Attribute.
\retval OC_EINDEX If the index is larger than the number of attributes.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_das_attr(OCobject link, OCobject dasnode, size_t index, OCtype* atomtypep, char** valuep)
{
OCnode* attr;
size_t nvalues;
OCVERIFY(OC_Node,dasnode);
OCDEREF(OCnode*,attr,dasnode);
if(attr->octype != OC_Attribute) return OCTHROW(OCTHROW(OC_EBADTYPE));
nvalues = nclistlength(attr->att.values);
if(index >= nvalues) return OCTHROW(OCTHROW(OC_EINDEX));
if(atomtypep) *atomtypep = attr->etype;
if(valuep) *valuep = nulldup((char*)nclistget(attr->att.values,index));
return OCTHROW(OC_NOERR);
}
/**@}*/
/**************************************************/
/*!\defgroup Interconnection Node Interconnection Management */
/**@{*/
/*!
As a rule, the attributes of an object are accessed using
the <i>oc_dds_attr</i> procedure rather than by traversing a
DAS. In order to support this, the <i>oc_merge_das</i>
procedure annotates a DDS node with attribute values taken
from a specified DAS node.
\param[in] link The link through which the server is accessed.
\param[in] dasroot The root object of a DAS tree.
\param[in] ddsroot The root object of a DDS (or DataDDS) tree.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_merge_das(OCobject link, OCobject dasroot, OCobject ddsroot)
{
OCstate* state;
OCnode* das;
OCnode* dds;
OCVERIFY(OC_State,link);
OCDEREF(OCstate*,state,link);
OCVERIFY(OC_Node,dasroot);
OCDEREF(OCnode*,das,dasroot);
OCVERIFY(OC_Node,ddsroot);
OCDEREF(OCnode*,dds,ddsroot);
return OCTHROW(ocddsdasmerge(state,das,dds));
}
/**@}*/
/**************************************************/
/*!\defgroup Data Data Management */
/**@{*/
/*!
Obtain the datanode root associated with a DataDDS tree.
Do not confuse this with oc_data_root.
This procedure, given the DDS tree root, gets the data tree root.
\param[in] link The link through which the server is accessed.
\param[in] ddsroot The DataDDS tree root.
\param[out] datarootp A pointer into which the datanode root is stored.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
*/
OCerror
oc_dds_getdataroot(OCobject link, OCobject ddsroot, OCobject* datarootp)
{
OCerror ocerr = OC_NOERR;
OCstate* state;
OCnode* root;
OCdata* droot;
OCVERIFY(OC_State,link);
OCDEREF(OCstate*,state,link);
OCVERIFY(OC_Node,ddsroot);
OCDEREF(OCnode*,root,ddsroot);
if(datarootp == NULL)
return OCTHROW(OCTHROW(OC_EINVAL));
ocerr = ocdata_getroot(state,root,&droot);
if(ocerr == OC_NOERR && datarootp)
*datarootp = (OCobject)droot;
return OCTHROW(ocerr);
}
/*!
Obtain the data instance corresponding to the i'th field
of a data node instance that itself is a container instance.
\param[in] link The link through which the server is accessed.
\param[in] datanode The container data node instance of interest.
\param[in] index The index (starting at zero) of the field instance to return.
\param[out] fieldp A pointer into which the i'th field instance is stored.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINDEX The index was greater than the number of fields.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
\retval OC_EBADTYPE The data node is not a container node.
*/
OCerror
oc_data_ithfield(OCobject link, OCobject datanode, size_t index, OCobject* fieldp)
{
OCerror ocerr = OC_NOERR;
OCstate* state;
OCdata* data;
OCdata* field;
OCVERIFY(OC_State,link);
OCDEREF(OCstate*,state,link);
OCVERIFY(OC_Data,datanode);
OCDEREF(OCdata*,data,datanode);
if(fieldp == NULL) return OCTHROW(OCTHROW(OC_EINVAL));
ocerr = ocdata_ithfield(state,data,index,&field);
if(ocerr == OC_NOERR)
*fieldp = (OCobject)field;
return OCTHROW(ocerr);
}
/*!
Obtain a data node by name from a container data node.
\param[in] link The link through which the server is accessed.
\param[in] datanode The container data node instance of interest.
\param[in] name The name of the field instance to return.
\param[out] fieldp A pointer into which the i'th field instance is stored.
\retval OC_NOERR The procedure executed normally.
\retval OC_EINDEX No field with the given name was found.
\retval OC_EINVAL One of the arguments (link, etc.) was invalid.
\retval OC_EBADTYPE The data node is not a container node.
*/
OCerror
oc_data_fieldbyname(OCobject link, OCobject datanode, const char* name, OCobject* fieldp)
{
OCerror err = OC_NOERR;
size_t count=0,i;
OCobject ddsnode;
OCVERIFY(OC_State,link);
OCVERIFY(OC_Data,datanode);
/* Get the dds node for this datanode */
err = oc_data_ddsnode(link,datanode,&ddsnode);
if(err != OC_NOERR) return err;
/* Search the fields to find a name match */
err = oc_dds_nsubnodes(link,ddsnode,&count);
if(err != OC_NOERR) return err;
for(i=0;i<count;i++) {
int match;
OCobject field;
char* fieldname = NULL;
err = oc_dds_ithfield(link,ddsnode,i,&field);
if(err != OC_NOERR) return err;
/* Get the field's name */
err = oc_dds_name(link,field,&fieldname);
if(err != OC_NOERR) return err;
if(!fieldname)
return OCTHROW(OC_EINVAL);
match = strcmp(name,fieldname);
if(fieldname != NULL) free(fieldname);
if(match == 0) {
/* Get the ith datasubnode */
err = oc_data_ithfield(link,datanode,i,&field);
if(err != OC_NOERR) return err;
if(fieldp) *fieldp = field;
return OCTHROW(OC_NOERR);
}
}
return OCTHROW(OC_EINDEX); /* name was not found */
}
/*!
Obtain the data instance corresponding to the array field
of a Grid container instance.
Equivalent to oc_data_ithfield(link,grid,0,arraydata).