This repository was archived by the owner on May 31, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathutil.cpp
More file actions
197 lines (183 loc) · 5.16 KB
/
Copy pathutil.cpp
File metadata and controls
197 lines (183 loc) · 5.16 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
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <CPUSnapshot.h>
#include <aegis.hpp>
#include <string>
#include <vector>
#include <chrono>
#include <sstream>
#include <future>
using namespace std;
vector<char> to_char_vector(string input)
{
return vector<char>(input.begin(), input.end());
}
vector<string> vector_slice(vector<string> arr, int i)
{
return vector<string>(arr.begin() + i, arr.end());
}
string string_round(float var)
{
char str[40];
sprintf(str, "%.2f", var);
return str;
}
future<string> get_cpu_usage()
{
return async(launch::async, []() {
CPUSnapshot previousSnap;
this_thread::sleep_for(chrono::seconds(1));
CPUSnapshot curSnap;
const float ACTIVE_TIME = curSnap.GetActiveTimeTotal() - previousSnap.GetActiveTimeTotal();
const float IDLE_TIME = curSnap.GetIdleTimeTotal() - previousSnap.GetIdleTimeTotal();
const float TOTAL_TIME = ACTIVE_TIME + IDLE_TIME;
float usage = 100.f * ACTIVE_TIME / TOTAL_TIME;
return string_round(usage);
});
}
int16_t to_i16(string str, int length)
{
string myThing = "Invalid number!";
if (str.length() > length)
throw myThing;
std::istringstream iss(str);
int16_t i16;
iss >> i16;
if (iss.fail())
throw myThing;
else
return i16;
}
int64_t to_number(string str, int length)
{
string myThing = "Invalid number!";
if (str.length() > length)
throw myThing;
std::istringstream iss(str);
int64_t i64;
iss >> i64;
if (iss.fail())
throw myThing;
else
return i64;
}
vector<string> split(string str, string splitBy)
{
vector<string> tokens;
tokens.push_back(str);
size_t splitAt;
size_t splitLen = splitBy.size();
string frag;
while (true)
{
frag = tokens.back();
splitAt = frag.find(splitBy);
if (splitAt == string::npos)
break;
tokens.back() = frag.substr(0, splitAt);
tokens.push_back(frag.substr(splitAt + splitLen, frag.size() - (splitAt + splitLen)));
}
return tokens;
}
string join(vector<string> arr, string joinwith)
{
string tosend;
int i = 0;
for (string thing : arr)
{
tosend += thing + ((i - 1) == arr.size() ? "" : joinwith);
i++;
}
return tosend;
}
string reverse_string(string line)
{
string::reverse_iterator it;
string result;
for (it = line.rbegin(); it < line.rend(); it++)
{
result += *it;
}
return result;
}
string date(int64_t timestamp)
{
chrono::system_clock::time_point starttime = chrono::system_clock::from_time_t(timestamp / 1000);
chrono::time_point<std::chrono::system_clock> now_t = chrono::system_clock::now();
auto time_is = now_t - starttime;
using seconds = chrono::duration<int, ratio<1, 1>>;
using minutes = chrono::duration<int, ratio<60, 1>>;
using hours = chrono::duration<int, ratio<3600, 1>>;
using days = chrono::duration<int, ratio<24 * 3600, 1>>;
using months = chrono::duration<int, ratio<24 * 3600 * 30, 1>>;
using years = chrono::duration<int, ratio<24 * 3600 * 365, 1>>;
std::stringstream ss;
auto y = chrono::duration_cast<years>(time_is).count();
time_is -= years(y);
auto mo = chrono::duration_cast<months>(time_is).count();
time_is -= months(mo);
auto d = chrono::duration_cast<days>(time_is).count();
time_is -= days(d);
auto h = chrono::duration_cast<hours>(time_is).count();
time_is -= hours(h);
auto m = chrono::duration_cast<minutes>(time_is).count();
time_is -= minutes(m);
auto s = chrono::duration_cast<seconds>(time_is).count();
if (y)
ss << y << " years, ";
if (mo)
ss << mo << " months, ";
if (d)
ss << d << " days, ";
if (h)
ss << h << " hours, ";
if (m)
ss << m << " minutes, ";
ss << s << " seconds ";
return ss.str();
}
//From aegis.cpp's example.cpp
aegis::snowflake get_snowflake(const string name, aegis::guild &_guild) noexcept
{
if (name.empty())
return 0;
try
{
if (name[0] == '<')
{
std::string::size_type pos = name.find_first_of('>');
if (pos == std::string::npos)
return 0;
if (name[2] == '!')
return std::stoull(std::string{name.substr(3, pos - 1)});
else if (name[2] == '&')
return std::stoull(std::string{name.substr(3, pos - 1)});
else if (name[1] == '#')
return std::stoull(std::string{name.substr(2, pos - 1)});
else
return std::stoull(std::string{name.substr(2, pos - 1)});
}
else if (std::isdigit(name[0]))
{
return std::stoull(std::string{name});
}
else
{
std::string::size_type n = name.find('#');
if (n != std::string::npos)
{
for (auto &m : _guild.get_members())
if (m.second->get_full_name() == name)
return {m.second->get_id()};
return 0;
}
return 0;
}
}
catch (std::invalid_argument &)
{
return 0;
}
catch (...)
{
return 0;
}
}