-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmrutil.h
95 lines (86 loc) · 1.86 KB
/
mrutil.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
#pragma once
#include "sstream"
#include "vector"
#include "algorithm"
#include "string"
#include <memory>
template <class T>
static std::string tostring(const T &t)
{
std::stringstream ss;
ss << t;
return ss.str();
}
template <class T>
static T fromstring(const std::string &s)
{
std::stringstream ss(s);
T t;
ss >> t;
return t;
}
static std::string int2string(int n)
{
std::stringstream ss;
ss << n;
return ss.str();
}
static int string2int(std::string s)
{
std::stringstream ss(s);
int n;
ss >> n;
return n;
}
static std::string double2string(double d)
{
std::stringstream ss;
ss << d;
return ss.str();
}
static double string2double(std::string s)
{
std::stringstream ss(s);
double d;
ss >> d;
return d;
}
static std::vector<std::string> split(const std::string& s, const std::string delim)
{
std::vector< std::string > ret;
size_t last = 0;
size_t index = s.find_first_of(delim, last);
while (index != std::string::npos)
{
ret.push_back(s.substr(last, index - last));
last = index + 1;
index = s.find_first_of(delim, last);
}
if (index - last > 0)
{
ret.push_back(s.substr(last, index - last));
}
return ret;
}
template<typename ... Args>
std::string string_format( const std::string& format, Args ... args )
{
size_t size = snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0'
if( size <= 0 ){ throw std::runtime_error( "Error during formatting." ); }
std::unique_ptr<char[]> buf( new char[ size ] );
snprintf( buf.get(), size, format.c_str(), args ... );
return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside
}
#ifdef _WIN32
#include <windows.h>
void static MRsleep(unsigned milliseconds)
{
Sleep(milliseconds);
}
#else
#include <unistd.h>
void static MRsleep(unsigned milliseconds)
{
usleep(milliseconds * 1000); // takes microseconds
}
#endif