[cig-commits] r11481 - cs/benchmark/cigma/trunk/src
luis at geodynamics.org
luis at geodynamics.org
Wed Mar 19 12:00:18 PDT 2008
Author: luis
Date: 2008-03-19 12:00:17 -0700 (Wed, 19 Mar 2008)
New Revision: 11481
Added:
cs/benchmark/cigma/trunk/src/HdfAttr.cpp
cs/benchmark/cigma/trunk/src/HdfAttr.h
cs/benchmark/cigma/trunk/src/HdfDataset.cpp
cs/benchmark/cigma/trunk/src/HdfDataset.h
cs/benchmark/cigma/trunk/src/HdfFile.cpp
cs/benchmark/cigma/trunk/src/HdfFile.h
Log:
Eliminate need for h5io library
Added: cs/benchmark/cigma/trunk/src/HdfAttr.cpp
===================================================================
--- cs/benchmark/cigma/trunk/src/HdfAttr.cpp (rev 0)
+++ cs/benchmark/cigma/trunk/src/HdfAttr.cpp 2008-03-19 19:00:17 UTC (rev 11481)
@@ -0,0 +1,465 @@
+#include <cassert>
+#include <cstring>
+#include <cstdlib>
+
+#include "HdfAttr.h"
+
+//
+// XXX: need copyright info and comments from h5io.h
+//
+
+using namespace HdfAttr;
+
+static herr_t attr_find_op(hid_t loc_id, const char *name, void *op_data)
+{
+ /* Define a default zero value for return. This will cause the
+ * iterator to continue if the palette attribute is not found yet.
+ */
+ int ret = 0;
+
+ char *attr_name = (char *)op_data;
+
+ /* Shut the compiler up */
+ loc_id = loc_id;
+
+ /*
+ * Define a positive value for return value if the attribute was
+ * found. This will cause the iterator to immediately return that
+ * positive value, indicating short-circuit success
+ */
+
+ if (strcmp(name, attr_name) == 0)
+ {
+ ret = 1;
+ }
+
+ return ret;
+}
+
+herr_t HdfAttr::find(hid_t loc_id, const char *attr_name)
+{
+ unsigned int attr_num;
+ herr_t ret;
+
+ attr_num = 0;
+ ret = H5Aiterate(loc_id, &attr_num, attr_find_op, (void *)attr_name);
+
+ return ret;
+}
+
+herr_t HdfAttr::get_dims(hid_t obj_id, const char *attr_name, int *rank, hsize_t *dims)
+{
+ hid_t attr_id;
+ hid_t space_id;
+ herr_t status;
+
+ /* Open the attribute */
+ attr_id = H5Aopen_name(obj_id, attr_name);
+ if (attr_id < 0)
+ {
+ return -1;
+ }
+
+ /* Get the dataspace handle */
+ space_id = H5Aget_space(attr_id);
+ if (space_id < 0)
+ {
+ goto out;
+ }
+
+ /* Get the number of dimensions */
+ *rank = H5Sget_simple_extent_ndims(space_id);
+ if (*rank < 0)
+ {
+ goto out;
+ }
+
+ /* Get dimensions */
+ status = H5Sget_simple_extent_dims(space_id, dims, NULL);
+ if (status < 0)
+ {
+ goto out;
+ }
+
+ /* Terminate access to the dataspace */
+ status = H5Sclose(space_id);
+ if (status < 0)
+ {
+ goto out;
+ }
+
+ /* End access to the attribute */
+ status = H5Aclose(attr_id);
+ if (status < 0)
+ {
+ goto out;
+ }
+
+ return 0;
+
+out:
+ H5Aclose(attr_id);
+ return -1;
+}
+
+
+// ---------------------------------------------------------------------------
+
+herr_t HdfAttr::get(hid_t obj_id, const char *attr_name, hid_t type_id, void *data)
+{
+ hid_t attr_id;
+ herr_t status;
+
+ attr_id = H5Aopen_name(obj_id, attr_name);
+ if (attr_id < 0)
+ {
+ return -1;
+ }
+
+ status = H5Aread(attr_id, type_id, data);
+ if (status < 0)
+ {
+ goto out;
+ }
+
+ status = H5Aclose(attr_id);
+ if (status < 0)
+ {
+ return -1;
+ }
+
+ return 0;
+
+out:
+ H5Aclose(attr_id);
+ return -1;
+}
+
+herr_t HdfAttr::set(hid_t obj_id, const char *attr_name, hid_t type_id, const void *data)
+{
+ hid_t space_id, attr_id;
+ herr_t status;
+
+ int has_attr;
+
+ /* Create the data space for the attribute. */
+ space_id = H5Screate(H5S_SCALAR);
+ if (space_id < 0)
+ {
+ goto out;
+ }
+
+ /* Verify if the attribute already exists. */
+ has_attr = HdfAttr::find(obj_id, attr_name);
+ if (has_attr == 1)
+ {
+ /* The attribute already exists. Delete it. */
+ status = H5Adelete(obj_id, attr_name);
+ if (status < 0)
+ {
+ goto out;
+ }
+ }
+
+ /* Create the attribute. */
+ attr_id = H5Acreate(obj_id, attr_name, type_id, space_id, H5P_DEFAULT);
+ if (attr_id < 0)
+ {
+ goto out;
+ }
+
+ /* Write the attribute data. */
+ status = H5Awrite(attr_id, type_id, data);
+ if (status < 0)
+ {
+ goto out;
+ }
+
+ /* Close the attribute. */
+ status = H5Aclose(attr_id);
+ if (status < 0)
+ {
+ goto out;
+ }
+
+ /* Close the data space. */
+ status = H5Sclose(space_id);
+ if (status < 0)
+ {
+ goto out;
+ }
+
+ return 0;
+
+out:
+ return -1;
+}
+
+
+// ---------------------------------------------------------------------------
+
+herr_t HdfAttr::get_string(hid_t obj_id, const char *attr_name, char **data)
+{
+ hid_t attr_id;
+ hid_t attr_type;
+ size_t type_size;
+ herr_t status;
+
+ *data = NULL;
+
+ attr_id = H5Aopen_name(obj_id, attr_name);
+ if (attr_id < 0)
+ {
+ return -1;
+ }
+
+ attr_type = H5Aget_type(attr_id);
+ if (attr_type < 0)
+ {
+ goto out;
+ }
+
+ /* Get the size */
+ type_size = H5Tget_size(attr_type);
+ if (type_size < 0)
+ {
+ goto out;
+ }
+
+ /* Malloc enough space for the string, plus 1 for the trailing '\0' */
+ *data = (char *)malloc((type_size + 1) * sizeof(char));
+
+ status = H5Aread(attr_id, attr_type, *data);
+ if (status < 0)
+ {
+ goto out;
+ }
+
+ /* Set the last character to \0 in case we are dealing with
+ * space padded strings.
+ */
+ (*data)[type_size] = '\0';
+
+ /* TODO: check that following statement doesn't fail */
+ status = H5Tclose(attr_type);
+ if (status < 0)
+ {
+ goto out;
+ }
+
+ status = H5Aclose(attr_id);
+ if (status < 0)
+ {
+ return -1;
+ }
+
+ return 0;
+
+out:
+
+ if (attr_type != 0)
+ H5Tclose(attr_type);
+
+ H5Aclose(attr_id);
+
+ if (*data)
+ free(*data);
+
+ return -1;
+}
+
+herr_t HdfAttr::set_string(hid_t obj_id, const char *attr_name, const char *attr_data)
+{
+ hid_t attr_type;
+ hid_t attr_size;
+ hid_t attr_space_id;
+ hid_t attr_id;
+ int has_attr;
+ herr_t status;
+
+ /* Create the attribute */
+ attr_type = H5Tcopy(H5T_C_S1);
+ if (attr_type < 0)
+ {
+ goto out;
+ }
+
+ attr_size = strlen(attr_data) + 1; /* extra null term */
+
+ status = H5Tset_size(attr_type, (size_t)attr_size);
+ if (status < 0)
+ {
+ goto out;
+ }
+
+ status = H5Tset_strpad(attr_type, H5T_STR_NULLTERM);
+ if (status < 0)
+ {
+ goto out;
+ }
+
+ attr_space_id = H5Screate(H5S_SCALAR);
+ if (status < 0)
+ {
+ goto out;
+ }
+
+ /* Verify if the attribute already exists */
+ has_attr = HdfAttr::find(obj_id, attr_name);
+
+ /* If the attribute already exists, delete it */
+ if (has_attr == 1)
+ {
+ status = H5Adelete(obj_id, attr_name);
+ if (status < 0)
+ {
+ goto out;
+ }
+ }
+
+ /* Create the attribute. */
+ attr_id = H5Acreate(obj_id, attr_name, attr_type, attr_space_id,
+ H5P_DEFAULT);
+ if (attr_id < 0)
+ {
+ goto out;
+ }
+
+ /* Write the attribute data. */
+ status = H5Awrite(attr_id, attr_type, attr_data);
+ if (status < 0)
+ {
+ goto out;
+ }
+
+ status = H5Aclose(attr_id);
+ if (status < 0)
+ {
+ goto out;
+ }
+
+ status = H5Sclose(attr_space_id);
+ if (status < 0)
+ {
+ goto out;
+ }
+
+ status = H5Tclose(attr_type);
+ if (status < 0)
+ {
+ goto out;
+ }
+
+ return 0;
+
+out:
+ return -1;
+}
+
+
+// ---------------------------------------------------------------------------
+
+herr_t HdfAttr::set_int(hid_t obj_id, const char *attr_name, int n)
+{
+ return HdfAttr::set(obj_id, attr_name, H5T_NATIVE_INT, &n);
+}
+
+herr_t HdfAttr::set_long(hid_t obj_id, const char *attr_name, long n)
+{
+ return HdfAttr::set(obj_id, attr_name, H5T_NATIVE_LONG, &n);
+}
+
+herr_t HdfAttr::set_llong(hid_t obj_id, const char *attr_name, long long n)
+{
+ return HdfAttr::set(obj_id, attr_name, H5T_NATIVE_LLONG, &n);
+}
+
+herr_t HdfAttr::set_float(hid_t obj_id, const char *attr_name, float n)
+{
+ return HdfAttr::set(obj_id, attr_name, H5T_NATIVE_FLOAT, &n);
+}
+
+herr_t HdfAttr::set_double(hid_t obj_id, const char *attr_name, double n)
+{
+ return HdfAttr::set(obj_id, attr_name, H5T_NATIVE_DOUBLE, &n);
+}
+
+
+// ---------------------------------------------------------------------------
+
+herr_t HdfAttr::set_array(hid_t obj_id, const char *attr_name,
+ size_t rank, hsize_t *dims,
+ hid_t type_id, const void *data)
+{
+ hid_t space_id, attr_id;
+ herr_t status;
+
+ int has_attr;
+
+ /* Create the data space for the attribute. */
+ space_id = H5Screate_simple(rank, dims, NULL);
+ if (space_id < 0)
+ {
+ goto out;
+ }
+
+ /* Verify if the attribute already exists. */
+ has_attr = HdfAttr::find(obj_id, attr_name);
+ if (has_attr == 1)
+ {
+ /* The attribute already exists. Delete it. */
+ status = H5Adelete(obj_id, attr_name);
+ if (status < 0)
+ {
+ goto out;
+ }
+ }
+
+ /* Create the attribute. */
+ attr_id = H5Acreate(obj_id, attr_name, type_id, space_id, H5P_DEFAULT);
+ if (attr_id < 0)
+ {
+ goto out;
+ }
+
+ /* Write the attribute data. */
+ status = H5Awrite(attr_id, type_id, data);
+ if (status < 0)
+ {
+ goto out;
+ }
+
+ /* Close the dataspace. */
+ status = H5Sclose(space_id);
+ if (status < 0)
+ {
+ goto out;
+ }
+
+ return 0;
+
+out:
+ return -1;
+}
+
+herr_t HdfAttr::set_array1(hid_t obj_id, const char *attr_name, hsize_t dim, hid_t type_id, const void *data)
+{
+ return HdfAttr::set_array(obj_id, attr_name, 1, &dim, type_id, data);
+}
+
+herr_t HdfAttr::set_array1(hid_t obj_id, const char *attr_name, hsize_t dim, const int *data)
+{
+ return HdfAttr::set_array(obj_id, attr_name, 1, &dim, H5T_NATIVE_INT, data);
+}
+
+herr_t HdfAttr::set_array1(hid_t obj_id, const char *attr_name, hsize_t dim, const float *data)
+{
+ return HdfAttr::set_array(obj_id, attr_name, 1, &dim, H5T_NATIVE_FLOAT, data);
+}
+
+herr_t HdfAttr::set_array1(hid_t obj_id, const char *attr_name, hsize_t dim, const double *data)
+{
+ return HdfAttr::set_array(obj_id, attr_name, 1, &dim, H5T_NATIVE_DOUBLE, data);
+}
+
+// ---------------------------------------------------------------------------
Added: cs/benchmark/cigma/trunk/src/HdfAttr.h
===================================================================
--- cs/benchmark/cigma/trunk/src/HdfAttr.h (rev 0)
+++ cs/benchmark/cigma/trunk/src/HdfAttr.h 2008-03-19 19:00:17 UTC (rev 11481)
@@ -0,0 +1,41 @@
+#ifndef __HDF_ATTR_H__
+#define __HDF_ATTR_H__
+
+#include "hdf5.h"
+
+namespace HdfAttr
+{
+ // test whether attribute exists
+ herr_t find(hid_t loc_id, const char *attr_name);
+
+ // get dimensions of attribute
+ herr_t get_dims(hid_t obj_id, const char *attr_name, int *rank, hsize_t *dims);
+
+ // general get/set methods
+ herr_t get(hid_t obj_id, const char *attr_name, hid_t type_id, void *data);
+ herr_t set(hid_t obj_id, const char *attr_name, hid_t type_id, const void *data);
+
+ // string get/set methods
+ herr_t get_string(hid_t obj_id, const char *attr_name, char **data);
+ herr_t set_string(hid_t obj_id, const char *attr_name, const char *attr_data);
+
+ // various other setters
+ herr_t set_int(hid_t obj_id, const char *attr_name, int n);
+ herr_t set_long(hid_t obj_id, const char *attr_name, long n);
+ herr_t set_llong(hid_t obj_id, const char *attr_name, long long n);
+ herr_t set_float(hid_t obj_id, const char *attr_name, float n);
+ herr_t set_double(hid_t obj_id, const char *attr_name, double n);
+
+ // general array setter
+ herr_t set_array(hid_t obj_id, const char *attr_name,
+ size_t rank, hsize_t *dims,
+ hid_t type_id, const void *data);
+
+ // various one-dimensional array setters
+ herr_t set_array1(hid_t obj_id, const char *attr_name, hsize_t dim, hid_t type_id, const void *data);
+ herr_t set_array1(hid_t obj_id, const char *attr_name, hsize_t dim, const int *data);
+ herr_t set_array1(hid_t obj_id, const char *attr_name, hsize_t dim, const float *data);
+ herr_t set_array1(hid_t obj_id, const char *attr_name, hsize_t dim, const double *data);
+}
+
+#endif
Added: cs/benchmark/cigma/trunk/src/HdfDataset.cpp
===================================================================
--- cs/benchmark/cigma/trunk/src/HdfDataset.cpp (rev 0)
+++ cs/benchmark/cigma/trunk/src/HdfDataset.cpp 2008-03-19 19:00:17 UTC (rev 11481)
@@ -0,0 +1,266 @@
+#include <cassert>
+#include <cstdlib>
+
+#include "HdfDataset.h"
+#include "HdfAttr.h"
+
+
+using namespace HdfDataset;
+
+
+
+// ---------------------------------------------------------------------------
+
+hid_t HdfDataset::create(hid_t loc_id, const char *name, const char *title,
+ hid_t type_id, int rank, int *shape)
+{
+ hid_t dataset_id;
+ hid_t dataspace_id;
+ herr_t status;
+
+ if (rank > 0)
+ {
+ hsize_t *dims;
+ int i;
+ dims = (hsize_t *)malloc(rank * sizeof(hsize_t));
+ for (i = 0; i < rank; i++)
+ {
+ dims[i] = shape[i];
+ }
+
+ dataspace_id = H5Screate_simple(rank, dims, NULL);
+ if (dataspace_id < 0)
+ {
+ free(dims);
+ return -1;
+ }
+ free(dims);
+ }
+ else
+ {
+ return -3;
+ }
+
+ dataset_id = H5Dcreate(loc_id, name, type_id, dataspace_id, H5P_DEFAULT);
+ if (dataset_id < 0)
+ {
+ H5Sclose(dataspace_id);
+ return -2;
+ }
+
+ status = HdfAttr::set_string(dataset_id, "TITLE", title);
+ status = HdfAttr::set_string(dataset_id, "CLASS", "ARRAY");
+ status = HdfAttr::set_string(dataset_id, "FLAVOR", "numpy");
+ status = HdfAttr::set_string(dataset_id, "VERSION", "2.3");
+
+ status = H5Sclose(dataspace_id);
+
+ return dataset_id;
+
+}
+
+
+hid_t HdfDataset::open(hid_t loc_id, const char *name,
+ hid_t *type_id, int *rank, int *shape, int *npoints)
+{
+ hid_t dataset_id;
+ hid_t dataspace_id;
+ herr_t status;
+
+ dataset_id = H5Dopen(loc_id, name);
+ if (dataset_id < 0)
+ {
+ return -1;
+ }
+
+ if (type_id != NULL)
+ {
+ *type_id = H5Dget_type(dataset_id);
+ }
+
+ if ((rank != NULL) || (shape != NULL) || (npoints != NULL))
+ {
+ dataspace_id = H5Dget_space(dataset_id);
+
+ if (rank != NULL)
+ {
+ *rank = H5Sget_simple_extent_ndims(dataspace_id);
+ }
+
+ // TODO: think about the shape[] array. should we allocate
+ // it here instead of copying it to a fixed array?
+ // after all, even the rank is unknown before opening
+ // a dataset. note that the type would then have to
+ // change to "int **shape"
+ if (shape != NULL)
+ {
+ int i;
+ hsize_t *dims;
+
+ dims = (hsize_t *)malloc((*rank) * sizeof(hsize_t));
+ status = H5Sget_simple_extent_dims(dataspace_id, dims, NULL);
+ for (i = 0; i < *rank; i++)
+ {
+ shape[i] = (int)dims[i];
+ }
+ free(dims);
+ }
+
+ if (npoints != NULL)
+ {
+ *npoints = H5Sget_simple_extent_npoints(dataspace_id);
+ }
+
+ status = H5Sclose(dataspace_id);
+ }
+
+ return dataset_id;
+}
+
+
+
+// ---------------------------------------------------------------------------
+
+int HdfDataset::read(hid_t loc_id, const char *name,
+ hid_t type_id, int rank, int *shape, void **data)
+{
+ int dset_rank;
+ int dset_npts;
+ hid_t dataset_id;
+ herr_t status;
+
+ dataset_id = HdfDataset::open(loc_id, name, NULL, &dset_rank, shape, &dset_npts);
+ if (dataset_id < 0)
+ {
+ return -1;
+ }
+ if (rank != dset_rank)
+ {
+ H5Dclose(dataset_id);
+ return -2;
+ }
+
+ *data = malloc(dset_npts * H5Tget_size(type_id));
+ if (*data == NULL)
+ {
+ H5Dclose(dataset_id);
+ return -3;
+ }
+
+ status = H5Dread(dataset_id, type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, *data);
+ if (status < 0)
+ {
+ H5Dclose(dataset_id);
+ return -4;
+ }
+
+ status = H5Dclose(dataset_id);
+
+ return 0;
+}
+
+
+int HdfDataset::read1(hid_t loc_id,
+ const char *name,
+ hid_t type_id,
+ void **data, int *n)
+{
+ return HdfDataset::read(loc_id, name, type_id, 1, n, data);
+}
+
+
+int HdfDataset::read2(hid_t loc_id,
+ const char *name,
+ hid_t type_id,
+ void **data, int *m, int *n)
+{
+ int shape[2];
+ int ret;
+
+ ret = HdfDataset::read(loc_id, name, type_id, 2, shape, data);
+
+ *m = shape[0];
+ *n = shape[1];
+
+ return ret;
+}
+
+int HdfDataset::read3(hid_t loc_id,
+ const char *name,
+ hid_t type_id,
+ void **data, int *l, int *m, int *n)
+{
+ int shape[3];
+ int ret;
+
+ ret = HdfDataset::read(loc_id, name, type_id, 3, shape, data);
+
+ *l = shape[0];
+ *m = shape[1];
+ *n = shape[2];
+
+ return ret;
+}
+
+
+
+// ---------------------------------------------------------------------------
+
+int HdfDataset::write(hid_t loc_id, const char *name, const char *title,
+ hid_t type_id, int rank, int *shape, void *data)
+{
+ hid_t dataset_id;
+ herr_t status;
+
+ dataset_id = HdfDataset::create(loc_id, name, title, type_id, rank, shape);
+ if (dataset_id < 0)
+ {
+ return -1;
+ }
+
+ status = H5Dwrite(dataset_id, type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
+
+ if (status < 0)
+ {
+ H5Dclose(dataset_id);
+ return -2;
+ }
+
+ H5Dclose(dataset_id);
+ return 0;
+}
+
+
+int HdfDataset::write1(hid_t loc_id,
+ const char *name,
+ const char *title,
+ hid_t type_id,
+ void *data, int n)
+{
+ return HdfDataset::write(loc_id, name, title, type_id, 1, &n, data);
+}
+
+
+int HdfDataset::write2(hid_t loc_id,
+ const char *name,
+ const char *title,
+ hid_t type_id,
+ void *data, int m, int n)
+{
+ int dims[2] = {m,n};
+ return HdfDataset::write(loc_id, name, title, type_id, 2, dims, data);
+}
+
+
+int HdfDataset::write3(hid_t loc_id,
+ const char *name,
+ const char *title,
+ hid_t type_id,
+ void *data, int l, int m, int n)
+{
+ int dims[3] = {l,m,n};
+ return HdfDataset::write(loc_id, name, title, type_id, 3, dims, data);
+}
+
+
+// ---------------------------------------------------------------------------
Added: cs/benchmark/cigma/trunk/src/HdfDataset.h
===================================================================
--- cs/benchmark/cigma/trunk/src/HdfDataset.h (rev 0)
+++ cs/benchmark/cigma/trunk/src/HdfDataset.h 2008-03-19 19:00:17 UTC (rev 11481)
@@ -0,0 +1,60 @@
+#ifndef __HDF_DATASET_H__
+#define __HDF_DATASET_H__
+
+#include "hdf5.h"
+
+namespace HdfDataset
+{
+
+ hid_t create(hid_t loc_id, const char *name, const char *title,
+ hid_t type_id, int rank, int *shape);
+
+ hid_t open(hid_t loc_id, const char *name,
+ hid_t *type_id, int *rank, int *shape, int *npoints);
+
+ // -----------------------------------------------------------------------
+
+ int read(hid_t loc_id, const char *name,
+ hid_t type_id, int rank, int *shape, void **data);
+
+ int read1(hid_t loc_id,
+ const char *name,
+ hid_t type_id,
+ void **data, int *n);
+
+ int read2(hid_t loc_id,
+ const char *name,
+ hid_t type_id,
+ void **data, int *m, int *n);
+
+ int read3(hid_t loc_id,
+ const char *name,
+ hid_t type_id,
+ void **data, int *l, int *m, int *n);
+
+ // -----------------------------------------------------------------------
+
+ int write(hid_t loc_id, const char *name, const char *title,
+ hid_t type_id, int rank, int *shape, void *data);
+
+ int write1(hid_t loc_id,
+ const char *name,
+ const char *title,
+ hid_t type_id,
+ void *data, int n);
+
+ int write2(hid_t loc_id,
+ const char *name,
+ const char *title,
+ hid_t type_id,
+ void *data, int m, int n);
+
+ int write3(hid_t loc_id,
+ const char *name,
+ const char *title,
+ hid_t type_id,
+ void *data, int l, int m, int n);
+
+}
+
+#endif
Added: cs/benchmark/cigma/trunk/src/HdfFile.cpp
===================================================================
--- cs/benchmark/cigma/trunk/src/HdfFile.cpp (rev 0)
+++ cs/benchmark/cigma/trunk/src/HdfFile.cpp 2008-03-19 19:00:17 UTC (rev 11481)
@@ -0,0 +1,182 @@
+#include <cstring>
+#include <cassert>
+
+#include "HdfFile.h"
+#include "HdfAttr.h"
+
+
+// ---------------------------------------------------------------------------
+
+HdfFile::HdfFile()
+{
+ file_id = -1;
+}
+
+HdfFile::~HdfFile()
+{
+ close();
+}
+
+int HdfFile::close()
+{
+ herr_t status;
+
+ if (file_id >= 0)
+ {
+ status = H5Fclose(file_id);
+ }
+
+ file_id = -1;
+
+ return (int)status;
+}
+
+
+// ---------------------------------------------------------------------------
+
+int HdfFile::create(const char *filename, const char *mode)
+{
+ if (strcmp(mode, "w") == 0)
+ {
+ /* Create file by truncating (i.e. overwriting previous file) */
+ file_id = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
+ }
+ else if (strcmp(mode, "x") == 0)
+ {
+ /* Create file exclusively (i.e. fail if it already exists) */
+ file_id = H5Fcreate(filename, H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);
+ }
+ else
+ {
+ assert(false);
+ return -1;
+ }
+
+ if (file_id < 0)
+ {
+ return -2;
+ }
+
+ hid_t root;
+ herr_t status;
+
+ root = H5Gopen(file_id, "/");
+ status = HdfAttr::set_string(root, "TITLE", "Cigma file"); // TODO: add title to argument list
+ status = HdfAttr::set_string(root, "CLASS", "GROUP");
+ status = HdfAttr::set_string(root, "VERSION", "1.0");
+ status = HdfAttr::set_string(root, "PYTABLES_FORMAT_VERSION", "1.5");
+ status = HdfAttr::set_string(root, "CigmaVersion", "0.9.1");
+ status = H5Gclose(root);
+
+ return 0;
+}
+
+
+int HdfFile::open(const char *filename, const char *mode)
+{
+ /*
+ * Open file for reading. Fail if file doesn't exist.
+ */
+ if (strcmp(mode, "r") == 0)
+ {
+ /* Open file in read-only mode */
+ file_id = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);
+
+ /* Check for failure */
+ if (file_id < 0)
+ {
+ return -1;
+ }
+
+ return 0;
+ }
+
+ /*
+ * Open file for writing. If file exists, it is truncated.
+ */
+ if (strcmp(mode, "w") == 0)
+ {
+ this->create(filename, "w");
+
+ if (file_id < 0)
+ {
+ return -2;
+ }
+
+ return 0;
+ }
+
+ /*
+ * Open file for reading and writing. Fail if file doesn't exist.
+ */
+ if (strcmp(mode, "rw") == 0)
+ {
+ /* Open file in read-write mode */
+ file_id = H5Fopen(filename, H5F_ACC_RDWR, H5P_DEFAULT);
+
+ if (file_id < 0)
+ {
+ return -3;
+ }
+
+ return 0;
+ }
+
+ /*
+ * Open file for reading and writing. Create the file if necessary.
+ */
+ if (strcmp(mode, "rw+") == 0)
+ {
+ /* See http://hdf.ncsa.uiuc.edu/HDF5/doc/Errors.html */
+ herr_t (*old_func)(void *);
+ void *old_client_data;
+
+ /* Save old error handler */
+ H5Eget_auto(&old_func, &old_client_data);
+
+ /* Turn off error handling */
+ H5Eset_auto(NULL, NULL);
+
+ /* Open file in read-write mode -- errors supprssed */
+ file_id = H5Fopen(filename, H5F_ACC_RDWR, H5P_DEFAULT);
+
+ /* Restore error handler */
+ H5Eset_auto(old_func, old_client_data);
+
+ /* If opening the file failed, try to create it */
+ if (file_id < 0)
+ {
+ this->create(filename, "w");
+
+ if (file_id < 0)
+ {
+ return -4;
+ }
+ }
+
+ return 0;
+ }
+
+ /*
+ * Exclusively open file for writing. Fail if file already exists.
+ */
+ if (strcmp(mode, "x") == 0)
+ {
+ this->create(filename, "x");
+
+ if (file_id < 0)
+ {
+ return -5;
+ }
+
+ return file_id;
+ }
+
+ /*
+ * Invalid mode was provided.
+ */
+ return -6;
+}
+
+
+// ---------------------------------------------------------------------------
Added: cs/benchmark/cigma/trunk/src/HdfFile.h
===================================================================
--- cs/benchmark/cigma/trunk/src/HdfFile.h (rev 0)
+++ cs/benchmark/cigma/trunk/src/HdfFile.h 2008-03-19 19:00:17 UTC (rev 11481)
@@ -0,0 +1,21 @@
+#ifndef __HDF_FILE_H__
+#define __HDF_FILE_H__
+
+#include "hdf5.h"
+
+class HdfFile
+{
+public:
+ HdfFile();
+ ~HdfFile();
+
+public:
+ int create(const char *filename, const char *mode);
+ int open(const char *filename, const char *mode);
+ int close();
+
+public:
+ hid_t file_id;
+};
+
+#endif
More information about the cig-commits
mailing list