Line data Source code
1 : #include "StringUtils.h"
2 : #include "IntegerCast.h"
3 :
4 : #include <libassert/assert.hpp>
5 :
6 : #include <fstream>
7 :
8 34884 : std::string readFileAsString(std::filesystem::path const &path) {
9 34884 : ASSERT(exists(path), "File not found!", path.string());
10 34884 : std::string result;
11 34884 : std::ifstream ifs(path);
12 34884 : ifs.seekg(0, std::ios::end);
13 34884 : std::size_t size = ifs.tellg();
14 34884 : ifs.seekg(0);
15 34884 : result.resize_and_overwrite(size, [&ifs](char *buffer, std::size_t size) {
16 34884 : ifs.read(buffer, integerCast<std::streamsize>(size));
17 34884 : return integerCast<std::size_t>(ifs.gcount());
18 34884 : });
19 34884 : return result;
20 34884 : }
|