1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2025-04-10 19:20:44 +01:00

IO: code improvement

This commit is contained in:
Antonin Portelli 2015-11-18 19:46:52 +00:00
parent 4f2e87cf4f
commit a4999a53fd
2 changed files with 12 additions and 44 deletions

View File

@ -25,20 +25,9 @@ using namespace Latan;
string Io::getFirstName(const string &fileName) string Io::getFirstName(const string &fileName)
{ {
string ext = extension(fileName); std::unique_ptr<File> file = open(fileName);
if (ext == "h5") return file->getFirstName();
{
return getFirstName<Hdf5File>(fileName);
}
else if ((ext == "dat")||(ext == "sample")||(ext == "seed"))
{
return getFirstName<AsciiFile>(fileName);
}
else
{
LATAN_ERROR(Io, "unknown file extension '" + ext + "'");
}
} }
unique_ptr<File> Io::open(const std::string &fileName, const unsigned int mode) unique_ptr<File> Io::open(const std::string &fileName, const unsigned int mode)

View File

@ -48,7 +48,7 @@ public:
static std::string getFirstName(const std::string &fileName); static std::string getFirstName(const std::string &fileName);
static std::string getFirstName(const std::string &fileName); static std::string getFirstName(const std::string &fileName);
static std::unique_ptr<File> open(const std::string &fileName, static std::unique_ptr<File> open(const std::string &fileName,
const unsigned int mode = File::Mode::write); const unsigned int mode = File::Mode::read);
}; };
// template implementation ///////////////////////////////////////////////////// // template implementation /////////////////////////////////////////////////////
@ -63,27 +63,16 @@ IoT Io::load(const std::string &fileName, const std::string &name)
template <typename IoT> template <typename IoT>
IoT Io::load(const std::string &fileName, const std::string &name) IoT Io::load(const std::string &fileName, const std::string &name)
{ {
std::string ext = extension(fileName); std::unique_ptr<File> file = open(fileName);
if (ext == "h5") return file->read<IoT>(name);
{
return load<IoT, Hdf5File>(fileName, name);
}
else if ((ext == "dat")||(ext == "sample")||(ext == "seed"))
{
return load<IoT, AsciiFile>(fileName, name);
}
else
{
LATAN_ERROR(Io, "unknown file extension '" + ext + "'");
}
} }
template <typename IoT, typename FileType> template <typename IoT, typename FileType>
void Io::save(const IoT &data, const std::string &fileName, void Io::save(const IoT &data, const std::string &fileName,
const unsigned int mode, const std::string &name) const unsigned int mode, const std::string &name)
{ {
FileType file(fileName, mode); FileType file(fileName, mode);
std::string realName = (name.empty()) ? fileName : name; std::string realName = (name.empty()) ? fileName : name;
file.save(data, realName); file.save(data, realName);
@ -93,20 +82,10 @@ template <typename IoT>
void Io::save(const IoT &data, const std::string &fileName, void Io::save(const IoT &data, const std::string &fileName,
const unsigned int mode, const std::string &name) const unsigned int mode, const std::string &name)
{ {
std::string ext = extension(fileName); std::unique_ptr<File> file = open(fileName, mode);
std::string realName = (name.empty()) ? fileName : name;
if (ext == "h5")
{ file->save(data, realName);
save<IoT, Hdf5File>(data, fileName, mode, name);
}
else if ((ext == "dat")||(ext == "sample")||(ext == "seed"))
{
save<IoT, AsciiFile>(data, fileName, mode, name);
}
else
{
LATAN_ERROR(Io, "unknown file extension '" + ext + "'");
}
} }
template <typename FileType> template <typename FileType>