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::string fs::readFileText(const std::filesystem::path& filepath, std::size_t startOffset) {
17 FileStream stream{filepath};
18 if (!stream) {
19 return {};
20 }
21 stream.seek_in_u(startOffset);
22 return stream.read_string();
23}
24
25bool fs::writeFileBuffer(const std::filesystem::path& filepath, std::span<const std::byte> buffer) {
26 FileStream stream{filepath, FileStream::OPT_TRUNCATE | FileStream::OPT_CREATE_IF_NONEXISTENT};
27 if (!stream) {
28 return false;
29 }
30 stream.seek_out(0).write(buffer);
31 return true;
32}
33
34bool fs::writeFileText(const std::filesystem::path& filepath, std::string_view text) {
35 FileStream stream{filepath, FileStream::OPT_TRUNCATE | FileStream::OPT_CREATE_IF_NONEXISTENT};
36 if (!stream) {
37 return false;
38 }
39 stream.seek_out(0).write(text, false);
40 return true;
41}
std::string readFileText(const std::filesystem::path &filepath, std::size_t startOffset=0)
Definition FS.cpp:16
bool writeFileText(const std::filesystem::path &filepath, std::string_view text)
Definition FS.cpp:34
bool writeFileBuffer(const std::filesystem::path &filepath, std::span< const std::byte > buffer)
Definition FS.cpp:25
std::vector< std::byte > readFileBuffer(const std::filesystem::path &filepath, std::size_t startOffset=0)
Definition FS.cpp:7