forked from Shuzhengz/JEC-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.cpp
77 lines (62 loc) · 1.86 KB
/
library.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
#include "library.hpp"
using namespace std;
namespace fs = std::experimental::filesystem;
namespace library {
fs::path Config::rm_filename(fs::path& path) {
if (path.has_filename()) {
return path.remove_filename();
} else {
return path;
}
}
bool Config::exists (fs::path& path) {
struct stat buffer{};
return (stat (path.c_str(), &buffer) == 0);
}
void ConfigFile::remove(fs::path& path) {
if (exists(path)){
const char *file = path.c_str();
std::remove(file);
} else {
throw std::invalid_argument("path does not exist \n");
}
}
void ConfigFile::create(fs::path& path, string& name) {
ofstream file;
if (exists(path)){
file.open(path.c_str() + name);
} else {
throw std::invalid_argument("path does not exist \n");
}
}
string ConfigFile::from_home(fs::path& path) {
if (exists(path)){
return fs::absolute(path);
} else {
throw std::invalid_argument("path does not exist \n");
}
}
void ConfigDir::remove(fs::path& path) {
fs::path p = rm_filename(path);
if (exists(p)){
rmdir(p.c_str());
} else {
throw std::invalid_argument("path does not exist \n");
}
}
void ConfigDir::create(fs::path& path, char name) {
fs::path p = rm_filename(path);
if (exists(p)){
mkdir(p.c_str() + name, S_IRWXU);
} else {
throw std::invalid_argument("path does not exist \n");
}
}
string ConfigDir::from_home(fs::path& path) {
if (exists(path)){
return fs::absolute(path);
} else {
throw std::invalid_argument("path does not exist \n");
}
}
} // namespace library