-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzablib.h
More file actions
335 lines (280 loc) · 8.24 KB
/
zablib.h
File metadata and controls
335 lines (280 loc) · 8.24 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#ifndef ZABLIB_H
#define ZABLIB_H
#include <cstdlib>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
class ZabLib {
private:
// Helper method to clear input stream
void clearInputStream();
// Helper method to clear console (platform agnostic)
void clearScreen();
// Helper method to validate numeric input
template <typename T>
bool isValidNumeric(const std::string &input, T &result);
public:
// Integer input functions
int get_int(const std::string &msg);
unsigned int get_unsigned_int(const std::string &msg);
long long get_long_long(const std::string &msg);
unsigned long long get_unsigned_long_long(const std::string &msg);
// Character and string input functions
char get_char(const std::string &msg);
bool get_bool(const std::string &msg);
std::string get_word(const std::string &msg);
std::string get_line(const std::string &msg);
// Choice and confirmation functions
int get_choice(const char *choices[], int num_choices,
const std::string &msg);
bool get_confirmation(const std::string &msg);
// Utility functions
void pause_console(const std::string &msg = "Press Enter to continue...");
void clear_console();
// Date and time functions (simplified format)
std::string get_date(const std::string &msg);
std::string get_time(const std::string &msg);
};
// Template implementation for numeric validation
template <typename T>
bool ZabLib::isValidNumeric(const std::string &input, T &result) {
std::stringstream ss(input);
ss >> result;
// Check if the entire string was consumed and no error occurred
return ss.eof() && !ss.fail();
}
// Helper method implementations
inline void ZabLib::clearInputStream() {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
inline void ZabLib::clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
// Integer input functions
inline int ZabLib::get_int(const std::string &msg) {
int value;
std::string input;
while (true) {
std::cout << msg;
std::getline(std::cin, input);
if (isValidNumeric(input, value)) {
// Additional check for range
std::stringstream ss(input);
long long temp;
ss >> temp;
if (temp >= std::numeric_limits<int>::min() &&
temp <= std::numeric_limits<int>::max()) {
return value;
} else {
std::cout << "Error: int value out of range. Please try again.\n";
}
} else {
std::cout << "Error: '" << input
<< "' is not a valid integer. Please try again.\n";
}
}
}
inline unsigned int ZabLib::get_unsigned_int(const std::string &msg) {
unsigned int value;
std::string input;
while (true) {
std::cout << msg;
std::getline(std::cin, input);
if (isValidNumeric(input, value)) {
// Additional check for negative values
if (input.find('-') == std::string::npos) {
std::stringstream ss(input);
unsigned long long temp;
ss >> temp;
if (temp <= std::numeric_limits<unsigned int>::max()) {
return value;
} else {
std::cout
<< "Error: unsigned int value out of range. Please try again.\n";
}
} else {
std::cout
<< "Error: unsigned int cannot be negative. Please try again.\n";
}
} else {
std::cout << "Error: '" << input
<< "' is not a valid unsigned integer. Please try again.\n";
}
}
}
inline long long ZabLib::get_long_long(const std::string &msg) {
long long value;
std::string input;
while (true) {
std::cout << msg;
std::getline(std::cin, input);
if (isValidNumeric(input, value)) {
return value;
} else {
std::cout << "Error: '" << input
<< "' is not a valid long long integer. Please try again.\n";
}
}
}
inline unsigned long long
ZabLib::get_unsigned_long_long(const std::string &msg) {
unsigned long long value;
std::string input;
while (true) {
std::cout << msg;
std::getline(std::cin, input);
if (isValidNumeric(input, value)) {
if (input.find('-') == std::string::npos) {
return value;
} else {
std::cout << "Error: unsigned long long cannot be negative. Please try "
"again.\n";
}
} else {
std::cout << "Error: '" << input
<< "' is not a valid unsigned long long. Please try again.\n";
}
}
}
// Character and string input functions
inline char ZabLib::get_char(const std::string &msg) {
std::string input;
while (true) {
std::cout << msg;
std::getline(std::cin, input);
if (input.length() == 1) {
return input[0];
} else if (input.empty()) {
std::cout << "Error: Please enter a character.\n";
} else {
std::cout << "Error: Please enter only one character.\n";
}
}
}
inline bool ZabLib::get_bool(const std::string &msg) {
std::string input;
while (true) {
std::cout << msg;
std::getline(std::cin, input);
// Convert to lowercase for comparison
for (char &c : input) {
c = std::tolower(c);
}
if (input == "true" || input == "t" || input == "yes" || input == "y" ||
input == "1") {
return true;
} else if (input == "false" || input == "f" || input == "no" ||
input == "n" || input == "0") {
return false;
} else {
std::cout
<< "Error: Please enter true/false, yes/no, y/n, t/f, or 1/0.\n";
}
}
}
inline std::string ZabLib::get_word(const std::string &msg) {
std::string input;
std::cout << msg;
std::cin >> input;
clearInputStream();
return input;
}
inline std::string ZabLib::get_line(const std::string &msg) {
std::string input;
std::cout << msg;
std::getline(std::cin, input);
return input;
}
// Choice and confirmation functions
inline int ZabLib::get_choice(const char *choices[], int num_choices,
const std::string &msg) {
int choice;
while (true) {
std::cout << msg << std::endl;
for (int i = 0; i < num_choices; i++) {
std::cout << (i + 1) << ". " << choices[i] << std::endl;
}
std::cout << "Enter your choice (1-" << num_choices << "): ";
choice = get_int("");
if (choice >= 1 && choice <= num_choices) {
return choice - 1; // Return 0-based index
} else {
std::cout << "Error: Please enter a number between 1 and " << num_choices
<< ".\n";
}
}
}
inline bool ZabLib::get_confirmation(const std::string &msg) {
std::string input;
while (true) {
std::cout << msg << " (y/n): ";
std::getline(std::cin, input);
for (char &c : input) {
c = std::tolower(c);
}
if (input == "y" || input == "yes") {
return true;
} else if (input == "n" || input == "no") {
return false;
} else {
std::cout << "Error: Please enter 'y' or 'n'.\n";
}
}
}
// Utility functions
inline void ZabLib::pause_console(const std::string &msg) {
std::cout << msg;
std::cin.get();
clearInputStream();
}
inline void ZabLib::clear_console() { clearScreen(); }
// Date and time functions (simplified format)
inline std::string ZabLib::get_date(const std::string &msg) {
std::string input;
while (true) {
std::cout << msg << " (MM/DD/YYYY): ";
std::getline(std::cin, input);
// Simple validation for MM/DD/YYYY format
if (input.length() == 10 && input[2] == '/' && input[5] == '/') {
bool valid = true;
for (int i = 0; i < 10; i++) {
if (i != 2 && i != 5 && !std::isdigit(input[i])) {
valid = false;
break;
}
}
if (valid) {
return input;
}
}
std::cout << "Error: Please enter date in MM/DD/YYYY format.\n";
}
}
inline std::string ZabLib::get_time(const std::string &msg) {
std::string input;
while (true) {
std::cout << msg << " (HH:MM): ";
std::getline(std::cin, input);
// Simple validation for HH:MM format
if (input.length() == 5 && input[2] == ':') {
bool valid = true;
for (int i = 0; i < 5; i++) {
if (i != 2 && !std::isdigit(input[i])) {
valid = false;
break;
}
}
if (valid) {
return input;
}
}
std::cout << "Error: Please enter time in HH:MM format.\n";
}
}
#endif // ZABLIB_H