27 #include <boost/xpressive/xpressive.hpp>
28 #include <boost/lexical_cast.hpp>
29 #include <boost/property_tree/xml_parser.hpp>
30 #include <boost/property_tree/ptree.hpp>
36 using boost::lexical_cast;
58 const sregex match_float = sregex::compile(
"^[+-]?(\\d*\\.\\d+|\\d+\\.\\d*)([eE][-+]?\\d+)?$");
59 return regex_match( str, match_float);
68 const sregex match_integer = sregex::compile(
"^[+-]?\\d+$");
69 return regex_match( str, match_integer);
73 void ptree2hdf5(
const ptree& pt, hid_t h5_id) {
75 for (
auto sub_pt_kv : pt) {
77 string key = sub_pt_kv.first;
80 if (key !=
"<xmlcomment>") {
83 if(!H5Lexists(h5_id, key.c_str(), H5P_DEFAULT) && !H5Aexists(h5_id, key.c_str())) {
86 string value = sub_pt_kv.second.get_value<
string>();
88 if (key ==
"value_array") {
96 double value_double = lexical_cast<
double>(value);
97 hid_t attr_dspace = H5Screate(H5S_SCALAR);
98 hid_t attr_id = H5Acreate(h5_id, key.c_str(), H5T_IEEE_F64LE, attr_dspace, H5P_DEFAULT, H5P_DEFAULT);
99 H5Awrite(attr_id, H5T_NATIVE_DOUBLE, &value_double);
101 H5Sclose(attr_dspace);
104 int value_int = lexical_cast<
int>(value);
105 hid_t attr_dspace = H5Screate(H5S_SCALAR);
106 hid_t attr_id = H5Acreate(h5_id, key.c_str(), H5T_STD_I32LE, attr_dspace, H5P_DEFAULT, H5P_DEFAULT);
107 H5Awrite(attr_id, H5T_NATIVE_INT, &value_int);
109 H5Sclose(attr_dspace);
112 const char* value_string = value.c_str();
113 hid_t attr_dspace = H5Screate(H5S_SCALAR);
114 hid_t attr_dtype = H5Tcopy(H5T_C_S1);
115 H5Tset_size(attr_dtype, H5T_VARIABLE);
116 hid_t attr_id = H5Acreate(h5_id, key.c_str(), attr_dtype, attr_dspace, H5P_DEFAULT, H5P_DEFAULT);
117 H5Awrite(attr_id, attr_dtype, &value_string);
119 H5Sclose(attr_dspace);
120 H5Tclose(attr_dtype);
126 hid_t h5_sub_id = H5Gcreate(h5_id, key.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
129 const ptree& sub_pt = pt.get_child(key);
130 ptree2hdf5(sub_pt, h5_sub_id);
151 herr_t attribute_info(hid_t loc_id,
const char* attr_name,
const H5A_info_t* ,
void* data) {
153 hid_t attr_id = H5Aopen(loc_id, attr_name, H5P_DEFAULT);
154 hid_t attr_dtype = H5Aget_type(attr_id);
155 hid_t attr_dspace = H5Aget_space(attr_id);
158 const int ndims = H5Sget_simple_extent_ndims(attr_dspace);
161 ptree* pt_attr =
static_cast<ptree*
>(data);
165 H5T_class_t attr_class = H5Tget_class(attr_dtype);
166 if (H5T_INTEGER == attr_class) {
169 H5Aread(attr_id, H5T_NATIVE_INT, &attr_value);
170 pt_attr->put(attr_name, attr_value);
171 }
else if (1 == ndims) {
173 H5Sget_simple_extent_dims(attr_dspace, &length, 0);
174 vector<int> attr_values(length);
175 H5Aread(attr_id, H5T_NATIVE_INT, attr_values.data());
176 for (
int attr_value : attr_values) {
177 pt_attr->add(
string(attr_name) +
".value_array.value", attr_value);
180 }
else if (H5T_FLOAT == attr_class) {
182 double attr_value = 0.0;
183 H5Aread(attr_id, H5T_NATIVE_DOUBLE, &attr_value);
184 pt_attr->put(attr_name, attr_value);
185 }
else if (1 == ndims) {
187 H5Sget_simple_extent_dims(attr_dspace, &length, 0);
188 vector<double> attr_values(length);
189 H5Aread(attr_id, H5T_NATIVE_DOUBLE, attr_values.data());
190 for (
double attr_value : attr_values) {
191 pt_attr->add(
string(attr_name) +
".value_array.value", attr_value);
194 }
else if (H5T_STRING == attr_class) {
198 hid_t attr_ntype = H5Tcopy(H5T_C_S1);
199 H5Tset_size(attr_ntype, H5T_VARIABLE);
200 H5Aread(attr_id, attr_ntype, &attr_value);
201 H5Tclose(attr_ntype);
203 pt_attr->put(attr_name, attr_value);
204 H5Dvlen_reclaim (attr_ntype, attr_dspace, H5P_DEFAULT, attr_value);
211 H5Sclose(attr_dspace);
212 H5Tclose(attr_dtype);
227 herr_t visit_group(hid_t loc_id,
const char *group_name,
const H5O_info_t *obj_info,
void *data) {
230 if (*group_name !=
'.' && H5O_TYPE_GROUP == obj_info->type) {
233 H5Aiterate_by_name(loc_id, group_name, H5_INDEX_NAME, H5_ITER_NATIVE, &n, &attribute_info, static_cast<void*>(&pt_attr), H5P_DEFAULT);
236 ptree* pt_root =
static_cast<ptree*
>(data);
239 pt_root->put_child(ptree::path_type(group_name,
'/'), pt_attr);
250 H5Ovisit(loc_id, H5_INDEX_NAME, H5_ITER_NATIVE, &visit_group, static_cast<void*>(&pt));
257 #endif // end_of_include_guard
Namespace for SimPT shell package.
ptree hdf52ptree(hid_t loc_id)
Return the ptree that represents the groups of attributes in HDF5.
bool isInteger(const std::string &str)
Returns true if the provided string represents an integer number, false otherwise.
bool isFloat(const std::string &str)
Returns true if the provided string represents a floating point number, false otherwise.