-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfile_manager.cpp
95 lines (73 loc) · 1.46 KB
/
file_manager.cpp
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
#include "file_manager.h"
bool FileManager::directory_exists(std::string dir)
{
path p(dir);
if(exists(p)) {
if(is_directory(p)) {
return true;
}
}
return false;
}
bool FileManager::file_exists(std::string file)
{
path p(file);
if(exists(p)) {
if(is_regular_file(p)) {
return true;
}
}
return false;
}
void FileManager::mkdirs(std::string dir)
{
create_directories(dir);
}
void FileManager::rmdirs(std::string dir)
{
if(directory_exists(dir)) {
path p(dir);
remove_all(p);
}
}
void FileManager::rm(std::string file)
{
if(file_exists(file)) {
path p(file);
remove(p);
}
}
void FileManager::write(std::string& content, std::string file_path)
{
std::ofstream file(file_path.c_str());
if(file) {
file << content;
file.close();
}
}
std::string FileManager::checksum(std::string file_path)
{
if(file_exists(file_path)) {
std::string content = "";
std::string line = "";
std::ifstream file(file_path.c_str());
if(file) {
while(file.good()) {
std::getline(file, line);
content += line;
}
}
boost::crc_32_type result;
result.process_bytes(content.data(), content.length());
std::stringstream ss;
ss << std::hex << std::uppercase << result.checksum();
return ss.str();
}
return "";
}
void FileManager::mv(std::string source, std::string destination)
{
path src(source);
path dest(destination);
rename(src, dest);
}