OGS
Writer.cpp
Go to the documentation of this file.
1 
15 #include "Writer.h"
16 
17 #include <fstream>
18 #include <limits>
19 
20 #include "BaseLib/Logging.h"
21 
22 namespace BaseLib
23 {
24 namespace IO
25 {
27 {
28  out.precision(std::numeric_limits<double>::digits10);
29 }
30 
31 std::string Writer::writeToString()
32 {
33  // Empty stream and clear error states.
34  out.str("");
35  out.clear();
36 
37  if (this->write())
38  {
39  return out.str();
40  }
41 
42  return std::string("");
43 }
44 
45 int writeStringToFile(std::string content,
46  std::filesystem::path const& file_path)
47 {
48  if (content.empty())
49  {
50  return 0;
51  }
52  std::ofstream fileStream;
53  fileStream.open(file_path.c_str());
54 
55  // check file stream
56  if (!fileStream)
57  {
58  ERR("Could not open file '{:s}'!", file_path.string());
59  return 0;
60  }
61 
62  fileStream << content;
63 
64  fileStream.close();
65  return 1;
66 }
67 
68 } // namespace IO
69 } // namespace BaseLib
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:42
Definition of the Writer class.
std::ostringstream out
The stream to write to.
Definition: Writer.h:46
virtual bool write()=0
Writes the object to the internal stream. This method must be implemented by a subclass....
std::string writeToString()
Writes the object to a string.
Definition: Writer.cpp:31
int writeStringToFile(std::string content, std::filesystem::path const &file_path)
Definition: Writer.cpp:45