-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBasicLib.cpp
More file actions
231 lines (194 loc) · 4 KB
/
BasicLib.cpp
File metadata and controls
231 lines (194 loc) · 4 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "BasicLib.h"
namespace BasicLib
{
#ifdef WIN32
class Win32PerformanceCounter {
public:
Win32PerformanceCounter() {
QueryPerformanceFrequency((LARGE_INTEGER*)(&m_frequency));
m_frequency = m_frequency / 1000;
}
sint64 m_frequency;
};
Win32PerformanceCounter g_win32counter;
#endif
sint64 GetTimeS()
{
return GetTimeMS() / 1000;
}
sint64 GetTimeM()
{
return GetTimeMS() / 60000;
}
sint64 GetTimeH()
{
return GetTimeMS() / 3600000;
}
std::string SearchAndReplace(const std::string& p_target, const std::string& p_search, const std::string& p_replace)
{
if (p_target.size() <= 0)
return "";
std::string str = p_target;
int begin = 0;
while (true)
{
begin = 0;
begin = str.find(p_search);
if (begin != std::string::npos)
{
str.replace(begin, p_search.size(), p_replace);
}
else
{
break;
}
}
return str;
}
std::string RemoveWord(const std::string& p_string, int p_index)
{
int wss = p_string.find_first_not_of(WHITESPACE);
while (p_index > 0)
{
p_index--;
wss = p_string.find_first_of(WHITESPACE, wss);
wss = p_string.find_first_not_of(WHITESPACE, wss);
}
int wse = p_string.find_first_of(WHITESPACE, wss);
wse = p_string.find_first_not_of(WHITESPACE, wss);
if (wss == std::string::npos)
{
wss = 0;
wse = 0;
}
std::string str = p_string;
str.erase(wss, wse - wss);
return str;
}
std::string ParseWord(const std::string& p_string, int p_index)
{
int wss = p_string.find_first_not_of(WHITESPACE);
while (p_index > 0)
{
p_index--;
wss = p_string.find_first_of(WHITESPACE, wss);
wss = p_string.find_first_not_of(WHITESPACE, wss);
}
int wse = p_string.find_first_of(WHITESPACE, wss);
if (wss == std::string::npos)
{
wss = 0;
wse = 0;
}
return p_string.substr(wss, wse - wss);
}
std::string TrimWihitespace(const std::string& p_string)
{
int wsf, wsb;
wsf = p_string.find_first_not_of(WHITESPACE);
wsb = p_string.find_last_not_of(WHITESPACE);
if (wsf == std::string::npos)
{
wsf = 0;
wsb = -1;
}
return p_string.substr(wsf, wsb - wsf + 1);
}
std::string LowerCase(const std::string& p_string)
{
std::string str = p_string;
for (int i = 0; i < str.size(); ++i)
{
str[i] = std::tolower(str[i]);
}
return str;
}
std::string UppercCase(const std::string& p_string)
{
std::string str = p_string;
for (int i = 0; i < str.size(); ++i)
{
str[i] = std::toupper(str[i]);
}
return str;
}
std::string DateStamp()
{
char str[11];
time_t a = time(0);
tm b;
gmtime_s(&b, &a);
strftime(str, 11, "%Y.%m.%d", &b);
return str;
}
std::string TimeStamp()
{
char str[9];
time_t a = time(0);
tm b;
gmtime_s(&b, &a);
strftime(str, 9, "%H:%M:%S", &b);
return str;
}
sint64 GetTimeMS()
{
#ifdef WIN32
sint64 t;
QueryPerformanceCounter((LARGE_INTEGER*)(&t));
return t / g_win32counter.m_frequency;
#else
struct timeval t;
sint64 s;
gettimeofday(&t, 0);
s = t.tv_sec;
s *= 1000;
s += (t.tv_usec / 1000);
return s;
#endif
}
void BasicLib::Timer::Reset(BasicLib::sint64 p_timepassed)
{
m_starttime = p_timepassed;
m_inittime = BasicLib::GetTimeMS();
}
BasicLib::sint64 BasicLib::Timer::GetMS()
{
return (BasicLib::GetTimeMS() - m_inittime) + m_starttime;
}
BasicLib::sint64 BasicLib::Timer::GetS()
{
return GetMS() / 1000;
}
BasicLib::sint64 BasicLib::Timer::GetM()
{
return GetS() / 60;
}
BasicLib::sint64 BasicLib::Timer::GetH()
{
return GetMS() / 3600000;
}
BasicLib::sint64 BasicLib::Timer::GetD()
{
return GetH() / 24;
}
BasicLib::sint64 BasicLib::Timer::GetY()
{
return GetD() / 365;
}
std::string Timer::GetString()
{
std::string str;
sint64 y = GetY();
sint64 d = GetD()%365;
sint64 h = GetH()%24;
sint64 m = GetM()%60;
if(y > 0)
str += BasicLib::tostring(y) + " years, ";
if (d > 0)
str += BasicLib::tostring(d) + " days, ";
if (h > 0)
str += BasicLib::tostring(h) + " hours, ";
str += BasicLib::tostring(m) + " minutes";
return str;
}
};