mirror of
https://github.com/bailwillharr/engine.git
synced 2024-09-21 13:01:19 +00:00
34 lines
598 B
C++
34 lines
598 B
C++
|
#include "util/files.hpp"
|
||
|
|
||
|
#include <fstream>
|
||
|
|
||
|
namespace engine::util {
|
||
|
|
||
|
std::unique_ptr<std::vector<char>> readTextFile(const std::string& path)
|
||
|
{
|
||
|
auto buffer = std::make_unique<std::vector<char>>();
|
||
|
|
||
|
std::ifstream file(path, std::ios::ate);
|
||
|
|
||
|
if (file.is_open() == false) {
|
||
|
throw std::runtime_error("Unable to open file " + path);
|
||
|
}
|
||
|
|
||
|
// reserve enough space for the text file, but leave the size at 0
|
||
|
buffer->reserve(file.tellg());
|
||
|
|
||
|
file.seekg(0);
|
||
|
|
||
|
while (!file.eof()) {
|
||
|
char c{};
|
||
|
file.read(&c, 1);
|
||
|
buffer->push_back(c);
|
||
|
}
|
||
|
|
||
|
file.close();
|
||
|
|
||
|
return buffer;
|
||
|
}
|
||
|
|
||
|
}
|