-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.h
182 lines (133 loc) · 3.48 KB
/
utils.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
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
#pragma once
// std
#include <chrono>
#include <iostream>
#include <thread>
#include <string>
#include <sstream>
#include <vector>
//=====================
// deletePointersInVector
//=====================
template < typename T >
inline void deletePointersInVector( std::vector< T > & _vector ){
for( T & pointer : _vector )
delete pointer;
}
//=====================
// unescape
//=====================
inline void unescape( const char * _escaped, const int _strLen, std::string & _unescaped ){
_unescaped.resize( _strLen );
int unescapedIdx = 0;
for( int i = 0; i < _strLen; i++ ){
if( '\\' == _escaped[ i ] ){
switch( _escaped[ ++i ] ){
case '\\' : _unescaped[ unescapedIdx++ ] = '\\'; break;
case '\"' : _unescaped[ unescapedIdx++ ] = '\"'; break;
}
}
else{
_unescaped[ unescapedIdx++ ] = _escaped[ i ];
}
}
_unescaped.shrink_to_fit();
}
//=====================
// isNumber
//=====================
inline bool isNumber( const std::string & _msg ){
for( int i = 0; i < _msg.size(); i++ ){
if( ! std::isdigit( _msg[ i ]) ){
std::cerr << "it's not a number: " << _msg << std::endl;
return false;
}
}
return true;
}
//=====================
// printCurrentTime
//=====================
inline std::string printCurrentTime(){
time_t timev;
time( & timev );
tm * timeinfo = localtime( & timev );
std::stringstream time;
time << "current time: " << timeinfo->tm_hour << "h " << timeinfo->tm_min << "m " << timeinfo->tm_sec << "s";
return time.str();
}
//=====================
// getCurrentTime (milliseconds)
//=====================
inline long getCurrentTimeMilliSec(){
using namespace std::chrono;
auto currentTime = high_resolution_clock::now().time_since_epoch();
return duration_cast<milliseconds>(currentTime).count();
}
//=====================
// getCurrentTime (seconds)
//=====================
inline long getCurrentTimeSec(){
using namespace std::chrono;
auto currentTime = high_resolution_clock::now().time_since_epoch();
return duration_cast<seconds>(currentTime).count();
}
//=====================
// strToWstr
//=====================
inline std::wstring strToWstr(const std::string _str){
return std::wstring( _str.begin(), _str.end() );
}
//=====================
// wstrToStr
//=====================
inline std::string wstrToStr(const std::wstring & _wstr){
char buffer[ _wstr.size() * 2 ];
int rt = wcstombs( buffer, _wstr.data(), sizeof(buffer) );
return buffer;
}
//=====================
// shutdownThread
//=====================
inline bool shutdownThread( std::thread * _thread ){
if( _thread ){
if( _thread->joinable() ){
_thread->join();
delete _thread;
_thread = nullptr;
return true;
}
else{
return false;
}
}
else{
return false;
}
}
//=====================
// Variant
//=====================
class Variant{
public:
Variant( const int _digit ) :
i(_digit),
s(std::to_string(_digit)),
c(s.c_str()) {
}
Variant( const char * _cstr ) :
i(atoi(_cstr)),
s(_cstr),
c(_cstr) {
}
Variant( const std::string _str ) :
i(atoi(_str.c_str())),
s(_str),
c(_str.c_str()) {
}
~Variant(){
}
const int i;
const std::string s;
const char * c;
};