forked from jboydt/cinreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcinreader.hpp
More file actions
350 lines (310 loc) · 9 KB
/
cinreader.hpp
File metadata and controls
350 lines (310 loc) · 9 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* CinReader, v2.02
*
* Copyright 2019 J Boyd Trolinger
*
* Contributors: Samuel Fuller, Andrew Mattly
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* REQUIREMENTS
* - must be compiled with std=c++11 or newer
* - must include this header file -> #include "cinreader.hpp"
* - must create a CinReader instance -> CinReader reader;
*
* EXAMPLES OF USAGE
*
* Reading bools
*
* bool b1 = reader.readBool();
* bool b2;
* reader >> b2;
*
* Reading chars
*
* char c1 = reader.readChar(); // any single character accepted
* char c2 = reader.readChar("YyNn"); // 'Y', 'y', 'N', 'n' only accepted
* char c3;
* reader >> c3; // any single character accepted
*
* Reading doubles
*
* double d1 = reader.readDouble();
* double d2;
* reader >> d2;
*
* Reading floats
*
* float f1 = reader.readFloat();
* float f2;
* reader >> f2;
*
* Reading integers
*
* int i1 = reader.readInt(); // any signed integer value accepted
* int i2 = reader.readInt(-10, 10); // between -10 and 10 (inclusive) only accepted
* int i3;
* reader >> i3; // any signed integer value accepted
*
* Reading strings
*
* string s1 = reader.readString(); // any string, including empty string accepted
* string s2 = reader.readString(false); // any string, will not accept empty string
*
* // only accepts "yes", "no", or "maybe"; case sensitive
* string s3 = reader.readString(true, {"yes", "no", "maybe"})
*
* // only accepts "yes", "no", or "maybe"; case insensitive
* string s4 = reader.readString(false, {"yes", "no", "maybe"})
*
* string s5;
* reader >> s5; // any string, including empty string accepted
*
* Changing error messages displayed by CinReader
* - note that the error messages become a secondary prompt for input
*
* reader.setMessage(MessageType::INT_INVALID, "Your input is invalid. Try again: ")
*
* Message types: INT_INVALID, INT_OUT_OF_RANGE, STRING_CANNOT_BE_EMPTY, CHAR_NOT_ALLOWED,
* DOUBLE_INVALID, FLOAT_INVALID, BOOL_INVALID, STRING_NOT_ALLOWED
*/
#pragma once
#if __cplusplus < 201103L
#error CinReader requires C++11 or newer.
#else
#include <algorithm>
#include <array>
#include <initializer_list>
#include <iostream>
#include <limits>
#include <stdexcept>
#include <string>
#include <vector>
using std::array;
using std::cin;
using std::cout;
using std::initializer_list;
using std::invalid_argument;
using std::stod;
using std::stof;
using std::stoi;
using std::string;
using std::vector;
#define MIN_INT std::numeric_limits<int>::min()
#define MAX_INT std::numeric_limits<int>::max()
enum MessageType {
INT_INVALID=0,
INT_OUT_OF_RANGE=1,
STRING_CANNOT_BE_EMPTY=2,
CHAR_NOT_ALLOWED=3,
DOUBLE_INVALID=4,
FLOAT_INVALID=5,
BOOL_INVALID=6,
STRING_NOT_ALLOWED=7,
INT_TOO_LARGE=8
};
class CinReader {
public:
CinReader() {
this->defaultMessages();
}
bool readBool() {
bool input(false);
bool validInput(false);
while (!validInput) {
char boolChar = toupper(this->readString(false).at(0));
if (boolChar == 'T') {
input = true;
validInput = true;
} else if (boolChar == 'F') {
input = false;
validInput = true;
} else {
cout << this->messages[BOOL_INVALID];
}
}
return input;
}
CinReader& operator>>(bool& input) {
input = this->readBool();
return *this;
}
char readChar(string allowed="") {
char input('x');
bool validInput(false);
while (!validInput) {
input = this->readString(false)[0];
for (char a : allowed) {
if (input == a) {
validInput = true;
break;
}
}
if (allowed.size() > 0 && !validInput) {
cout << this->messages[CHAR_NOT_ALLOWED];
} else {
break;
}
}
return input;
}
CinReader& operator>>(char& input) {
input = this->readChar();
return *this;
}
double readDouble() {
double input(0.0);
bool validInput(false);
while (!validInput) {
try {
size_t last;
string doubleStr = this->readString(false);
input = stod(doubleStr, &last);
if (last != doubleStr.size()) {
cout << this->messages[DOUBLE_INVALID];
continue;
}
validInput = true;
} catch (invalid_argument &e) {
cout << this->messages[DOUBLE_INVALID];
}
}
return input;
}
CinReader& operator>>(double& input) {
input = this->readDouble();
return *this;
}
float readFloat() {
float input(0.0);
bool validInput(false);
while (!validInput) {
try {
string floatStr = this->readString(false);
size_t last;
input = stof(floatStr, &last);
if (last != floatStr.size()) {
cout << this->messages[FLOAT_INVALID];
continue;
}
validInput = true;
} catch (invalid_argument &e) {
cout << this->messages[FLOAT_INVALID];
}
}
return input;
}
CinReader& operator>>(float& input) {
input = this->readFloat();
return *this;
}
int readInt(int min=MIN_INT, int max=MAX_INT) {
int input(0);
bool validInput(false);
while (!validInput) {
try {
size_t last;
string intStr = this->readString(false);
if (intStr.size() > 9){
cout << this->messages[INT_TOO_LARGE];
continue;
}
input = stoi(intStr, &last);
if (input < min || input > max) {
cout << this->messages[INT_OUT_OF_RANGE];
continue;
} else if (last != intStr.size()) {
cout << this->messages[INT_INVALID];
continue;
}
validInput = true;
} catch (invalid_argument &e) {
cout << this->messages[INT_INVALID];
}
}
return input;
}
CinReader& operator>>(int& input) {
input = this->readInt();
return *this;
}
string readString(bool allowEmpty=true) {
string input;
bool validInput(false);
while (!validInput) {
getline(cin, input);
if (!allowEmpty && input.size() == 0) {
cout << this->messages[STRING_CANNOT_BE_EMPTY];
continue;
}
validInput = true;
}
return input;
}
string readString(bool caseSensitive, initializer_list<string> allowedStrings) {
string input;
vector<string> allowed(allowedStrings);
if (!caseSensitive) {
for (string& s : allowed) {
transform(s.begin(), s.end(), s.begin(), ::toupper);
}
}
bool validInput(false);
while (!validInput) {
input = this->readString();
if (!caseSensitive) {
transform(input.begin(), input.end(), input.begin(), ::toupper);
}
for (string a : allowed) {
if (a == input) {
validInput = true;
break;
}
}
if (!validInput) {
cout << this->messages[STRING_NOT_ALLOWED];
}
}
return input;
}
CinReader& operator>>(string& input) {
input = this->readString();
return *this;
}
void setMessage(MessageType type, string message) {
if (type >= INT_INVALID && type <= STRING_NOT_ALLOWED) {
this->messages[type] = message;
}
}
private:
array<string, 9> messages;
void defaultMessages() {
this->messages[INT_INVALID] = "Input is not a valid integer. Re-enter: ";
this->messages[FLOAT_INVALID] = "Input is not a valid float. Re-enter: ";
this->messages[DOUBLE_INVALID] = "Input is not a valid double. Re-enter: ";
this->messages[INT_OUT_OF_RANGE] = "Input is not in the required range. Re-enter: ";
this->messages[STRING_CANNOT_BE_EMPTY] = "Input cannot be empty. Re-enter: ";
this->messages[CHAR_NOT_ALLOWED] = "Input character is not allowed. Re-enter: ";
this->messages[BOOL_INVALID] = "Input is not valid for boolean. Re-enter: ";
this->messages[STRING_NOT_ALLOWED] = "Input is not allowed. Re-enter: ";
this->messages[INT_TOO_LARGE] = "Input is too large, Re-enter: ";
}
};
#endif