-
Notifications
You must be signed in to change notification settings - Fork 0
/
ioutil.cpp
381 lines (313 loc) · 15.4 KB
/
ioutil.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*******************************************************************************
* Filename: ioutil.cpp *
* Author: Daniel Wilson *
* E-Mail: [email protected] *
* Date: 5 May 2017 *
* *
* Description: Provides the implementation fof classes and methods that aid in *
* the collection of user input from a keyboard and displaying output to a *
* command line interface. *
* *
*******************************************************************************/
#include "ioutil.hpp" // Menu, ValidatedInput, terminalClear()
#include <string> // string
#include <iostream> // cout, endl
#include <cstdio> // printf()
#include <stdexcept> //out_of_range
using std::cout;
using std::endl;
using std::out_of_range;
/*******************************************************************************
* Function: Menu::Menu *
* *
* Description: Default constructor. Sets data members to unitialized values so *
* testing and debugging are simpler. *
* *
* Inputs: void *
* *
* Outputs: void *
*******************************************************************************/
Menu::Menu()
{
title = "UNINITIALIZED TITLE";
prompt = "UNINITIALIZED PROMPT";
}
/*******************************************************************************
* Function: Menu::Menu *
* *
* Description: Constructor. Sets data members based on parameters passed in. *
* *
* Inputs: title - const string& *
* items - const vector<string>& *
* *
* Outputs: void *
*******************************************************************************/
Menu::Menu(const std::string& title,
const std::vector<std::string>& items,
const std::string& prompt)
{
this->title = title;
this->items = items;
this->prompt = prompt;
}
/*******************************************************************************
* Function: Menu::print const *
* *
* Description: Method that prints the title and all of the items, each on a new*
* line, with each item being prepended by its item number, for ex.*
* Welcome to Tic-Tac-Toe *
* 1. Play Game *
* 2. Configure Settings *
* 3. Quit Game *
* *
* Inputs: void *
* *
* Outputs: void *
*******************************************************************************/
void Menu::print() const
{
cout << title << endl;
for (unsigned int i = 0; i < items.size(); ++i)
cout << i + 1 << ". " << items.at(i) << endl;
}
/*******************************************************************************
* Function: Menu::changeItem const *
* *
* Description: Method that allows the user to change what a particular menu *
* says. This is useful if you want a menu item to change based on *
* some variable being set. *
* *
* Inputs: index - int (index of item to change) *
* newItem - const string& *
* *
* Outputs: void *
*******************************************************************************/
void Menu::changeItem(int index, const std::string& newItem)
{
// If the index is within a valid range
if (index < static_cast<int>(items.size()) && index >= 0)
items.at(index) = newItem;
}
/*******************************************************************************
* Function: Menu::addItem *
* *
* Description: Takes a string and pushes it back into the items vector. *
* *
* Inputs: newItem - const string& *
* *
* Outputs: void *
*******************************************************************************/
void Menu::addItem(const std::string& newItem)
{
items.push_back(newItem);
}
/*******************************************************************************
* Function: Menu::getSelection const *
* *
* Description: Returns a menu selection from the user. If the selection is not *
* valid, then the method will return -1, otherwise, it returns a *
* valid menu selection. *
* *
* Inputs: void *
* *
* Outputs: void *
*******************************************************************************/
int Menu::getSelection() const
{
int selection = -1;
cout << endl << prompt;
ValidatedInput userInput;
userInput.fromKeyboard();
if (userInput.isInteger(UNSIGNED))
selection = stoi(userInput.getInput());
if (selection <= static_cast<int>(items.size()) && selection > 0)
return selection;
else
return -1;
}
/******************
***** Setters *****
******************/
void Menu::setTitle(const std::string& title)
{
this->title = title;
}
void Menu::setItems(const std::vector<std::string>& items)
{
this->items.clear();
this->items = items;
}
void Menu::setPrompt(const std::string& prompt)
{
this->prompt = prompt;
}
/*******************************************************************************
* Function: ValidatedInput::fromKeyboard *
* *
* Description: Read in keyboard input character by character from the terminal *
* and stop reading after the user enters the '\n' character. The *
* newline character is not left in the keyboard buffer. *
* *
* Inputs: void *
* *
* Outputs: void *
*******************************************************************************/
void ValidatedInput::fromKeyboard()
{
char currentCharacter;
// clear all old data from userInput
input.clear();
while ((currentCharacter = std::cin.get()) != '\n')
input.push_back(currentCharacter);
}
/*******************************************************************************
* Function: input::isInteger *
* *
* Description: Determines if userInput can represent an integer. *
* *
* Inputs: isSigned - bool *
* *
* Outputs: bool - true if userInput contains numeric characters only *
*******************************************************************************/
bool ValidatedInput::isInteger(bool isSigned) const
{
unsigned int i = 0;
// If the user hasn't entered any input
if(!input.size())
return false;
// First character CAN be a '-' if we're looking for a signed integer
else if (isSigned && input.at(i) == '-' && input.size() > 1)
++i;
for (; i < input.size(); ++i)
{
// Does inputBuffer contain a non-numeric value?
if (input.at(i) < '0' || input.at(i) > '9')
return false;
}
try
{
stoi(input);
}
catch (std::out_of_range)
{
return false;
}
// No invalid characters were found
return true;
}
/*******************************************************************************
* Function: input::isIntegerBetween *
* *
* Description: Determines if the input value is between low and highinclusive).*
* *
* Inputs: low - int *
* high - int *
* *
* Outputs: bool - true if input is a value from low through high. *
*******************************************************************************/
bool ValidatedInput::isIntegerBetween(int low, int high) const
{
// If value is not an integer, return false
if (!isInteger(SIGNED))
return false;
int value;
// Assign input to integer variable and return false if it's too big/small
try
{
value = stoi(input);
}
catch (std::out_of_range)
{
return false;
}
// If integer value in input is between low and high, inclusive, it's true
if (value >= low && value <= high)
return true;
else
return false;
}
/*******************************************************************************
* Function: input::isInteger *
* *
* Description: Determines if userInput can represent a floating point number. *
* *
* Inputs: isSigned - bool *
* *
* Outputs: bool - true if userInput contains numeric characters only *
*******************************************************************************/
bool ValidatedInput::isFloat(bool isSigned) const
{
unsigned int i = 0;
// If the user hasn't entered any input
if(!input.size())
return false;
// First character CAN be a '-' if we're looking for a signed integer
else if (isSigned && input.at(i) == '-' && input.size() > 1)
++i;
bool decimalSeen = false;
for (; i < input.size(); ++i)
{
// If current character is a decimal point
if (input.at(i) == '.')
{
// If this is the first decimal seen, it's okay
if(!decimalSeen)
decimalSeen = true;
// More than one decimal has beem seen, not a floating point number
else
return false;
}
// Does inputBuffer contain a non-numeric value?
else if (input.at(i) < '0' || input.at(i) > '9')
return false;
}
// No invalid characters were found
return true;
}
/*******************************************************************************
* Function: input::isYesOrNo *
* *
* Description: Returns true if input is a 'y' or 'n'. If capsAllowed is true, *
* then it will also return true on 'Y' or 'N'. *
* *
* Inputs: capsAllowed - bool *
* *
* Outputs: void *
*******************************************************************************/
bool ValidatedInput::isYesOrNo(bool capsAllowed) const
{
// If input has no characters
if (!input.size())
return false;
// If input has more than a single character
else if (input.size() > 1)
return false;
// input consists of a single character
if (input.at(0) == 'y' || input.at(0) == 'n')
return true;
else if (capsAllowed && (input.at(0) == 'Y' || input.at(0) == 'N'))
return true;
else
return false;
}
/******************
***** Getters *****
******************/
std::string ValidatedInput::getInput() const
{
return input;
}
/*******************************************************************************
* Function: terminalClear *
* *
* Description: Only works on *nix-systems. Clears the terminal window of any *
* displayed text. *
* *
* Inputs: void *
* *
* Outputs: void *
*******************************************************************************/
void terminalClear()
{
std::printf("\033[2J\033[1;1H");
}