Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IO: add functions to get the path of a binary archive #52

Merged
merged 1 commit into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 45 additions & 7 deletions src/IO/binary_archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/IO/binary_archive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,22 @@ 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();
~BinaryArchive();

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);
Expand Down