diff --git a/libsrc4/nc4internal.c b/libsrc4/nc4internal.c index 326c74f195..a410afd63b 100644 --- a/libsrc4/nc4internal.c +++ b/libsrc4/nc4internal.c @@ -33,7 +33,6 @@ extern float nc4_chunk_cache_preemption; severity 0 for errors, 1 for important log messages, 2 for less important, etc. */ int nc_log_level = NC_TURN_OFF_LOGGING; - #endif /* LOGGING */ /** @@ -292,7 +291,7 @@ nc4_find_g_var_nc(NC *nc, int ncid, int varid, /* Find the var info. */ (*var) = (NC_VAR_INFO_T*)ncindexith((*grp)->vars,varid); if((*var) == NULL) - return NC_ENOTVAR; + return NC_ENOTVAR; return NC_NOERR; } @@ -322,11 +321,11 @@ nc4_find_dim(NC_GRP_INFO_T *grp, int dimid, NC_DIM_INFO_T **dim, /* Find the dim info. */ (*dim) = nclistget(h5->alldims,dimid); if((*dim) == NULL) - return NC_EBADDIM; + return NC_EBADDIM; /* Redundant: Verify that this dim is in fact in the group or its parent */ for (found=0, g = grp; g ; g = g->parent) { - if(g == (*dim)->container) {found = 1; break;} + if(g == (*dim)->container) {found = 1; break;} } /* If we didn't find it, return an error. */ assert(found); @@ -379,14 +378,14 @@ nc4_rec_find_named_type(NC_GRP_INFO_T *start_grp, char *name) /* Does this group have the type we are searching for? */ type = (NC_TYPE_INFO_T*)ncindexlookup(start_grp->type,name); if(type != NULL) - return type; + return type; /* Search subgroups. */ for(i=0;ichildren);i++) { g = (NC_GRP_INFO_T*)ncindexith(start_grp->children,i); if(g == NULL) continue; if ((res = nc4_rec_find_named_type(g, name))) - return res; + return res; } /* Can't find it. Oh, woe is me! */ return NULL; @@ -495,12 +494,12 @@ nc4_find_grp_att(NC_GRP_INFO_T *grp, int varid, const char *name, int attnum, if(attlist) { NC_ATT_INFO_T* a; if(name != NULL) - a = (NC_ATT_INFO_T*)ncindexlookup(attlist,name); + a = (NC_ATT_INFO_T*)ncindexlookup(attlist,name); else - a = (NC_ATT_INFO_T*)ncindexith(attlist,attnum); + a = (NC_ATT_INFO_T*)ncindexith(attlist,attnum); if(a != NULL) { - *att = a; - return NC_NOERR; + *att = a; + return NC_NOERR; } } @@ -580,9 +579,9 @@ nc4_find_nc_file(int ext_ncid, NC_FILE_INFO_T** h5p) static void obj_list_add(NCindex* index, NC_OBJ* obj) { - /* Insert object into the index */ + /* Insert object into the index */ // ncindexset(index,obj->id,obj); - ncindexadd(index,obj); + ncindexadd(index,obj); } /** @@ -596,17 +595,17 @@ obj_list_add(NCindex* index, NC_OBJ* obj) static void obj_track(NC_FILE_INFO_T* file, NC_OBJ* obj) { - NClist* list = NULL; - /* record the object in the file */ - switch (obj->sort) { - case NCDIM: list = file->alldims; break; - case NCTYP: list = file->alltypes; break; - case NCGRP: list = file->allgroups; break; - default: - assert(NC_FALSE); - } - /* Insert at the appropriate point in the list */ - nclistset(list,obj->id,obj); + NClist* list = NULL; + /* record the object in the file */ + switch (obj->sort) { + case NCDIM: list = file->alldims; break; + case NCTYP: list = file->alltypes; break; + case NCGRP: list = file->allgroups; break; + default: + assert(NC_FALSE); + } + /* Insert at the appropriate point in the list */ + nclistset(list,obj->id,obj); } /** @@ -625,11 +624,11 @@ obj_list_del(NCindex* index, NC_OBJ *obj) } /** - * @internal Create a new variable and insert int relevant lists + * @internal Create a new variable and insert into relevant + * lists. Dimensionality info need not be known. * * @param grp the containing group * @param name the name for the new variable - * @param ndims the rank of the new variable * @param var Pointer in which to return a pointer to the new var. * * @param var Pointer to pointer that gets variable info struct. @@ -639,14 +638,13 @@ obj_list_del(NCindex* index, NC_OBJ *obj) * @author Ed Hartnett */ int -nc4_var_list_add(NC_GRP_INFO_T* grp, const char* name, int ndims, NC_VAR_INFO_T **var) +nc4_var_list_add2(NC_GRP_INFO_T *grp, const char *name, NC_VAR_INFO_T **var) { - NC_VAR_INFO_T *new_var = NULL; - int retval = NC_NOERR; + NC_VAR_INFO_T *new_var; /* Allocate storage for new variable. */ if (!(new_var = calloc(1, sizeof(NC_VAR_INFO_T)))) - BAIL(NC_ENOMEM); + return NC_ENOMEM; new_var->hdr.sort = NCVAR; /* These are the HDF5-1.8.4 defaults. */ @@ -656,45 +654,89 @@ nc4_var_list_add(NC_GRP_INFO_T* grp, const char* name, int ndims, NC_VAR_INFO_T /* Now fill in the values in the var info structure. */ new_var->hdr.id = ncindexsize(grp->vars); - if (!(new_var->hdr.name = strdup(name))) - BAIL(NC_ENOMEM); - - new_var->hdr.hashkey = NC_hashmapkey(new_var->hdr.name,strlen(new_var->hdr.name)); - new_var->ndims = ndims; - - /* Allocate space for dimension information. */ - if (ndims) - { - if (!(new_var->dim = calloc(ndims, sizeof(NC_DIM_INFO_T *)))) - BAIL(NC_ENOMEM); - if (!(new_var->dimids = calloc(ndims, sizeof(int)))) - BAIL(NC_ENOMEM); - /* Initialize dimids to illegal values (-1). See the comment - in nc4hdf.c#nc4_rec_match_dimscales. */ - memset(new_var->dimids, -1, ndims*sizeof(int)); - } + return NC_ENOMEM; + new_var->hdr.hashkey = NC_hashmapkey(new_var->hdr.name, + strlen(new_var->hdr.name)); + /* Create an indexed list for the attributes. */ new_var->att = ncindexnew(0); /* Officially track it */ - ncindexadd(grp->vars,(NC_OBJ*)new_var); + ncindexadd(grp->vars, (NC_OBJ *)new_var); /* Set the var pointer, if one was given */ if (var) *var = new_var; -exit: - if(retval != NC_NOERR) { - nc4_var_list_del(grp,new_var); + return NC_NOERR; +} + +/** + * @internal Set the number of dims in an NC_VAR_INFO_T struct. + * + * @param var Pointer to the var. + * @param ndims Number of dimensions for this var. + * + * @param var Pointer to pointer that gets variable info struct. + * + * @return ::NC_NOERR No error. + * @return ::NC_ENOMEM Out of memory. + * @author Ed Hartnett + */ +int +nc4_var_set_ndims(NC_VAR_INFO_T *var, int ndims) +{ + + /* Remember the number of dimensions. */ + var->ndims = ndims; + + /* Allocate space for dimension information. */ + if (ndims) + { + if (!(var->dim = calloc(ndims, sizeof(NC_DIM_INFO_T *)))) + return NC_ENOMEM; + if (!(var->dimids = calloc(ndims, sizeof(int)))) + return NC_ENOMEM; + + /* Initialize dimids to illegal values (-1). See the comment + in nc4hdf.c#nc4_rec_match_dimscales. */ + memset(var->dimids, -1, ndims * sizeof(int)); } - return retval; + return NC_NOERR; +} +/** + * @internal Create a new variable and insert int relevant list. + * + * @param grp the containing group + * @param name the name for the new variable + * @param ndims the rank of the new variable + * @param var Pointer in which to return a pointer to the new var. + * + * @param var Pointer to pointer that gets variable info struct. + * + * @return ::NC_NOERR No error. + * @return ::NC_ENOMEM Out of memory. + * @author Ed Hartnett + */ +int +nc4_var_list_add(NC_GRP_INFO_T* grp, const char* name, int ndims, + NC_VAR_INFO_T **var) +{ + int retval; + + if ((retval = nc4_var_list_add2(grp, name, var))) + return retval; + if ((retval = nc4_var_set_ndims(*var, ndims))) + return retval; + + return NC_NOERR; } /** - * @internal Add to the beginning of a dim list. + * @internal Add a dimension to the dimension list for a group. * * @param grp container for the dim * @param name for the dim @@ -703,46 +745,52 @@ nc4_var_list_add(NC_GRP_INFO_T* grp, const char* name, int ndims, NC_VAR_INFO_T * @param dim Pointer to pointer that gets the new dim info struct. * * @return ::NC_NOERR No error. + * @return ::NC_ENOMEM Out of memory. * @author Ed Hartnett */ int -nc4_dim_list_add(NC_GRP_INFO_T* grp, const char* name, size_t len, int assignedid, NC_DIM_INFO_T **dim) +nc4_dim_list_add(NC_GRP_INFO_T *grp, const char *name, size_t len, + int assignedid, NC_DIM_INFO_T **dim) { - NC_DIM_INFO_T *new_dim = NULL; - NC_FILE_INFO_T *h5 = grp->nc4_info; - int retval = NC_NOERR; + NC_DIM_INFO_T *new_dim; + + assert(grp && name); + /* Allocate memory for dim metadata. */ if (!(new_dim = calloc(1, sizeof(NC_DIM_INFO_T)))) - BAIL(NC_ENOMEM); + return NC_ENOMEM; + new_dim->hdr.sort = NCDIM; - if(assignedid >= 0) - new_dim->hdr.id = assignedid; + /* Assign the dimension ID. */ + if (assignedid >= 0) + new_dim->hdr.id = assignedid; else - new_dim->hdr.id = h5->next_dimid++; + new_dim->hdr.id = grp->nc4_info->next_dimid++; - /* Initialize the metadata for this dimension. */ + /* Remember the name and create a hash. */ if (!(new_dim->hdr.name = strdup(name))) - BAIL(NC_ENOMEM); - /* Create a hash of the name. */ - new_dim->hdr.hashkey = NC_hashmapkey(new_dim->hdr.name,strlen(new_dim->hdr.name)); + return NC_ENOMEM; + new_dim->hdr.hashkey = NC_hashmapkey(new_dim->hdr.name, + strlen(new_dim->hdr.name)); + + /* Is dimension unlimited? */ new_dim->len = len; if (len == NC_UNLIMITED) new_dim->unlimited = NC_TRUE; + + /* Remember the containing group. */ new_dim->container = grp; - /* Add object to lists */ - obj_list_add(grp->dim, (NC_OBJ*)new_dim); - obj_track(h5,(NC_OBJ*)new_dim); + /* Add object to dimension list for this group. */ + obj_list_add(grp->dim, (NC_OBJ *)new_dim); + obj_track(grp->nc4_info, (NC_OBJ *)new_dim); /* Set the dim pointer, if one was given */ if (dim) *dim = new_dim; -exit: - if(retval != NC_NOERR) - nc4_dim_free(new_dim); /* would not be in list yet*/ - return retval; + return NC_NOERR; } /** @@ -784,7 +832,7 @@ nc4_att_list_add(NCindex* list, const char* name, NC_ATT_INFO_T **att) exit: if(retval) { - nc4_att_list_del(list,new_att); + nc4_att_list_del(list,new_att); } return retval; } @@ -922,17 +970,17 @@ nc4_check_dup_name(NC_GRP_INFO_T *grp, char *name) /* Any types of this name? */ type = (NC_TYPE_INFO_T*)ncindexlookup(grp->type,name); if(type != NULL) - return NC_ENAMEINUSE; + return NC_ENAMEINUSE; /* Any child groups of this name? */ g = (NC_GRP_INFO_T*)ncindexlookup(grp->children,name); if(g != NULL) - return NC_ENAMEINUSE; + return NC_ENAMEINUSE; /* Any variables of this name? */ var = (NC_VAR_INFO_T*)ncindexlookup(grp->vars,name); if(var != NULL) - return NC_ENAMEINUSE; + return NC_ENAMEINUSE; return NC_NOERR; } @@ -954,28 +1002,28 @@ nc4_check_dup_name(NC_GRP_INFO_T *grp, char *name) int nc4_type_new(NC_GRP_INFO_T *grp, size_t size, const char *name, int assignedid, NC_TYPE_INFO_T **type) { - NC_TYPE_INFO_T *new_type = NULL; + NC_TYPE_INFO_T *new_type = NULL; - /* Allocate memory for the type */ - if (!(new_type = calloc(1, sizeof(NC_TYPE_INFO_T)))) - return NC_ENOMEM; - new_type->hdr.sort = NCTYP; + /* Allocate memory for the type */ + if (!(new_type = calloc(1, sizeof(NC_TYPE_INFO_T)))) + return NC_ENOMEM; + new_type->hdr.sort = NCTYP; - /* Remember info about this type. */ - new_type->hdr.id = assignedid; - new_type->size = size; - if (!(new_type->hdr.name = strdup(name))) { - free(new_type); - return NC_ENOMEM; - } + /* Remember info about this type. */ + new_type->hdr.id = assignedid; + new_type->size = size; + if (!(new_type->hdr.name = strdup(name))) { + free(new_type); + return NC_ENOMEM; + } - new_type->hdr.hashkey = NC_hashmapkey(name,strlen(name)); + new_type->hdr.hashkey = NC_hashmapkey(name,strlen(name)); - /* Return a pointer to the new type, if requested */ - if (type) - *type = new_type; + /* Return a pointer to the new type, if requested */ + if (type) + *type = new_type; - return NC_NOERR; + return NC_NOERR; } /** @@ -1000,7 +1048,7 @@ nc4_type_list_add(NC_GRP_INFO_T *grp, size_t size, const char *name, retval = nc4_type_new(grp,size,name, grp->nc4_info->next_typeid,&new_type); if(retval) - return retval; + return retval; grp->nc4_info->next_typeid++; /* Increment the ref. count on the type */ @@ -1184,12 +1232,12 @@ nc4_type_free(NC_TYPE_INFO_T *type) /* Delete all the fields in this type (there will be some if its a * compound). */ - for(i=0;iu.c.field);i++) { + for(i=0;iu.c.field);i++) { field = nclistget(type->u.c.field,i); field_free(field); } - nclistfree(type->u.c.field); - type->u.c.field = NULL; /* belt and suspenders */ + nclistfree(type->u.c.field); + type->u.c.field = NULL; /* belt and suspenders */ } break; @@ -1198,14 +1246,14 @@ nc4_type_free(NC_TYPE_INFO_T *type) NC_ENUM_MEMBER_INFO_T *enum_member; /* Delete all the enum_members, if any. */ - for(i=0;iu.e.enum_member);i++) { + for(i=0;iu.e.enum_member);i++) { enum_member = nclistget(type->u.e.enum_member,i); free(enum_member->value); free(enum_member->name); free(enum_member); } - nclistfree(type->u.e.enum_member); - type->u.e.enum_member = NULL; /* belt and suspenders */ + nclistfree(type->u.e.enum_member); + type->u.e.enum_member = NULL; /* belt and suspenders */ if (H5Tclose(type->u.e.base_hdf_typeid) < 0) return NC_EHDFERR; @@ -1247,9 +1295,9 @@ nc4_var_free(NC_VAR_INFO_T *var) /* First delete all the attributes attached to this var. */ for(i=0;iatt);i++) { - att = (NC_ATT_INFO_T*)ncindexith(var->att,i); - if(att == NULL) continue; /* might be a gap */ - if ((ret = nc4_att_free(att))) + att = (NC_ATT_INFO_T*)ncindexith(var->att,i); + if(att == NULL) continue; /* might be a gap */ + if ((ret = nc4_att_free(att))) return ret; } ncindexfree(var->att); @@ -1336,9 +1384,9 @@ nc4_var_list_del(NC_GRP_INFO_T* grp, NC_VAR_INFO_T *var) /* Remove from lists */ if(grp) { - i = ncindexfind(grp->vars,(NC_OBJ*)var); - if(i >= 0) - ncindexidel(grp->vars,i); + i = ncindexfind(grp->vars,(NC_OBJ*)var); + if(i >= 0) + ncindexidel(grp->vars,i); } return nc4_var_free(var); } @@ -1355,13 +1403,13 @@ int nc4_dim_free(NC_DIM_INFO_T *dim) { /* Free memory allocated for names. */ - if(dim) { - if (dim->hdr.name) - free(dim->hdr.name); + if(dim) { + if (dim->hdr.name) + free(dim->hdr.name); - free(dim); - } - return NC_NOERR; + free(dim); + } + return NC_NOERR; } /** @@ -1377,9 +1425,9 @@ int nc4_dim_list_del(NC_GRP_INFO_T* grp, NC_DIM_INFO_T *dim) { if(grp && dim) { - int pos = ncindexfind(grp->dim,(NC_OBJ*)dim); - if(pos >= 0) - ncindexidel(grp->dim,pos); + int pos = ncindexfind(grp->dim,(NC_OBJ*)dim); + if(pos >= 0) + ncindexidel(grp->dim,pos); } return nc4_dim_free(dim); } @@ -1414,9 +1462,9 @@ nc4_rec_grp_del(NC_GRP_INFO_T *grp) /* Recursively call this function for each child, if any, stopping * if there is an error. */ for(i=0;ichildren);i++) { - g = (NC_GRP_INFO_T*)ncindexith(grp->children,i); - if(g == NULL) continue; - if ((retval = nc4_rec_grp_del(g))) + g = (NC_GRP_INFO_T*)ncindexith(grp->children,i); + if(g == NULL) continue; + if ((retval = nc4_rec_grp_del(g))) return retval; } ncindexfree(grp->children); @@ -1425,10 +1473,10 @@ nc4_rec_grp_del(NC_GRP_INFO_T *grp) /* Delete all the list contents for vars, dims, and atts, in this * group. */ for(i=0;iatt);i++) { - att = (NC_ATT_INFO_T*)ncindexith(grp->att,i); - if(att == NULL) continue; - LOG((4, "%s: deleting att %s", __func__, att->hdr.name)); - if ((retval = nc4_att_free(att))) /* free but leave in parent list */ + att = (NC_ATT_INFO_T*)ncindexith(grp->att,i); + if(att == NULL) continue; + LOG((4, "%s: deleting att %s", __func__, att->hdr.name)); + if ((retval = nc4_att_free(att))) /* free but leave in parent list */ return retval; } ncindexfree(grp->att); @@ -1504,9 +1552,9 @@ nc4_rec_grp_del(NC_GRP_INFO_T *grp) int nc4_att_list_del(NCindex* list, NC_ATT_INFO_T *att) { - assert(att != NULL); - if(list != NULL) obj_list_del(list, (NC_OBJ *)att); - return nc4_att_free(att); + assert(att != NULL); + if(list != NULL) obj_list_del(list, (NC_OBJ *)att); + return nc4_att_free(att); } /** @@ -1711,7 +1759,7 @@ rec_print_metadata(NC_GRP_INFO_T *grp, int tab_count) (dims_string ? dims_string : " -"),var->type_info->endianness, var->type_info->native_hdf_typeid)); for(j=0;jatt);j++) { att = (NC_ATT_INFO_T*)ncindexith(var->att,j); - if(att == NULL) continue; + if(att == NULL) continue; LOG((2, "%s VAR ATTRIBUTE - attnum: %d name: %s type: %d len: %d", tabs, att->hdr.id, att->hdr.name, att->nc_typeid, att->len)); } @@ -1731,13 +1779,13 @@ rec_print_metadata(NC_GRP_INFO_T *grp, int tab_count) /* Is this a compound type? */ if (type->nc_type_class == NC_COMPOUND) { - int j; + int j; LOG((3, "compound type")); - for(j=0;ju.c.field);j++) { - field = (NC_FIELD_INFO_T*)nclistget(type->u.c.field,j); + for(j=0;ju.c.field);j++) { + field = (NC_FIELD_INFO_T*)nclistget(type->u.c.field,j); LOG((4, "field %s offset %d nctype %d ndims %d", field->hdr.name, field->offset, field->nc_typeid, field->ndims)); - } + } } else if (type->nc_type_class == NC_VLEN) { @@ -1763,7 +1811,7 @@ rec_print_metadata(NC_GRP_INFO_T *grp, int tab_count) { if((g = (NC_GRP_INFO_T*)ncindexith(grp->children,i)) == NULL) continue; if ((retval = rec_print_metadata(g, tab_count + 1))) - return retval; + return retval; } return NC_NOERR; } @@ -1793,7 +1841,7 @@ log_metadata_nc(NC *nc) h5->cmode, (int)h5->parallel, (int)h5->redef, h5->fill_mode, (int)h5->no_write, h5->next_nc_grpid)); if(nc_log_level >= 2) - return rec_print_metadata(h5->root_grp, 0); + return rec_print_metadata(h5->root_grp, 0); return NC_NOERR; }