OGS
Writer.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include "Writer.h"
5
6#include <fstream>
7#include <limits>
8
9#include "BaseLib/Logging.h"
10
11namespace BaseLib
12{
13namespace IO
14{
16{
17 out.precision(std::numeric_limits<double>::max_digits10);
18}
19
21{
22 // Empty stream and clear error states.
23 out.str("");
24 out.clear();
25
26 if (this->write())
27 {
28 return out.str();
29 }
30
31 return std::string("");
32}
33
34int writeStringToFile(std::string_view content,
35 std::filesystem::path const& file_path)
36{
37 if (content.empty())
38 {
39 return 0;
40 }
41 std::ofstream fileStream;
42 fileStream.open(file_path.c_str());
43
44 // check file stream
45 if (!fileStream)
46 {
47 ERR("Could not open file '{:s}'!", file_path.string());
48 return 0;
49 }
50
51 fileStream << content;
52
53 fileStream.close();
54 return 1;
55}
56
57} // namespace IO
58} // namespace BaseLib
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
std::ostringstream out
The stream to write to.
Definition Writer.h:36
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:20
int writeStringToFile(std::string_view content, std::filesystem::path const &file_path)
Definition Writer.cpp:34