SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
FS.cpp
Go to the documentation of this file.
1#include <sourcepp/FS.h>
2
3#include <FileStream.h>
4
5using namespace sourcepp;
6
7std::vector<std::byte> fs::readFileBuffer(const std::filesystem::path& filepath, std::size_t startOffset) {
8 FileStream stream{filepath};
9 if (!stream) {
10 return {};
11 }
12 stream.seek_in_u(startOffset);
13 return stream.read_bytes(std::filesystem::file_size(filepath) - startOffset);
14}
15
16std::vector<std::byte> fs::readFileBuffer(const std::filesystem::path& filepath, bool& exists, std::size_t startOffset) {
17 auto buffer = readFileBuffer(filepath, startOffset);
18 exists = !buffer.empty() || std::filesystem::exists(filepath);
19 return buffer;
20}
21
22std::string fs::readFileText(const std::filesystem::path& filepath, std::size_t startOffset) {
23 FileStream stream{filepath};
24 if (!stream) {
25 return {};
26 }
27 stream.seek_in_u(startOffset);
28 return stream.read_string();
29}
30
31std::string fs::readFileText(const std::filesystem::path& filepath, bool& exists, std::size_t startOffset) {
32 auto text = readFileText(filepath, startOffset);
33 exists = !text.empty() || std::filesystem::exists(filepath);
34 return text;
35}
36
37bool fs::writeFileBuffer(const std::filesystem::path& filepath, std::span<const std::byte> buffer) {
38 FileStream stream{filepath, FileStream::OPT_TRUNCATE | FileStream::OPT_CREATE_IF_NONEXISTENT};
39 if (!stream) {
40 return false;
41 }
42 stream.seek_out(0).write(buffer);
43 return true;
44}
45
46bool fs::writeFileText(const std::filesystem::path& filepath, std::string_view text) {
47 FileStream stream{filepath, FileStream::OPT_TRUNCATE | FileStream::OPT_CREATE_IF_NONEXISTENT};
48 if (!stream) {
49 return false;
50 }
51 stream.seek_out(0).write(text, false);
52 return true;
53}
std::string readFileText(const std::filesystem::path &filepath, std::size_t startOffset=0)
Definition FS.cpp:22
bool writeFileText(const std::filesystem::path &filepath, std::string_view text)
Definition FS.cpp:46
bool writeFileBuffer(const std::filesystem::path &filepath, std::span< const std::byte > buffer)
Definition FS.cpp:37
std::vector< std::byte > readFileBuffer(const std::filesystem::path &filepath, std::size_t startOffset=0)
Definition FS.cpp:7