OGS
BaseLib::IO Namespace Reference

Classes

class  Writer
 Base class which enables writing an object to string, stringstream or file. More...
 
struct  XMLInterface
 Base class for writing any information to and from XML files. More...
 
class  XMLQtInterface
 

Functions

std::vector< std::string > readStringListFromFile (std::string const &filename)
 Reads non-empty lines from a list of strings from a file into a vector.
 
int writeStringToFile (std::string_view content, std::filesystem::path const &file_path)
 

Function Documentation

◆ readStringListFromFile()

std::vector< std::string > BaseLib::IO::readStringListFromFile ( std::string const & filename)

Reads non-empty lines from a list of strings from a file into a vector.

Definition at line 21 of file readStringListFromFile.cpp.

22{
23 std::vector<std::string> string_list;
24 std::ifstream in(filename);
25 if (!in)
26 {
27 ERR("Could not open file {:s}.", filename);
28 return string_list;
29 }
30 std::string line;
31 while (std::getline(in, line))
32 {
33 trim(line);
34 if (line.empty())
35 {
36 continue;
37 }
38 string_list.push_back(line);
39 }
40 return string_list;
41}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
void trim(std::string &str, char ch)

References ERR(), and BaseLib::trim().

Referenced by main().

◆ writeStringToFile()

int BaseLib::IO::writeStringToFile ( std::string_view content,
std::filesystem::path const & file_path )
Returns
0 if string is empty, or if there is an error, and 1 otherwise.

Definition at line 45 of file Writer.cpp.

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}

References ERR().

Referenced by SaveMeshDialog::accept(), MainWindow::callGMSH(), consolidateGeometry(), OGSFileConverter::convertGLI2GML(), convertPoints(), OGSFileConverter::convertVTU2MSH(), FileIO::createSurface(), generateMesh(), main(), MainWindow::save(), FileIO::XmlPrjInterface::write(), FileIO::SwmmInterface::writeCsvForObject(), FileIO::SwmmInterface::writeCsvForTimestep(), FileIO::writeGeometryToFile(), MainWindow::writeGeometryToFile(), writeGeoOutput(), MeshLib::IO::writeMeshToFile(), MainWindow::writeStationListToFile(), and FileIO::XmlPrjInterface::writeToFile().