summaryrefslogtreecommitdiffstats
path: root/c++/examples
diff options
context:
space:
mode:
authorBinh-Minh Ribler <bmribler@hdfgroup.org>2003-06-25 17:25:11 (GMT)
committerBinh-Minh Ribler <bmribler@hdfgroup.org>2003-06-25 17:25:11 (GMT)
commitfa77b6691e218c49163f477c84fb8664fb5cda8b (patch)
treec2007e35ae40e79718b3b16146a2f0bf2a149818 /c++/examples
parentc9f162a58c26355b70724b211877e2d3ce9c3d94 (diff)
downloadhdf5-fa77b6691e218c49163f477c84fb8664fb5cda8b.zip
hdf5-fa77b6691e218c49163f477c84fb8664fb5cda8b.tar.gz
hdf5-fa77b6691e218c49163f477c84fb8664fb5cda8b.tar.bz2
[svn-r7104] Purpose:
Bug fix Description: Missing returning error for failures occur in C++ examples. Solution: Added "return -1;" when an exception is caught. Also, turned off automatic error printing so that the C++ API will catch and handle the failures. Platforms: SunOS 5.7 (arabica) Linux 2.4 (eirene) IRIX 6.5.11 (modi4)
Diffstat (limited to 'c++/examples')
-rw-r--r--c++/examples/chunks.cpp20
-rw-r--r--c++/examples/compound.cpp10
-rw-r--r--c++/examples/create.cpp78
-rw-r--r--c++/examples/extend_ds.cpp14
-rw-r--r--c++/examples/h5group.cpp130
-rw-r--r--c++/examples/readdata.cpp80
-rw-r--r--c++/examples/writedata.cpp42
7 files changed, 240 insertions, 134 deletions
diff --git a/c++/examples/chunks.cpp b/c++/examples/chunks.cpp
index 556472f..3cc4e20 100644
--- a/c++/examples/chunks.cpp
+++ b/c++/examples/chunks.cpp
@@ -45,21 +45,30 @@ int main (void)
try
{
/*
+ * Turn off the auto-printing when failure occurs so that we can
+ * handle the errors appropriately
+ */
+ Exception::dontPrint();
+
+ /*
* Open the file and the dataset.
*/
H5File file( FILE_NAME, H5F_ACC_RDONLY );
DataSet dataset = file.openDataSet( DATASET_NAME );
/*
- * Get dataset rank and dimension.
+ * Get filespace for rank and dimension
*/
- // Get filespace first.
DataSpace filespace = dataset.getSpace();
- // Get number of dimensions in the file dataspace
+ /*
+ * Get number of dimensions in the file dataspace
+ */
int rank = filespace.getSimpleExtentNdims();
- // Get and print the dimension sizes of the file dataspace
+ /*
+ * Get and print the dimension sizes of the file dataspace
+ */
hsize_t dims[2]; // dataset dimensions
rank = filespace.getSimpleExtentDims( dims );
cout << "dataset rank = " << rank << ", dimensions "
@@ -199,18 +208,21 @@ int main (void)
catch( FileIException error )
{
error.printError();
+ return -1;
}
// catch failure caused by the DataSet operations
catch( DataSetIException error )
{
error.printError();
+ return -1;
}
// catch failure caused by the DataSpace operations
catch( DataSpaceIException error )
{
error.printError();
+ return -1;
}
return 0;
diff --git a/c++/examples/compound.cpp b/c++/examples/compound.cpp
index 68de654..d3163d6 100644
--- a/c++/examples/compound.cpp
+++ b/c++/examples/compound.cpp
@@ -70,6 +70,12 @@ int main(void)
}
/*
+ * Turn off the auto-printing when failure occurs so that we can
+ * handle the errors appropriately
+ */
+ Exception::dontPrint();
+
+ /*
* Create the data space.
*/
hsize_t dim[] = {LENGTH}; /* Dataspace dimensions */
@@ -181,24 +187,28 @@ int main(void)
catch( FileIException error )
{
error.printError();
+ return -1;
}
// catch failure caused by the DataSet operations
catch( DataSetIException error )
{
error.printError();
+ return -1;
}
// catch failure caused by the DataSpace operations
catch( DataSpaceIException error )
{
error.printError();
+ return -1;
}
// catch failure caused by the DataSpace operations
catch( DataTypeIException error )
{
error.printError();
+ return -1;
}
return 0;
diff --git a/c++/examples/create.cpp b/c++/examples/create.cpp
index 06952f9..06d5d4d 100644
--- a/c++/examples/create.cpp
+++ b/c++/examples/create.cpp
@@ -12,9 +12,9 @@
* access to either file, you may request a copy from hdfhelp@ncsa.uiuc.edu. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
-//
-// This example writes a dataset to a new HDF5 file.
-//
+/*
+ * This example writes a dataset to a new HDF5 file.
+ */
#include <string>
@@ -37,9 +37,9 @@ const int RANK = 2;
int main (void)
{
- //
- // Data initialization.
- //
+ /*
+ * Data initialization.
+ */
int i, j;
int data[NX][NY]; // buffer for data to write
for (j = 0; j < NX; j++)
@@ -47,50 +47,56 @@ int main (void)
for (i = 0; i < NY; i++)
data[j][i] = i + j;
}
- //
- // 0 1 2 3 4 5
- // 1 2 3 4 5 6
- // 2 3 4 5 6 7
- // 3 4 5 6 7 8
- // 4 5 6 7 8 9
- //
+ /*
+ * 0 1 2 3 4 5
+ * 1 2 3 4 5 6
+ * 2 3 4 5 6 7
+ * 3 4 5 6 7 8
+ * 4 5 6 7 8 9
+ */
// Try block to detect exceptions raised by any of the calls inside it
try
{
- //
- // Create a new file using H5F_ACC_TRUNC access,
- // default file creation properties, and default file
- // access properties.
- //
+ /*
+ * Turn off the auto-printing when failure occurs so that we can
+ * handle the errors appropriately
+ */
+ Exception::dontPrint();
+
+ /*
+ * Create a new file using H5F_ACC_TRUNC access,
+ * default file creation properties, and default file
+ * access properties.
+ */
H5File file( FILE_NAME, H5F_ACC_TRUNC );
- //
- // Define the size of the array and create the data space for fixed
- // size dataset.
- //
+ /*
+ * Define the size of the array and create the data space for fixed
+ * size dataset.
+ */
hsize_t dimsf[2]; // dataset dimensions
dimsf[0] = NX;
dimsf[1] = NY;
DataSpace dataspace( RANK, dimsf );
- //
- // Define datatype for the data in the file.
- // We will store little endian INT numbers.
- //
+ /*
+ * Define datatype for the data in the file.
+ * We will store little endian INT numbers.
+ */
IntType datatype( PredType::NATIVE_INT );
datatype.setOrder( H5T_ORDER_LE );
- //
- // Create a new dataset within the file using defined dataspace and
- // datatype and default dataset creation properties.
- //
+ /*
+ * Create a new dataset within the file using defined dataspace and
+ * datatype and default dataset creation properties.
+ */
DataSet dataset = file.createDataSet( DATASET_NAME, datatype, dataspace );
- //
- // Write the data to the dataset using default memory space, file
- // space, and transfer properties.
- //
+ /*
+ * Write the data to the dataset using default memory space, file
+ * space, and transfer properties.
+ */
dataset.write( data, PredType::NATIVE_INT );
} // end of try block
@@ -98,24 +104,28 @@ int main (void)
catch( FileIException error )
{
error.printError();
+ return -1;
}
// catch failure caused by the DataSet operations
catch( DataSetIException error )
{
error.printError();
+ return -1;
}
// catch failure caused by the DataSpace operations
catch( DataSpaceIException error )
{
error.printError();
+ return -1;
}
// catch failure caused by the DataSpace operations
catch( DataTypeIException error )
{
error.printError();
+ return -1;
}
return 0; // successfully terminated
diff --git a/c++/examples/extend_ds.cpp b/c++/examples/extend_ds.cpp
index f748cde..34a50c3 100644
--- a/c++/examples/extend_ds.cpp
+++ b/c++/examples/extend_ds.cpp
@@ -40,10 +40,18 @@ const int RANK = 2;
int main (void)
{
- // Try block to detect exceptions raised by any of the calls inside it
+ /*
+ * Try block to detect exceptions raised by any of the calls inside it
+ */
try
{
/*
+ * Turn off the auto-printing when failure occurs so that we can
+ * handle the errors appropriately
+ */
+ Exception::dontPrint();
+
+ /*
* Create the data space with unlimited dimensions.
*/
hsize_t dims[2] = { 3, 3}; // dataset dimensions at creation
@@ -192,24 +200,28 @@ int main (void)
catch( FileIException error )
{
error.printError();
+ return -1;
}
// catch failure caused by the DataSet operations
catch( DataSetIException error )
{
error.printError();
+ return -1;
}
// catch failure caused by the DataSpace operations
catch( DataSpaceIException error )
{
error.printError();
+ return -1;
}
// catch failure caused by the DataSpace operations
catch( DataTypeIException error )
{
error.printError();
+ return -1;
}
return 0;
}
diff --git a/c++/examples/h5group.cpp b/c++/examples/h5group.cpp
index 95161da..d7f3c66 100644
--- a/c++/examples/h5group.cpp
+++ b/c++/examples/h5group.cpp
@@ -12,14 +12,14 @@
* access to either file, you may request a copy from hdfhelp@ncsa.uiuc.edu. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
-//
-// This example creates a group in the file and dataset in the group.
-// Hard link to the group object is created and the dataset is accessed
-// under different names.
-// Iterator function is used to find the object names in the root group.
-// Note that the C++ API iterator function is not completed yet, thus
-// the C version is used in this example.
-//
+/*
+ * This example creates a group in the file and dataset in the group.
+ * Hard link to the group object is created and the dataset is accessed
+ * under different names.
+ * Iterator function is used to find the object names in the root group.
+ * Note that the C++ API iterator function is not completed yet, thus
+ * the C version is used in this example.
+ */
#include <string>
#ifdef OLD_HEADER_FILENAME
@@ -48,19 +48,29 @@ int main(void)
// Try block to detect exceptions raised by any of the calls inside it
try
{
- // Create the named file, truncating the existing one if any,
- // using default create and access property lists.
+ /*
+ * Turn off the auto-printing when failure occurs so that we can
+ * handle the errors appropriately
+ */
+ Exception::dontPrint();
+
+ /*
+ * Create the named file, truncating the existing one if any,
+ * using default create and access property lists.
+ */
H5File *file = new H5File( FILE_NAME, H5F_ACC_TRUNC );
- // Create a group in the file
+ /*
+ * Create a group in the file
+ */
Group* group = new Group( file->createGroup( "/Data" ));
- //
- // Create dataset "Compressed Data" in the group using absolute
- // name. Dataset creation property list is modified to use
- // GZIP compression with the compression effort set to 6.
- // Note that compression can be used only when dataset is chunked.
- //
+ /*
+ * Create dataset "Compressed Data" in the group using absolute
+ * name. Dataset creation property list is modified to use
+ * GZIP compression with the compression effort set to 6.
+ * Note that compression can be used only when dataset is chunked.
+ */
dims[0] = 1000;
dims[1] = 20;
cdims[0] = 20;
@@ -74,22 +84,22 @@ int main(void)
DataSet* dataset = new DataSet( file->createDataSet( "/Data/Compressed_Data", PredType::NATIVE_INT, dataspace, ds_creatplist ));
- //
- // Close the dataset and the file.
- //
+ /*
+ * Close the dataset and the file.
+ */
delete dataset;
delete group;
delete file;
- //
- // Now reopen the file and group in the file.
- //
+ /*
+ * Now reopen the file and group in the file.
+ */
file = new H5File( FILE_NAME, H5F_ACC_RDWR );
group = new Group( file->openGroup( "Data" ));
- //
- // Access "Compressed_Data" dataset in the group.
- //
+ /*
+ * Access "Compressed_Data" dataset in the group.
+ */
try { // to determine if the dataset exists in the group
dataset = new DataSet( group->openDataSet( "Compressed_Data" ));
}
@@ -99,20 +109,20 @@ int main(void)
}
cout << "dataset \"/Data/Compressed_Data\" is open" << endl;
- //
- // Close the dataset.
- //
+ /*
+ * Close the dataset.
+ */
delete dataset;
- //
- // Create hard link to the Data group.
- //
+ /*
+ * Create hard link to the Data group.
+ */
file->link( H5G_LINK_HARD, "Data", "Data_new" );
- //
- // We can access "Compressed_Data" dataset using created
- // hard link "Data_new".
- //
+ /*
+ * We can access "Compressed_Data" dataset using created
+ * hard link "Data_new".
+ */
try { // to determine if the dataset exists in the file
dataset = new DataSet( file->openDataSet( "/Data_new/Compressed_Data" ));
}
@@ -122,23 +132,23 @@ int main(void)
}
cout << "dataset \"/Data_new/Compressed_Data\" is open" << endl;
- //
- // Close the dataset.
- //
+ /*
+ * Close the dataset.
+ */
delete dataset;
- //
- // Use iterator to see the names of the objects in the file
- // root directory.
- //
+ /*
+ * Use iterator to see the names of the objects in the file
+ * root directory.
+ */
cout << endl << "Iterating over elements in the file" << endl;
herr_t idx = H5Giterate(file->getId(), "/", NULL, file_info, NULL);
cout << endl;
- //
- // Unlink name "Data" and use iterator to see the names
- // of the objects in the file root direvtory.
- //
+ /*
+ * Unlink name "Data" and use iterator to see the names
+ * of the objects in the file root direvtory.
+ */
cout << "Unlinking..." << endl;
try { // attempt to unlink the dataset
file->unlink( "Data" );
@@ -153,7 +163,9 @@ int main(void)
idx = H5Giterate(file->getId(), "/", NULL, file_info, NULL);
cout << endl;
- // Close the file.
+ /*
+ * Close the file.
+ */
delete file;
} // end of try block
@@ -162,43 +174,47 @@ int main(void)
catch( FileIException error )
{
error.printError();
+ return -1;
}
// catch failure caused by the DataSet operations
catch( DataSetIException error )
{
error.printError();
+ return -1;
}
// catch failure caused by the DataSpace operations
catch( DataSpaceIException error )
{
error.printError();
+ return -1;
}
// catch failure caused by the Attribute operations
catch( AttributeIException error )
{
error.printError();
+ return -1;
}
return 0;
}
-//
-// Operator function.
-//
+/*
+ * Operator function.
+ */
herr_t
file_info(hid_t loc_id, const char *name, void *opdata)
{
hid_t group;
- //
- // Open the group using its name.
- //
+ /*
+ * Open the group using its name.
+ */
group = H5Gopen(loc_id, name);
- //
- // Display group name.
- //
+ /*
+ * Display group name.
+ */
cout << "Name : " << name << endl;
H5Gclose(group);
diff --git a/c++/examples/readdata.cpp b/c++/examples/readdata.cpp
index fa5e141..1797d27 100644
--- a/c++/examples/readdata.cpp
+++ b/c++/examples/readdata.cpp
@@ -42,9 +42,9 @@ const int RANK_OUT = 3;
int main (void)
{
- //
- // Output buffer initialization.
- //
+ /*
+ * Output buffer initialization.
+ */
int i, j, k;
int data_out[NX][NY][NZ ]; /* output buffer */
for (j = 0; j < NX; j++)
@@ -56,52 +56,78 @@ int main (void)
}
}
- // Try block to detect exceptions raised by any of the calls inside it
+ /*
+ * Try block to detect exceptions raised by any of the calls inside it
+ */
try
{
- //
- // Open the specified file and the specified dataset in the file.
- //
+ /*
+ * Turn off the auto-printing when failure occurs so that we can
+ * handle the errors appropriately
+ */
+ Exception::dontPrint();
+
+ /*
+ * Open the specified file and the specified dataset in the file.
+ */
H5File file( FILE_NAME, H5F_ACC_RDONLY );
DataSet dataset = file.openDataSet( DATASET_NAME );
- // Get the class of the datatype that is used by the dataset.
+ /*
+ * Get the class of the datatype that is used by the dataset.
+ */
H5T_class_t type_class = dataset.getTypeClass();
- // Get class of datatype and print message if it's an integer.
+ /*
+ * Get class of datatype and print message if it's an integer.
+ */
if( type_class == H5T_INTEGER )
{
cout << "Data set has INTEGER type" << endl;
- // Get the integer datatype
+ /*
+ * Get the integer datatype
+ */
IntType intype = dataset.getIntType();
- // Get order of datatype and print message if it's a little endian.
+ /*
+ * Get order of datatype and print message if it's a little endian.
+ */
string order_string;
H5T_order_t order = intype.getOrder( order_string );
cout << order_string << endl;
- // Get size of the data element stored in file and print it.
+ /*
+ * Get size of the data element stored in file and print it.
+ */
size_t size = intype.getSize();
cout << "Data size is " << size << endl;
}
- // Get dataspace of the dataset.
+ /*
+ * Get dataspace of the dataset.
+ */
DataSpace dataspace = dataset.getSpace();
- // Get the number of dimensions in the dataspace.
+ /*
+ * Get the number of dimensions in the dataspace.
+ */
int rank = dataspace.getSimpleExtentNdims();
- // Get the dimension size of each dimension in the dataspace and
- // display them.
+ /*
+ * Get the dimension size of each dimension in the dataspace and
+ * display them.
+ */
hsize_t dims_out[2];
int ndims = dataspace.getSimpleExtentDims( dims_out, NULL);
cout << "rank " << rank << ", dimensions " <<
(unsigned long)(dims_out[0]) << " x " <<
(unsigned long)(dims_out[1]) << endl;
- // Define hyperslab in the dataset; implicitly giving strike and
- // block NULL.
+ /*
+ * Define hyperslab in the dataset; implicitly giving strike and
+ * block NULL.
+ */
hssize_t offset[2]; // hyperslab offset in the file
hsize_t count[2]; // size of the hyperslab in the file
offset[0] = 1;
@@ -110,14 +136,18 @@ int main (void)
count[1] = NY_SUB;
dataspace.selectHyperslab( H5S_SELECT_SET, count, offset );
- // Define the memory dataspace.
+ /*
+ * Define the memory dataspace.
+ */
hsize_t dimsm[3]; /* memory space dimensions */
dimsm[0] = NX;
dimsm[1] = NY;
dimsm[2] = NZ ;
DataSpace memspace( RANK_OUT, dimsm );
- // Define memory hyperslab.
+ /*
+ * Define memory hyperslab.
+ */
hssize_t offset_out[3]; // hyperslab offset in memory
hsize_t count_out[3]; // size of the hyperslab in memory
offset_out[0] = 3;
@@ -128,8 +158,10 @@ int main (void)
count_out[2] = 1;
memspace.selectHyperslab( H5S_SELECT_SET, count_out, offset_out );
- // Read data from hyperslab in the file into the hyperslab in
- // memory and display the data.
+ /*
+ * Read data from hyperslab in the file into the hyperslab in
+ * memory and display the data.
+ */
dataset.read( data_out, PredType::NATIVE_INT, memspace, dataspace );
for (j = 0; j < NX; j++)
@@ -153,24 +185,28 @@ int main (void)
catch( FileIException error )
{
error.printError();
+ return -1;
}
// catch failure caused by the DataSet operations
catch( DataSetIException error )
{
error.printError();
+ return -1;
}
// catch failure caused by the DataSpace operations
catch( DataSpaceIException error )
{
error.printError();
+ return -1;
}
// catch failure caused by the DataSpace operations
catch( DataTypeIException error )
{
error.printError();
+ return -1;
}
return 0; // successfully terminated
diff --git a/c++/examples/writedata.cpp b/c++/examples/writedata.cpp
index 2a7381c..fbaac1f 100644
--- a/c++/examples/writedata.cpp
+++ b/c++/examples/writedata.cpp
@@ -70,28 +70,34 @@ int main (void)
// Try block to detect exceptions raised by any of the calls inside it
try
{
- //
- //Create a file.
- //
+ /*
+ * Turn off the auto-printing when failure occurs so that we can
+ * handle the errors appropriately
+ */
+ Exception::dontPrint();
+
+ /*
+ * Create a file.
+ */
H5File* file = new H5File( FILE_NAME, H5F_ACC_TRUNC );
- //
- //Create dataspace for the dataset in the file.
- //
+ /*
+ * Create dataspace for the dataset in the file.
+ */
hsize_t fdim[] = {FSPACE_DIM1, FSPACE_DIM2}; // dim sizes of ds (on disk)
DataSpace fspace( FSPACE_RANK, fdim );
- //
- // Create dataset and write it into the file.
- //
+ /*
+ * Create dataset and write it into the file.
+ */
DataSet* dataset = new DataSet(
file->createDataSet( DATASET_NAME, PredType::NATIVE_INT, fspace ));
dataset->write( matrix, PredType::NATIVE_INT );
- //
- // Select hyperslab for the dataset in the file, using 3x2 blocks,
- // (4,3) stride and (2,4) count starting at the position (0,1).
- //
+ /*
+ * Select hyperslab for the dataset in the file, using 3x2 blocks,
+ * (4,3) stride and (2,4) count starting at the position (0,1).
+ */
hssize_t start[2]; // Start of hyperslab
hsize_t stride[2]; // Stride of hyperslab
hsize_t count[2]; // Block count
@@ -102,9 +108,9 @@ int main (void)
block[0] = 3; block[1] = 2;
fspace.selectHyperslab( H5S_SELECT_SET, count, start, stride, block);
- //
- // Create dataspace for the first dataset.
- //
+ /*
+ * Create dataspace for the first dataset.
+ */
hsize_t dim1[] = {MSPACE1_DIM}; /* Dimension size of the first dataset
(in memory) */
DataSpace mspace1( MSPACE1_RANK, dim1 );
@@ -220,19 +226,23 @@ int main (void)
catch( FileIException error )
{
error.printError();
+ return -1;
}
// catch failure caused by the DataSet operations
catch( DataSetIException error )
{
error.printError();
+ return -1;
}
// catch failure caused by the DataSpace operations
catch( DataSpaceIException error )
{
error.printError();
+ return -1;
}
return 0;
+
}