-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvars.cpp
199 lines (149 loc) · 3.92 KB
/
vars.cpp
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
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <ctime>
#include <chrono>
#include <vector>
#include <sys/time.h>
#include "utl.h"
#include "vars.h"
using namespace std;
extern std::mt19937 rnd_mt19937;
/*
std::mt19937 &vmt()
{
// initialize once per thread
//thread_local static std::random_device srd;
//thread_local static std::mt19937 smt(srd());
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
thread_local static std::mt19937 smt(seed);
return smt;
}
*/
int get_value_nature (const string &s)
{
if (s.empty())
return 0;
if (s.find ("|") != string::npos)
return VT_SEQ;
if (s.find ("..") != string::npos)
return VT_RANGE;
return VT_SINGLE;
}
CVar::CVar (const string &key, const string &val)
{
k = key;
string value = val;
// cout << "CVar::CVar key:" << key << " val:" << val << endl;
rnd_length = 8;
precision = 3;
// rnd_generator = new std::mt19937 (rnd_dev());
//rnd_generator = new std::mt19937 (std::chrono::system_clock::now().time_since_epoch().count());
vartype = get_value_nature (val);
//если макрос и не SEQ
if (value[0] == '@' && vartype == VT_SINGLE)
{
macroname = get_macro_name (value);
if (! macroname.empty())
{
auto f = pool.macros.find (macroname);
if (f != pool.macros.end())
f->second->parse (value);
}
}
if (vartype == VT_SINGLE || vartype == VT_DATETIME)
v.push_back (value);
if (vartype == VT_SEQ)
{
v = split_string_to_vector (value, "|");
for (size_t i = 0; i < v.size(); i++)
{
string t = v[i];
if (t[0] != '@')
continue;
string name = get_macro_name (t);
if (name.empty())
continue;
auto f = pool.macros.find (name);
if (f == pool.macros.end())
continue;
CMacro *tm = f->second->create_self (t);
cache.add (i, tm);
}
}
if (vartype == VT_RANGE)
{
v = split_string_to_vector (value, "..");
//check for int or fractional
size_t pos = v[0].find (".");
if (pos != string::npos)
{
vartype = VT_FLOATRANGE;
fa = atof (v[0].c_str());
fb = atof (v[1].c_str());
precision = v[0].size() - pos - 1;
}
else
{
a = atoi (v[0].c_str());
b = atoi (v[1].c_str());
}
}
}
CVar::~CVar()
{
//delete rnd_generator;
}
int CVar::get_rnd (int ta, int tb)
{
std::uniform_int_distribution <> distrib (ta, tb);
return distrib (rnd_mt19937);
}
string CVar::gen_msecs()
{
std::uniform_real_distribution <> distrib (fa, fb);
// std::string s;
std::stringstream sstream;
sstream.setf (std::ios::fixed);
sstream.precision (precision);
sstream << distrib (rnd_mt19937);
return sstream.str();
}
string CVar::get_val()
{
string result;
if (vartype == VT_SINGLE)
result = v[0];
else
if (vartype == VT_RANGE)
result = std::to_string (get_rnd (a, b));
else
if (vartype == VT_SEQ)
{
int i = get_rnd (0, v.size()-1);
result = v[i];
if (result[0] == '@')
{
auto f = cache.macros.find (i);
if (f != cache.macros.end())
result = f->second->process();
}
}
else
if (vartype == VT_FLOATRANGE)
result = gen_msecs();
//pre process macros
//Если VT_SEQ и result еще не что-либо а макрос, инициализуем (парсим) его
//иначе просто макрос, который был инициализиван в CVar
if (! macroname.empty() && vartype == VT_SINGLE)
{
auto f = pool.macros.find (macroname);
if (f != pool.macros.end())
result = f->second->process();
}
return result;
}