[svn-r6990] Purpose:

Bug fix and minor code enhancement

Description:
    Missing methods to read/write C++ String for an attribute and
    a dataset.

Solution:
    Added overloaded functions read and write to H5::Attribute and
    H5::DataSet.

    Also, added another constructor StrType so the need to separately
    set the length of the string type can be eliminated.  It's minor
    but convenient.

    Made some minor changes to make error messages more readable.

Platforms:
    SunOS 5.7 (arabica)
    Linux 2.4 (eirene)
    IRIX 6.5.11 (modi4)
    HPUX 11.00 (kelgia)
This commit is contained in:
Binh-Minh Ribler 2003-06-06 23:02:11 -05:00
parent 4e4ab0d320
commit 5d1b56cb81
12 changed files with 83 additions and 25 deletions

View File

@ -52,6 +52,33 @@ void Attribute::write( const DataType& mem_type, const void *buf ) const
}
}
void Attribute::write( const DataType& mem_type, const string& strg ) const
{
// 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 = H5Awrite( id, mem_type.getId(), strg_C );
if( ret_value < 0 )
{
throw AttributeIException("Attribute::write", "H5Awrite failed");
}
}
// Reads data from this attribute.
void Attribute::read( const DataType& mem_type, string& strg ) const
{
size_t size = mem_type.getSize();
char* strg_C = new char[size+1]; // temporary C-string for C API
herr_t ret_value = H5Aread( id, mem_type.getId(), strg_C );
if( ret_value < 0 )
{
throw AttributeIException("Attribute::read", "H5Aread failed");
}
strg = strg_C;
delete strg_C;
}
// Reads data from this attribute.
void Attribute::read( const DataType& mem_type, void *buf ) const
{
@ -109,7 +136,7 @@ ssize_t Attribute::getName( size_t buf_size, string& attr_name ) const
throw AttributeIException("Attribute::getName", "H5Aget_name failed");
}
// otherwise, convert the C string attribute name and return
attr_name = string( name_C );
attr_name = name_C;
delete name_C;
return( name_size );
}
@ -145,7 +172,7 @@ Attribute::~Attribute()
try {
resetIdComponent( this ); }
catch (Exception close_error) { // thrown by p_close
cerr << "Attribute::~Attribute" << close_error.getDetailMsg() << endl;
cerr << "Attribute::~Attribute - " << close_error.getDetailMsg() << endl;
}
}

View File

@ -24,9 +24,11 @@ class H5_DLLCPP Attribute : public AbstractDs {
public:
// Writes data to this attribute.
void write(const DataType& mem_type, const void *buf ) const;
void write(const DataType& mem_type, const string& strg ) const;
// Reads data from this attribute.
void read( const DataType& mem_type, void *buf ) const;
void read( const DataType& mem_type, string& strg ) const;
// Gets a copy of the dataspace for this attribute.
virtual DataSpace getSpace() const;

View File

@ -156,6 +156,20 @@ void DataSet::read( void* buf, const DataType& mem_type, const DataSpace& mem_sp
}
}
void DataSet::read( string& strg, const DataType& mem_type, const DataSpace& mem_space, const DataSpace& file_space, const DSetMemXferPropList& xfer_plist ) const
{
// Allocate C character string for reading
size_t size = mem_type.getSize();
char* strg_C = new char[size+1]; // temporary C-string for C API
// Use the overloaded member to read
read(strg_C, mem_type, mem_space, file_space, xfer_plist);
// Get the String and clean up
strg = strg_C;
delete strg_C;
}
// Writes raw data from an application buffer buffer to a dataset,
// converting from memory datatype and dataspace to file datatype
// and dataspace.
@ -174,6 +188,16 @@ void DataSet::write( const void* buf, const DataType& mem_type, const DataSpace&
}
}
void DataSet::write( const string& strg, const DataType& mem_type, const DataSpace& mem_space, const DataSpace& file_space, const DSetMemXferPropList& xfer_plist ) const
{
// 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
// Use the overloaded member
write(strg_C, mem_type, mem_space, file_space, xfer_plist);
}
// Iterates over all selected elements in a dataspace.
int DataSet::iterateElems( void* buf, const DataType& type, const DataSpace& space, H5D_operator_t op, void* op_data )
{
@ -262,7 +286,7 @@ DataSet::~DataSet()
try {
resetIdComponent( this ); }
catch (Exception close_error) { // thrown by p_close
cerr << "DataSet::~DataSet" << close_error.getDetailMsg() << endl;
cerr << "DataSet::~DataSet - " << close_error.getDetailMsg() << endl;
}
}

View File

