/* * Formerly NC_new_attr(name,type,count,value) */ static NC_attr * new_NC_attr( const char *uname, nc_type type, size_t nelems) { NC_string *strp; NC_attr *attrp; char *name; int stat; stat = nc_utf8_normalize((const unsigned char *)uname,(unsigned char**)&name); if(stat != NC_NOERR) return NULL; assert(name != NULL && *name != 0); strp = new_NC_string(strlen(name), name); free(name); if(strp == NULL) return NULL; attrp = new_x_NC_attr(strp, type, nelems); if(attrp == NULL) { free_NC_string(strp); return NULL; } return(attrp); }
/* * Formerly NC_new_dim(const char *uname, long size) */ static NC_dim * new_NC_dim(const char *uname, size_t size) { NC_string *strp; NC_dim *dimp = NULL; int stat = NC_NOERR; char* name = NULL; stat = nc_utf8_normalize((const unsigned char *)uname,(unsigned char **)&name); if(stat != NC_NOERR) goto done; strp = new_NC_string(strlen(name), name); if(strp == NULL) {stat = NC_ENOMEM; goto done;} dimp = new_x_NC_dim(strp); if(dimp == NULL) { free_NC_string(strp); goto done; } dimp->size = size; done: if(name) free(name); return (dimp); }
/* * 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) { int hash_var_id; char *name; int stat; assert(ncap != NULL); if(ncap->nelems == 0) return -1; /* normalized version of uname */ stat = nc_utf8_normalize((const unsigned char *)uname,(unsigned char **)&name); if(stat != NC_NOERR) return stat; hash_var_id = (int)NC_hashmapGetVar(ncap, name); free(name); if (hash_var_id >= 0) { if (varpp != NULL) *varpp = ncap->value[hash_var_id]; return(hash_var_id); /* Normal return */ } return(-1); /* not found */ }
/* * Formerly NC_new_var() */ static NC_var * new_NC_var(const char *uname, nc_type type, size_t ndims, const int *dimids) { NC_string *strp = NULL; NC_var *varp = NULL; int stat; char* name; stat = nc_utf8_normalize((const unsigned char *)uname,(unsigned char **)&name); if(stat != NC_NOERR) return NULL; strp = new_NC_string(strlen(name), name); free(name); if(strp == NULL) return NULL; varp = new_x_NC_var(strp, ndims); if(varp == NULL ) { free_NC_string(strp); return NULL; } varp->type = type; if( ndims != 0 && dimids != NULL) (void) memcpy(varp->dimids, dimids, ndims * sizeof(int)); else varp->dimids=NULL; return(varp); }
/* * 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 = -1; char *name = NULL; uintptr_t data; assert(ncap != NULL); if(ncap->nelems == 0) goto done; /* normalized version of uname */ if(nc_utf8_normalize((const unsigned char *)uname,(unsigned char **)&name)) goto done; if(NC_hashmapget(ncap->hashmap, name, strlen(name), &data) == 0) goto done; dimid = (int)data; if(dimpp) *dimpp = ncap->value[dimid]; done: if(name) free(name); return dimid; }
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; }
int NC3_del_att(int ncid, int varid, const char *uname) { int status; NC *nc; NC3_INFO* ncp; NC_attrarray *ncap; NC_attr **attrpp; NC_attr *old = NULL; int attrid; size_t slen; status = NC_check_id(ncid, &nc); if(status != NC_NOERR) return status; ncp = NC3_DATA(nc); if(!NC_indef(ncp)) return NC_ENOTINDEFINE; ncap = NC_attrarray0(ncp, varid); if(ncap == NULL) return NC_ENOTVAR; { char* name; int stat = nc_utf8_normalize((const unsigned char *)uname,(unsigned char**)&name); if(stat != NC_NOERR) return stat; /* sortof inline NC_findattr() */ slen = strlen(name); attrpp = (NC_attr **) ncap->value; for(attrid = 0; (size_t) attrid < ncap->nelems; attrid++, attrpp++) { if( slen == (*attrpp)->name->nchars && strncmp(name, (*attrpp)->name->cp, slen) == 0) { old = *attrpp; break; } } free(name); } if( (size_t) attrid == ncap->nelems ) return NC_ENOTATT; /* end inline NC_findattr() */ /* shuffle down */ for(attrid++; (size_t) attrid < ncap->nelems; attrid++) { *attrpp = *(attrpp + 1); attrpp++; } *attrpp = NULL; /* decrement count */ ncap->nelems--; free_NC_attr(old); return NC_NOERR; }
int NC3_rename_att( int ncid, int varid, const char *name, const char *unewname) { int status; NC *nc; NC3_INFO* ncp; NC_attrarray *ncap; NC_attr **tmp; NC_attr *attrp; NC_string *newStr, *old; char *newname; /* normalized version */ /* sortof inline clone of NC_lookupattr() */ status = NC_check_id(ncid, &nc); if(status != NC_NOERR) return status; ncp = NC3_DATA(nc); if(NC_readonly(ncp)) return NC_EPERM; ncap = NC_attrarray0(ncp, varid); if(ncap == NULL) return NC_ENOTVAR; status = NC_check_name(unewname); if(status != NC_NOERR) return status; tmp = NC_findattr(ncap, name); if(tmp == NULL) return NC_ENOTATT; attrp = *tmp; /* end inline clone NC_lookupattr() */ if(NC_findattr(ncap, unewname) != NULL) { /* name in use */ return NC_ENAMEINUSE; } old = attrp->name; status = nc_utf8_normalize((const unsigned char *)unewname,(unsigned char**)&newname); if(status != NC_NOERR) return status; if(NC_indef(ncp)) { newStr = new_NC_string(strlen(newname), newname); free(newname); if( newStr == NULL) return NC_ENOMEM; attrp->name = newStr; free_NC_string(old); return NC_NOERR; } /* 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(old->nchars < strlen(newname)) { free(newname); return NC_ENOTINDEFINE; } status = set_NC_string(old, 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_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; } status = NC_lookupvar(ncp, varid, &varp); if(status != NC_NOERR) { /* invalid varid */ return status; } old = varp->name; status = nc_utf8_normalize((const unsigned char *)unewname,(unsigned char **)&newname); if(status != NC_NOERR) return status; if(NC_indef(ncp)) { /* Remove old name from hashmap; add new... */ NC_hashmapRemoveVar(&ncp->vars, old->cp); newStr = new_NC_string(strlen(newname),newname); free(newname); if(newStr == NULL) return(-1); varp->name = newStr; NC_hashmapAddVar(&ncp->vars, varid, newStr->cp); free_NC_string(old); return NC_NOERR; } /* else, not in define mode */ /* Remove old name from hashmap; add new... */ NC_hashmapRemoveVar(&ncp->vars, old->cp); status = set_NC_string(varp->name, newname); free(newname); if(status != NC_NOERR) return status; NC_hashmapAddVar(&ncp->vars, varid, varp->name->cp); set_NC_hdirty(ncp); if(NC_doHsync(ncp)) { status = NC_sync(ncp); if(status != NC_NOERR) return status; } return NC_NOERR; }