From 8cd66446010053cfa8f0d628ff8db7adf1331f5e Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Tue, 3 Mar 2020 22:23:53 +0100 Subject: [PATCH] IO: add functions to get the path of a binary archive --- src/IO/binary_archive.cpp | 52 +++++++++++++++++++++++++++++++++------ src/IO/binary_archive.hpp | 5 ++++ 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/IO/binary_archive.cpp b/src/IO/binary_archive.cpp index affa6ea5bd..df621c8d95 100644 --- a/src/IO/binary_archive.cpp +++ b/src/IO/binary_archive.cpp @@ -41,6 +41,39 @@ namespace bitpit { const std::string BinaryArchive::EXTENSION_DEFAULT = "dat"; +/*! + Generates the path of the archive with the specified properties. + + \param name is the name of the file + \param block is the parallel block thefile belongs to, a negative value + mean that the file is serial + \result The path of the archive with the specified properties. +*/ +std::string BinaryArchive::generatePath(const std::string &name, int block) +{ + return generatePath(name, EXTENSION_DEFAULT, block); +} + +/*! + Generates the path of the archive with the specified properties. + + \param name is the name of the file + \param extension is the extension of the file + \param block is the parallel block thefile belongs to, a negative value + mean that the file is serial + \result The path of the archive with the specified properties. +*/ +std::string BinaryArchive::generatePath(const std::string &name, const std::string &extension, int block) +{ + FileHandler fileHandler("", name, extension); + if (block >= 0) { + fileHandler.setParallel(true); + fileHandler.setBlock(block); + } + + return fileHandler.getPath(); +} + /*! Default constructor */ @@ -74,13 +107,8 @@ void BinaryArchive::open(const std::string &name, const std::string &extension, close(); } - FileHandler fileHandler("", name, extension); - if (block >= 0) { - fileHandler.setParallel(true); - fileHandler.setBlock(block); - } - - std::fstream::open(fileHandler.getPath().c_str(), std::ios::binary | mode); + m_path = generatePath(name, extension, block); + std::fstream::open(m_path.c_str(), std::ios::binary | mode); if (fail() && bad()) { return; } @@ -106,6 +134,16 @@ std::string BinaryArchive::getHeader() const return m_header; } +/*! + Gets the path of the archive. + + \result The path of the archive. +*/ +std::string BinaryArchive::getPath() const +{ + return m_path; +} + /*! \ingroup Binary \class IBinaryArchive diff --git a/src/IO/binary_archive.hpp b/src/IO/binary_archive.hpp index a78517865f..0902e15e43 100644 --- a/src/IO/binary_archive.hpp +++ b/src/IO/binary_archive.hpp @@ -40,6 +40,9 @@ class BinaryArchive : protected std::fstream static const std::string EXTENSION_DEFAULT; + static std::string generatePath(const std::string &name, int block = -1); + static std::string generatePath(const std::string &name, const std::string &extension, int block = -1); + using std::fstream::close; BinaryArchive(); @@ -47,10 +50,12 @@ class BinaryArchive : protected std::fstream int getVersion() const; std::string getHeader() const; + std::string getPath() const; protected: int m_version; std::string m_header; + std::string m_path; void open(const std::string &name, const std::string &extension, ios_base::openmode mode, int block = -1);