-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs.h
125 lines (105 loc) · 2.36 KB
/
args.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
#pragma once
#include <string>
#include <sstream>
#include <map>
#include <functional>
#include <inttypes.h>
#include <chrono>
#include <time.h>
#include <thread>
#include <limits.h>
using namespace std;
using UsageHandle = std::function<void (void)>;
static std::map<string, string> params;
namespace robin
{
int inline parseArgs(int argc, char ** argv, UsageHandle usage)
{
params.clear();
for (int i = 1; i < argc; )
{
string key = argv[i];
i++;
if (i >= argc)
{
if (usage)
usage();
break;
}
string val = argv[i];
i++;
params.insert(std::make_pair(key, val));
}
return (int)params.size();
}
int inline getIntArg(const char * name, int& val, int init_val = 1, int lower = INT_MIN, int upper = INT_MAX)
{
string tmp = params[name];
if (!tmp.empty())
{
val = std::atoi(tmp.c_str());
if (val < lower)
val = lower;
if (val > upper)
val = upper;
return 1;
}
else
{
val = init_val;
return 0;
}
}
int inline getInt64Arg(const char * name, int64_t& val, int64_t init_val = 1, int64_t lower = LLONG_MIN, int64_t upper = LLONG_MAX)
{
string tmp = params[name];
if (!tmp.empty())
{
val = std::atoi(tmp.c_str());
if (val < lower)
val = lower;
if (val > upper)
val = upper;
return 1;
}
else
{
val = init_val;
return 0;
}
}
int inline getStringArg(const char * name, string & val, const char * init_val)
{
val = params[name];
if (val.empty())
{
val = init_val;
return 0;
}
return 1;
}
static inline void sleepForMs(int msecs)
{
#if defined (_WIN32) || defined (_WIN64)
std::this_thread::sleep_for(std::chrono::milliseconds(msecs));
#else
static struct timespec req;
req.tv_sec = msecs / 1000;
req.tv_nsec = msecs * 1000000 % 1000000000; // ns
int ret = nanosleep(&req, NULL);
//printf("--------nano-----------\n");
#endif
}
static inline void sleepForUs(int usecs)
{
#if defined (_WIN32) || defined (_WIN64)
std::this_thread::sleep_for(std::chrono::microseconds(usecs));
#else
static struct timespec req;
req.tv_sec = usecs / 1000000;
req.tv_nsec = usecs * 1000 % 1000000000; // ns
int ret = nanosleep(&req, NULL);
//printf("--------nano-----------\n");
#endif
}
}// endof namespace robin