-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathas_io.h
108 lines (80 loc) · 2.47 KB
/
as_io.h
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
/* IO functions specially made for SaveGuard
* Written by Aaron Sigal
*
*
*/
#ifndef AS_IO_H
#define AS_IO_H
// File IO
bool CopyFile(const std::string &source, const std::string &dest);
bool CreateConfigFile(const std::string &file_name, const std::string &path);
// Property IO
bool WriteProperty(const std::string &file_name, const std::string &prop_name, const std::string &prop_value);
std::string ReadProperty(const std::string &file_name, const std::string &prop_name);
// Console IO
std::string log(const std::string &text);
std::string err(const std::string &text);
bool CopyFile(const std::string &source, const std::string &dest) {
std::ifstream src(source, std::ios::binary);
if (src.good()) {
std::ofstream dst(dest, std::ios::binary);
dst << src.rdbuf();
dst.close();
src.close();
} else {
std::cout << "\nFailed to read " << source << "\n";
src.close();
return false;
}
return true;
}
// Generates an empty config file if one doesn't already exist
bool CreateConfigFile(const std::string &file_name, const std::string &path) {
std::ifstream ifs( (path + file_name).c_str());
if (!ifs.good() ) { // If the config file doesn't exist
std::ofstream ofs((path + file_name).c_str()); // Make it
ofs.close();
ifs.close();
return true; // Return true to show that we successfully made the file
} else {
ifs.close();
return false; // Return false to show we didn't make a file
}
}
// Writes a property and a value to a file
bool WriteProperty(const std::string &file_name, const std::string &prop_name, const std::string &prop_value) {
std::ifstream ifs(file_name.c_str());
if (!ifs.good() ) { // If the config file doesn't exist
ifs.close();
return false;
} else {
std::ofstream ofs(file_name.c_str(), std::ios_base::app);
ofs << prop_name + " " + prop_value << std::endl;
ofs.close();
ifs.close();
return true;
}
}
// Reads a property and returns it
std::string ReadProperty(const std::string &file_name, const std::string &prop_name) {
std::ifstream ifs(file_name.c_str());
if (!ifs.good() ) { // If the config file doesn't exist
ifs.close();
return "error";
} else {
std::string input;
std::vector<std::string> terms;
while (ifs >> input) {
terms.push_back(input);
}
for (int i = 0; i < terms.size(); i++) {
if (terms.at(i) == prop_name) return terms.at(i+1);
}
return "not found";
}
}
std::string log(const std::string &text, std::time_t tt) {
}
std::string err(const std::string &text) {
}
#endif