engine/src/util/files.cpp

53 lines
1023 B
C++
Raw Normal View History

2022-11-22 21:50:06 +00:00
#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;
}
2022-11-23 16:20:08 +00:00
std::unique_ptr<std::vector<uint8_t>> readBinaryFile(const std::string& path)
{
std::ifstream file(path, std::ios::ate | std::ios::binary);
if (file.is_open() == false) {
throw std::runtime_error("Unable to open file " + path);
}
auto buffer = std::make_unique<std::vector<uint8_t>>(file.tellg());
file.seekg(0);
file.read((char*)buffer->data(), buffer->size());
file.close();
return buffer;
}
2022-11-22 21:50:06 +00:00
}
2022-11-23 16:20:08 +00:00