int NC3_inq_dim(int ncid, int dimid, char *name, size_t *sizep) { int status; NC *nc; NC3_INFO* ncp; NC_dim *dimp; status = NC_check_id(ncid, &nc); if(status != NC_NOERR) return status; ncp = NC3_DATA(nc); dimp = elem_NC_dimarray(&ncp->dims, (size_t)dimid); if(dimp == NULL) return NC_EBADDIM; if(name != NULL) { (void)strncpy(name, dimp->name->cp, dimp->name->nchars); name[dimp->name->nchars] = 0; } if(sizep != NULL) { if(dimp->size == NC_UNLIMITED) *sizep = NC_get_numrecs(ncp); else *sizep = dimp->size; } return NC_NOERR; }
int CALLCONVENTION nc_inq_dimlen(int ncid, int dimid, size_t *lenp) { int status; NC *ncp; NC_dim *dimp; status = NC_check_id(ncid, &ncp); if(status != NC_NOERR) return status; dimp = elem_NC_dimarray(&ncp->dims, (size_t)dimid); if(dimp == NULL) return NC_EBADDIM; if(lenp != 0) { if(dimp->size == NC_UNLIMITED) *lenp = NC_get_numrecs(ncp); else *lenp = dimp->size; } return NC_NOERR; }
int CALLCONVENTION nc_inq_dimname(int ncid, int dimid, char *name) { int status; NC *ncp; NC_dim *dimp; status = NC_check_id(ncid, &ncp); if(status != NC_NOERR) return status; dimp = elem_NC_dimarray(&ncp->dims, (size_t)dimid); if(dimp == NULL) return NC_EBADDIM; if(name != NULL) { (void)strncpy(name, dimp->name->cp, dimp->name->nchars); name[dimp->name->nchars] = 0; } return NC_NOERR; }
int NC3_rename_dim( int ncid, int dimid, const char *unewname) { int status; NC *nc; NC3_INFO* ncp; int existid; NC_dim *dimp; char *newname; /* normalized */ status = NC_check_id(ncid, &nc); if(status != NC_NOERR) return status; ncp = NC3_DATA(nc); if(NC_readonly(ncp)) return NC_EPERM; status = NC_check_name(unewname); if(status != NC_NOERR) return status; existid = NC_finddim(&ncp->dims, unewname, &dimp); if(existid != -1) return NC_ENAMEINUSE; dimp = elem_NC_dimarray(&ncp->dims, (size_t)dimid); if(dimp == NULL) return NC_EBADDIM; NC_string *old = dimp->name; newname = (char *)utf8proc_NFC((const unsigned char *)unewname); if(newname == NULL) return NC_ENOMEM; if(NC_indef(ncp)) { NC_string *newStr = new_NC_string(strlen(newname), newname); free(newname); if(newStr == NULL) return NC_ENOMEM; dimp->name = newStr; /* Remove old name from hashmap; add new... */ NC_hashmapRemoveDim(&ncp->dims, old->cp); NC_hashmapAddDim(&ncp->dims, dimid, newStr->cp); free_NC_string(old); return NC_NOERR; } /* else, not in define mode */ status = set_NC_string(dimp->name, newname); free(newname); if(status != NC_NOERR) return status; /* Remove old name from hashmap; add new... */ NC_hashmapRemoveDim(&ncp->dims, old->cp); NC_hashmapAddDim(&ncp->dims, dimid, dimp->name->cp); set_NC_hdirty(ncp); if(NC_doHsync(ncp)) { status = NC_sync(ncp); if(status != NC_NOERR) return status; } return NC_NOERR; }
/* * 'compile' the shape and len of a variable * Formerly NC_var_shape(var, dims) */ int NC_var_shape(NC_var *varp, const NC_dimarray *dims) { size_t *shp, *op; off_t *dsp; int *ip; const NC_dim *dimp; off_t product = 1; varp->xsz = ncx_szof(varp->type); if(varp->ndims == 0) { goto out; } /* * use the user supplied dimension indices * to determine the shape */ for(ip = varp->dimids, op = varp->shape ; ip < &varp->dimids[varp->ndims]; ip++, op++) { if(*ip < 0 || (size_t) (*ip) >= ((dims != NULL) ? dims->nelems : 1) ) return NC_EBADDIM; dimp = elem_NC_dimarray(dims, (size_t)*ip); *op = dimp->size; if(*op == NC_UNLIMITED && ip != varp->dimids) return NC_EUNLIMPOS; } /* * Compute the dsizes */ /* ndims is > 0 here */ for(shp = varp->shape + varp->ndims -1, dsp = varp->dsizes + varp->ndims -1; shp >= varp->shape; shp--, dsp--) { if(!(shp == varp->shape && IS_RECVAR(varp))) { if( (off_t)(*shp) <= OFF_T_MAX / product ) { product *= *shp; } else { product = OFF_T_MAX ; } } *dsp = product; } out : if( varp->xsz <= (X_UINT_MAX - 1) / product ) /* if integer multiply will not overflow */ { varp->len = product * varp->xsz; switch(varp->type) { case NC_BYTE : case NC_CHAR : case NC_SHORT : if( varp->len%4 != 0 ) { varp->len += 4 - varp->len%4; /* round up */ /* *dsp += 4 - *dsp%4; */ } break; default: /* already aligned */ break; } } else { /* OK for last var to be "too big", indicated by this special len */ varp->len = X_UINT_MAX; } #if 0 arrayp("\tshape", varp->ndims, varp->shape); arrayp("\tdsizes", varp->ndims, varp->dsizes); #endif return NC_NOERR; }
int NC3_rename_dim( int ncid, int dimid, const char *unewname) { int status = NC_NOERR; NC *nc; NC3_INFO* ncp; int existid; NC_dim *dimp; char *newname = NULL; /* normalized */ NC_string *old = NULL; uintptr_t intdata; status = NC_check_id(ncid, &nc); if(status != NC_NOERR) goto done; ncp = NC3_DATA(nc); if(NC_readonly(ncp)) {status = NC_EPERM; goto done;} status = NC_check_name(unewname); if(status != NC_NOERR) goto done; existid = NC_finddim(&ncp->dims, unewname, &dimp); if(existid != -1) {status = NC_ENAMEINUSE; goto done;} dimp = elem_NC_dimarray(&ncp->dims, (size_t)dimid); if(dimp == NULL) {status = NC_EBADDIM; goto done;} old = dimp->name; status = nc_utf8_normalize((const unsigned char *)unewname,(unsigned char **)&newname); if(status != NC_NOERR) goto done; if(NC_indef(ncp)) { NC_string *newStr = new_NC_string(strlen(newname), newname); if(newStr == NULL) {status = NC_ENOMEM; goto done;} /* Remove old name from hashmap; add new... */ NC_hashmapremove(ncp->dims.hashmap, old->cp, strlen(old->cp), NULL); dimp->name = newStr; intdata = dimid; NC_hashmapadd(ncp->dims.hashmap, intdata, newStr->cp, strlen(newStr->cp)); free_NC_string(old); goto done; } /* else, not in define mode */ /* If new name is longer than old, then complain, but otherwise, no change (test is same as set_NC_string)*/ if(dimp->name->nchars < strlen(newname)) { {status = NC_ENOTINDEFINE; goto done;} } /* Remove old name from hashmap; add new... */ /* WARNING: strlen(NC_string.cp) may be less than NC_string.nchars */ NC_hashmapremove(ncp->dims.hashmap, old->cp, strlen(old->cp), NULL); /* WARNING: strlen(NC_string.cp) may be less than NC_string.nchars */ status = set_NC_string(dimp->name, newname); if(status != NC_NOERR) goto done; intdata = (uintptr_t)dimid; NC_hashmapadd(ncp->dims.hashmap, intdata, dimp->name->cp, strlen(dimp->name->cp)); set_NC_hdirty(ncp); if(NC_doHsync(ncp)) { status = NC_sync(ncp); if(status != NC_NOERR) goto done; } done: if(newname) free(newname); return status; }
/* * 'compile' the shape and len of a variable * Formerly NC_var_shape(var, dims) */ int NC_var_shape(NC_var *varp, const NC_dimarray *dims) { size_t *shp, *dsp, *op; int *ip; const NC_dim *dimp; size_t product = 1; varp->xsz = ncx_szof(varp->type); if(varp->ndims == 0) { goto out; } /* * use the user supplied dimension indices * to determine the shape */ for(ip = varp->dimids, op = varp->shape ; ip < &varp->dimids[varp->ndims]; ip++, op++) { if(*ip < 0 || (size_t) (*ip) >= ((dims != NULL) ? dims->nelems : 1) ) return NC_EBADDIM; dimp = elem_NC_dimarray(dims, (size_t)*ip); *op = dimp->size; if(*op == NC_UNLIMITED && ip != varp->dimids) return NC_EUNLIMPOS; } /* * Compute the dsizes */ /* ndims is > 0 here */ for(shp = varp->shape + varp->ndims -1, dsp = varp->dsizes + varp->ndims -1; shp >= varp->shape; shp--, dsp--) { if(!(shp == varp->shape && IS_RECVAR(varp))) product *= *shp; *dsp = product; } out : varp->len = product * varp->xsz; switch(varp->type) { case NC_BYTE : case NC_CHAR : case NC_SHORT : if( varp->len%4 != 0 ) { varp->len += 4 - varp->len%4; /* round up */ /* *dsp += 4 - *dsp%4; */ } break; default: /* already aligned */ break; } #if 0 arrayp("\tshape", varp->ndims, varp->shape); arrayp("\tdsizes", varp->ndims, varp->dsizes); #endif return NC_NOERR; }
/* * 'compile' the shape and len of a variable * Formerly NC_var_shape(var, dims) */ int NC_var_shape(NC_var *varp, const NC_dimarray *dims) { size_t *shp, *op; off_t *dsp; int *ip = NULL; const NC_dim *dimp; off_t product = 1; varp->xsz = ncx_szof(varp->type); if(varp->ndims == 0 || varp->dimids == NULL) { goto out; } /* * use the user supplied dimension indices * to determine the shape */ for(ip = varp->dimids, op = varp->shape ; ip < &varp->dimids[varp->ndims]; ip++, op++) { if(*ip < 0 || (size_t) (*ip) >= ((dims != NULL) ? dims->nelems : 1) ) return NC_EBADDIM; dimp = elem_NC_dimarray(dims, (size_t)*ip); *op = dimp->size; if(*op == NC_UNLIMITED && ip != varp->dimids) return NC_EUNLIMPOS; } /* * Compute the dsizes */ /* ndims is > 0 here */ for(shp = varp->shape + varp->ndims -1, dsp = varp->dsizes + varp->ndims -1; shp >= varp->shape; shp--, dsp--) { /*if(!(shp == varp->shape && IS_RECVAR(varp)))*/ if( shp != NULL && (shp != varp->shape || !IS_RECVAR(varp))) { if( ((off_t)(*shp)) <= OFF_T_MAX / product ) { product *= (*shp > 0 ? *shp : 1); } else { product = OFF_T_MAX ; } } *dsp = product; } out : /* No variable size can be > X_INT64_MAX - 3 */ if (0 == NC_check_vlen(varp, X_INT64_MAX-3)) return NC_EVARSIZE; /* * For CDF-1 and CDF-2 formats, the total number of array elements * cannot exceed 2^32, unless this variable is the last fixed-size * variable, there is no record variable, and the file starting * offset of this variable is less than 2GiB. * This will be checked in NC_check_vlens() during NC_endef() */ varp->len = product * varp->xsz; if (varp->len % 4 > 0) varp->len += 4 - varp->len % 4; /* round up */ #if 0 arrayp("\tshape", varp->ndims, varp->shape); arrayp("\tdsizes", varp->ndims, varp->dsizes); #endif return NC_NOERR; }