summaryrefslogtreecommitdiffstats
path: root/c++/src
diff options
context:
space:
mode:
authorBinh-Minh Ribler <bmribler@hdfgroup.org>2009-07-27 04:00:17 (GMT)
committerBinh-Minh Ribler <bmribler@hdfgroup.org>2009-07-27 04:00:17 (GMT)
commitc1ad3676c4b56b80663d271ac367a65cc41c39ab (patch)
tree42f19502941ff1d02d45b92a7f8a0a3f6945002e /c++/src
parent405ea51bb7728dc696e29694b22e81dc4b6ce2c1 (diff)
downloadhdf5-c1ad3676c4b56b80663d271ac367a65cc41c39ab.zip
hdf5-c1ad3676c4b56b80663d271ac367a65cc41c39ab.tar.gz
hdf5-c1ad3676c4b56b80663d271ac367a65cc41c39ab.tar.bz2
[svn-r17238] Purpose: Fix bug and improve readability
Description: - Revised DataSet::write to pass in correct string buffer - Added member function DataSet::getInMemDataSize() to simplify getting the dataset's data size in memory. - Added private functions for reading fixed- and variable-length string data: p_read_fixed_len and p_read_variable_len. - Added tests to write/read array of strings to datasets. Platforms tested: Linux/32 2.6 (jam) FreeBSD/64 6.3 (liberty) SunOS 5.10 (linew)
Diffstat (limited to 'c++/src')
-rw-r--r--c++/src/H5AbstractDs.cpp5
-rw-r--r--c++/src/H5AbstractDs.h3
-rw-r--r--c++/src/H5Attribute.cpp14
-rw-r--r--c++/src/H5Attribute.h6
-rw-r--r--c++/src/H5DataSet.cpp203
-rw-r--r--c++/src/H5DataSet.h9
6 files changed, 214 insertions, 26 deletions
diff --git a/c++/src/H5AbstractDs.cpp b/c++/src/H5AbstractDs.cpp
index a61cc88..19eeb33 100644
--- a/c++/src/H5AbstractDs.cpp
+++ b/c++/src/H5AbstractDs.cpp
@@ -25,6 +25,11 @@
#include "H5CommonFG.h"
#include "H5Alltypes.h"
+#include <iostream> // remove when done
+
+ using std::cerr;
+ using std::endl;
+
#ifndef H5_NO_NAMESPACE
namespace H5 {
#endif
diff --git a/c++/src/H5AbstractDs.h b/c++/src/H5AbstractDs.h
index c98e5e1..1d04d6c 100644
--- a/c++/src/H5AbstractDs.h
+++ b/c++/src/H5AbstractDs.h
@@ -51,6 +51,9 @@ class H5_DLLCPP AbstractDs {
StrType getStrType() const;
VarLenType getVarLenType() const;
+ // Gets the size in memory of this abstract dataset.
+ virtual size_t getInMemDataSize() const = 0;
+
// Gets the dataspace of this abstract dataset - pure virtual.
virtual DataSpace getSpace() const = 0;
diff --git a/c++/src/H5Attribute.cpp b/c++/src/H5Attribute.cpp
index b1ab73e..6de5e63 100644
--- a/c++/src/H5Attribute.cpp
+++ b/c++/src/H5Attribute.cpp
@@ -198,35 +198,37 @@ void Attribute::read(const DataType& mem_type, H5std_string& strg) const
//--------------------------------------------------------------------------
size_t Attribute::getInMemDataSize() const
{
+ char *func = "Attribute::getInMemDataSize";
+
// Get the data type of this attribute
hid_t mem_type_id = H5Aget_type(id);
- if (mem_type_id <= 0)
+ if( mem_type_id < 0 )
{
- throw AttributeIException("Attribute::getInMemDataSize", "H5Aget_type failed");
+ throw AttributeIException(func, "H5Aget_type failed");
}
// Get the data type's size
hid_t native_type = H5Tget_native_type(mem_type_id, H5T_DIR_DEFAULT);
if (native_type < 0)
{
- throw AttributeIException("Attribute::getInMemDataSize", "H5Tget_native_type failed");
+ throw AttributeIException(func, "H5Tget_native_type failed");
}
size_t type_size = H5Tget_size(native_type);
if (type_size == 0)
{
- throw AttributeIException("Attribute::getInMemDataSize", "H5Tget_size failed");
+ throw AttributeIException(func, "H5Tget_size failed");
}
// Get number of elements of the attribute
hid_t space_id = H5Aget_space(id);
if (space_id < 0)
{
- throw AttributeIException("Attribute::getInMemDataSize", "H5Aget_space failed");
+ throw AttributeIException(func, "H5Aget_space failed");
}
hssize_t num_elements = H5Sget_simple_extent_npoints(space_id);
if (num_elements < 0)
{
- throw AttributeIException("Attribute::getInMemDataSize", "H5Sget_simple_extent_npoints failed");
+ throw AttributeIException(func, "H5Sget_simple_extent_npoints failed");
}
// Calculate and return the size of the data
diff --git a/c++/src/H5Attribute.h b/c++/src/H5Attribute.h
index abece10..284e5bc 100644
--- a/c++/src/H5Attribute.h
+++ b/c++/src/H5Attribute.h
@@ -38,10 +38,10 @@ class H5_DLLCPP Attribute : public AbstractDs, public IdComponent {
virtual DataSpace getSpace() const;
// Returns the amount of storage size required for this attribute.
- hsize_t getStorageSize() const;
+ virtual hsize_t getStorageSize() const;
// Returns the in memory size of this attribute's data.
- size_t getInMemDataSize() const;
+ virtual size_t getInMemDataSize() const;
// Reads data from this attribute.
void read( const DataType& mem_type, void *buf ) const;
@@ -82,7 +82,7 @@ class H5_DLLCPP Attribute : public AbstractDs, public IdComponent {
// sub-types
virtual hid_t p_get_type() const;
- // Reads variable or fixed len strings
+ // Reads variable or fixed len strings from this attribute.
void p_read_variable_len(const DataType& mem_type, H5std_string& strg) const;
void p_read_fixed_len(const DataType& mem_type, H5std_string& strg) const;
diff --git a/c++/src/H5DataSet.cpp b/c++/src/H5DataSet.cpp
index 494d3ea..cddb5d4 100644
--- a/c++/src/H5DataSet.cpp
+++ b/c++/src/H5DataSet.cpp
@@ -37,6 +37,7 @@
#include "H5File.h"
#include "H5Attribute.h"
#include "H5DataSet.h"
+#include "H5private.h" // for HDfree
#ifndef H5_NO_NAMESPACE
namespace H5 {
@@ -220,6 +221,53 @@ hsize_t DataSet::getStorageSize() const
}
//--------------------------------------------------------------------------
+// Function: DataSet::getInMemDataSize
+///\brief Gets the size in memory of the dataset's data.
+///\return Size of data (in memory)
+///\exception H5::DataSetIException
+// Programmer Binh-Minh Ribler - Apr 2009
+//--------------------------------------------------------------------------
+size_t DataSet::getInMemDataSize() const
+{
+ char *func = "DataSet::getInMemDataSize";
+
+ // Get the data type of this dataset
+ hid_t mem_type_id = H5Dget_type(id);
+ if( mem_type_id < 0 )
+ {
+ throw DataSetIException(func, "H5Dget_type failed");
+ }
+
+ // Get the data type's size
+ hid_t native_type = H5Tget_native_type(mem_type_id, H5T_DIR_DEFAULT);
+ if (native_type < 0)
+ {
+ throw DataSetIException(func, "H5Tget_native_type failed");
+ }
+ size_t type_size = H5Tget_size(native_type);
+ if (type_size == 0)
+ {
+ throw DataSetIException(func, "H5Tget_size failed");
+ }
+
+ // Get number of elements of the dataset
+ hid_t space_id = H5Dget_space(id); // first get its data space
+ if (space_id < 0)
+ {
+ throw DataSetIException(func, "H5Dget_space failed");
+ }
+ hssize_t num_elements = H5Sget_simple_extent_npoints(space_id);
+ if (num_elements < 0)
+ {
+ throw DataSetIException(func, "H5Sget_simple_extent_npoints failed");
+ }
+
+ // Calculate and return the size of the data
+ size_t data_size = type_size * num_elements;
+ return(data_size);
+}
+
+//--------------------------------------------------------------------------
// Function: DataSet::getOffset
///\brief Returns the address of this dataset in the file.
///\return Address of dataset
@@ -364,20 +412,45 @@ void DataSet::read( void* buf, const DataType& mem_type, const DataSpace& mem_sp
// Function: DataSet::read
///\brief This is an overloaded member function, provided for convenience.
/// It takes a reference to a \c std::string for the buffer.
+///\param buf - IN: Buffer for read data
+///\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
// Programmer Binh-Minh Ribler - 2000
-//--------------------------------------------------------------------------
-void DataSet::read( H5std_string& strg, const DataType& mem_type, const DataSpace& mem_space, const DataSpace& file_space, const DSetMemXferPropList& xfer_plist ) const
+// Modification
+// Jul 2009
+// Follow the change to Attribute::read and use the following
+// private functions to read datasets with fixed- and
+// variable-length string:
+// DataSet::p_read_fixed_len and
+// DataSet::p_read_variable_len
+//--------------------------------------------------------------------------
+void DataSet::read(H5std_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
+ // Check if this dataset 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::read", "H5Tis_variable_str failed");
+ }
- // Use the overloaded member to read
- read(strg_C, mem_type, mem_space, file_space, xfer_plist);
+ // 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();
- // Get the String and clean up
- strg = strg_C;
- delete []strg_C;
+ if (!is_variable_len) // only allocate for fixed-len string
+ {
+ p_read_fixed_len(mem_type_id, mem_space_id, file_space_id, xfer_plist_id, strg);
+ }
+ else
+ {
+ p_read_variable_len(mem_type_id, mem_space_id, file_space_id, xfer_plist_id, strg);
+ }
}
//--------------------------------------------------------------------------
@@ -416,15 +489,46 @@ void DataSet::write( const void* buf, const DataType& mem_type, const DataSpace&
///\brief This is an overloaded member function, provided for convenience.
/// It takes a reference to a \c std::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
{
- // 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
+ // 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;
- // Use the overloaded member
- write(strg_C, mem_type, mem_space, file_space, xfer_plist);
+ // 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");
+ }
}
//--------------------------------------------------------------------------
@@ -586,7 +690,74 @@ hid_t DataSet::getId() const
}
//--------------------------------------------------------------------------
-// Function: DataSet::p_setId
+// Function: DataSet::p_read_fixed_len (private)
+// brief Reads a fixed length \a H5std_string from an dataset.
+// param mem_type - IN: DataSet datatype (in memory)
+// param strg - IN: Buffer for read string
+// exception H5::DataSetIException
+// Programmer Binh-Minh Ribler - Jul, 2009
+// Modification
+// Jul 2009
+// Added in follow to the change in Attribute::read
+//--------------------------------------------------------------------------
+void DataSet::p_read_fixed_len(const hid_t mem_type_id, const hid_t mem_space_id, const hid_t file_space_id, const hid_t xfer_plist_id, H5std_string& strg) const
+{
+ // Only allocate for fixed-len string.
+
+ // Get the size of the dataset's data
+ size_t attr_size = getInMemDataSize();
+
+ // If there is data, allocate buffer and read it.
+ if (attr_size > 0)
+ {
+ char *strg_C = NULL;
+
+ strg_C = new char [(size_t)attr_size+1];
+ herr_t ret_value = H5Dread(id, mem_type_id, mem_space_id, file_space_id, xfer_plist_id, strg_C);
+
+ if( ret_value < 0 )
+ {
+ delete []strg_C; // de-allocate for fixed-len string
+ throw DataSetIException("DataSet::read", "H5Dread failed for fixed length string");
+ }
+
+ // Get string from the C char* and release resource allocated locally
+ strg = strg_C;
+ delete []strg_C;
+ }
+}
+
+//--------------------------------------------------------------------------
+// Function: DataSet::p_read_variable_len (private)
+// brief Reads a variable length \a H5std_string from an dataset.
+// param mem_type - IN: DataSet datatype (in memory)
+// param strg - IN: Buffer for read string
+// exception H5::DataSetIException
+// Programmer Binh-Minh Ribler - Jul, 2009
+// Modification
+// Jul 2009
+// Added in follow to the change in Attribute::read
+//--------------------------------------------------------------------------
+void DataSet::p_read_variable_len(const hid_t mem_type_id, const hid_t mem_space_id, const hid_t file_space_id, const hid_t xfer_plist_id, H5std_string& strg) const
+{
+ // Prepare and call C API to read dataset.
+ char *strg_C;
+
+ // Read dataset, no allocation for variable-len string; C library will
+ herr_t ret_value = H5Dread(id, mem_type_id, mem_space_id, file_space_id, xfer_plist_id, &strg_C);
+
+ if( ret_value < 0 )
+ {
+ throw DataSetIException("DataSet::read", "H5Dread failed for variable length string");
+ }
+
+ // Get string from the C char* and release resource allocated by C API
+ strg = strg_C;
+ HDfree(strg_C);
+}
+
+//--------------------------------------------------------------------------
+// Function: DataSet::p_setId (private)
///\brief Sets the identifier of this object to a new value.
///
///\exception H5::IdComponentException when the attempt to close the HDF5
diff --git a/c++/src/H5DataSet.h b/c++/src/H5DataSet.h
index f26d775..3d9183d 100644
--- a/c++/src/H5DataSet.h
+++ b/c++/src/H5DataSet.h
@@ -49,7 +49,10 @@ class H5_DLLCPP DataSet : public H5Object, public AbstractDs {
void getSpaceStatus(H5D_space_status_t& status) const;
// Returns the amount of storage size required for this dataset.
- hsize_t getStorageSize() const;
+ virtual hsize_t getStorageSize() const;
+
+ // Returns the in memory size of this attribute's data.
+ virtual size_t getInMemDataSize() const;
// Returns the number of bytes required to store VL data.
hsize_t getVlenBufSize( DataType& type, DataSpace& space ) const;
@@ -113,6 +116,10 @@ class H5_DLLCPP DataSet : public H5Object, public AbstractDs {
// sub-types
virtual hid_t p_get_type() const;
+ // Reads variable or fixed len strings from this dataset.
+ void p_read_fixed_len(const hid_t mem_type_id, const hid_t mem_space_id, const hid_t file_space_id, const hid_t xfer_plist_id, H5std_string& strg) const;
+ void p_read_variable_len(const hid_t mem_type_id, const hid_t mem_space_id, const hid_t file_space_id, const hid_t xfer_plist_id, H5std_string& strg) const;
+
protected:
// Sets the dataset id.
virtual void p_setId(const hid_t new_id);