Commit 5be5933b authored by GILLES Sebastien's avatar GILLES Sebastien

#1422 Extend FilesystemNS::File function API to enable their use for binary files.

parent b2dca788
......@@ -76,7 +76,6 @@ namespace MoReFEM
case binary_or_ascii::ascii:
{
std::ifstream stream;
FilesystemNS::File::Read(stream, file, __FILE__, __LINE__);
std::string line;
......
......@@ -53,14 +53,13 @@ namespace MoReFEM
{
case binary_or_ascii::binary:
{
std::ofstream out_binary(output_file, std::ofstream::binary);
if (!out_binary)
throw Exception("Unable to create file " + output_file, invoking_file, invoking_line);
std::ofstream out;
FilesystemNS::File::Create(out, output_file, invoking_file, invoking_line,
std::ofstream::binary);
const auto array = content.GetArray();
out_binary.write(reinterpret_cast<const char *>(array), Nvalue * sizeof(double));
out_binary.close();
out.write(reinterpret_cast<const char *>(array), Nvalue * sizeof(double));
out.close();
break;
}
case binary_or_ascii::ascii:
......
......@@ -59,31 +59,35 @@ namespace MoReFEM
void Create(std::ofstream& out, const std::string& filename, const char* invoking_file, int invoking_line)
void Create(std::ofstream& out, const std::string& filename,
const char* invoking_file, int invoking_line,
std::ios_base::openmode openmode)
{
CheckFilenameNotEmpty(filename, invoking_file, invoking_line);
out.open(filename);
out.open(filename, openmode);
if (!out)
throw Exception("Unable to create file " + filename, invoking_file, invoking_line);
}
void Read(std::ifstream& in, const std::string& filename, const char* invoking_file, int invoking_line)
void Read(std::ifstream& in, const std::string& filename, const char* invoking_file, int invoking_line,
std::ios_base::openmode openmode)
{
CheckFilenameNotEmpty(filename, invoking_file, invoking_line);
in.open(filename);
in.open(filename, openmode);
if (!in)
throw Exception("Unable to read file " + filename, invoking_file, invoking_line);
}
void Append(std::ofstream& inout, const std::string& filename, const char* invoking_file, int invoking_line)
void Append(std::ofstream& inout, const std::string& filename, const char* invoking_file, int invoking_line,
std::ios_base::openmode openmode)
{
CheckFilenameNotEmpty(filename, invoking_file, invoking_line);
inout.open(filename, std::ios::app);
inout.open(filename, openmode|std::ios::app);
if (!inout)
throw Exception("Unable to read/modify file \"" + filename + "\".", invoking_file, invoking_line);
......
......@@ -32,6 +32,12 @@ namespace MoReFEM
namespace File
{
/*! \class doxygen_hide_openmode
*
* \param[in] openmode Open mode given to the stream (for instance to specify you want binary output).
* It follows the exact same mask syntax as what you would use directly in std::ofstream or std::ofstream.
*/
/*!
* \brief A wrapper over std::remove that throws an exception in case the operation fails.
......@@ -49,10 +55,12 @@ namespace MoReFEM
*
* \param[in] filename Name of the file to be created.
* \param[out] out Stream associated to the file.
* \copydoc doxygen_hide_openmode
*
* \copydoc doxygen_hide_invoking_file_and_line
*/
void Create(std::ofstream& out, const std::string& filename, const char* invoking_file, int invoking_line);
void Create(std::ofstream& out, const std::string& filename, const char* invoking_file, int invoking_line,
std::ios_base::openmode openmode = std::ios_base::out);
/*!
......@@ -61,9 +69,11 @@ namespace MoReFEM
* \param[in] filename Name of the file to be read.
* \param[out] stream Stream associated to the file.
* \copydoc doxygen_hide_invoking_file_and_line
* \copydoc doxygen_hide_openmode
*
*/
void Read(std::ifstream& stream, const std::string& filename, const char* invoking_file, int invoking_line);
void Read(std::ifstream& stream, const std::string& filename, const char* invoking_file, int invoking_line,
std::ios_base::openmode openmode = std::ios_base::in);
/*!
......@@ -71,10 +81,13 @@ namespace MoReFEM
*
* \param[in] filename Name of the file to be loaded and modified.
* \param[out] stream Stream associated to the file.
* \copydoc doxygen_hide_openmode
*
* \copydoc doxygen_hide_invoking_file_and_line
*/
void Append(std::ofstream& stream, const std::string& filename, const char* invoking_file, int invoking_line);
void Append(std::ofstream& stream, const std::string& filename,
const char* invoking_file, int invoking_line,
std::ios_base::openmode openmode = std::ios_base::out);
/*!
......
......@@ -13,6 +13,7 @@
#include "Utilities/Exceptions/Exception.hpp"
#include "Utilities/OutputFormat/ReadBinaryFile.hpp"
#include "Utilities/Filesystem/File.hpp"
namespace MoReFEM::Advanced
......@@ -25,7 +26,8 @@ namespace MoReFEM::Advanced
{
std::vector<double> ret;
std::ifstream in (binary_file, std::ifstream::binary);
std::ifstream in;
FilesystemNS::File::Read(in, binary_file, invoking_file, invoking_line);
if (in)
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment