-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSystem.hpp
More file actions
161 lines (131 loc) · 3.37 KB
/
Copy pathSystem.hpp
File metadata and controls
161 lines (131 loc) · 3.37 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
#pragma once
#include <fstream>
#include <iostream>
#include <unistd.h>
std::string getCurTime()
{
time_t t = time(0);
tm* now = localtime(&t);
int hour = now->tm_hour;
std::string hour_str = std::to_string(hour);
if (hour < 10) {
hour_str = "0" + hour_str;
}
int min = now->tm_min;
std::string min_str = std::to_string(min);
if (min < 10)
min_str = "0" + min_str;
int sec = now->tm_sec;
std::string sec_str = std::to_string(sec);
if (sec < 10)
sec_str = "0" + sec_str;
#ifdef PC
// return "12:12:12";
return hour_str + ':' + min_str + ':' + sec_str;
#else
return hour_str + ':' + min_str + ':' + sec_str;
#endif
}
std::string getDay()
{
time_t t = time(0);
tm* now = localtime(&t);
int day = now->tm_mday;
std::string day_str = std::to_string(day);
if (day < 10)
day_str = "0" + day_str;
return day_str;
}
std::string getMonth()
{
time_t t = time(0);
tm* now = localtime(&t);
int month = 1 + now->tm_mon;
std::string month_str = std::to_string(month);
if (month < 10)
month_str = "0" + month_str;
return month_str;
}
std::string getYear()
{
time_t t = time(0);
tm* now = localtime(&t);
int year = 1900 + now->tm_year;
std::string year_str = std::to_string(year);
if (year < 10)
year_str = "0" + year_str;
return year_str;
}
std::string getHostname()
{
std::string file = "/etc/hostname";
std::ifstream getFile(file.c_str());
if (!getFile.is_open()) {
std::cout << "unable to open /etc/hostname" << std::endl;
perror(file.c_str());
exit(4);
}
std::string ret;
getFile >> ret;
getFile.close();
return ret;
}
// Point3f readPointFromFile(std::string path) {
// // std::ifstream file("learningFile/known/" + filename + "/min.txt");
// std::ifstream file(path);
// if (!file.is_open()) {
// std::cout << "cannot open file known" << std::endl;
// exit(1);
// }
// // while (getline(file, line)) {
// std::string line;
// getline(file, line);
// file.close();
// float h, h2, h3;
// sscanf(line.c_str(), "%f %f %f", &h, &h2, &h3);
// return std::move(Point3f(h, h2, h3));
// }
std::vector<std::string> forEachFileInDir(std::string dirPath)
{
// std::string dirPath = "learningFile/known/";
std::vector<std::string> files;
DIR* dp;
struct dirent* dirp;
if ((dp = opendir(dirPath.c_str())) == NULL) {
std::cout << "Error cannot opening " << dirPath << std::endl;
exit(2);
}
while ((dirp = readdir(dp)) != NULL) {
std::string filename(dirp->d_name);
if (filename.compare(".") && filename.compare("..")) {
files.push_back(std::move(filename));
}
}
closedir(dp);
return files;
// return std::move(files);
}
bool emptyDir(std::string dirPath)
{
int cpt = 0;
for (auto& file : forEachFileInDir(dirPath)) {
(void)file;
++cpt;
}
return cpt == 0;
}
void thread_alert(std::string filename)
{
std::cout << "new thread : " << filename << std::endl;
std::fstream fs;
fs.open(filename, std::ios::out);
if (!fs.is_open()) {
std::cout << "unable to open file : " << filename << std::endl;
}
fs.close();
usleep(1000000 * 18);
std::remove(filename.c_str());
if (emptyDir("alert/")) {
std::remove("alert.jpg");
}
}