-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.cpp
More file actions
184 lines (168 loc) · 5.01 KB
/
io.cpp
File metadata and controls
184 lines (168 loc) · 5.01 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <ctime>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <iostream>
#include <filesystem>
#include "io.h"
#include "misc.h"
#include "time.h"
using namespace std;
void print_shell_result(ShellResult result) {
cout << "Result:" << endl;
cout << " command: \"" << result.command << "\"" << endl;
cout << " cwd: \"" << result.cwd << "\"" << endl;
cout << " return_code: " << result.return_code << endl;
cout << " output:" << endl;
cout << result.output << endl;
}
ShellResult run_command(string command, string cwd, bool debug) {
ShellResult result;
result.command = command;
char command_c_string[1024];
strcpy(command_c_string, command.c_str());
char existing_cwd_c_string[1024];
getcwd(existing_cwd_c_string, 1024);
if (cwd == "") {
result.cwd = string(existing_cwd_c_string);
if (debug) {
cout << "Running command \"" << command << "\" from directory \"" << result.cwd << "\"..." << endl;
}
result.return_code = system(command_c_string);
} else {
result.cwd = cwd;
char cwd_c_string[1024];
strcpy(cwd_c_string, cwd.c_str());
chdir(cwd_c_string);
if (debug) {
cout << "Running command \"" << command << "\" from directory \"" << cwd << endl;
}
result.return_code = system(command_c_string);
chdir(existing_cwd_c_string);
}
return result;
}
ShellResult return_output(string command, string cwd, bool debug) {
ShellResult result;
char command_c_string[1024];
int return_code;
char path[1024];
char existing_cwd_c_string[1024];
char cwd_c_string[1024];
FILE *handle;
strcpy(command_c_string, command.c_str());
result.command = command;
char buffer[128];
result.cwd = (cwd == "") ? string(existing_cwd_c_string) : cwd;
if (cwd != "") {
getcwd(existing_cwd_c_string, 1024);
strcpy(cwd_c_string, cwd.c_str());
chdir(cwd_c_string);
}
if (debug) {
cout << "Running command \"" << command << "\" from directory \"" << result.cwd << "\"..." << endl;
}
handle = popen(command_c_string, "r");
if (handle == NULL) {
cout << "Could not open handle for running command: \"" << command << "\"";
return result;
}
while (fgets(buffer, sizeof(buffer), handle) != nullptr) {
result.output.append(buffer);
}
result.return_code = pclose(handle);
if (cwd != "") {
chdir(existing_cwd_c_string);
}
return result;
}
void write_file(string path, string str) {
ofstream handle(path);
if (handle.is_open()) {
handle << str;
handle.close();
} else {
cout << "Unable to open file \"" << path << "\" for writing";
}
}
void append_file(string path, string str) {
ofstream handle(path, ios_base::app);
if (handle.is_open()) {
handle << str;
handle.close();
} else {
cout << "Unable to open file \"" << path << "\" for appending";
}
}
string read_file(string path) {
string contents;
string line;
ifstream handle(path);
if (handle.is_open()) {
while (getline(handle, line)) {
contents.append(line);
contents.append("\n");
}
handle.close();
return contents;
} else {
cout << "Unable to open file \"" << path << "\" for reading";
}
return "";
}
string getLogLevelString(LogLevel log_level) {
switch (log_level) {
case LogLevel::TRACE:
return "TRACE";
case LogLevel::DEBUG:
return "DEBUG";
case LogLevel::INFO:
return "INFO";
case LogLevel::WARN:
return "WARN";
case LogLevel::ERROR:
return "ERROR";
case LogLevel::FATAL:
return "FATAL";
default:
return "UNKNOWN";
}
}
void log(string log_file, string text, LogLevel log_level) {
time_t unix_time = get_unix_time();
string time_string = get_time_string(unix_time);
ostringstream entry;
entry << time_string << " " << getLogLevelString(log_level) << ": " << text << endl;
cout << entry.str();
append_file(log_file, entry.str());
}
bool file_exists(string path) {
return filesystem::exists(path);
}
string get_symlink_target(string path) {
if (file_exists(path)) {
if (filesystem::is_symlink(path)) {
return filesystem::read_symlink(path);
}
cout << "\"" << path << "\" is not a symlink" << endl;
return "";
}
cout << "\"" << path << "\" does not exist" << endl;
return "";
}
void create_dir(string path) {
filesystem::create_directories(path);
}
void remove_dir(string path) {
filesystem::remove_all(path);
}
void copy_dir(string source, string target) {
try {
filesystem::copy(source, target, filesystem::copy_options::overwrite_existing | filesystem::copy_options::recursive);
} catch (exception& e) {
cout << e.what();
}
}