diff options
author | Binh-Minh Ribler <bmribler@hdfgroup.org> | 2003-06-07 04:02:11 (GMT) |
---|---|---|
committer | Binh-Minh Ribler <bmribler@hdfgroup.org> | 2003-06-07 04:02:11 (GMT) |
commit | 5d1b56cb8170363220e21c8c8c38d2bddef27939 (patch) | |
tree | d1450c0b8b2b1a7628371342250372071ba754ae /c++/src/H5Attribute.cpp | |
parent | 4e4ab0d320597f3aa9a45170d92e8d578cc203db (diff) | |
download | hdf5-5d1b56cb8170363220e21c8c8c38d2bddef27939.zip hdf5-5d1b56cb8170363220e21c8c8c38d2bddef27939.tar.gz hdf5-5d1b56cb8170363220e21c8c8c38d2bddef27939.tar.bz2 |
[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)
Diffstat (limited to 'c++/src/H5Attribute.cpp')
-rw-r--r-- | c++/src/H5Attribute.cpp | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/c++/src/H5Attribute.cpp b/c++/src/H5Attribute.cpp index 09e53d3..5c9dfb3 100644 --- a/c++/src/H5Attribute.cpp +++ b/c++/src/H5Attribute.cpp @@ -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; } } |