2017-01-27 01:00:41 +00:00
|
|
|
#include <Grid/Grid.h>
|
2017-01-18 00:21:18 +00:00
|
|
|
|
|
|
|
using namespace Grid;
|
|
|
|
#ifndef H5_NO_NAMESPACE
|
|
|
|
using namespace H5NS;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Writer implementation ///////////////////////////////////////////////////////
|
|
|
|
Hdf5Writer::Hdf5Writer(const std::string &fileName)
|
|
|
|
: fileName_(fileName)
|
|
|
|
, file_(fileName.c_str(), H5F_ACC_TRUNC)
|
|
|
|
{
|
|
|
|
group_ = file_.openGroup("/");
|
2017-01-19 00:50:21 +00:00
|
|
|
writeSingleAttribute(dataSetThres_, HDF5_GRID_GUARD "dataset_threshold",
|
2017-01-20 02:23:55 +00:00
|
|
|
Hdf5Type<unsigned int>::type());
|
2017-01-18 00:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Hdf5Writer::push(const std::string &s)
|
|
|
|
{
|
|
|
|
group_ = group_.createGroup(s);
|
|
|
|
path_.push_back(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Hdf5Writer::pop(void)
|
|
|
|
{
|
|
|
|
path_.pop_back();
|
|
|
|
if (path_.empty())
|
|
|
|
{
|
|
|
|
group_ = file_.openGroup("/");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto binOp = [](const std::string &a, const std::string &b)->std::string
|
|
|
|
{
|
|
|
|
return a + "/" + b;
|
|
|
|
};
|
|
|
|
|
|
|
|
group_ = group_.openGroup(std::accumulate(path_.begin(), path_.end(),
|
|
|
|
std::string(""), binOp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
void Hdf5Writer::writeDefault(const std::string &s, const std::string &x)
|
|
|
|
{
|
|
|
|
StrType strType(PredType::C_S1, x.size());
|
|
|
|
|
2017-01-19 00:50:21 +00:00
|
|
|
writeSingleAttribute(*(x.data()), s, strType);
|
2017-01-18 00:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Hdf5Writer::writeDefault(const std::string &s, const char *x)
|
|
|
|
{
|
|
|
|
std::string sx(x);
|
|
|
|
|
|
|
|
writeDefault(s, sx);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reader implementation ///////////////////////////////////////////////////////
|
|
|
|
Hdf5Reader::Hdf5Reader(const std::string &fileName)
|
2017-01-19 00:50:21 +00:00
|
|
|
: fileName_(fileName)
|
|
|
|
, file_(fileName.c_str(), H5F_ACC_RDONLY)
|
2017-01-18 00:21:18 +00:00
|
|
|
{
|
2017-01-19 00:50:21 +00:00
|
|
|
group_ = file_.openGroup("/");
|
|
|
|
readSingleAttribute(dataSetThres_, HDF5_GRID_GUARD "dataset_threshold",
|
2017-01-20 02:23:55 +00:00
|
|
|
Hdf5Type<unsigned int>::type());
|
2017-01-18 00:21:18 +00:00
|
|
|
}
|
|
|
|
|
2017-01-19 00:50:21 +00:00
|
|
|
void Hdf5Reader::push(const std::string &s)
|
2017-01-18 00:21:18 +00:00
|
|
|
{
|
2017-01-19 00:50:21 +00:00
|
|
|
group_ = group_.openGroup(s);
|
|
|
|
path_.push_back(s);
|
2017-01-18 00:21:18 +00:00
|
|
|
}
|
|
|
|
|
2017-01-19 00:50:21 +00:00
|
|
|
void Hdf5Reader::pop(void)
|
2017-01-18 00:21:18 +00:00
|
|
|
{
|
2017-01-19 00:50:21 +00:00
|
|
|
path_.pop_back();
|
|
|
|
if (path_.empty())
|
|
|
|
{
|
|
|
|
group_ = file_.openGroup("/");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto binOp = [](const std::string &a, const std::string &b)->std::string
|
|
|
|
{
|
|
|
|
return a + "/" + b;
|
|
|
|
};
|
2017-01-18 00:21:18 +00:00
|
|
|
|
2017-01-19 00:50:21 +00:00
|
|
|
group_ = group_.openGroup(std::accumulate(path_.begin(), path_.end(),
|
|
|
|
std::string(""), binOp));
|
|
|
|
}
|
2017-01-18 00:21:18 +00:00
|
|
|
}
|
|
|
|
|
2017-01-19 00:50:21 +00:00
|
|
|
template <>
|
|
|
|
void Hdf5Reader::readDefault(const std::string &s, std::string &x)
|
2017-01-18 00:21:18 +00:00
|
|
|
{
|
2017-01-19 00:50:21 +00:00
|
|
|
Attribute attribute;
|
|
|
|
|
|
|
|
attribute = group_.openAttribute(s);
|
|
|
|
StrType strType = attribute.getStrType();
|
|
|
|
|
|
|
|
x.resize(strType.getSize());
|
|
|
|
attribute.read(strType, &(x[0]));
|
2017-01-18 00:21:18 +00:00
|
|
|
}
|