-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathdumplib.c
1898 lines (1752 loc) · 49.6 KB
/
dumplib.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 2018, University Corporation for Atmospheric Research
* See netcdf/README file for copying and redistribution conditions.
* $Header: /upc/share/CVS/netcdf-3/ncdump/dumplib.c,v 1.85 2010/05/05 22:15:39 dmh Exp $
*********************************************************************/
/*
* We potentially include <stdarg.h> before <stdio.h> in order to obtain a
* definition for va_list from the GNU C compiler.
*/
#include "config.h"
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#ifndef NO_FLOAT_H
#include <float.h> /* for FLT_EPSILON, DBL_EPSILON */
#endif /* NO_FLOAT_H */
#include <math.h>
#include <netcdf.h>
#include "utils.h"
#include "nccomps.h"
#include "dumplib.h"
#include "ncdump.h"
#include "isnan.h"
#include "nctime0.h"
static float float_eps;
static double double_eps;
extern fspec_t formatting_specs; /* set from command-line options */
static float
float_epsilon(void)
{
float float_eps;
#ifndef NO_FLOAT_H
float_eps = FLT_EPSILON;
#else /* NO_FLOAT_H */
{
float etop, ebot, eps;
float one = 1.0;
float two = 2.0;
etop = 1.0;
ebot = 0.0;
eps = ebot + (etop - ebot)/two;
while (eps != ebot && eps != etop) {
float epsp1;
epsp1 = one + eps;
if (epsp1 > one)
etop = eps;
else
ebot = eps;
eps = ebot + (etop - ebot)/two;
}
float_eps = two * etop;
}
#endif /* NO_FLOAT_H */
return float_eps;
}
static double
double_epsilon(void)
{
double double_eps;
#ifndef NO_FLOAT_H
double_eps = DBL_EPSILON;
#else /* NO_FLOAT_H */
{
double etop, ebot, eps;
double one = 1.0;
double two = 2.0;
etop = 1.0;
ebot = 0.0;
eps = ebot + (etop - ebot)/two;
while (eps != ebot && eps != etop) {
double epsp1;
epsp1 = one + eps;
if (epsp1 > one)
etop = eps;
else
ebot = eps;
eps = ebot + (etop - ebot)/two;
}
double_eps = two * etop;
}
#endif /* NO_FLOAT_H */
return double_eps;
}
void
init_epsilons(void)
{
float_eps = float_epsilon();
double_eps = double_epsilon();
}
static char* has_c_format_att(int ncid, int varid);
int float_precision_specified = 0; /* -p option specified float precision */
int double_precision_specified = 0; /* -p option specified double precision */
char float_var_fmt[] = "%.NNg";
char double_var_fmt[] = "%.NNg";
char float_att_fmt[] = "%#.NNgf";
char float_attx_fmt[] = "%#.NNg";
char double_att_fmt[] = "%#.NNg";
/* magic number stored in a safebuf and checked, hoping it will be
* changed if buffer was overwritten inadvertently */
#define SAFEBUF_CERT 2147114711
/* expression for where SAFEBUF_CERT is stored within safebuf (at end
* of buffer, after data) */
#define SAFEBUF_EXPR(sbuf) (*(int *)((sbuf)->buf + (sbuf)->len))
/* expression to be checked whenever a safebuf is used */
#define SAFEBUF_CHECK(sbuf) (SAFEBUF_EXPR(sbuf) == SAFEBUF_CERT)
/* somewhat arbitrary initial size of safebufs, grow as needed */
#define SAFEBUF_INIT_LEN 128
/* initialize safe buffer */
safebuf_t *
sbuf_new() {
size_t len = SAFEBUF_INIT_LEN;
safebuf_t *sb;
sb = (safebuf_t *) emalloc(sizeof(safebuf_t));
sb->buf = (char *)emalloc(len + sizeof(int));
sb->len = len;
/* write a "stamp" in last 4 bytes of buffer for id and to check for overflow */
SAFEBUF_EXPR(sb) = SAFEBUF_CERT;
sb->buf[0] = 0;
sb->cl = strlen(sb->buf);
assert(SAFEBUF_CHECK(sb));
return sb;
}
/* grow buffer to at least len bytes, copying previous contents if
* necessary */
void
sbuf_grow(safebuf_t *sb, size_t len) {
size_t m = sb->len;
void *tmp;
assert(SAFEBUF_CHECK(sb));
if (len <= m)
return;
/* Make sure we at least double size of buffer to get what's
* needed. If we just used realloc(), no guarantee that length
* would be expanded by a multiple, which we want. */
while(len > m) {
m *= 2;
}
tmp = emalloc(m + sizeof(int));
memcpy(tmp, sb->buf, sb->len);
sb->len = m;
free(sb->buf);
sb->buf = tmp;
SAFEBUF_EXPR(sb) = SAFEBUF_CERT;
assert(SAFEBUF_CHECK(sb));
}
/* Copy string s2 to safe buffer, growing if necessary */
void
sbuf_cpy(safebuf_t *sb, const char *s2) {
size_t s2len;
assert(SAFEBUF_CHECK(sb));
s2len = strlen(s2);
sbuf_grow(sb, 1 + s2len);
strncpy(sb->buf, s2, sb->len);
sb->cl = s2len;
assert(SAFEBUF_CHECK(sb));
}
/* Concatenate string s2 to end of string in safe buffer, growing if necessary */
void
sbuf_cat(safebuf_t *sb, const char *s2) {
size_t s2len;
size_t res;
assert(SAFEBUF_CHECK(sb));
s2len = strlen(s2);
sbuf_grow(sb, 1 + sb->cl + s2len);
res = strlcat(sb->buf + sb->cl, s2, sb->len - sb->cl);
assert( res < sb->len );
sb->cl += s2len;
assert(SAFEBUF_CHECK(sb));
}
/* Concatenate string in safebuf s2 to end of string in safebuf s1,
* growing if necessary */
void
sbuf_catb(safebuf_t *s1, const safebuf_t *s2) {
size_t s2len;
size_t res;
assert(SAFEBUF_CHECK(s1));
assert(SAFEBUF_CHECK(s2));
s2len = (size_t)sbuf_len(s2);
sbuf_grow(s1, 1 + s1->cl + s2len);
res = strlcat(s1->buf + s1->cl, s2->buf, s1->len - s1->cl);
assert( res < s1->len );
s1->cl += s2len;
assert(SAFEBUF_CHECK(s1));
}
/* Return length of string in sbuf */
int
sbuf_len(const safebuf_t *sb) {
assert(SAFEBUF_CHECK(sb));
return (int)sb->cl;
}
/* Return C string in an sbuf */
char *
sbuf_str(const safebuf_t *sb) {
assert(SAFEBUF_CHECK(sb));
return sb->buf;
}
/* free safe buffer */
void
sbuf_free(safebuf_t *sb) {
assert(SAFEBUF_CHECK(sb));
free(sb->buf);
free(sb);
}
/* In case different formats specified with -d option, set them here. */
void
set_formats(int float_digits, int double_digits)
{
int res;
res = snprintf(float_var_fmt, sizeof float_var_fmt, "%%.%dg",
float_digits) + 1;
assert(res <= sizeof(float_var_fmt));
res = snprintf(double_var_fmt, sizeof double_var_fmt, "%%.%dg",
double_digits) + 1;
assert(res <= sizeof(double_var_fmt));
res = snprintf(float_att_fmt, sizeof float_att_fmt, "%%#.%dgf",
float_digits) + 1;
assert(res <= sizeof(float_att_fmt));
res = snprintf(float_attx_fmt, sizeof float_attx_fmt, "%%#.%dg",
float_digits) + 1;
assert(res <= sizeof(float_attx_fmt));
res = snprintf(double_att_fmt, sizeof double_att_fmt, "%%#.%dg",
double_digits) + 1;
assert(res <= sizeof(double_att_fmt));
}
static char *
has_c_format_att(
int ncid, /* netcdf id */
int varid /* variable id */
)
{
nc_type cfmt_type;
size_t cfmt_len;
#define C_FMT_NAME "C_format" /* name of C format attribute */
#define MAX_CFMT_LEN 100 /* max length of C format attribute */
static char cfmt[MAX_CFMT_LEN];
/* we expect nc_inq_att to fail if there is no "C_format" attribute */
int nc_stat = nc_inq_att(ncid, varid, "C_format", &cfmt_type, &cfmt_len);
switch(nc_stat) {
case NC_NOERR:
if (cfmt_type == NC_CHAR && cfmt_len != 0 && cfmt_len < MAX_CFMT_LEN) {
int nc_stat = nc_get_att_text(ncid, varid, "C_format", cfmt);
if(nc_stat != NC_NOERR) {
fprintf(stderr, "Getting 'C_format' attribute %s\n",
nc_strerror(nc_stat));
(void) fflush(stderr);
}
cfmt[cfmt_len] = '\0';
return &cfmt[0];
}
break;
case NC_ENOTATT:
break;
default:
fprintf(stderr, "Inquiring about 'C_format' attribute %s\n",
nc_strerror(nc_stat));
(void) fflush(stderr);
break;
}
return 0;
}
/* Return default format to use for a primitive type */
const char *
get_default_fmt(nc_type typeid) {
/* Otherwise return sensible default. */
switch (typeid) {
case NC_BYTE:
return "%d";
case NC_CHAR:
return "%s";
case NC_SHORT:
return "%d";
case NC_INT:
return "%d";
case NC_FLOAT:
return float_var_fmt;
case NC_DOUBLE:
return double_var_fmt;
case NC_UBYTE:
return "%u";
case NC_USHORT:
return "%u";
case NC_UINT:
return "%u";
case NC_INT64:
return "%lld";
case NC_UINT64:
return "%llu";
case NC_STRING:
return "\"%s\"";
default:
break;
}
return ""; /* user-defined types don't use fmt member */
}
/*
* Determine print format to use for each primitive value for this
* variable. Use value of attribute C_format if it exists, otherwise
* a sensible default.
*/
const char *
get_fmt(
int ncid,
int varid,
nc_type typeid
)
{
char *c_format_att;
/* float or double precision specified with -p option overrides any
C_format attribute value, so check for that first. */
if (float_precision_specified && typeid == NC_FLOAT)
return float_var_fmt;
if (double_precision_specified && typeid == NC_DOUBLE)
return double_var_fmt;
/* If C_format attribute exists, return it */
c_format_att = has_c_format_att(ncid, varid);
if (c_format_att)
return c_format_att;
return get_default_fmt(typeid);
}
/* Return primitive type name */
static const char *
prim_type_name(nc_type type)
{
switch (type) {
case NC_BYTE:
return "byte";
case NC_CHAR:
return "char";
case NC_SHORT:
return "short";
case NC_INT:
return "int";
case NC_FLOAT:
return "float";
case NC_DOUBLE:
return "double";
case NC_UBYTE:
return "ubyte";
case NC_USHORT:
return "ushort";
case NC_UINT:
return "uint";
case NC_INT64:
return "int64";
case NC_UINT64:
return "uint64";
case NC_STRING:
return "string";
default:
error("prim_type_name: bad type %d", type);
return "bogus";
}
}
static int max_type = 0;
static int max_atomic_type = 0;
static nctype_t **nctypes = 0; /* holds all types in a netCDF dataset */
#ifdef USE_NETCDF4
/* return number of user-defined types in a group and all its subgroups */
static int
count_udtypes(int ncid) {
int ntypes = 0;
int numgrps;
int *ncids;
int i;
int format;
NC_CHECK( nc_inq_format(ncid, &format) );
if (format == NC_FORMAT_NETCDF4) {
/* Get number of types in this group */
NC_CHECK( nc_inq_typeids(ncid, &ntypes, NULL) ) ;
NC_CHECK( nc_inq_grps(ncid, &numgrps, NULL) ) ;
ncids = (int *) emalloc(sizeof(int) * (size_t)(numgrps + 1));
NC_CHECK( nc_inq_grps(ncid, NULL, ncids) ) ;
/* Add number of types in each subgroup, if any */
for (i=0; i < numgrps; i++) {
ntypes += count_udtypes(ncids[i]);
}
free(ncids);
}
return ntypes;
}
#endif /*USE_NETCDF4*/
/* This routine really is intended to return the max atomic typeid */
static int
max_typeid(int ncid) {
int maxtypes = NC_NAT;
int maxatomictypes = NC_NAT;
int format = 0;
int err = NC_NOERR;
/* get the file type */
err = nc_inq_format(ncid,&format);
if(err) {
fprintf(stderr,"%s: Cannot get file format.\n",nc_strerror(err));
return 0;
}
switch (format) {
case NC_FORMAT_CLASSIC:
case NC_FORMAT_NETCDF4_CLASSIC:
case NC_FORMAT_64BIT_OFFSET:
maxatomictypes = (maxtypes = NC_DOUBLE); /*ignore NC_NAT?*/
break;
case NC_FORMAT_64BIT_DATA:
maxatomictypes = (maxtypes = NC_UINT64);
break;
case NC_FORMAT_NETCDF4:
#ifdef USE_NETCDF4
{
int nuser = 0;
maxatomictypes = (maxtypes = NC_STRING); /* extra netCDF-4 primitive types */
maxtypes += 4; /* user-defined classes */
nuser = count_udtypes(ncid);
if(nuser > 0)
maxtypes = NC_FIRSTUSERTYPEID + (nuser - 1);
} break;
#else
/* fallthru */
#endif
default:
fprintf(stderr,"Unexpected file format: %d\n",format);
return 0;
}
max_type = maxtypes;
max_atomic_type = maxatomictypes;
return maxtypes;
}
void typeadd(nctype_t *typep) {
nctypes[typep->tid] = typep;
}
/* From type id, get full type info */
nctype_t *
get_typeinfo ( int typeid ) {
if(typeid < 0 || typeid > max_type)
error("ncdump: %d is an invalid type id", typeid);
return nctypes[typeid];
}
/* void */
/* xfree_typeinfo(int ncid) { */
/* int i; */
/* for (i = 0; i < number_of_types; i++) { */
/* nctype_t *tinfop = nctypes[i]; */
/* if (tinfop) { */
/* if(tinfop->name) */
/* free(tinfop->name); */
/* if(tinfop->grps) */
/* free(tinfop->grps); */
/* free(tinfop); */
/* } */
/* } */
/* } */
bool_t
ncbyte_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
return ( *(signed char* )v1p == *(signed char* )v2p);
}
bool_t
ncchar_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
return ( *(char* )v1p == *(char* )v2p);
}
bool_t
ncshort_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
return ( *(short* )v1p == *(short* )v2p);
}
bool_t
ncint_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
return ( *(int* )v1p == *(int* )v2p);
}
#define absval(x) ( (x) < 0 ? -(x) : (x) )
/*
* Return ( *(float* )v1p == *(float* )v2p);
* except use floating epsilon to compare very close vals as equal
* and handle IEEE NaNs and infinities.
*/
bool_t
ncfloat_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
float v1 = *(float* )v1p;
float v2 = *(float* )v2p;
if((v1 > 0.0F) != (v2 > 0.0F)) /* avoid overflow */
return false;
if(isfinite(v1) && isfinite(v2))
return (absval(v1 - v2) <= absval(float_eps * v2)) ;
if(isnan(v1) && isnan(v2))
return true;
if(isinf(v1) && isinf(v2))
return true;
return false;
}
/*
* Return ( *(double* )v1p == *(double* )v2p);
* except use floating epsilon to compare very close vals as equal
* and handle IEEE NaNs and infinities.
*/
bool_t
ncdouble_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
double v1 = *(double* )v1p;
double v2 = *(double* )v2p;
if((v1 > 0.0) != (v2 > 0.0)) /* avoid overflow */
return false;
if(isfinite(v1) && isfinite(v2))
return (absval(v1 - v2) <= absval(double_eps * v2)) ;
if(isnan(v1) && isnan(v2))
return true;
if(isinf(v1) && isinf(v2))
return true;
return false;
}
bool_t
ncubyte_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
return ( *(unsigned char* )v1p == *(unsigned char* )v2p);
}
bool_t
ncushort_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
return ( *(unsigned short* )v1p == *(unsigned short* )v2p);
}
bool_t
ncuint_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
return ( *(unsigned int* )v1p == *(unsigned int* )v2p);
}
bool_t
ncint64_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
return ( *(long long* )v1p == *(long long* )v2p);
}
bool_t
ncuint64_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
return ( *(unsigned long long* )v1p == *(unsigned long long* )v2p);
}
bool_t
ncstring_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
if (NULL == *((char **)v1p) && NULL == *((char **)v2p))
return(1);
else if (NULL != *((char **)v1p) && NULL == *((char **)v2p))
return(0);
else if (NULL == *((char **)v1p) && NULL != *((char **)v2p))
return(0);
return (strcmp(*((char **)v1p), *((char **)v2p)) == 0);
}
#ifdef USE_NETCDF4
bool_t
ncopaque_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
size_t nbytes = this->size;
const char *c1p = (const char *) v1p;
const char *c2p = (const char *) v2p;
int i;
for (i=0; i < nbytes; i++) {
if (*c1p++ != *c2p++)
return false;
}
return true;
}
bool_t
ncvlen_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
size_t v1len = ((nc_vlen_t *)v1p)->len;
size_t v2len = ((nc_vlen_t *)v2p)->len;
if (v1len != v2len)
return false;
{
size_t base_size = this->size;
nc_type base_type = this->base_tid;
nctype_t *base_info = get_typeinfo(base_type);
val_equals_func base_val_equals = base_info->val_equals;
const char *v1dat = ((nc_vlen_t *)v1p)->p;
const char *v2dat = ((nc_vlen_t *)v2p)->p;
size_t i;
for(i = 0; i < v1len; i++) {
if (base_val_equals(base_info, (const void *)v1dat,
(const void *)v2dat) != true)
return false;
v1dat += base_size;
v2dat += base_size;
}
}
return true;
}
/* Determine if two compound values are equal, by testing equality of
* each member field. */
bool_t
nccomp_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
size_t nfields = this->nfields;
size_t fidx; /* field id */
for (fidx = 0; fidx < nfields; fidx++) {
size_t offset = this->offsets[fidx];
nc_type fid = this->fids[fidx]; /* field type id */
nctype_t *finfo = get_typeinfo(fid);
if(finfo->ranks == 0 || finfo->ranks[fidx] == 0) {
if(! finfo->val_equals(finfo,
(char *)v1p + offset, (char *)v2p + offset))
return false;
} else { /* this field is an array */
int i; /* array element counter when rank > 0 */
void *v1elem = (char *)v1p + offset;
void *v2elem = (char *)v2p + offset;
for(i = 0; i < finfo->nvals[fidx]; i++) {
if(! finfo->val_equals(finfo, v1elem, v2elem))
return false;
v1elem = (char *)v1elem + finfo->size;
v2elem = (char *)v1elem + finfo->size;
}
}
}
return true;
}
#endif /* USE_NETCDF4 */
int
ncbyte_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, typ->fmt, *(signed char *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncchar_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
char cstr[2];
int res;
cstr[0] = *(char*)valp;
cstr[1] = '\0';
res = snprintf(sout, PRIM_LEN, typ->fmt, cstr);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncshort_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, typ->fmt, *(short *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncint_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, typ->fmt, *(int *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
/* CDL canonical representations of some special floating point values */
#define NCDL_NANF "NaNf"
#define NCDL_NAN "NaN"
#define NCDL_INFF "Infinityf"
#define NCDL_INF "Infinity"
/* Convert a float NaN or Infinity to an allocated string large enough
* to hold it (at least PRIM_LEN chars) */
static void
float_special_tostring(float vv, char *sout) {
if(isnan(vv)) {
snprintf(sout, PRIM_LEN, "%s", NCDL_NANF);
} else if(isinf(vv)) {
if(vv < 0.0) {
snprintf(sout, PRIM_LEN, "-%s", NCDL_INFF);
} else {
snprintf(sout, PRIM_LEN, "%s", NCDL_INFF);
}
} else
assert(false); /* vv was finite */
}
/* Convert a double NaN or Infinity to an allocated string large enough
* to hold it (at least PRIM_LEN chars) */
static void
double_special_tostring(double vv, char *sout) {
if(isnan(vv)) {
snprintf(sout, PRIM_LEN, "%s", NCDL_NAN);
} else if(isinf(vv)) {
if(vv < 0.0) {
snprintf(sout, PRIM_LEN, "-%s", NCDL_INF);
} else {
snprintf(sout, PRIM_LEN, "%s", NCDL_INF);
}
} else
assert(false); /* vv was finite */
}
int
ncfloat_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
float vv = *(float *)valp;
if(isfinite(vv)) {
int res;
res = snprintf(sout, PRIM_LEN, typ->fmt, vv);
assert(res < PRIM_LEN);
} else {
float_special_tostring(vv, sout);
}
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncdouble_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
double vv = *(double *)valp;
if(isfinite(vv)) {
int res;
res = snprintf(sout, PRIM_LEN, typ->fmt, vv);
assert(res < PRIM_LEN);
} else {
double_special_tostring(vv, sout);
}
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncubyte_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, typ->fmt, *(unsigned char *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncushort_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, typ->fmt, *(unsigned short *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncuint_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, typ->fmt, *(unsigned int *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncint64_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, typ->fmt, *(long long *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncuint64_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, typ->fmt, *(unsigned long long *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int ncstring_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp)
{
const char *cp;
cp = ((char **)valp)[0];
if(cp) {
size_t slen;
char *sout;
char *sp;
unsigned char uc;
slen = 4 + 5 * strlen(cp); /* need "'s around string, and extra space to escape control characters */
slen++; /* nul term */
sout = emalloc(slen);
sp = sout;
*sp++ = '"' ;
while(*cp) {
switch (uc = (unsigned char)*cp++ & 0377) {
case '\b':
*sp++ = '\\';
*sp++ = 'b' ;
break;
case '\f':
*sp++ = '\\';
*sp++ = 'f';
break;
case '\n':
*sp++ = '\\';
*sp++ = 'n';
break;
case '\r':
*sp++ = '\\';
*sp++ = 'r';
break;
case '\t':
*sp++ = '\\';
*sp++ = 't';
break;
case '\v':
*sp++ = '\\';
*sp++ = 'n';
break;
case '\\':
*sp++ = '\\';
*sp++ = '\\';
break;
case '\'':
*sp++ = '\\';
*sp++ = '\'';
break;
case '\"':
*sp++ = '\\';
*sp++ = '\"';
break;
default:
if (iscntrl(uc)) {
snprintf(sp,4+1,"\\%03o",uc); /* +1 for nul */
sp += 4;
}
else
*sp++ = (char)uc;
break;
}
}
*sp++ = '"' ;
*sp = '\0' ;
sbuf_cpy(sfbf, sout);
free(sout);
}
else {
sbuf_cpy(sfbf, "NIL");
}
return sbuf_len(sfbf);
}
#ifdef USE_NETCDF4
int
ncenum_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char symbol[NC_MAX_NAME + 1];
long long val = 0;
switch (typ->base_tid) {
case NC_BYTE:
val = *(signed char *)valp;
break;
case NC_UBYTE:
val = *(unsigned char *)valp;
break;
case NC_SHORT:
val = *(short *)valp;
break;
case NC_USHORT:
val = *(unsigned short *)valp;
break;
case NC_INT:
val = *(int *)valp;
break;
case NC_UINT:
val = *(unsigned int *)valp;
break;
case NC_INT64:
val = *(long long *)valp;
break;
case NC_UINT64:
val = *(long long *)valp;
break;
default:
error("bad base type for enum");
break;
}
NC_CHECK( nc_inq_enum_ident(typ->ncid, typ->tid, val, symbol));
sbuf_cpy(sfbf, symbol);
return sbuf_len(sfbf);
}
/* Given an opaque type size and opaque value, convert to a string,
* represented as hexadecimal characters, returning number of chars in
* output string */
int
ncopaque_val_as_hex(size_t size, char *sout, const void *valp) {
const unsigned char *cp = valp;
char *sp = sout;
int i;
char *prefix = "0X";
size_t prelen = strlen(prefix);
snprintf(sp, prelen + 1, "%s", prefix);
sp += prelen;
for(i = 0; i < size; i++) {
int res;
res = snprintf(sp, prelen + 1, "%.2X", *cp++);
assert (res == 2);
sp += 2;
}
*sp = '\0';
return (int)(2*size + prelen);
}
/* Convert an opaque value to a string, represented as hexadecimal
* characters */
int
ncopaque_typ_tostring(const nctype_t *typ, safebuf_t *sfbf,
const void *valp) {
char* sout = (char *) emalloc(2 * typ->size + strlen("0X") + 1);
(void) ncopaque_val_as_hex(typ->size, sout, valp);
sbuf_cpy(sfbf, sout);
free(sout);
return sbuf_len(sfbf);
}
/* Convert a vlen value to a string, by using tostring function for base type */
int
ncvlen_typ_tostring(const nctype_t *tinfo, safebuf_t *sfbf, const void *valp) {