cae  0.0.0
Cross-API graphics engine
Loading...
Searching...
No Matches
file.cpp
Go to the documentation of this file.
1#include "Utils/File.hpp"
2
3#include <fstream>
4#include <sstream>
5
6std::vector<char> utl::fileToVector(const std::filesystem::path &path)
7{
8 std::ifstream file(path, std::ios::in);
9 if (!file.is_open())
10 {
11 throw std::runtime_error("failed to open file " + path.string());
12 }
13 const long int fileSize = file.tellg();
14 if (fileSize <= 0)
15 {
16 throw std::runtime_error("file " + path.string() + " is empty");
17 }
18 std::vector<char> buffer(static_cast<long unsigned int>(fileSize));
19 file.seekg(0, std::ios::beg);
20 if (!file.read(buffer.data(), fileSize))
21 {
22 throw std::runtime_error("failed to read file " + path.string());
23 }
24 return buffer;
25}
26
27std::string utl::fileToString(const std::filesystem::path &path)
28{
29 const std::ifstream file(path, std::ios::in);
30 if (!file.is_open())
31 {
32 throw std::runtime_error("Failed to open file: " + path.string());
33 }
34 std::stringstream buffer;
35 buffer << file.rdbuf();
36 return buffer.str();
37}
This file contains file utility functions.
std::string fileToString(const std::filesystem::path &path)
Read a file and return its contents as a string.
Definition file.cpp:27
std::vector< char > fileToVector(const std::filesystem::path &path)
Read a file and return its contents as a vector of chars.
Definition file.cpp:6