/* * 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; char *name = (char *)utf8proc_NFC((const unsigned char *)uname); if(name == NULL) return NULL; strp = new_NC_string(strlen(name), name); free(name); if(strp == NULL) return NULL; dimp = new_x_NC_dim(strp); if(dimp == NULL) { free_NC_string(strp); return NULL; } dimp->size = size; return(dimp); }
/* * 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 = (char *)utf8proc_NFC((const unsigned char *)uname); if(name == NULL) 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_var() */ static NC_var * new_NC_var(const char *uname, nc_type type, size_t ndims, const int *dimids) { NC_string *strp; NC_var *varp; char *name = (char *)utf8proc_NFC((const unsigned char *)uname); if(name == NULL) 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)); return(varp); }
/* * 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); }
/* * 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_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); }
/* Read a NC_string from the header */ static int v1h_get_NC_string(v1hs *gsp, NC_string **ncstrpp) { int status; size_t nchars = 0; NC_string *ncstrp; status = v1h_get_size_t(gsp, &nchars); if(status != ENOERR) return status; ncstrp = new_NC_string(nchars, NULL); if(ncstrp == NULL) { return NC_ENOMEM; } #if 0 /* assert(ncstrp->nchars == nchars || ncstrp->nchars - nchars < X_ALIGN); */ assert(ncstrp->nchars % X_ALIGN == 0); status = check_v1hs(gsp, ncstrp->nchars); #else status = check_v1hs(gsp, _RNDUP(ncstrp->nchars, X_ALIGN)); #endif if(status != ENOERR) goto unwind_alloc; status = ncx_pad_getn_text((const void **)(&gsp->pos), nchars, ncstrp->cp); if(status != ENOERR) goto unwind_alloc; *ncstrpp = ncstrp; return ENOERR; unwind_alloc: free_NC_string(ncstrp); return status; }
/* * Formerly NC_new_attr(name,type,count,value) */ static NC_attr * new_NC_attr( const char *name, nc_type type, size_t nelems) { NC_string *strp; NC_attr *attrp; assert(name != NULL && *name != 0); strp = new_NC_string(strlen(name), 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); }
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; }
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 nc_rename_att( int ncid, int varid, const char *name, const char *newname) { int status; NC *ncp; NC_attrarray *ncap; NC_attr **tmp; NC_attr *attrp; NC_string *newStr, *old; /* sortof inline clone of NC_lookupattr() */ status = NC_check_id(ncid, &ncp); if(status != NC_NOERR) return status; if(NC_readonly(ncp)) return NC_EPERM; ncap = NC_attrarray0(ncp, varid); if(ncap == NULL) return NC_ENOTVAR; status = NC_check_name(newname); 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, newname) != NULL) { /* name in use */ return NC_ENAMEINUSE; } old = attrp->name; if(NC_indef(ncp)) { newStr = new_NC_string(strlen(newname), newname); if( newStr == NULL) return NC_ENOMEM; attrp->name = newStr; free_NC_string(old); return NC_NOERR; } /* else */ status = set_NC_string(old, 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_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 nc_rename_var(int ncid, int varid, const char *newname) { int status; NC *ncp; NC_var *varp; NC_string *old, *newStr; int other; status = NC_check_id(ncid, &ncp); if(status != NC_NOERR) return status; if(NC_readonly(ncp)) { return NC_EPERM; } status = NC_check_name(newname); if(status != NC_NOERR) return status; /* check for name in use */ other = NC_findvar(&ncp->vars, newname, &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; if(NC_indef(ncp)) { newStr = new_NC_string(strlen(newname),newname); if(newStr == NULL) return(-1); varp->name = newStr; free_NC_string(old); return NC_NOERR; } /* else, not in define mode */ status = set_NC_string(varp->name, 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_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; newname = (char *)utf8proc_NFC((const unsigned char *)unewname); if(newname == NULL) return NC_EBADNAME; 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 */ 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; 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; /* Remove old name from hashmap; add new... */ NC_hashmapRemoveVar(&ncp->vars, old->cp); NC_hashmapAddVar(&ncp->vars, varid, newStr->cp); free_NC_string(old); return NC_NOERR; } /* else, not in define mode */ status = set_NC_string(varp->name, newname); free(newname); if(status != NC_NOERR) return status; /* Remove old name from hashmap; add new... */ NC_hashmapRemoveVar(&ncp->vars, old->cp); 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; }
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; }