Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
file_handler.cpp
Go to the documentation of this file.
1
5
6// standard includes
7#include <filesystem>
8#include <fstream>
9
10// local includes
11#include "file_handler.h"
12#include "logging.h"
13
14namespace file_handler {
15 std::string get_parent_directory(const std::string &path) {
16 // remove any trailing path separators
17 std::string trimmed_path = path;
18 while (!trimmed_path.empty() && trimmed_path.back() == '/') {
19 trimmed_path.pop_back();
20 }
21
22 std::filesystem::path p(trimmed_path);
23 return p.parent_path().string();
24 }
25
26 bool make_directory(const std::string &path) {
27 // first, check if the directory already exists
28 if (std::filesystem::exists(path)) {
29 return true;
30 }
31
32 return std::filesystem::create_directories(path);
33 }
34
35 std::string read_file(const char *path) {
36 if (!std::filesystem::exists(path)) {
37 BOOST_LOG(debug) << "Missing file: " << path;
38 return {};
39 }
40
41 std::ifstream in(path);
42 return std::string {(std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()};
43 }
44
45 int write_file(const char *path, const std::string_view &contents) {
46 std::ofstream out(path);
47
48 if (!out.is_open()) {
49 return -1;
50 }
51
52 out << contents;
53
54 return 0;
55 }
56} // namespace file_handler
Declarations for file handling functions.
Declarations for logging related functions.
Responsible for file handling functions.
std::string get_parent_directory(const std::string &path)
Get the parent directory of a file or directory.
std::string read_file(const char *path)
Read a file to string.
int write_file(const char *path, const std::string_view &contents)
Writes a file.
bool make_directory(const std::string &path)
Make a directory.