2
dnl Process using m4 to produce 'C' language file.
4
dnl If you see this line, you can ignore the next one.
5
/* Do not edit this file. It is produced from the corresponding .m4 source */
8
* Copyright 1996, University Corporation for Atmospheric Research
9
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
11
/* $Id: attr.m4,v 1.1 2010-01-06 16:34:05 kwleiter Exp $ */
21
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
22
# pragma warning ( disable : 4130 ) /* logical operation on address of string constant */
23
#endif /* defined(_MSC_VER) && (_MSC_VER >= 1400) */
31
free_NC_attr(NC_attr *attrp)
36
free_NC_string(attrp->name);
42
* How much space will 'nelems' of 'type' take in
43
* external representation (as the values of an attribute)?
46
ncx_len_NC_attrV(nc_type type, size_t nelems)
51
return ncx_len_char(nelems);
53
return ncx_len_short(nelems);
55
return ncx_len_int(nelems);
57
return ncx_len_float(nelems);
59
return ncx_len_double(nelems);
61
break; /* Some compilers complain if enums are missing from a switch */
64
assert("ncx_len_NC_attr bad type" == 0);
76
const size_t xsz = ncx_len_NC_attrV(type, nelems);
77
size_t sz = M_RNDUP(sizeof(NC_attr));
79
assert(!(xsz == 0 && nelems != 0));
83
attrp = (NC_attr *) malloc(sz);
91
attrp->nelems = nelems;
93
attrp->xvalue = (char *)attrp + M_RNDUP(sizeof(NC_attr));
103
NC_new_attr(name,type,count,value)
114
assert(name != NULL && *name != 0);
116
strp = new_NC_string(strlen(name), name);
120
attrp = new_x_NC_attr(strp, type, nelems);
123
free_NC_string(strp);
132
dup_NC_attr(const NC_attr *rattrp)
134
NC_attr *attrp = new_NC_attr(rattrp->name->cp,
135
rattrp->type, rattrp->nelems);
138
(void) memcpy(attrp->xvalue, rattrp->xvalue, rattrp->xsz);
145
* Free the stuff "in" (referred to by) an NC_attrarray.
146
* Leaves the array itself allocated.
149
free_NC_attrarrayV0(NC_attrarray *ncap)
151
assert(ncap != NULL);
153
if(ncap->nelems == 0)
156
assert(ncap->value != NULL);
159
NC_attr **app = ncap->value;
160
NC_attr *const *const end = &app[ncap->nelems];
161
for( /*NADA*/; app < end; app++)
172
* Free NC_attrarray values.
177
free_NC_attrarrayV(NC_attrarray *ncap)
179
assert(ncap != NULL);
181
if(ncap->nalloc == 0)
184
assert(ncap->value != NULL);
186
free_NC_attrarrayV0(ncap);
195
dup_NC_attrarrayV(NC_attrarray *ncap, const NC_attrarray *ref)
197
int status = NC_NOERR;
200
assert(ncap != NULL);
204
const size_t sz = ref->nelems * sizeof(NC_attr *);
205
ncap->value = (NC_attr **) malloc(sz);
206
if(ncap->value == NULL)
209
(void) memset(ncap->value, 0, sz);
210
ncap->nalloc = ref->nelems;
215
NC_attr **app = ncap->value;
216
const NC_attr **drpp = (const NC_attr **)ref->value;
217
NC_attr *const *const end = &app[ref->nelems];
218
for( /*NADA*/; app < end; drpp++, app++, ncap->nelems++)
220
*app = dup_NC_attr(*drpp);
229
if(status != NC_NOERR)
231
free_NC_attrarrayV(ncap);
235
assert(ncap->nelems == ref->nelems);
242
* Add a new handle on the end of an array of handles
244
NC_incr_array(array, tail)
247
incr_NC_attrarray(NC_attrarray *ncap, NC_attr *newelemp)
251
assert(ncap != NULL);
253
if(ncap->nalloc == 0)
255
assert(ncap->nelems == 0);
256
vp = (NC_attr **) malloc(NC_ARRAY_GROWBY * sizeof(NC_attr *));
261
ncap->nalloc = NC_ARRAY_GROWBY;
263
else if(ncap->nelems +1 > ncap->nalloc)
265
vp = (NC_attr **) realloc(ncap->value,
266
(ncap->nalloc + NC_ARRAY_GROWBY) * sizeof(NC_attr *));
271
ncap->nalloc += NC_ARRAY_GROWBY;
276
ncap->value[ncap->nelems] = newelemp;
284
elem_NC_attrarray(const NC_attrarray *ncap, size_t elem)
286
assert(ncap != NULL);
287
/* cast needed for braindead systems with signed size_t */
288
if(ncap->nelems == 0 || (unsigned long) elem >= ncap->nelems)
291
assert(ncap->value != NULL);
293
return ncap->value[elem];
296
/* End attarray per se */
299
* Given ncp and varid, return ptr to array of attributes
302
static NC_attrarray *
303
NC_attrarray0( NC *ncp, int varid)
307
if(varid == NC_GLOBAL) /* Global attribute, attach to cdf */
311
else if(varid >= 0 && (size_t) varid < ncp->vars.nelems)
314
vpp = (NC_var **)ncp->vars.value;
325
* Step thru NC_ATTRIBUTE array, seeking match on name.
326
* return match or NULL if Not Found.
329
NC_findattr(const NC_attrarray *ncap, const char *name)
335
assert(ncap != NULL);
337
if(ncap->nelems == 0)
340
attrpp = (NC_attr **) ncap->value;
344
for(attrid = 0; attrid < ncap->nelems; attrid++, attrpp++)
346
if(strlen((*attrpp)->name->cp) == slen &&
347
strncmp((*attrpp)->name->cp, name, slen) == 0)
349
return(attrpp); /* Normal return */
357
* Look up by ncid, varid and name, return NULL if not found
360
NC_lookupattr(int ncid,
362
const char *name, /* attribute name */
363
NC_attr **attrpp) /* modified on return */
370
status = NC_check_id(ncid, &ncp);
371
if(status != NC_NOERR)
374
ncap = NC_attrarray0(ncp, varid);
378
tmp = NC_findattr(ncap, name);
391
nc_inq_attname(int ncid, int varid, int attnum, char *name)
398
status = NC_check_id(ncid, &ncp);
399
if(status != NC_NOERR)
402
ncap = NC_attrarray0(ncp, varid);
406
attrp = elem_NC_attrarray(ncap, (size_t)attnum);
410
(void) strncpy(name, attrp->name->cp, attrp->name->nchars);
411
name[attrp->name->nchars] = 0;
418
nc_inq_attid(int ncid, int varid, const char *name, int *attnump)
425
status = NC_check_id(ncid, &ncp);
426
if(status != NC_NOERR)
429
ncap = NC_attrarray0(ncp, varid);
434
attrpp = NC_findattr(ncap, name);
439
*attnump = (int)(attrpp - ncap->value);
445
nc_inq_atttype(int ncid, int varid, const char *name, nc_type *datatypep)
450
status = NC_lookupattr(ncid, varid, name, &attrp);
451
if(status != NC_NOERR)
454
if(datatypep != NULL)
455
*datatypep = attrp->type;
461
nc_inq_attlen(int ncid, int varid, const char *name, size_t *lenp)
466
status = NC_lookupattr(ncid, varid, name, &attrp);
467
if(status != NC_NOERR)
471
*lenp = attrp->nelems;
479
const char *name, /* input, attribute name */
486
status = NC_lookupattr(ncid, varid, name, &attrp);
487
if(status != NC_NOERR)
490
if(datatypep != NULL)
491
*datatypep = attrp->type;
493
*lenp = attrp->nelems;
500
nc_rename_att( int ncid, int varid, const char *name, const char *newname)
507
NC_string *newStr, *old;
509
/* sortof inline clone of NC_lookupattr() */
510
status = NC_check_id(ncid, &ncp);
511
if(status != NC_NOERR)
517
ncap = NC_attrarray0(ncp, varid);
521
status = NC_check_name(newname);
522
if(status != NC_NOERR)
525
tmp = NC_findattr(ncap, name);
529
/* end inline clone NC_lookupattr() */
531
if(NC_findattr(ncap, newname) != NULL)
534
return NC_ENAMEINUSE;
540
newStr = new_NC_string(strlen(newname), newname);
543
attrp->name = newStr;
548
status = set_NC_string(old, newname);
549
if( status != NC_NOERR)
556
status = NC_sync(ncp);
557
if(status != NC_NOERR)
566
nc_copy_att(int ncid_in, int varid_in, const char *name, int ncid_out, int ovarid)
576
status = NC_lookupattr(ncid_in, varid_in, name, &iattrp);
577
if(status != NC_NOERR)
580
status = NC_check_id(ncid_out, &ncp);
581
if(status != NC_NOERR)
587
ncap = NC_attrarray0(ncp, ovarid);
591
attrpp = NC_findattr(ncap, name);
592
if(attrpp != NULL) /* name in use */
596
attrp = *attrpp; /* convenience */
598
if(iattrp->xsz > attrp->xsz)
599
return NC_ENOTINDEFINE;
600
/* else, we can reuse existing without redef */
602
attrp->xsz = iattrp->xsz;
603
attrp->type = iattrp->type;
604
attrp->nelems = iattrp->nelems;
606
(void) memcpy(attrp->xvalue, iattrp->xvalue,
613
status = NC_sync(ncp);
614
if(status != NC_NOERR)
620
/* else, redefine using existing array slot */
626
return NC_ENOTINDEFINE;
628
if(ncap->nelems >= NC_MAX_ATTRS)
632
attrp = new_NC_attr(name, iattrp->type, iattrp->nelems);
636
(void) memcpy(attrp->xvalue, iattrp->xvalue,
647
status = incr_NC_attrarray(ncap, attrp);
648
if(status != NC_NOERR)
660
nc_del_att(int ncid, int varid, const char *name)
670
status = NC_check_id(ncid, &ncp);
671
if(status != NC_NOERR)
675
return NC_ENOTINDEFINE;
677
ncap = NC_attrarray0(ncp, varid);
681
/* sortof inline NC_findattr() */
684
attrpp = (NC_attr **) ncap->value;
685
for(attrid = 0; (size_t) attrid < ncap->nelems; attrid++, attrpp++)
687
if( slen == (*attrpp)->name->nchars &&
688
strncmp(name, (*attrpp)->name->cp, slen) == 0)
694
if( (size_t) attrid == ncap->nelems )
696
/* end inline NC_findattr() */
699
for(attrid++; (size_t) attrid < ncap->nelems; attrid++)
701
*attrpp = *(attrpp + 1);
705
/* decrement count */
714
dnl XNCX_PAD_PUTN(Type)
716
define(`XNCX_PAD_PUTN',dnl
719
ncx_pad_putn_I$1(void **xpp, size_t nelems, const $1 *tp, nc_type type)
725
return ncx_pad_putn_schar_$1(xpp, nelems, tp);
727
return ncx_pad_putn_short_$1(xpp, nelems, tp);
729
return ncx_putn_int_$1(xpp, nelems, tp);
731
return ncx_putn_float_$1(xpp, nelems, tp);
733
return ncx_putn_double_$1(xpp, nelems, tp);
735
break; /* Some compilers complain if enums are missing from a switch */
737
assert("ncx_pad_putn_I$1 invalid type" == 0);
742
dnl XNCX_PAD_GETN(Type)
744
define(`XNCX_PAD_GETN',dnl
747
ncx_pad_getn_I$1(const void **xpp, size_t nelems, $1 *tp, nc_type type)
753
return ncx_pad_getn_schar_$1(xpp, nelems, tp);
755
return ncx_pad_getn_short_$1(xpp, nelems, tp);
757
return ncx_getn_int_$1(xpp, nelems, tp);
759
return ncx_getn_float_$1(xpp, nelems, tp);
761
return ncx_getn_double_$1(xpp, nelems, tp);
763
break; /* Some compilers complain if enums are missing from a switch */
765
assert("ncx_pad_getn_I$1 invalid type" == 0);
789
XNCX_PAD_PUTN(double)
790
XNCX_PAD_GETN(double)
794
nc_put_att_text(int ncid, int varid, const char *name,
795
size_t nelems, const char *value)
804
status = NC_check_id(ncid, &ncp);
805
if(status != NC_NOERR)
811
ncap = NC_attrarray0(ncp, varid);
815
status = NC_check_name(name);
816
if(status != NC_NOERR)
819
/* cast needed for braindead systems with signed size_t */
820
if((unsigned long) nelems > X_INT_MAX) /* backward compat */
821
return NC_EINVAL; /* Invalid nelems */
823
if(nelems != 0 && value == NULL)
824
return NC_EINVAL; /* Null arg */
826
attrpp = NC_findattr(ncap, name);
827
if(attrpp != NULL) /* name in use */
831
const size_t xsz = ncx_len_NC_attrV(NC_CHAR, nelems);
832
attrp = *attrpp; /* convenience */
835
return NC_ENOTINDEFINE;
836
/* else, we can reuse existing without redef */
839
attrp->type = NC_CHAR;
840
attrp->nelems = nelems;
844
void *xp = attrp->xvalue;
845
status = ncx_pad_putn_text(&xp, nelems, value);
846
if(status != NC_NOERR)
854
status = NC_sync(ncp);
855
if(status != NC_NOERR)
861
/* else, redefine using existing array slot */
867
return NC_ENOTINDEFINE;
869
if(ncap->nelems >= NC_MAX_ATTRS)
873
attrp = new_NC_attr(name, NC_CHAR, nelems);
879
void *xp = attrp->xvalue;
880
status = ncx_pad_putn_text(&xp, nelems, value);
881
if(status != NC_NOERR)
893
status = incr_NC_attrarray(ncap, attrp);
894
if(status != NC_NOERR)
906
nc_get_att_text(int ncid, int varid, const char *name, char *str)
911
status = NC_lookupattr(ncid, varid, name, &attrp);
912
if(status != NC_NOERR)
915
if(attrp->nelems == 0)
918
if(attrp->type != NC_CHAR)
923
const void *xp = attrp->xvalue;
924
return ncx_pad_getn_text(&xp, attrp->nelems, str);
930
dnl NC_PUT_ATT(Abbrv, Type)
932
define(`NC_PUT_ATT',dnl
935
nc_put_att_$1(int ncid, int varid, const char *name,
936
nc_type type, size_t nelems, const $2 *value)
945
status = NC_check_id(ncid, &ncp);
946
if(status != NC_NOERR)
952
ncap = NC_attrarray0(ncp, varid);
956
status = nc_cktype(type);
957
if(status != NC_NOERR)
963
/* cast needed for braindead systems with signed size_t */
964
if((unsigned long) nelems > X_INT_MAX) /* backward compat */
965
return NC_EINVAL; /* Invalid nelems */
967
if(nelems != 0 && value == NULL)
968
return NC_EINVAL; /* Null arg */
970
attrpp = NC_findattr(ncap, name);
971
if(attrpp != NULL) /* name in use */
975
const size_t xsz = ncx_len_NC_attrV(type, nelems);
976
attrp = *attrpp; /* convenience */
979
return NC_ENOTINDEFINE;
980
/* else, we can reuse existing without redef */
984
attrp->nelems = nelems;
988
void *xp = attrp->xvalue;
989
status = ncx_pad_putn_I$1(&xp, nelems,
997
const int lstatus = NC_sync(ncp);
999
* N.B.: potentially overrides NC_ERANGE
1000
* set by ncx_pad_putn_I$1
1002
if(lstatus != ENOERR)
1008
/* else, redefine using existing array slot */
1014
return NC_ENOTINDEFINE;
1016
if(ncap->nelems >= NC_MAX_ATTRS)
1020
status = NC_check_name(name);
1021
if(status != NC_NOERR)
1024
attrp = new_NC_attr(name, type, nelems);
1030
void *xp = attrp->xvalue;
1031
status = ncx_pad_putn_I$1(&xp, nelems,
1037
assert(old != NULL);
1043
const int lstatus = incr_NC_attrarray(ncap, attrp);
1045
* N.B.: potentially overrides NC_ERANGE
1046
* set by ncx_pad_putn_I$1
1048
if(lstatus != NC_NOERR)
1050
free_NC_attr(attrp);
1059
dnl NC_GET_ATT(Abbrv, Type)
1061
define(`NC_GET_ATT',dnl
1064
nc_get_att_$1(int ncid, int varid, const char *name, $2 *tp)
1069
status = NC_lookupattr(ncid, varid, name, &attrp);
1070
if(status != NC_NOERR)
1073
if(attrp->nelems == 0)
1076
if(attrp->type == NC_CHAR)
1080
const void *xp = attrp->xvalue;
1081
return ncx_pad_getn_I$1(&xp, attrp->nelems, tp, attrp->type);
1087
NC_PUT_ATT(schar, signed char)
1088
NC_GET_ATT(schar, signed char)
1090
NC_PUT_ATT(uchar, unsigned char)
1091
NC_GET_ATT(uchar, unsigned char)
1093
NC_PUT_ATT(short, short)
1094
NC_GET_ATT(short, short)
1096
NC_PUT_ATT(int, int)
1097
NC_GET_ATT(int, int)
1099
NC_PUT_ATT(long, long)
1100
NC_GET_ATT(long, long)
1102
NC_PUT_ATT(float, float)
1103
NC_GET_ATT(float, float)
1105
NC_PUT_ATT(double, double)
1106
NC_GET_ATT(double, double)