-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.h
More file actions
232 lines (202 loc) · 4.82 KB
/
utils.h
File metadata and controls
232 lines (202 loc) · 4.82 KB
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
#pragma once
#include "includeHeaders.h"
inline void line(int j = 50, char k = '=', bool nextLineAtEnd = true) {
for (int i = 0; i < j; i++) {
cout << k;
}
if (nextLineAtEnd) {
cout << endl;
}
}
inline void pause() {
cout << endl;
system("pause");
}
inline void clearScreen() {
system("cls");
}
inline void heading(std::string additional = "") {
system("cls");
cout << "############################################################" << endl;
cout << "## Bluecoins Import Tool v2.2 by KwongTN ###" << endl;
cout << "############################################################" << endl << endl;
if (additional != "") {
cout << additional << endl;
cout << "-------------------------------------------------------------" << endl;
}
}
inline std::string returnString(json i) {
return i;
}
// Outputs the following in the console
// --------------------------------------------------
// ID Details
// --------------------------------------------------
inline void menuHeading() {
line(50, '-');
cout << "ID " << "Description" << endl;
line(50, '-');
}
inline int menuGen(json rowNames) {
int j = 0;
for (int i = 0; i < rowNames.size(); i++) {
cout << i << "\t" << returnString(rowNames[i]) << endl;
j++;
}
cout << endl;
return j;
}
inline int menuGen(json rowNames, std::string myOutput) {
int j = 0;
for (int i = 0; i < rowNames.size(); i++) {
cout << i << "\t" << returnString(rowNames[i][myOutput]) << endl;
j++;
}
cout << endl;
return j;
}
inline int menuGen(json rowNames, std::string myOutput, std::string determiner) {
int j = 0;
for (int i = 0; i < rowNames.size(); i++) {
if (rowNames[i][determiner]) {
cout << i << "\t" << returnString(rowNames[i][myOutput]) << endl;
j++;
}
}
cout << endl;
return j;
}
inline bool decider(std::string custString = "Your selection (y / n): ") {
bool x = true;
std::string selection;
while (true) {
cout << custString;
try {
getline(cin, selection);
if (selection == "y" || selection == "Y") {
x = true;
break;
}
else if (selection == "n" || selection == "N") {
x = false;
break;
}
else {
throw "Error";
}
if (cin.fail() || selection != "y" || selection != "Y" || selection != "n" || selection != "N") {
throw "Error";
}
}
catch (...) {
cout << "Please enter a valid character." << endl;
}
}
return x;
}
// Whether an array of values contain a key
inline bool arrayContains(json myJSON, std::string element) {
if (myJSON.size() == 0) {
return false;
}
for (int i = 0; i < myJSON.size(); i++) {
if (myJSON[i].contains(element)) {
return true;
}
}
return false;
}
// Whether a vector contains a value
template <typename T1>
inline bool vectorContains(std::vector<T1> myVector, T1 against) {
try {
if (myVector.size() == 0) {
return false;
}
for (int i = 0; i < myVector.size(); i++) {
if (myVector[i] == against) {
return true;
}
}
return false;
}
catch (...) {
return false;
}
}
#pragma warning( push )
#pragma warning( disable : 4244)
template <typename T1>
inline T1 inputNumber(bool prompter = true, bool forceInput = true) {
const char* dataType = typeid(T1).name();
T1 value = 0;
std::string myString = "";
while (true) {
cin.clear();
if (prompter) {
cout << "\n> ";
}
getline(cin, myString);
if (myString == "" && !forceInput) {
return NO_INPUT_NUM;
}
try
{
if (*dataType == 'i') {
value = std::stoi(myString);
}
else if (*dataType == 'd') {
value = std::stod(myString);
}
else if (*dataType == 'f') {
value = std::stof(myString);
}
else {
throw;
}
break;
}
catch (std::invalid_argument const& e)
{
cout << "Bad input: std::invalid_argument thrown. Please re-input." << '\n';
!prompter ? prompter = !prompter : prompter;
continue;
}
catch (std::out_of_range const& e)
{
cout << "Integer overflow: std::out_of_range thrown. Please re-input." << '\n';
!prompter ? prompter = !prompter : prompter;
continue;
}
catch (...) {
cout << "Unknown data type. If you see this this is a bug.";
}
}
cin.clear();
return value;
}
#pragma warning( pop )
inline std::string random_string(size_t length = rand() + 1) {
srand((unsigned int)time(NULL));
auto randchar = []() -> char
{
const char charset[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
const size_t max_index = (sizeof(charset) - 1);
return charset[rand() % max_index];
};
std::string str(length, 0);
std::generate_n(str.begin(), length, randchar);
return str;
}
inline std::string return_current_time_and_date()
{
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %X");
return ss.str();
}
inline void emptyVoidFunction() {};