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