-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.cpp
278 lines (246 loc) · 6.57 KB
/
generator.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "generator.h"
#include <random>
#include <ctime>
#include <iostream>
#include <algorithm>
Generator::Generator(int _numColumns, int _maxLengthString) :
numColumns(_numColumns),
maxLengthString(_maxLengthString)
{
// std::cout << symbols.size() << std::endl;
// std::cout << symbols[symbols.size()-1] << std::endl;
}
Generator::~Generator()
{
}
std::string Generator::generateRow()
{
std::string row = "";
for (size_t i = 0; i < header.size(); i++)
{
Column col = header[i];
std::string value;
switch (col.typeName)
{
case Type::String:
{
int length = randomInt(1, maxLengthString);
value += generateRandomString(Cell::simple, length);
if (isUnacceptableChar)
{
value = shielding + value + shielding;
isUnacceptableChar = false;
}
break;
}
case Type::Integer:
{
value += std::to_string(randomInt(0, maxIntLength));
break;
}
case Type::Float:
{
float randomFloatNum = randomFloat(0, maxFloatLength);
value += std::to_string(randomFloatNum);
std::replace(value.begin(), value.end(), '.', ',');
break;
}
case Type::Date:
{
value += getRandomData();
}
}
if (i != (header.size()-1))
{
value += delimeter;
}
else
{
value += endLine;
}
row += value;
}
//std::cout << row << std::endl;
return row;
}
std::string Generator::getHeader()
{
generateHeader();
// for (int i = 0; i < header.size(); i++)
// {
// std::cout << header[i].columnName << std::endl;
// std::cout << header[i].typeName << std::endl;
// std::cout << header[i].isBadChar << std::endl;
// std::cout << "\n-------------------------------\n" << std::endl;
// }
std::string outputData ="";
for(size_t i = 0; i < header.size(); i++)
{
std::string columnData = header[i].columnName +
whiteSpace +
getTypeAsString(header[i].typeName);
if (header[i].isBadChar)
{
columnData = shielding + columnData + shielding;
}
if (i != header.size()-1)
{
columnData += delimeter;
}
else
{
columnData += endLine;
}
outputData += columnData;
}
// std::cout << outputData << std::endl;
return outputData;
}
Type Generator::getRandomType()
{
int randomNum = randomInt(1,4);
return static_cast<Type>(randomNum);
}
int Generator::randomInt(int min, int max)
{
std::random_device rd; // only used once to initialise (seed) engine
std::mt19937 rng(rd()); // random-number engine used (Mersenne-Twister in this case)
std::uniform_int_distribution<int> uni(min,max); // guaranteed unbiased
return uni(rng);
}
float Generator::randomFloat(float min, float max)
{
std::random_device rd; // only used once to initialise (seed) engine
std::mt19937 gen(rd()); // random-number engine used (Mersenne-Twister in this case)
std::uniform_real_distribution<double> dis(min, max); // guaranteed unbiased
return static_cast<float>(dis(gen));
}
std::string Generator::getRandomData()
{
bool isLeapYear = false;
int year = randomInt(1, 9999);
int month = randomInt(1, 12);
int day = 0;
if (month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12)
{
day = randomInt(1, 31);
}
else if (month == 2)
{
if (year > 1582)
{
if(year%100 == 0)
{
isLeapYear = (year%400 == 0);
}
else
{
isLeapYear = (year%4 == 0);
}
}
if (isLeapYear)
{
day = randomInt(1, 29);
}
else
{
day = randomInt(1, 28);
}
}
else
{
day = randomInt(1, 30);
}
std::string dayStr;
std::string monthStr;
std::string yearStr;
dayStr = std::to_string(day);
monthStr = std::to_string(month);
yearStr = std::to_string(year);
if (day < 10)
{
dayStr = "0" + dayStr;
}
if (month < 10)
{
monthStr = "0" + monthStr;
}
unsigned long additionalZeros = maxNumSymbolsYear - yearStr.size();
while (additionalZeros--)
{
yearStr = "0" + yearStr;
}
return dayStr + pointer + monthStr + pointer + yearStr;
}
void Generator::generateHeader()
{
Column column;
for (int i = 0; i < numColumns; i++)
{
int lengthString = randomInt(1, maxLengthString);
column.columnName = generateRandomString(Cell::header, lengthString);
column.isBadChar = isUnacceptableChar;
isUnacceptableChar = false;
column.typeName = getRandomType();
header.push_back(column);
}
// for (int i = 0; i < header.size(); i++)
// {
// std::cout << header[i].columnName << std::endl;
// std::cout << header[i].typeName << std::endl;
// std::cout << header[i].isBadChar << std::endl;
// std::cout << "\n-------------------------------\n" << std::endl;
// }
}
std::string Generator::getTypeAsString(Type type)
{
std::string outPut;
switch (type)
{
case Type::String:
outPut = "String";
break;
case Type::Float:
outPut = "Float";
break;
case Type::Integer:
outPut = "Integer";
break;
case Type::Date:
outPut = "Date";
break;
}
return outPut;
}
std::string Generator::generateRandomString(Generator::Cell flag, int len)
{
std::string outputStr = "";
for (int i = 0; i < len; i++)
{
unsigned long num = static_cast<unsigned long>(randomInt(0, static_cast<int>(symbols.size()-1)));
if (symbols[num] == delimeter || symbols[num] == quotes)
{
isUnacceptableChar = true;
}
if (symbols[num] == quotes)
{
outputStr += shielding;
}
if(symbols[num] == whiteSpace)
{
if (flag == Cell::header)
{
outputStr = outputStr + underscore;
}
else
{
outputStr = outputStr + whiteSpace;
}
}
else
{
outputStr += symbols[num];
}
}
return outputStr;
}