@ -33,7 +33,7 @@ class H5_DLLCPP DataSet : public AbstractDs {
// Gets the storage size of this dataset.
hsize_t getStorageSize() const;
// - C version not yet implemented??
// not yet implemented??
hsize_t getVlenBufSize( DataType& type, DataSpace& space ) const;
void vlenReclaim( DataType& type, DataSpace& space, DSetMemXferPropList& xfer_plist, void* buf ) const;
@ -41,11 +41,13 @@ class H5_DLLCPP DataSet : public AbstractDs {
// The memory and file dataspaces and the transferring property list
// can be defaults.
void read( void* buf, const DataType& mem_type, const DataSpace& mem_space = DataSpace::ALL, const DataSpace& file_space = DataSpace::ALL, const DSetMemXferPropList& xfer_plist = DSetMemXferPropList::DEFAULT ) const;
void read( string& buf, const DataType& mem_type, const DataSpace& mem_space = DataSpace::ALL, const DataSpace& file_space = DataSpace::ALL, const DSetMemXferPropList& xfer_plist = DSetMemXferPropList::DEFAULT ) const;
// Writes the buffered data to this dataset.
// The memory and file dataspaces and the transferring property list
// can be defaults.
void write( const void* buf, const DataType& mem_type, const DataSpace& mem_space = DataSpace::ALL, const DataSpace& file_space = DataSpace::ALL, const DSetMemXferPropList& xfer_plist = DSetMemXferPropList::DEFAULT ) const;
void write( const string& buf, const DataType& mem_type, const DataSpace& mem_space = DataSpace::ALL, const DataSpace& file_space = DataSpace::ALL, const DSetMemXferPropList& xfer_plist = DSetMemXferPropList::DEFAULT ) const;
// Iterates the selected elements in the specified dataspace - not implemented in C++ style yet
int iterateElems( void* buf, const DataType& type, const DataSpace& space, H5D_operator_t op, void* op_data = NULL );

View File

@ -57,7 +57,11 @@ DataSpace::DataSpace( int rank, const hsize_t * dims, const hsize_t * maxdims) :
/* Constructor that takes an existing dataspace id
Description:
Uses an HDF5 id to create a DataSpace identifier instance. This id can be either an existing dataspace id or a default dataspace id. Design note: in the case of default dataspace, the identifier still has reference counter; the p_close function will take care of not to call H5Sclose on the default id.
Uses an HDF5 id to create a DataSpace identifier instance.
This id can be either an existing dataspace id or a default
dataspace id. Design note: in the case of default dataspace,
the identifier still has reference counter; the p_close function
will take care of not to call H5Sclose on the default id.
*/
DataSpace::DataSpace( const hid_t space_id ) : IdComponent( space_id ) {}
@ -361,7 +365,7 @@ DataSpace::~DataSpace()
try {
resetIdComponent( this ); }
catch (Exception close_error) { // thrown by p_close
cerr << "DataSpace::~DataSpace" << close_error.getDetailMsg() << endl;
cerr << "DataSpace::~DataSpace - " << close_error.getDetailMsg() << endl;
}
}

View File

@ -357,7 +357,7 @@ DataType::~DataType()
try {
resetIdComponent( this ); }
catch (Exception close_error) { // thrown by p_close
cerr << "DataType::~DataType" << close_error.getDetailMsg() << endl;
cerr << "DataType::~DataType - " << close_error.getDetailMsg() << endl;
}
}

View File

@ -18,7 +18,7 @@
#ifndef H5_NO_NAMESPACE
namespace H5 {
#ifndef H5_NO_STD
using namespace std;
using namespace std;
#endif // H5_NO_STD
#endif
@ -206,14 +206,6 @@ IdComponentException::IdComponentException(const string& func_name, const string
IdComponentException::IdComponentException(const char* func_name, const char* message) : Exception(func_name, message) {}
IdComponentException::~IdComponentException() {}
// The following are from Java API but not done here:
// AtomException, BtreeException, DataFiltersException,
// ExternalFileListException, FunctionEntryExitException,
// HeapException, InternalErrorException, LowLevelIOException,
// MetaDataCacheException, ResourceUnavailableException,
// SymbolTableException, ObjectHeaderException, FunctionArgumentException,
// DataStorageException
#ifndef H5_NO_NAMESPACE
} // end namespace
#endif

View File

@ -167,13 +167,6 @@ class H5_DLLCPP IdComponentException : public Exception {
virtual ~IdComponentException();
};
// The following are from Java API but not done here:
// AtomException, BtreeException, DataFiltersException,
// ExternalFilelistException, FunctionEntryExitException,
// HeapException, InternalErrorException, LowLevelIOException,
// MetaDataCacheException, ResourceUnavailableException,
// SymbolTableException, ObjectHeaderException, FunctionArgumentException,
// DataStorageException
#ifndef H5_NO_NAMESPACE
}
#endif

View File

@ -203,7 +203,7 @@ H5File::~H5File()
try {
resetIdComponent( this ); }
catch (Exception close_error) { // thrown by p_close
cerr << "H5File::~H5File" << close_error.getDetailMsg() << endl;
cerr << "H5File::~H5File - " << close_error.getDetailMsg() << endl;
}
}

View File

@ -156,7 +156,7 @@ Group::~Group()
try {
resetIdComponent( this ); }
catch (Exception close_error) { // thrown by p_close
cerr << "Group::~Group" << close_error.getDetailMsg() << endl;
cerr << "Group::~Group - " << close_error.getDetailMsg() << endl;
}
}

View File

@ -43,6 +43,17 @@ StrType::StrType( const PredType& pred_type ) : AtomType()
copy( pred_type );
}
// Creates a string type with a specified length - 1st argument could
// have been skipped, but this constructor will collide with the one
// that takes an existing id below
StrType::StrType( const PredType& pred_type, const size_t size ) : AtomType()
{
// use DataType::copy to make a copy of the string predefined type
// then set its length
copy(pred_type);
setSize(size);
}
// Creates a string datatype using an existing id
StrType::StrType( const hid_t existing_id ) : AtomType( existing_id ) {}

View File

@ -28,6 +28,9 @@ class H5_DLLCPP StrType : public AtomType {
// Creates a string type using a predefined type
StrType( const PredType& pred_type );
// Creates a string type with specified length
StrType( const PredType& pred_type, const size_t size );
// Creates a string datatype using an existing id
StrType( const hid_t existing_id );