Hermes
0.7.0
Linux game streaming host
Toggle main menu visibility
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
14
namespace
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
file_handler.h
Declarations for file handling functions.
logging.h
Declarations for logging related functions.
file_handler
Responsible for file handling functions.
Definition
file_handler.cpp:14
file_handler::get_parent_directory
std::string get_parent_directory(const std::string &path)
Get the parent directory of a file or directory.
Definition
file_handler.cpp:15
file_handler::read_file
std::string read_file(const char *path)
Read a file to string.
Definition
file_handler.cpp:35
file_handler::write_file
int write_file(const char *path, const std::string_view &contents)
Writes a file.
Definition
file_handler.cpp:45
file_handler::make_directory
bool make_directory(const std::string &path)
Make a directory.
Definition
file_handler.cpp:26
src
file_handler.cpp
Generated by
1.18.0