mirror of
https://github.com/aportelli/LatAnalyze.git
synced 2024-11-10 00:45:36 +00:00
IO: stricter control of data names
This commit is contained in:
parent
e31b8f0dcb
commit
f9fcf25012
@ -26,7 +26,7 @@ int main(void)
|
||||
|
||||
// write
|
||||
cout << "-- saving and loading A*B using '" + fileName + "'..." << endl;
|
||||
Io::save(A*B, fileName, File::Mode::write, "AB");
|
||||
Io::save(A*B, fileName, File::Mode::write);
|
||||
|
||||
DMat C = Io::load<DMat>(fileName);
|
||||
cout << C << endl;
|
||||
|
@ -55,6 +55,11 @@ AsciiFile::~AsciiFile(void)
|
||||
// access //////////////////////////////////////////////////////////////////////
|
||||
void AsciiFile::save(const DMat &m, const std::string &name)
|
||||
{
|
||||
if (name.empty())
|
||||
{
|
||||
LATAN_ERROR(Io, "trying to save data with an empty name");
|
||||
}
|
||||
|
||||
const auto defaultPrec = fileStream_.precision(defaultDoublePrec);
|
||||
|
||||
checkWritability();
|
||||
@ -68,6 +73,11 @@ void AsciiFile::save(const DMat &m, const std::string &name)
|
||||
|
||||
void AsciiFile::save(const DMatSample &s, const std::string &name)
|
||||
{
|
||||
if (name.empty())
|
||||
{
|
||||
LATAN_ERROR(Io, "trying to save data with an empty name");
|
||||
}
|
||||
|
||||
checkWritability();
|
||||
isParsed_ = false;
|
||||
fileStream_ << "#L latan_begin rs_sample " << name << endl;
|
||||
@ -82,6 +92,11 @@ void AsciiFile::save(const DMatSample &s, const std::string &name)
|
||||
|
||||
void AsciiFile::save(const RandGenState &state, const std::string &name)
|
||||
{
|
||||
if (name.empty())
|
||||
{
|
||||
LATAN_ERROR(Io, "trying to save data with an empty name");
|
||||
}
|
||||
|
||||
checkWritability();
|
||||
isParsed_ = false;
|
||||
fileStream_ << "#L latan_begin rg_state " << name << endl;
|
||||
|
@ -54,6 +54,11 @@ Hdf5File::~Hdf5File(void)
|
||||
// access //////////////////////////////////////////////////////////////////////
|
||||
void Hdf5File::save(const DMat &m, const string &name)
|
||||
{
|
||||
if (name.empty())
|
||||
{
|
||||
LATAN_ERROR(Io, "trying to save data with an empty name");
|
||||
}
|
||||
|
||||
Group group;
|
||||
Attribute attr;
|
||||
DataSet dataset;
|
||||
@ -72,6 +77,11 @@ void Hdf5File::save(const DMat &m, const string &name)
|
||||
|
||||
void Hdf5File::save(const DMatSample &sample, const string &name)
|
||||
{
|
||||
if (name.empty())
|
||||
{
|
||||
LATAN_ERROR(Io, "trying to save data with an empty name");
|
||||
}
|
||||
|
||||
Group group;
|
||||
Attribute attr;
|
||||
DataSet dataset;
|
||||
@ -98,6 +108,11 @@ void Hdf5File::save(const DMatSample &sample, const string &name)
|
||||
|
||||
void Hdf5File::save(const RandGenState &state, const string &name)
|
||||
{
|
||||
if (name.empty())
|
||||
{
|
||||
LATAN_ERROR(Io, "trying to save data with an empty name");
|
||||
}
|
||||
|
||||
Group group;
|
||||
Attribute attr;
|
||||
DataSet dataset;
|
||||
@ -124,6 +139,25 @@ bool Hdf5File::isOpen(void) const
|
||||
return (h5File_ != nullptr);
|
||||
}
|
||||
|
||||
// check names for forbidden characters ////////////////////////////////////////
|
||||
size_t Hdf5File::nameOffset(const string &name)
|
||||
{
|
||||
size_t ret = 0;
|
||||
string badChars = "/";
|
||||
|
||||
for (auto c : badChars)
|
||||
{
|
||||
size_t pos = name.rfind(c);
|
||||
|
||||
if (pos != string::npos and pos > ret)
|
||||
{
|
||||
ret = pos;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// IO //////////////////////////////////////////////////////////////////////////
|
||||
void Hdf5File::close(void)
|
||||
{
|
||||
@ -149,6 +183,7 @@ void Hdf5File::open(const string &name, const unsigned int mode)
|
||||
|
||||
name_ = name;
|
||||
mode_ = mode;
|
||||
|
||||
if (mode & Mode::write)
|
||||
{
|
||||
h5Mode |= H5F_ACC_TRUNC;
|
||||
@ -184,8 +219,7 @@ string Hdf5File::getFirstGroupName(void)
|
||||
return 0;
|
||||
};
|
||||
|
||||
char groupName[maxGroupNameSize];
|
||||
groupName[0] = 0; // Need to make sure it's null-terminated
|
||||
char groupName[maxGroupNameSize] = "";
|
||||
|
||||
h5File_->iterateElems("/", nullptr, firstGroupName, groupName);
|
||||
res = groupName;
|
||||
@ -243,6 +277,10 @@ string Hdf5File::load(const string &name)
|
||||
IoObject::IoType type;
|
||||
|
||||
groupName = (name.empty()) ? getFirstGroupName() : name;
|
||||
if (groupName.empty())
|
||||
{
|
||||
LATAN_ERROR(Io, "file '" + name_ + "' is empty");
|
||||
}
|
||||
group = h5File_->openGroup(groupName.c_str());
|
||||
attribute = group.openAttribute("type");
|
||||
attribute.read(PredType::NATIVE_SHORT, &type);
|
||||
@ -314,18 +352,3 @@ string Hdf5File::load(const string &name)
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
size_t Hdf5File::nameOffset(const string& name)
|
||||
{
|
||||
size_t ret = 0;
|
||||
string badChars = "/";
|
||||
|
||||
for (auto c : badChars) {
|
||||
size_t pos = name.rfind(c);
|
||||
if (pos != string::npos and pos > ret) {
|
||||
ret = pos;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -62,8 +62,8 @@ private:
|
||||
void load(DMat &m, const H5NS::DataSet &d);
|
||||
void load(DMatSample &s, const H5NS::DataSet &d);
|
||||
void load(RandGenState &state, const H5NS::DataSet &d);
|
||||
|
||||
static size_t nameOffset(const std::string& name);
|
||||
// check name for forbidden characters
|
||||
static size_t nameOffset(const std::string &name);
|
||||
private:
|
||||
// file name
|
||||
std::unique_ptr<H5NS::H5File> h5File_{nullptr};
|
||||
|
Loading…
Reference in New Issue
Block a user