Skip to main content

BasicStringFileReader Class

Basic implementation of a string file reader. More...

Declaration

class helios::util::io::BasicStringFileReader { ... }

Base class

classStringFileReader

Abstract base class for reading file contents into a string. More...

Public Member Functions Index

std::stringgetContents (const std::string &filename) const override

Reads the entire contents of the file and returns them as a string. More...

boolreadInto (const std::string &filename, std::string &contents) const noexcept override

Attempts to read the entire contents of the file into the provided string. More...

Description

Basic implementation of a string file reader.

Definition at line 22 of file BasicStringFileReader.ixx.

Public Member Functions

getContents()

std::string helios::util::io::BasicStringFileReader::getContents (const std::string & filename)
inline nodiscard virtual

Reads the entire contents of the file and returns them as a string.

Parameters
filename

The path to the file to read.

Returns

The contents of the file as a std::string. If reading fails, an empty string is returned.

Exceptions
std::runtime_error

if the file operation fails.

Definition at line 29 of file BasicStringFileReader.ixx.

29 [[nodiscard]] std::string getContents(const std::string& filename) const override {
30 std::ifstream infile {filename};
31
32 if (!infile) {
33 std::string msg = std::format("Cannot open {0}", filename);
34 logger_.error(msg);
35 throw std::runtime_error(msg);
36 }
37
38 std::string line;
39 std::string fileContents;
40 while (getline(infile, line)) {
41 fileContents += line + "\n";
42 }
43 return fileContents;
44 }

Reference helios::util::io::StringFileReader::logger_.

readInto()

bool helios::util::io::BasicStringFileReader::readInto (const std::string & filename, std::string & contents)
inline nodiscard noexcept virtual

Attempts to read the entire contents of the file into the provided string.

Parameters
filename

The path to the file to read.

contents

Output parameter receiving the file contents on success.

Returns

true if reading succeeded and `contents` was populated, otherwise false.

Definition at line 49 of file BasicStringFileReader.ixx.

49 [[nodiscard]] bool readInto( const std::string& filename, std::string& contents) const noexcept override {
50 std::ifstream infile{ filename };
51
52 if (!infile) {
53 logger_.error(std::format("Cannot open {0}", filename));
54 return false;
55 }
56 std::string line;
57 std::string fileContents;
58
59 while (getline(infile, line)) {
60 fileContents += line + "\n";
61 }
62
63 contents = fileContents;
64 return true;
65 }

Reference helios::util::io::StringFileReader::logger_.


The documentation for this class was generated from the following file:


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.