File filetostring.h
File List > code_source > templat > include > TempLat > util > filetostring.h
Go to the documentation of this file
#ifndef TEMPLAT_UTIL_FILETOSTRING_H
#define TEMPLAT_UTIL_FILETOSTRING_H
/* This file is part of TempLat, available at https://cosmolattice.github.io/templat .
Copyright 2021-2026 The TempLat authors, see AUTHORS.md.
Released under the MIT license, see LICENSE.md. */
// File info: Main contributor(s): Wessel Valkenburg, Year: 2019
#include <fstream>
namespace TempLat
{
class FileToString
{
public:
// Put public methods here. These should change very little over time.
FileToString(const std::string fName) : mFName(fName), didIt(false) {}
operator std::string()
{
readFile();
return payload;
}
friend std::ostream &operator<<(std::ostream &ostream, FileToString &obj)
{
ostream << (std::string)obj;
return ostream;
}
private:
/* Put all member variables and private methods here. These may change arbitrarily. */
const std::string mFName;
std::string payload;
bool didIt;
void readFile()
{
if (didIt) return;
didIt = true;
std::ifstream t(mFName);
t.seekg(0, std::ios::end);
size_t size = t.tellg();
std::string buffer(size, ' ');
t.seekg(0);
t.read(&buffer[0], size);
payload = buffer;
}
};
} // namespace TempLat
#endif