static herr_t get_attribute_disk(hid_t loc_id, const char *attr_name, void *attr_out) { hid_t attr_id; hid_t attr_type; herr_t status; attr_id = H5Aopen_name(loc_id, attr_name); if (attr_id < 0) return -1; attr_type = H5Aget_type(attr_id); if (attr_type < 0) goto out; status = H5Aread(attr_id, attr_type, attr_out); if (status < 0) goto out; status = H5Tclose(attr_type); if (status < 0) goto out; status = H5Aclose(attr_id); if (status < 0) return -1; return 0; out: H5Tclose(attr_type); H5Aclose(attr_id); return -1; }
/* read attribute value */ char* H5Dataset_read_attr_value(hid_t aid) { hid_t asid=-1, atid=-1; int i, rank; hsize_t *dims; size_t size=1; char *attr_buf=NULL; asid = H5Aget_space(aid); atid = H5Aget_type(aid); rank = H5Sget_simple_extent_ndims(asid); if (rank > 0) { dims = (hsize_t *)malloc(rank * sizeof(hsize_t)); H5Sget_simple_extent_dims(asid, dims, NULL); for (i=0; i<rank; i++) { size *= (size_t)dims[i]; free(dims); } size *= H5Tget_size(atid); attr_buf = (char *)malloc(size); if (H5Aread( aid, atid, attr_buf) < 0) { free(attr_buf); attr_buf = NULL; } } if( atid > 0) H5Tclose(atid); if (asid > 0) H5Sclose(asid); return attr_buf; }
void write(const hid_attribute_adaptor& attrib, const std::string& value) { hid_t attr_type_id = H5CPP_ERR_ON_NEG(H5Aget_type(attrib.id())); if(H5Tget_class(attr_type_id) != H5T_STRING) H5CPP_THROW("Attempted to set string value on non-string attribute '" << object::get_name(attrib.id()) << '\''); const char* cstr = value.c_str(); if(H5Tis_variable_str(attr_type_id)) H5CPP_ERR_ON_NEG(H5Awrite(attrib.id(), attr_type_id, &cstr)); else { std::size_t attriblen = H5Aget_storage_size(attrib.id()); if(attriblen > value.length()) { // avoid reading past end of value into unalloc'd mem std::vector<char> buf(attriblen,'\0'); std::copy(cstr, cstr+value.length(), &buf[0]); H5CPP_ERR_ON_NEG(H5Awrite(attrib.id(), attr_type_id, &buf[0])); } else H5CPP_ERR_ON_NEG(H5Awrite(attrib.id(), attr_type_id, cstr)); } }
// Read string attribute <attr_name> given by address <path> char AH5_read_str_attr(hid_t loc_id, const char *path, char *attr_name, char **rdata) { hid_t attr_id, filetype, memtype; size_t sdim; char success = AH5_FALSE; if (AH5_path_valid(loc_id, path) || strcmp(path, ".") == 0) if (H5Aexists_by_name(loc_id, path, attr_name, H5P_DEFAULT) > 0) { attr_id = H5Aopen_by_name(loc_id, path, attr_name, H5P_DEFAULT, H5P_DEFAULT); filetype = H5Aget_type(attr_id); sdim = H5Tget_size(filetype); sdim++; // make a space for null terminator *rdata = (char *) malloc(sdim * sizeof(char)); memtype = H5Tcopy(H5T_C_S1); H5Tset_size(memtype, sdim); if (H5Aread(attr_id, memtype, *rdata) >= 0) success = AH5_TRUE; else free(*rdata); H5Tclose(memtype); H5Tclose(filetype); H5Aclose(attr_id); } if (!success) *rdata = NULL; return success; }
// JRC: This fat interface may not scale? What about // scalar attributes? herr_t VsH5Attribute::getDoubleVectorValue(std::vector<double>* dvals) { herr_t err = 0; size_t npoints; hid_t atype = H5Aget_type(getId()); H5T_class_t type = H5Tget_class(atype); hid_t aspace = H5Aget_space(getId()); size_t rank = H5Sget_simple_extent_ndims(aspace); if (type != H5T_FLOAT) { VsLog::warningLog() <<"VsH5Attribute::getDoubleVectorValue() - Requested attribute " <<getShortName() <<" is not a floating point vector." <<std::endl; dvals->resize(0); return -1; } if (rank == 0) { dvals->resize(1); double v; err = H5Aread(getId(), H5T_NATIVE_DOUBLE, &v); (*dvals)[0] = v; return err; } // rank>0 npoints = H5Sget_simple_extent_npoints(aspace); double* v = new double[npoints]; err = H5Aread(getId(), H5T_NATIVE_DOUBLE, v); dvals->resize(npoints); for (size_t i = 0; i<npoints; ++i) { (*dvals)[i] = v[i]; } delete [] v; return err; }
static herr_t get_attribute_float(hid_t input, const char *name, float *val) { hid_t attr_id; hid_t type_id; H5T_class_t type_class; size_t type_size; herr_t status; char *strval; attr_id = H5Aopen_name(input, name); type_id = H5Aget_type(attr_id); type_class = H5Tget_class(type_id); type_size = H5Tget_size(type_id); H5Tclose(type_id); H5Aclose(attr_id); switch(type_class) { case H5T_STRING: status = get_attribute_str(input, name, &strval); if (status < 0) return -1; *val = atof(strval); free(strval); return 0; case H5T_FLOAT: status = get_attribute(input, name, H5T_NATIVE_FLOAT, val); if (status < 0) return -1; return 0; } return -1; }
hdf5attribute::hdf5attribute(hdf5dataset& hdataset, const std::string& name) { _attribute_id = H5Aopen(hdataset.handle(), name.c_str(), H5P_DEFAULT); if(_attribute_id < 0) throw std::runtime_error("Failed to open attribute (" + name + ") in dataset (" + "not available" + ") file (" + "not available" + ")"); _type_id = H5Aget_type(_attribute_id); }
/*-------------------------------------------------------------*/ static void validateFloat3Attribute(pNXVcontext self, hid_t dpField, char *name) { hid_t attID, attType, attSpace; H5T_class_t h5class; hsize_t dims[2], maxDims[2]; char fname[512]; memset(fname,0,sizeof(fname)); H5Iget_name(dpField,fname,sizeof(fname)); if(!H5LTfind_attribute(dpField,name)){ NXVsetLog(self,"sev","error"); NXVprintLog(self,"message", "Missing attribute %s on %s", name, fname); NXVlog(self); self->errCount++; } else { attID = H5Aopen(dpField,name,H5P_DEFAULT); assert(attID >= 0); attType = H5Aget_type(attID); assert(attType >= 0); h5class = H5Tget_class(attType); if(h5class != H5T_FLOAT){ NXVsetLog(self,"sev","error"); NXVprintLog(self,"message", "%s attribute on %s is of wrong type, expected float", name, fname); NXVlog(self); self->errCount++; } else { attSpace = H5Aget_space(attID); if(H5Sget_simple_extent_ndims(attSpace) != 1){ NXVsetLog(self,"sev","error"); NXVprintLog(self,"message", "%s attribute on %s is of wrong rank, expected 1", name, fname); NXVlog(self); self->errCount++; } else { H5Sget_simple_extent_dims(attSpace,dims,maxDims); if(dims[0] != 3){ NXVsetLog(self,"sev","error"); NXVprintLog(self,"message", "%s attribute on %s is of wrong size, expected 3", name, fname); NXVlog(self); self->errCount++; } } H5Sclose(attSpace); } H5Tclose(attType); H5Aclose(attID); } }
void mhdf_getElemTypeName( mhdf_FileHandle file_handle, const char* elem_handle, char* buffer, size_t buf_len, mhdf_Status* status ) { FileHandle* file_ptr; hid_t elem_id, type_id, attr_id; char bytes[16]; herr_t rval; API_BEGIN; if (NULL == buffer || buf_len < 2) { mhdf_setFail( status, "invalid input" ); return; } buffer[0] = '\0'; file_ptr = (FileHandle*)(file_handle); if (!mhdf_check_valid_file( file_ptr, status )) return; elem_id = mhdf_elem_group_from_handle( file_ptr, elem_handle, status ); if (elem_id < 0) return; attr_id = H5Aopen_name( elem_id, ELEM_TYPE_ATTRIB ); H5Gclose( elem_id ); if (attr_id < 0) { mhdf_setFail( status, "Missing element type attribute. Invalid file." ); return; } type_id = H5Aget_type( attr_id ); assert( type_id > 0 ); rval = H5Aread( attr_id, type_id, bytes ); H5Aclose( attr_id ); if (rval < 0) { H5Tclose( type_id ); mhdf_setFail( status, "Failed to read element type attribute. Invalid file." ); return; } rval = H5Tenum_nameof( type_id, bytes, buffer, buf_len ); H5Tclose( type_id ); if (rval < 0) { mhdf_setFail( status, "Invalid datatype for element type attribute. Invalid file." ); return; } mhdf_setOkay( status ); API_END; return ; }
/* fetch num_cols and the col for a particular trackname */ void get_cols(chromosome_t *chromosome, char *trackname, hsize_t *num_cols, hsize_t *col) { hid_t attr, root, dataspace, datatype; hsize_t data_size, cell_size, num_cells; char *attr_data; /* Tracknames are stored in the attributes of the root group of each file */ root = H5Gopen(chromosome->h5group, "/", H5P_DEFAULT); assert(root >= 0); attr = H5Aopen_name(root, "tracknames"); assert(attr >= 0); dataspace = H5Aget_space(attr); assert(dataspace >= 0); assert(H5Sget_simple_extent_dims(dataspace, num_cols, NULL) == 1); assert(H5Sclose(dataspace) >= 0); if (trackname && col) { datatype = H5Aget_type(attr); assert(datatype >= 0); assert(H5Tget_class(datatype) == H5T_STRING); cell_size = H5Tget_size(datatype); assert(cell_size > 0); data_size = H5Aget_storage_size(attr); assert(data_size > 0); num_cells = data_size / cell_size; /* allocate room for tracknames */ attr_data = xmalloc(data_size); assert(attr_data); assert(H5Aread(attr, datatype, attr_data) >= 0); *col = 0; for (*col = 0; *col <= num_cells; (*col)++) { if (*col == num_cells) { fprintf(stderr, "can't find trackname: %s\n", trackname); free(attr_data); exit(EXIT_FAILURE); } else { if (!strncmp(attr_data + (*col * cell_size), trackname, cell_size)) { break; } } } /* clean up read tracknames */ free(attr_data); } assert(H5Aclose(attr) >= 0); }
/*-------------------------------------------------------------- This validates the lesser depends_on chain fields like vector, offset and transformation_type ----------------------------------------------------------------*/ static void validateDependsOnAttributes(pNXVcontext self,hid_t dpField) { char fname[512], transData[512]; hid_t attID, attType, attSpace; H5T_class_t h5class; memset(fname,0,sizeof(fname)); memset(transData,0,sizeof(transData)); H5Iget_name(dpField,fname,sizeof(fname)); /* deal with transformation_type */ if(!H5LTfind_attribute(dpField,"transformation_type")){ NXVsetLog(self,"sev","error"); NXVprintLog(self,"message", "Missing attribute transformation_type on %s", fname); NXVlog(self); self->errCount++; } else { attID = H5Aopen(dpField,"transformation_type",H5P_DEFAULT); assert(attID >= 0); attType = H5Aget_type(attID); assert(attType >= 0); h5class = H5Tget_class(attType); if(h5class != H5T_STRING){ NXVsetLog(self,"sev","error"); NXVprintLog(self,"message", "transformation_type on %s is of wrong type, expected string", fname); NXVlog(self); self->errCount++; } else { H5NXget_attribute_string(self->fileID, fname, "transformation_type",transData); if(strcmp(transData,"translation") != 0 && strcmp(transData,"rotation") != 0){ NXVsetLog(self,"sev","error"); NXVprintLog(self,"message", "transformation_type on %s contains bad data: %s", fname, "expected rotation or translation"); NXVlog(self); self->errCount++; } } H5Tclose(attType); H5Aclose(attID); } validateFloat3Attribute(self,dpField,"offset"); validateFloat3Attribute(self,dpField,"vector"); }
H5Type & H5Attribute::getDataType() { hid_t type = H5Aget_type(attr); if (type < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot get the attribute type")); } return *new H5Type(*this, type); }
static herr_t get_attribute_str(hid_t obj_id, const char *attr_name, char **data) { hid_t attr_id; hid_t attr_type; size_t type_size; herr_t status; *data = NULL; attr_id = H5Aopen_name(obj_id, attr_name); if (attr_id < 0) return -1; attr_type = H5Aget_type(attr_id); if (attr_type < 0) goto out; /* Get the size */ type_size = H5Tget_size(attr_type); if (type_size < 0) goto out; /* malloc enough space for the string, plus 1 for trailing '\0' */ *data = (char *)malloc(type_size + 1); status = H5Aread(attr_id, attr_type, *data); if (status < 0) goto out; /* Set the last character to '\0' in case we are dealing with * null padded or space padded strings */ (*data)[type_size] = '\0'; status = H5Tclose(attr_type); if (status < 0) goto out; status = H5Aclose(attr_id); if (status < 0) return -1; return 0; out: H5Tclose(attr_type); H5Aclose(attr_id); if (*data) free(*data); return -1; }
/* * Class: hdf_hdf5lib_H5 * Method: H5Aget_type * Signature: (J)J */ JNIEXPORT jlong JNICALL Java_hdf_hdf5lib_H5__1H5Aget_1type (JNIEnv *env, jclass clss, jlong attr_id) { hid_t retVal = -1; retVal = H5Aget_type((hid_t)attr_id); if (retVal < 0) h5libraryError(env); return (jlong)retVal; } /* end Java_hdf_hdf5lib_H5__1H5Aget_1type */
static herr_t get_attribute_info(hid_t obj_id, const char *attr_name, hsize_t *dims, H5T_class_t *type_class, size_t *type_size, hid_t *type_id) { hid_t attr_id; hid_t space_id; herr_t status; int rank; /* Open the attribute. */ attr_id = H5Aopen_name(obj_id, attr_name); if (attr_id < 0) return -1; /* Get an identifier for the datatype. */ *type_id = H5Aget_type(attr_id); /* Get the class. */ *type_class = H5Tget_class(*type_id); /* Get the size. */ *type_size = H5Tget_size(*type_id); /* Get the dataspace handle */ space_id = H5Aget_space(attr_id); if (space_id < 0) goto out; /* Get dimensions */ rank = H5Sget_simple_extent_dims(space_id, dims, NULL); if (rank < 0) goto out; /* Terminate access to the dataspace */ status = H5Sclose(space_id); if (status < 0) goto out; /* End access to the attribute */ status = H5Aclose(attr_id); if (status < 0) goto out; return 0; out: H5Tclose(*type_id); H5Aclose(attr_id); return -1; }
void ReadStringsT( hid_t iParent, const std::string &iAttrName, size_t iNumStrings, StringT *oStrings ) { ABCA_ASSERT( iParent >= 0, "Invalid parent in ReadStringsT" ); // Open the attribute. hid_t attrId = H5Aopen( iParent, iAttrName.c_str(), H5P_DEFAULT ); ABCA_ASSERT( attrId >= 0, "Couldn't open attribute named: " << iAttrName ); AttrCloser attrCloser( attrId ); // Checking code. { hid_t attrFtype = H5Aget_type( attrId ); DtypeCloser dtypeCloser( attrFtype ); hid_t nativeDtype = GetNativeDtype<CharT>(); ABCA_ASSERT( H5Tget_class( attrFtype ) == H5Tget_class( nativeDtype ) && H5Tget_sign( attrFtype ) == H5Tget_sign( nativeDtype ), "Invalid datatype for stringT" ); } hid_t attrSpace = H5Aget_space( attrId ); ABCA_ASSERT( attrSpace >= 0, "Couldn't get dataspace for attribute: " << iAttrName ); DspaceCloser dspaceCloser( attrSpace ); hssize_t numPoints = H5Sget_simple_extent_npoints( attrSpace ); ABCA_ASSERT( numPoints > 0, "Degenerate string dimensions in ReadStringsT" ); // Create temporary char storage buffer. std::vector<CharT> charStorage( ( size_t )( 1 + numPoints ), ( CharT )0 ); // Read into it. herr_t status = H5Aread( attrId, GetNativeDtype<CharT>(), ( void * )&charStorage.front() ); ABCA_ASSERT( status >= 0, "Couldn't read from attribute: " << iAttrName ); // Extract 'em. ExtractStrings( oStrings, ( const CharT * )&charStorage.front(), 1 + numPoints, iNumStrings ); }
int hdf5_read(void *output, hid_t file, char *n_group, char *n_dset, char *n_attr, int c){ hid_t memtype, type, group=-1, dset=-1, attr=-1, tmp_id, space; herr_t status; size_t sdim; int ndims; tmp_id = file; if(strlen(n_group)>0){ group = H5Gopen(tmp_id, n_group, H5P_DEFAULT); tmp_id = group; } if(strlen(n_dset)>0){ dset = H5Dopen(tmp_id, n_dset, H5P_DEFAULT); tmp_id = dset; } if(strlen(n_attr)>0){ attr = H5Aopen(tmp_id, n_attr, H5P_DEFAULT); tmp_id = attr; } if(c == 'c'){ memtype = H5Tcopy (H5T_C_S1); type = H5Aget_type (tmp_id); sdim = H5Tget_size (type); sdim++; status = H5Tset_size (memtype, sdim); } else if(c == 'd'){ memtype = H5T_NATIVE_DOUBLE; } else if(c == 'i' || c == 'n'){ memtype = H5T_NATIVE_INT; } else if(c == 'f'){ memtype = H5T_NATIVE_FLOAT; } if (tmp_id == attr){ status = H5Aread(tmp_id, memtype, output); } else if(tmp_id == dset && c == 'n'){ space = H5Dget_space (dset); ndims = H5Sget_simple_extent_dims (space, output, NULL); } else{ return(-1); } return(1); }
/*---------------------------------------------------------------------------- * Name: h5aget_type_c * Purpose: Call H5Aget_space to get attribute's datatype * Inputs: attr_id - attribute identifier * Outputs: type_id - datatype identifier * Returns: 0 on success, -1 on failure * Programmer: Elena Pourmal * Thursday, August 12, 1999 * Modifications: *---------------------------------------------------------------------------*/ int_f nh5aget_type_c (hid_t_f *attr_id, hid_t_f *type_id) { int_f ret_value=0; /* Return value */ /* * Call H5Aget_type function. */ if ((*type_id = (hid_t_f)H5Aget_type((hid_t)*attr_id)) < 0) HGOTO_DONE(FAIL); done: return ret_value; }
bool GH5_FetchAttribute( hid_t loc_id, const char *pszAttrName, CPLString &osResult, bool bReportError ) { bool retVal = false; hid_t hAttr = H5Aopen_name( loc_id, pszAttrName ); osResult.clear(); if( hAttr < 0 ) { if( bReportError ) CPLError( CE_Failure, CPLE_AppDefined, "Attempt to read attribute %s failed, not found.", pszAttrName ); return false; } hid_t hAttrTypeID = H5Aget_type( hAttr ); hid_t hAttrNativeType = H5Tget_native_type( hAttrTypeID, H5T_DIR_DEFAULT ); if( H5Tget_class( hAttrNativeType ) == H5T_STRING ) { int nAttrSize = H5Tget_size( hAttrTypeID ); char *pachBuffer = (char *) CPLCalloc(nAttrSize+1,1); H5Aread( hAttr, hAttrNativeType, pachBuffer ); osResult = pachBuffer; CPLFree( pachBuffer ); retVal = true; } else { if( bReportError ) CPLError( CE_Failure, CPLE_AppDefined, "Attribute %s of unsupported type for conversion to string.", pszAttrName ); retVal = false; } H5Tclose( hAttrNativeType ); H5Tclose( hAttrTypeID ); H5Aclose( hAttr ); return retVal; }
void AbstractHdf5Access::SetUnlimitedDatasetId() { // Now deal with the unlimited dimension // In terms of an Unlimited dimension dataset: // * Files pre - r16738 (inc. Release 3.1 and earlier) use simply "Time" for "Data"'s unlimited variable. // * Files generated by r16738 - r18257 used "<DatasetName>_Time" for "<DatasetName>"'s unlimited variable, // - These are not to be used and there is no backwards compatibility for them, since they weren't in a release. // * Files post r18257 (inc. Release 3.2 onwards) use "<DatasetName>_Unlimited" for "<DatasetName>"'s // unlimited variable, // - a new attribute "Name" has been added to the Unlimited Dataset to allow it to assign // any name to the unlimited variable. Which can then be easily read by Hdf5DataReader. // - if this dataset is missing we look for simply "Time" to remain backwards compatible with Releases <= 3.1 if (DoesDatasetExist(mDatasetName + "_Unlimited")) { mUnlimitedDatasetId = H5Dopen(mFileId, (mDatasetName + "_Unlimited").c_str()); hid_t name_attribute_id = H5Aopen_name(mUnlimitedDatasetId, "Name"); hid_t unit_attribute_id = H5Aopen_name(mUnlimitedDatasetId, "Unit"); hid_t attribute_type = H5Aget_type(name_attribute_id); // Read into it. char* string_array = (char *)malloc(sizeof(char)*MAX_STRING_SIZE); H5Aread( name_attribute_id, attribute_type, string_array); std::string name_string(&string_array[0]); mUnlimitedDimensionName = name_string; H5Aread( unit_attribute_id, attribute_type, string_array); std::string unit_string(&string_array[0]); mUnlimitedDimensionUnit = unit_string; free(string_array); H5Tclose(attribute_type); H5Aclose(name_attribute_id); H5Aclose(unit_attribute_id); } else if (DoesDatasetExist("Time")) { mUnlimitedDimensionName = "Time"; mUnlimitedDimensionUnit = "msec"; mUnlimitedDatasetId = H5Dopen(mFileId, mUnlimitedDimensionName.c_str()); } else { NEVER_REACHED; } mIsUnlimitedDimensionSet = true; }
estatus_t e5_read_attr_list( hid_t e5_group_id, e5_attr* e5_attr_list) { int i; estatus_t status = E5_SUCCESS; hid_t h5_type; hid_t h5_native_type; hid_t h5_space; hid_t e5_attribute_id; for(i = 0; e5_attr_list && e5_attr_list[i].name != 0; i++) { e5_attr *attr = &e5_attr_list[i]; if(attr->name == 0 || strlen(attr->name) < 1) continue; e5_info(e5_group_id, "Reading attribute [name='%s']\n", attr->name); if(H5Aexists(e5_group_id, attr->name) <= 0) { status = E5_INVALID_ATTRIBUTE; e5_error(e5_group_id, status, "Invalid name requested for attribute [type='%d', name='%s', value='%p']\n", attr->type, attr->name, attr->value); continue; } e5_attribute_id = H5Aopen(e5_group_id, attr->name, H5P_DEFAULT); h5_type = H5Aget_type(e5_attribute_id); h5_space = H5Aget_space(e5_attribute_id); h5_native_type = e5_get_native_h5_type(h5_type); attr->type = e5_convert_hdf_type(h5_type); if(e5_is_valid_type(attr->type) != E5_TRUE) { status = E5_INVALID_TYPE; e5_error(e5_group_id, status, "Invalid type requested for attribute [type='%d', name='%s', value='%p']\n", attr->type, attr->name, attr->value); continue; } H5Aread(e5_attribute_id, h5_native_type, attr->value); H5Aclose(e5_attribute_id); e5_info(e5_group_id, "Read attribute [type='%s', name='%s', value='%p']\n", e5_typename(attr->type), attr->name, attr->value); } return status; }
void ReadStringT<std::string,char>( hid_t iParent, const std::string &iAttrName, std::string &oString ) { ABCA_ASSERT( iParent >= 0, "Invalid parent in ReadStringT" ); // Open the attribute. hid_t attrId = H5Aopen( iParent, iAttrName.c_str(), H5P_DEFAULT ); ABCA_ASSERT( attrId >= 0, "Couldn't open attribute named: " << iAttrName ); AttrCloser attrCloser( attrId ); // Checking code. hid_t attrFtype = H5Aget_type( attrId ); DtypeCloser dtypeCloser( attrFtype ); ssize_t numChars = H5Tget_size( attrFtype ); ABCA_ASSERT( numChars >= 0, "ReadStringT() H5Aget_size() failed" ); // Read and check space { hid_t attrSpace = H5Aget_space( attrId ); ABCA_ASSERT( attrSpace >= 0, "Couldn't get dataspace for attribute: " << iAttrName ); DspaceCloser dspaceCloser( attrSpace ); H5S_class_t attrSpaceClass = H5Sget_simple_extent_type( attrSpace ); ABCA_ASSERT( attrSpaceClass == H5S_SCALAR, "Tried to read non-scalar attribute: " << iAttrName << " as scalar" ); } // Create temporary char storage buffer. std::vector<char> charStorage( ( size_t )( 1 + numChars ), ( char )0 ); // Read into it. herr_t status = H5Aread( attrId, attrFtype, ( void * )&charStorage.front() ); ABCA_ASSERT( status >= 0, "Couldn't read from attribute: " << iAttrName ); // Return it. oString = ( const char * )&charStorage.front(); }
static int NC4_get_propattr(NC_HDF5_FILE_INFO_T* h5) { int ncstat = NC_NOERR; size_t size; H5T_class_t t_class; hid_t grp = -1; hid_t attid = -1; hid_t aspace = -1; hid_t atype = -1; hid_t ntype = -1; herr_t herr = 0; char* text = NULL; /* Get root group */ grp = h5->root_grp->hdf_grpid; /* get root group */ /* Try to extract the NCPROPS attribute */ if(H5Aexists(grp,NCPROPS) > 0) { /* Does exist */ attid = H5Aopen_name(grp, NCPROPS); herr = -1; aspace = H5Aget_space(attid); /* dimensions of attribute data */ atype = H5Aget_type(attid); /* Verify that atype and size */ t_class = H5Tget_class(atype); if(t_class != H5T_STRING) {ncstat = NC_EATTMETA; goto done;} size = H5Tget_size(atype); if(size == 0) goto done; text = (char*)malloc(size+1); if(text == NULL) {ncstat = NC_ENOMEM; goto done;} HCHECK((ntype = H5Tget_native_type(atype, H5T_DIR_ASCEND))); HCHECK((H5Aread(attid, ntype, text))); /* Make sure its null terminated */ text[size] = '\0'; /* Try to parse text */ ncstat = NC4_properties_parse(&h5->fileinfo->propattr,text); herr = 0; } done: if(attid >= 0) HCHECK((H5Aclose(attid))); if(aspace >= 0) HCHECK((H5Sclose(aspace))); if(ntype >= 0) HCHECK((H5Tclose(ntype))); if(atype >= 0) HCHECK((H5Tclose(atype))); if(text != NULL) free(text); return ncstat; }
char HVLStringAttribute::read(std::string & str) { hid_t ftype = H5Aget_type(fObjectId); H5T_class_t type_class = H5Tget_class (ftype); if (type_class != H5T_STRING) { std::cout<<"\n type class is not H5T_STRING"; return HStringAttribute::read(str); } char * t[1]; herr_t status = H5Aread(fObjectId, dataType(), &t); if(status < 0) return 0; str = std::string(t[0]); free( t[0]); return 1; }
void h5_att_str(hid_t file_id, char *group, char *name, char *value) { int ii, kk; void *buf = NULL; hid_t attr_id; H5O_info_t object_info; char *attr_name = (char *) MALLOC(sizeof(char)*50); H5Oget_info_by_name(file_id, group, &object_info, H5P_DEFAULT); for (ii=0; ii<object_info.num_attrs; ii++) { attr_id = H5Aopen_by_idx(file_id, group, H5_INDEX_NAME, H5_ITER_NATIVE, ii, H5P_DEFAULT, H5P_DEFAULT); H5Aget_name(attr_id, 50, attr_name); if (strcmp_case(name, attr_name) == 0) { hid_t attr_type = H5Aget_type(attr_id); H5T_class_t data_type = H5Tget_native_type(attr_type, H5T_DIR_DEFAULT); size_t data_size = H5Tget_size(data_type); hid_t attr_space = H5Aget_space(attr_id); hsize_t dims[H5S_MAX_RANK]; int rank = H5Sget_simple_extent_dims(attr_space, dims, NULL); hsize_t elements = 1; for (kk=0; kk<rank; kk++) elements *= dims[kk]; buf = (void *) MALLOC((unsigned)(elements*data_size)); H5Aread(attr_id, attr_type, buf); H5Tclose(attr_type); H5Tclose(data_type); H5Sclose(attr_space); } else { H5Aclose(attr_id); continue; } H5Aclose(attr_id); } if (buf) { strcpy(value, buf); FREE(buf); } else strcpy(value, "???"); FREE(attr_name); }
herr_t VsH5Attribute::getStringValue(std::string* val) { hid_t atype = H5Aget_type(getId()); H5T_class_t type = H5Tget_class(atype); hid_t aspace = H5Aget_space(getId()); size_t rank = H5Sget_simple_extent_ndims(aspace); if (type != H5T_STRING) { VsLog::warningLog() <<"VsH5Attribute::getStringValue() - Requested attribute " <<getShortName() <<" is not a string value." <<std::endl; val->clear(); return -1; } //Yes, this is correct (rank != 0) if (rank != 0) { VsLog::warningLog() <<"VsH5Attribute::getStringValue() - Requested attribute " <<getShortName() <<" is not a string value." <<std::endl; val->clear(); return -1; } size_t len = H5Aget_storage_size(getId()); val->resize(len); char* v = new char[len]; herr_t err = H5Aread(getId(), atype, v); // JRC: is this right? // err = H5Aread(id, H5T_NATIVE_CHAR, v); for (size_t i = 0; i < len; ++i) { if (v[i] == 0) { //This happens when a program (IDL, HdfView) declares a string of length 5 //and uses the 5th character for the null terminator //In h5dump this shows up as a string "mesh" being declared as 5 characters //If dumped to a terminal, it prints like so: "mesh^@" VsLog::warningLog() <<"VsH5Attribute::getStringValue() - Found null char inside string attribute: " <<getFullName() <<std::endl; val->resize(i); break; } (*val)[i] = v[i]; } delete [] v; return err; }
static int NC4_get_propattr(NC_HDF5_FILE_INFO_T* h5) { int ncstat = NC_NOERR; size_t size; H5T_class_t t_class; char text[NCPROPS_LENGTH+1]; hid_t grp = -1; hid_t attid = -1; hid_t aspace = -1; hid_t atype = -1; hid_t ntype = -1; herr_t herr = 0; /* Get root group */ grp = h5->root_grp->hdf_grpid; /* get root group */ /* Try to extract the NCPROPS attribute */ attid = H5Aopen_name(grp, NCPROPS); if(attid >= 0) { herr = -1; aspace = H5Aget_space(attid); /* dimensions of attribute data */ atype = H5Aget_type(attid); /* Verify that atype and size */ t_class = H5Tget_class(atype); if(t_class != H5T_STRING) {ncstat = NC_EATTMETA; goto done;} size = H5Tget_size(atype); if(size != NCPROPS_LENGTH) {ncstat = NC_EATTMETA; goto done;} HCHECK((ntype = H5Tget_native_type(atype, H5T_DIR_ASCEND))); HCHECK((H5Aread(attid, ntype, text))); /* Try to parse text */ strncpy(h5->fileinfo->propattr.text,text,NCPROPS_LENGTH); h5->fileinfo->propattr.text[NCPROPS_LENGTH-1] = '\0'; ncstat = NC4_properties_parse(&h5->fileinfo->propattr); herr = 0; } done: if(attid >= 0) HCHECK((H5Aclose(attid))); if(aspace >= 0) HCHECK((H5Sclose(aspace))); if(ntype >= 0) HCHECK((H5Tclose(ntype))); if(atype >= 0) HCHECK((H5Tclose(atype))); return ncstat; }
hdf5_datatype::hdf5_datatype(hdf5_annotation const& annotation) : hdf5_object(H5Aget_type(annotation.get_id())), type_class_(H5Tget_class(get_id())) { if(annotation.get_id() < 0) { boost::serialization::throw_exception( hdf5_archive_exception( hdf5_archive_exception::hdf5_archive_annotation_access_error ) ); } if(get_id() < 0) { boost::serialization::throw_exception( hdf5_archive_exception( hdf5_archive_exception::hdf5_archive_datatype_access_error ) ); } }
/// Return the attribute name of obj, and "" if the attribute does not exist. void h5_read_attribute(hid_t id, std::string const &name, std::string & s) { s = ""; // if the attribute is not present, return 0 if (H5LTfind_attribute(id, name.c_str()) == 0) return; // not present attribute attr = H5Aopen(id, name.c_str(), H5P_DEFAULT); if (!attr.is_valid()) TRIQS_RUNTIME_ERROR << "Cannot open the attribute " << name; dataspace space = H5Aget_space(attr); int rank = H5Sget_simple_extent_ndims(space); if (rank != 0) TRIQS_RUNTIME_ERROR << "Reading a string attribute and got rank !=0"; datatype strdatatype = H5Aget_type(attr); std::vector<char> buf(H5Aget_storage_size(attr) + 1, 0x00); auto err = H5Aread(attr, strdatatype, (void *)(&buf[0])); if (err < 0) TRIQS_RUNTIME_ERROR << "Cannot read the attribute " << name; s.append(&(buf.front())); }
void DCAttribute::readAttribute(const char* name, hid_t parent, void* dst) throw (DCException) { hid_t attr = H5Aopen(parent, name, H5P_DEFAULT); if (attr < 0) throw DCException(getExceptionString(name, "Attribute could not be opened for reading")); hid_t attr_type = H5Aget_type(attr); if (attr_type < 0) { H5Aclose(attr); throw DCException(getExceptionString(name, "Could not get type of attribute")); } if (H5Aread(attr, attr_type, dst) < 0) { H5Aclose(attr); throw DCException(getExceptionString(name, "Attribute could not be read")); } H5Aclose(attr); }