//-------------------------------------------------------------------------- // Function: Attribute::read ///\brief Reads data from this attribute. ///\param mem_type - IN: Attribute datatype (in memory) ///\param buf - OUT: Buffer for read data ///\exception H5::AttributeIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- void Attribute::read( const DataType& mem_type, void *buf ) const { herr_t ret_value = H5Aread( id, mem_type.getId(), buf ); if( ret_value < 0 ) { throw AttributeIException("Attribute::read", "H5Aread failed"); } }
//-------------------------------------------------------------------------- // Function: Attribute::write ///\brief Writes data to this attribute. ///\param mem_type - IN: Attribute datatype (in memory) ///\param buf - IN: Data to be written ///\exception H5::AttributeIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- void Attribute::write( const DataType& mem_type, const void *buf ) const { herr_t ret_value = H5Awrite( id, mem_type.getId(), buf ); if( ret_value < 0 ) { throw AttributeIException("Attribute::write", "H5Awrite failed"); } }
//-------------------------------------------------------------------------- // Function: DSetCreatPropList::getFillValue ///\brief Retrieves a dataset fill value ///\param fvalue_type - IN: Data type for the value passed via \a value ///\param value - OUT: Pointer to buffer to hold the retrieved fill value ///\exception H5::PropListIException ///\par Description /// The fill value is returned through \a value pointer /// and the memory is allocated by the caller. The fill /// value will be converted from its current data type to the /// specified by \a fvalue_type. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- void DSetCreatPropList::getFillValue( const DataType& fvalue_type, void* value ) const { herr_t ret_value = H5Pget_fill_value( id, fvalue_type.getId(), value ); if( ret_value < 0 ) { throw PropListIException("DSetCreatPropList::getFillValue", "H5Pget_fill_value failed"); } }
//-------------------------------------------------------------------------- // Function: DataType::find ///\brief Finds a conversion function that can handle a conversion /// from this datatype to the specified datatype, \a dest. ///\param dest - IN: Destination datatype ///\param pcdata - IN: Pointer to type conversion data ///\return Pointer to a suitable conversion function ///\exception H5::DataTypeIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- H5T_conv_t DataType::find( const DataType& dest, H5T_cdata_t **pcdata ) const { // Call C routine to find the conversion function H5T_conv_t func = H5Tfind( id, dest.getId(), pcdata ); if( func == NULL ) { throw DataTypeIException(inMemFunc("find"), "H5Tfind returns a NULL function"); } return( func ); }
//-------------------------------------------------------------------------- // Function: DataSet::fillMemBuf ///\brief This is an overloaded member function, provided for convenience. /// It differs from the above function in that it only takes the /// the last three arguments. ///\param buf - IN/OUT: Memory buffer to fill selection within ///\param buf_type - IN: Datatype of the elements in buffer ///\param space - IN: Dataspace describing memory buffer & containing selection to use ///\exception H5::DataSetIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- void DataSet::fillMemBuf(void *buf, DataType& buf_type, DataSpace& space) { hid_t buf_type_id = buf_type.getId(); hid_t space_id = space.getId(); herr_t ret_value = H5Dfill(NULL, buf_type_id, buf, buf_type_id, space_id); if( ret_value < 0 ) { throw DataSetIException("DataSet::fillMemBuf", "H5Dfill failed"); } }
//-------------------------------------------------------------------------- // Function: DataType::unregister ///\brief Removes a conversion function from all conversion paths. ///\param pers - IN: Conversion option /// \li \c H5T_PERS_HARD for hard conversion functions /// \li \c H5T_PERS_SOFT for soft conversion functions. ///\param name - IN: Name displayed in diagnostic output. ///\param dest - IN: Destination datatype. ///\param func - IN: Function to convert between source and /// destination datatypes. ///\exception H5::DataTypeIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- void DataType::unregister( H5T_pers_t pers, const char* name, const DataType& dest, H5T_conv_t func ) const { hid_t dest_id = dest.getId(); // get id of the dest datatype for C API // Call C routine H5Tunregister to remove the conversion function herr_t ret_value = H5Tunregister( pers, name, id, dest_id, func ); if( ret_value < 0 ) { throw DataTypeIException(inMemFunc("unregister"), "H5Tunregister failed"); } }
//-------------------------------------------------------------------------- // Function: DataSet::vlenReclaim ///\brief Reclaims VL datatype memory buffers. ///\param type - IN: Datatype, which is the datatype stored in the buffer ///\param space - IN: Selection for the memory buffer to free the /// VL datatypes within ///\param xfer_plist - IN: Property list used to create the buffer ///\param buf - IN: Pointer to the buffer to be reclaimed ///\exception H5::DataSetIException // Programmer Binh-Minh Ribler - 2000 //\parDescription // This function has better prototype for the users than the // other, which might be removed at some point. BMR - 2006/12/20 //-------------------------------------------------------------------------- void DataSet::vlenReclaim(void* buf, const DataType& type, const DataSpace& space, const DSetMemXferPropList& xfer_plist) { // Obtain identifiers for C API hid_t type_id = type.getId(); hid_t space_id = space.getId(); hid_t xfer_plist_id = xfer_plist.getId(); herr_t ret_value = H5Dvlen_reclaim(type_id, space_id, xfer_plist_id, buf); if (ret_value < 0) { throw DataSetIException("DataSet::vlenReclaim", "H5Dvlen_reclaim failed"); } }
//-------------------------------------------------------------------------- // Function: DataSet::iterateElems ///\brief Iterates over all selected elements in a dataspace. ///\param buf - IN/OUT: Pointer to the buffer in memory containing the /// elements to iterate over ///\param type - IN: Datatype for the elements stored in \a buf ///\param space - IN: Dataspace for \a buf. Also contains the selection /// to iterate over. ///\param op - IN: Function pointer to the routine to be called for /// each element in \a buf iterated over ///\param op_data - IN/OUT: Pointer to any user-defined data associated /// with the operation ///\exception H5::DataSetIException ///\note This function may not work correctly yet - it's still /// under development. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- int DataSet::iterateElems( void* buf, const DataType& type, const DataSpace& space, H5D_operator_t op, void* op_data ) { // Obtain identifiers for C API hid_t type_id = type.getId(); hid_t space_id = space.getId(); herr_t ret_value = H5Diterate( buf, type_id, space_id, op, op_data ); if( ret_value >= 0 ) return( ret_value ); else // raise exception when H5Diterate returns a negative value { throw DataSetIException("DataSet::iterateElems", "H5Diterate failed"); } }
//-------------------------------------------------------------------------- // Function: DataSet::write ///\brief Writes raw data from an application buffer to a dataset. ///\param buf - IN: Buffer containing data to be written ///\param mem_type - IN: Memory datatype ///\param mem_space - IN: Memory dataspace ///\param file_space - IN: Dataset's dataspace in the file ///\param xfer_plist - IN: Transfer property list for this I/O operation ///\exception H5::DataSetIException ///\par Description /// This function writes raw data from an application buffer /// \a buf to a dataset, converting from memory datatype /// \a mem_type and dataspace \a mem_space to file datatype /// and dataspace. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- void DataSet::write( const void* buf, const DataType& mem_type, const DataSpace& mem_space, const DataSpace& file_space, const DSetMemXferPropList& xfer_plist ) const { // Obtain identifiers for C API hid_t mem_type_id = mem_type.getId(); hid_t mem_space_id = mem_space.getId(); hid_t file_space_id = file_space.getId(); hid_t xfer_plist_id = xfer_plist.getId(); herr_t ret_value = H5Dwrite( id, mem_type_id, mem_space_id, file_space_id, xfer_plist_id, buf ); if( ret_value < 0 ) { throw DataSetIException("DataSet::write", "H5Dwrite failed"); } }
QOSGWidgetFactory::ValueEditorCF QOSGWidgetFactory::getValueEditor(const DataType &dataType) { SINFO << "QOSGWidgetFactory::getValueEditor: " << dataType.getId() << endLog; ValueEditorCF cFunc = NULL; ValueEditorMapIt it = _typeEditorMap.find(dataType.getId()); if(it != _typeEditorMap.end()) { cFunc = it->second; } else if(strstr(dataType.getCName(), "Ptr") != NULL) { cFunc = _cfFCPtrEditor; } else { cFunc = _cfDefaultEditor; } return cFunc; }
//-------------------------------------------------------------------------- // Function: DataType::convert ///\brief Converts data from this datatype to the specified datatypes. ///\param dest - IN: Destination datatype ///\param nelmts - IN: Size of array \a buf ///\param buf - IN/OUT: Array containing pre- and post-conversion /// values ///\param background - IN: Optional backgroud buffer ///\param plist - IN: Property list - default to PropList::DEFAULT ///\return Pointer to a suitable conversion function ///\exception H5::DataTypeIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- void DataType::convert( const DataType& dest, size_t nelmts, void *buf, void *background, const PropList& plist ) const { // Get identifiers for C API hid_t dest_id = dest.getId(); hid_t plist_id = plist.getId(); // Call C routine H5Tconvert to convert the data herr_t ret_value; ret_value = H5Tconvert( id, dest_id, nelmts, buf, background, plist_id ); if( ret_value < 0 ) { throw DataTypeIException(inMemFunc("convert"), "H5Tconvert failed"); } }
//-------------------------------------------------------------------------- // Function: DataType::operator== ///\brief Compares this DataType against the given one to determines /// whether the two objects refer to the same actual datatype. ///\param compared_type - IN: Reference to the datatype to compare ///\return true if the datatypes are equal, and false, otherwise. ///\exception H5::DataTypeIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- bool DataType::operator==(const DataType& compared_type ) const { // Call C routine H5Tequal to determines whether two datatype // identifiers refer to the same datatype htri_t ret_value = H5Tequal( id, compared_type.getId() ); if( ret_value > 0 ) return true; else if( ret_value == 0 ) return false; else { throw DataTypeIException(inMemFunc("operator=="), "H5Tequal returns negative value"); } }
//-------------------------------------------------------------------------- // Function: CompType::insertMember ///\brief Inserts a new member to this compound datatype. ///\param name - IN: Name of the new member ///\param offset - IN: Offset in memory structure of the field to insert ///\param new_member - IN: New member to be inserted ///\exception H5::DataTypeIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- void CompType::insertMember( const H5std_string& name, size_t offset, const DataType& new_member ) const { // Convert string to C-string const char* name_C; name_C = name.c_str(); // name_C refers to the contents of name as a C-str hid_t new_member_id = new_member.getId(); // get new_member id for C API // Call C routine H5Tinsert to add the new member herr_t ret_value = H5Tinsert( id, name_C, offset, new_member_id ); if( ret_value < 0 ) { throw DataTypeIException("CompType::insertMember", "H5Tinsert failed"); } }
//-------------------------------------------------------------------------- // Function: DataType::copy ///\brief Copies an existing datatype to this datatype object ///\param like_type - IN: Datatype to be copied ///\exception H5::DataTypeIException // Programmer Binh-Minh Ribler - 2000 // Modification // - Replaced resetIdComponent() with decRefCount() to use C // library ID reference counting mechanism - BMR, Jun 1, 2004 // - Replaced decRefCount with close() to let the C library // handle the reference counting - BMR, Jun 1, 2006 //-------------------------------------------------------------------------- void DataType::copy( const DataType& like_type ) { // close the current data type before copying like_type to this object try { close(); } catch (Exception close_error) { throw DataTypeIException(inMemFunc("copy"), close_error.getDetailMsg()); } // call C routine to copy the datatype id = H5Tcopy( like_type.getId() ); if( id < 0 ) throw DataTypeIException(inMemFunc("copy"), "H5Tcopy failed"); }
//-------------------------------------------------------------------------- // Function: DataSet::write ///\brief This is an overloaded member function, provided for convenience. /// It takes a reference to a \c H5std_string for the buffer. // Programmer Binh-Minh Ribler - 2000 // Modification // Jul 2009 // Modified to pass the buffer into H5Dwrite properly depending // whether the dataset has variable- or fixed-length string. //-------------------------------------------------------------------------- void DataSet::write( const H5std_string& strg, const DataType& mem_type, const DataSpace& mem_space, const DataSpace& file_space, const DSetMemXferPropList& xfer_plist ) const { // Check if this attribute has variable-len string or fixed-len string and // proceed appropriately. htri_t is_variable_len = H5Tis_variable_str(mem_type.getId()); if (is_variable_len < 0) { throw DataSetIException("DataSet::write", "H5Tis_variable_str failed"); } // Obtain identifiers for C API hid_t mem_type_id = mem_type.getId(); hid_t mem_space_id = mem_space.getId(); hid_t file_space_id = file_space.getId(); hid_t xfer_plist_id = xfer_plist.getId(); // Convert string to C-string const char* strg_C; strg_C = strg.c_str(); // strg_C refers to the contents of strg as a C-str herr_t ret_value = 0; // Pass string in differently depends on variable or fixed length if (!is_variable_len) { ret_value = H5Dwrite( id, mem_type_id, mem_space_id, file_space_id, xfer_plist_id, strg_C ); } else { // passing string argument by address ret_value = H5Dwrite( id, mem_type_id, mem_space_id, file_space_id, xfer_plist_id, &strg_C ); } if (ret_value < 0) { throw DataSetIException("DataSet::write", "H5Dwrite failed"); } }
//-------------------------------------------------------------------------- // Function: DataSet::getVlenBufSize ///\brief Returns the number of bytes required to store VL data. ///\return Amount of storage ///\exception H5::DataSetIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- hsize_t DataSet::getVlenBufSize( DataType& type, DataSpace& space ) const { // Obtain identifiers for C API hid_t type_id = type.getId(); hid_t space_id = space.getId(); hsize_t size; // for amount of storage herr_t ret_value = H5Dvlen_get_buf_size( id, type_id, space_id, &size ); if( ret_value < 0 ) { throw DataSetIException("DataSet::getVlenBufSize", "H5Dvlen_get_buf_size failed"); } return( size ); }
//-------------------------------------------------------------------------- // Function: H5Object::createAttribute ///\brief Creates an attribute for a group, dataset, or named datatype. ///\param name - IN: Name of the attribute ///\param data_type - IN: Datatype for the attribute ///\param data_space - IN: Dataspace for the attribute - only simple /// dataspaces are allowed at this time ///\param create_plist - IN: Creation property list - default to /// PropList::DEFAULT ///\return Attribute instance ///\exception H5::AttributeIException ///\par Description /// The attribute name specified in \a name must be unique. /// Attempting to create an attribute with the same name as an /// existing attribute will raise an exception, leaving the /// pre-existing attribute intact. To overwrite an existing /// attribute with a new attribute of the same name, first /// delete the existing one with \c H5Object::removeAttr, then /// recreate it with this function. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- Attribute H5Object::createAttribute( const char* name, const DataType& data_type, const DataSpace& data_space, const PropList& create_plist ) const { hid_t type_id = data_type.getId(); hid_t space_id = data_space.getId(); hid_t plist_id = create_plist.getId(); hid_t attr_id = H5Acreate2(getId(), name, type_id, space_id, plist_id, H5P_DEFAULT ); // If the attribute id is valid, create and return the Attribute object if( attr_id > 0 ) { Attribute attr( attr_id ); return( attr ); } else throw AttributeIException(inMemFunc("createAttribute"), "H5Acreate2 failed"); }
//-------------------------------------------------------------------------- // Function: ArrayType overloaded constructor ///\brief Creates a new array data type based on the specified /// \a base_type. ///\param base_type - IN: Existing datatype ///\param ndims - IN: Rank of the array, [0..H5S_MAX_RANK] ///\param dims - IN: Size of each array dimension ///\exception H5::DataTypeIException // Programmer Binh-Minh Ribler - May 2004 //-------------------------------------------------------------------------- ArrayType::ArrayType(const DataType& base_type, int ndims, const hsize_t* dims) : DataType() { // Call C API to create an array data type hid_t new_type_id = H5Tarray_create2(base_type.getId(), ndims, dims); if (new_type_id < 0) throw DataTypeIException("ArrayType constructor", "H5Tarray_create2 failed"); // Set the id and rank for this object id = new_type_id; rank = ndims; // Allocate space then set the dimensions as provided by caller dimensions = new hsize_t[rank]; for (int i = 0; i < rank; i++) dimensions[i] = dims[i]; }
//-------------------------------------------------------------------------- // Function: Attribute::p_read_variable_len (private) // brief Reads a variable length \a H5std_string from an attribute. // param mem_type - IN: Attribute datatype (in memory) // param strg - IN: Buffer for read string // exception H5::AttributeIException // Programmer Binh-Minh Ribler - Jul, 2009 // Modification // Jul 2009 // Separated the variable length case from the original // Attribute::read //-------------------------------------------------------------------------- void Attribute::p_read_variable_len(const DataType& mem_type, H5std_string& strg) const { // Prepare and call C API to read attribute. char *strg_C; // Read attribute, no allocation for variable-len string; C library will herr_t ret_value = H5Aread(id, mem_type.getId(), &strg_C); if( ret_value < 0 ) { throw AttributeIException("Attribute::read", "H5Aread failed"); } // Get string from the C char* and release resource allocated by C API strg = strg_C; HDfree(strg_C); }
// [[Rcpp::export]] bool WriteAttribute(XPtr<Attribute> attribute, SEXP mat, char datatype, NumericVector count) { try { size_t stsize = -1; DataType dsettype = attribute->getDataType(); if (!H5Tis_variable_str(dsettype.getId())) { stsize = dsettype.getSize(); } DTYPE dtype = GetTypechar(datatype); const void *buf = ConvertBuffer(mat, dtype, stsize); attribute->write(GetDataType(dtype, stsize), buf); return TRUE; } catch(Exception& error) { string msg = error.getDetailMsg() + " in " + error.getFuncName(); throw Rcpp::exception(msg.c_str()); } }
//-------------------------------------------------------------------------- // Function: Attribute::read ///\brief This is an overloaded member function, provided for convenience. /// It reads a \a H5std_string from this attribute. ///\param mem_type - IN: Attribute datatype (in memory) ///\param strg - IN: Buffer for read string ///\exception H5::AttributeIException // Programmer Binh-Minh Ribler - Apr, 2003 // Modification // Mar 2008 // Corrected a misunderstanding that H5Aread would allocate // space for the buffer. Obtained the attribute size and // allocated memory properly. - BMR // Apr 2009 // Used getInMemDataSize to get attribute data size. - BMR // Jul 2009 // Divided into specific private functions for fixed- and // variable-len string data: p_read_fixed_len and // p_read_variable_len. This should improve readability. //-------------------------------------------------------------------------- void Attribute::read(const DataType& mem_type, H5std_string& strg) const { // Check if this attribute has variable-len string or fixed-len string and // proceed appropriately. htri_t is_variable_len = H5Tis_variable_str(mem_type.getId()); if (is_variable_len < 0) { throw AttributeIException("Attribute::read", "H5Tis_variable_str failed"); } if (!is_variable_len) // only allocate for fixed-len string { p_read_fixed_len(mem_type, strg); } else { p_read_variable_len(mem_type, strg); } }
//-------------------------------------------------------------------------- // Function: CommonFG::createDataSet ///\brief Creates a new dataset at this location. ///\param name - IN: Name of the dataset to create ///\param data_type - IN: Datatype of the dataset ///\param data_space - IN: Dataspace for the dataset ///\param create_plist - IN: Creation properly list for the dataset ///\return DataSet instance ///\exception H5::FileIException or H5::GroupIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- DataSet CommonFG::createDataSet( const char* name, const DataType& data_type, const DataSpace& data_space, const DSetCreatPropList& create_plist ) const { // Obtain identifiers for C API hid_t type_id = data_type.getId(); hid_t space_id = data_space.getId(); hid_t create_plist_id = create_plist.getId(); // Call C routine H5Dcreate2 to create the named dataset hid_t dataset_id = H5Dcreate2( getLocId(), name, type_id, space_id, H5P_DEFAULT, create_plist_id, H5P_DEFAULT ); // If the creation of the dataset failed, throw an exception if( dataset_id < 0 ) { throwException("createDataSet", "H5Dcreate2 failed"); } // No failure, create and return the DataSet object DataSet dataset( dataset_id ); return( dataset ); }
//-------------------------------------------------------------------------- // Function: Attribute::p_read_fixed_len (private) // brief Reads a fixed length \a H5std_string from an attribute. // param mem_type - IN: Attribute datatype (in memory) // param strg - IN: Buffer for read string // exception H5::AttributeIException // Programmer Binh-Minh Ribler - Jul, 2009 // Modification // Jul 2009 // Separated the fixed length case from the original // Attribute::read //-------------------------------------------------------------------------- void Attribute::p_read_fixed_len(const DataType& mem_type, H5std_string& strg) const { // Only allocate for fixed-len string. // Get the size of the attribute's data size_t attr_size = getInMemDataSize(); // If there is data, allocate buffer and read it. if (attr_size > 0) { char *strg_C = new char[(size_t)attr_size+1]; herr_t ret_value = H5Aread(id, mem_type.getId(), strg_C); if( ret_value < 0 ) { delete []strg_C; // de-allocate for fixed-len string throw AttributeIException("Attribute::read", "H5Aread failed"); } // Get string from the C char* and release resource allocated locally strg_C[attr_size] = '\0'; strg = strg_C; delete []strg_C; } }
//-------------------------------------------------------------------------- // Function: DataType copy constructor ///\brief Copy constructor: makes a copy of the original DataType object. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- DataType::DataType(const DataType& original) : H5Object() { id = original.getId(); incRefCount(); // increment number of references to this id }