-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathfiles.cpp
More file actions
110 lines (81 loc) · 2.45 KB
/
Copy pathfiles.cpp
File metadata and controls
110 lines (81 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#pragma once
#include "files.h"
#include <Windows.h>
#include <fstream>
#include <filesystem>
namespace files {
std::string get_directory() {
char buffer[MAX_PATH];
GetModuleFileNameA(NULL, buffer, MAX_PATH);
std::string::size_type position = std::string(buffer).find_last_of("\\/");
return std::string(buffer).substr(0, position);
}
std::string get_tmp_directory() {
return std::filesystem::temp_directory_path().generic_string();
}
std::string read(const char* directory) {
// get document contents
std::ifstream file(directory, std::ios::in);
std::string contents;
char current_character = file.get();
while (file.good()) {
contents += current_character;
current_character = file.get();
}
file.close();
return contents;
}
bool write(const char* directory, const char* contents) {
std::ofstream file(directory, std::ofstream::out);
file << contents;
file.close();
return true;
}
bool append(const char* directory, const char* contents) {
std::string file_contents = read(directory);
std::string new_contents(file_contents + (std::string(contents) + "\n").c_str());
return write(directory, new_contents.c_str());
}
bool create_directory(const char* string) {
return std::filesystem::create_directories(string);
}
bool delete_directory(const char* string) {
return std::filesystem::remove(string);
}
std::string load_binary(const char* filepath) {
std::ifstream ifs(filepath, std::ios::binary | std::ios::ate);
std::string result;
if (!ifs)
return result;
auto end = ifs.tellg();
ifs.seekg(0, std::ios::beg);
auto size = std::size_t(end - ifs.tellg());
if (size == 0) // avoid undefined behavior
return {};
result.resize(size);
if (!ifs.read((char*)result.data(), result.size()))
return result;
return result;
}
std::string load_binary(const wchar_t* filepath) {
std::ifstream ifs(filepath, std::ios::binary | std::ios::ate);
std::string result;
if (!ifs)
return result;
auto end = ifs.tellg();
ifs.seekg(0, std::ios::beg);
auto size = std::size_t(end - ifs.tellg());
if (size == 0) // avoid undefined behavior
return {};
result.resize(size);
if (!ifs.read((char*)result.data(), result.size()))
return result;
return result;
}
bool write_binary(const char* filepath, const char* contents, size_t size) {
std::ofstream ofs(filepath, std::ios::binary | std::ios::ate);
if (!ofs || !ofs.write(contents, size))
return false;
return true;
}
};