/* * Step thru NC_VARIABLE array, seeking match on name. * Return varid or -1 on not found. * *varpp is set to the appropriate NC_var. * Formerly (sort of) NC_hvarid */ int NC_findvar(const NC_vararray *ncap, const char *uname, NC_var **varpp) { NC_var **loc; uint32_t shash; int varid; char *name; assert(ncap != NULL); if(ncap->nelems == 0) return -1; loc = (NC_var **) ncap->value; /* normalized version of uname */ name = (char *)utf8proc_NFC((const unsigned char *)uname); if(name == NULL) return NC_ENOMEM; shash = hash_fast(name, strlen(name)); for(varid = 0; (size_t) varid < ncap->nelems; varid++, loc++) { if((*loc)->hash == shash && strncmp((*loc)->name->cp, name, strlen(name)) == 0) { if(varpp != NULL) *varpp = *loc; free(name); return(varid); /* Normal return */ } } free(name); return(-1); /* not found */ }
/* * Common code for new_NC_var() * and ncx_get_NC_var() */ NC_var * new_x_NC_var( NC_string *strp, size_t ndims) { NC_var *varp; const size_t o1 = M_RNDUP(ndims * sizeof(int)); const size_t o2 = M_RNDUP(ndims * sizeof(size_t)); #ifdef MALLOCHACK const size_t sz = M_RNDUP(sizeof(NC_var)) + o1 + o2 + ndims * sizeof(off_t); #else /*!MALLOCHACK*/ const size_t o3 = ndims * sizeof(off_t); const size_t sz = sizeof(NC_var); #endif /*!MALLOCHACK*/ varp = (NC_var *) malloc(sz); if(varp == NULL ) return NULL; (void) memset(varp, 0, sz); varp->name = strp; varp->ndims = ndims; varp->hash = hash_fast(strp->cp, strlen(strp->cp)); if(ndims != 0) { #ifdef MALLOCHACK /* * NOTE: lint may complain about the next 3 lines: * "pointer cast may result in improper alignment". * We use the M_RNDUP() macro to get the proper alignment. */ varp->dimids = (int *)((char *)varp + M_RNDUP(sizeof(NC_var))); varp->shape = (size_t *)((char *)varp->dimids + o1); varp->dsizes = (off_t *)((char *)varp->shape + o2); #else /*!MALLOCHACK*/ varp->dimids = (int*)malloc(o1); varp->shape = (size_t*)malloc(o2); if(varp->shape) { memset(varp->shape,0,o2); } varp->dsizes = (off_t*)malloc(o3); #endif /*!MALLOCHACK*/ } else { varp->dimids = NULL; varp->shape = NULL; varp->dsizes=NULL; } varp->xsz = 0; varp->len = 0; varp->begin = 0; return varp; }
NC_dim * new_x_NC_dim(NC_string *name) { NC_dim *dimp; dimp = (NC_dim *) malloc(sizeof(NC_dim)); if(dimp == NULL) return NULL; dimp->name = name; dimp->hash = hash_fast(name->cp, strlen(name->cp)); dimp->size = 0; return(dimp); }
/* * Step thru NC_DIMENSION array, seeking match on uname. * Return dimid or -1 on not found. * *dimpp is set to the appropriate NC_dim. * The loop structure is odd. In order to parallelize, * we moved a clearer 'break' inside the loop body to the loop test. */ static int NC_finddim(const NC_dimarray *ncap, const char *uname, NC_dim **dimpp) { int dimid; uint32_t shash; NC_dim ** loc; char *name; assert(ncap != NULL); if(ncap->nelems == 0) return -1; { dimid = 0; loc = (NC_dim **) ncap->value; /* normalized version of uname */ name = (char *)utf8proc_NFC((const unsigned char *)uname); if(name == NULL) return NC_ENOMEM; shash = hash_fast(name, strlen(name)); for(; (size_t) dimid < ncap->nelems && ((*loc)->hash != shash || strncmp((*loc)->name->cp, name, strlen(name)) != 0); dimid++, loc++) { /*EMPTY*/ } free(name); if(dimid >= ncap->nelems) return(-1); /* not found */ /* else, normal return */ if(dimpp != NULL) *dimpp = *loc; return(dimid); } }
int NC3_rename_var(int ncid, int varid, const char *unewname) { int status; NC *nc; NC3_INFO* ncp; NC_var *varp; NC_string *old, *newStr; int other; 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; /* check for name in use */ other = NC_findvar(&ncp->vars, unewname, &varp); if(other != -1) { return NC_ENAMEINUSE; } varp = NC_lookupvar(ncp, varid); if(varp == NULL) { /* invalid varid */ return NC_ENOTVAR; /* TODO: is this the right error code? */ } old = varp->name; newname = (char *)utf8proc_NFC((const unsigned char *)unewname); if(newname == NULL) return NC_ENOMEM; if(NC_indef(ncp)) { newStr = new_NC_string(strlen(newname),newname); free(newname); if(newStr == NULL) return(-1); varp->name = newStr; varp->hash = hash_fast(newStr->cp, strlen(newStr->cp)); free_NC_string(old); return NC_NOERR; } /* else, not in define mode */ status = set_NC_string(varp->name, newname); varp->hash = hash_fast(newname, strlen(newname)); free(newname); if(status != NC_NOERR) return status; set_NC_hdirty(ncp); if(NC_doHsync(ncp)) { status = NC_sync(ncp); if(status != NC_NOERR) return status; } return NC_NOERR; }
int NC3_rename_dim( int ncid, int dimid, const char *unewname) { int status; NC *ncp; int existid; NC_dim *dimp; char *newname; /* normalized */ status = NC_check_id(ncid, &ncp); if(status != NC_NOERR) return status; 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; newname = (char *)utf8proc_NFC((const unsigned char *)unewname); if(newname == NULL) return NC_ENOMEM; if(NC_indef(ncp)) { NC_string *old = dimp->name; NC_string *newStr = new_NC_string(strlen(newname), newname); free(newname); if(newStr == NULL) return NC_ENOMEM; dimp->name = newStr; dimp->hash = hash_fast(newStr->cp, strlen(newStr->cp)); free_NC_string(old); return NC_NOERR; } /* else, not in define mode */ status = set_NC_string(dimp->name, newname); dimp->hash = hash_fast(newname, strlen(newname)); free(newname); if(status != NC_NOERR) return status; set_NC_hdirty(ncp); if(NC_doHsync(ncp)) { status = NC_sync(ncp); if(status != NC_NOERR) return status; } return NC_NOERR; }