languages

A collection of programs made with different programming languages.
git clone git://evanalba.com/languages
Log | Files | Refs

CinReader.cpp (7241B)


      1 #include "CinReader.h"
      2 
      3 CinReader::CinReader ()
      4 {
      5 	intErrors = true;
      6 	charRangeErrors = true;
      7 
      8 	boolMsg = "Please enter \"true\" or \"false\": ";
      9 	charMsg = "Please enter a single character: ";
     10 	charRangeMsg = "is not valid. Re-enter: ";
     11 	intMsg = "Re-enter number: ";
     12 	doubleMsg = "Input is not a double. Re-enter: ";
     13 	floatMsg = "Input is not a float. Re-enter: ";
     14 	stringMsg = "Input cannot be blank--enter text: ";
     15 }
     16 
     17 CinReader::~CinReader ()
     18 {
     19 }
     20 
     21 int CinReader::readInt (int lower, int upper)
     22 {
     23 	string input = "";
     24 	string msg = "";
     25 	getline (cin, input);
     26 	input = stripCommas(input);
     27 	if (lower > upper)
     28 	{
     29 		int temp = upper;
     30 		lower = upper;
     31 		upper = temp;
     32 	}
     33 
     34 	while (!testIntInput(input, msg, lower, upper))
     35 	{
     36 		if (intErrors)
     37 			cout << "[ ERROR: " << msg << " ]\n";
     38 		cout << intMsg;
     39 		getline (cin, input);
     40 	}
     41 
     42 	return (atoi(input.c_str()));
     43 }
     44 
     45 int CinReader::readInt (bool userLimit, int lower, int upper)
     46 {
     47 	if (userLimit)
     48 		return (readInt(lower, upper));
     49 	else
     50 		return (readInt());
     51 }
     52 
     53 double CinReader::readDouble ()
     54 {
     55 	string input = "";
     56 	getline(cin, input);
     57 	input = stripCommas(input);
     58 	while (!testDoubleInput(input))
     59 	{
     60 		cout << doubleMsg;
     61 		getline(cin, input);
     62 	}
     63 
     64 	return (atof(input.c_str()));
     65 }
     66 
     67 float CinReader::readFloat ()
     68 {
     69 	string input = "";
     70 	getline(cin, input);
     71 	input = stripCommas(input);
     72 	while (!testDoubleInput(input))
     73 	{
     74 		cout << floatMsg;
     75 		getline(cin, input);
     76 	}
     77 
     78 	return (atof(input.c_str()));
     79 }
     80 
     81 char CinReader::readChar (string range)
     82 {
     83 	bool valid = false;
     84 	string input = "";
     85 	getline(cin, input);
     86 
     87 	if (range.length() > 0)
     88 	{
     89 		do
     90 		{
     91 			while (input.length() != 1)
     92 			{
     93 				cout << charMsg;
     94 				getline(cin, input);
     95 			}
     96 			if (!testCharInput(input[0], range))
     97 			{
     98 				if (charRangeErrors)
     99 					cout << "[" << input[0] << "] " << charRangeMsg;
    100 				input = "";
    101 			}
    102 			else
    103 				valid = true;
    104 		} while (!valid);
    105 	}
    106 	else
    107 	{
    108 		while (input.length() != 1)
    109 		{
    110 			cout << charMsg;
    111 			getline(cin, input);
    112 		}
    113 	}
    114 
    115 	return (input[0]);
    116 }
    117 
    118 bool CinReader::readBool ()
    119 {
    120 	string input = "";
    121 	getline(cin, input);
    122 	while (!testBoolInput(input))
    123 	{
    124 		cout << boolMsg;
    125 		getline(cin, input);
    126 	}
    127 
    128 	return (getBoolValue(input));
    129 }
    130 
    131 string CinReader::readString (bool allowEmpty, unsigned int limitTo)
    132 {
    133 	string input = "";
    134 	getline(cin, input);
    135 	if (!allowEmpty)
    136 	{
    137 		while (input.length() == 0)
    138 		{
    139 			cout << stringMsg;
    140 			getline(cin, input);
    141 		}
    142 	}
    143 	if (limitTo > 0)
    144 	{
    145 		if (input.length() > limitTo)
    146 			input = input.substr(0, limitTo);
    147 	}
    148 	return input;
    149 }
    150 
    151 void CinReader::showIntErrors (bool show)
    152 {
    153     intErrors = show;
    154 }
    155 
    156 void CinReader::showCharRangeErrors (bool show)
    157 {
    158     charRangeErrors = show;
    159 }
    160 
    161 void CinReader::setBoolMessage (string newMessage)
    162 {
    163 	boolMsg = newMessage;
    164 	if (boolMsg.length() > 0 && boolMsg[boolMsg.length()-1] != ' ')
    165 		boolMsg += ' ';
    166 }
    167 
    168 void CinReader::setCharMessage (string newMessage)
    169 {
    170 	charMsg = newMessage;
    171 	if (charMsg.length() > 0 && charMsg[charMsg.length()-1] != ' ')
    172 		charMsg += ' ';
    173 }
    174 
    175 void CinReader::setCharRangeMessage (string newMessage)
    176 {
    177 	charRangeMsg = newMessage;
    178 	if (charRangeMsg.length() > 0 && charRangeMsg[charRangeMsg.length()-1] != ' ')
    179 		charRangeMsg += ' ';
    180 }
    181 
    182 void CinReader::setIntMessage (string newMessage)
    183 {
    184 	intMsg = newMessage;
    185 	if (intMsg.length() > 0 && intMsg[intMsg.length()-1] != ' ')
    186 		intMsg += ' ';
    187 }
    188 
    189 void CinReader::setDoubleMessage (string newMessage)
    190 {
    191 	doubleMsg = newMessage;
    192 	if (doubleMsg.length() > 0 && doubleMsg[doubleMsg.length()-1] != ' ')
    193 		doubleMsg += ' ';
    194 }
    195 
    196 void CinReader::setFloatMessage (string newMessage)
    197 {
    198 	floatMsg = newMessage;
    199 	if (floatMsg.length() > 0 && floatMsg[floatMsg.length()-1] != ' ')
    200 		floatMsg += ' ';
    201 }
    202 
    203 void CinReader::setStringMessage (string newMessage)
    204 {
    205 	stringMsg = newMessage;
    206 	if (stringMsg.length() > 0 && stringMsg[stringMsg.length()-1] != ' ')
    207 		stringMsg += ' ';
    208 }
    209 
    210 string CinReader::stripCommas (string input)
    211 {
    212 	string outs = "";
    213 	for (unsigned int i=0; i<input.length(); i++)
    214 	{
    215 		if (input[i] != ',')
    216 			outs += input[i];
    217 	}
    218 	return outs;
    219 }
    220 
    221 bool CinReader::testIntInput (string input, string& msg, int lowerLimit, int upperLimit)
    222 {
    223 	int upperMarker = -1;
    224 	int lowerMarker = -1;
    225 	if (upperLimit == INT_MAX)
    226 		upperMarker = 47;
    227 	if (lowerLimit == INT_MIN)
    228 		lowerMarker = 48;
    229 
    230 	long linput = strtol(input.c_str(), NULL, 10);
    231 	stringstream ssinput;
    232 	ssinput << linput;
    233 	if (ssinput.str() != input)
    234 	{
    235 	   msg = "integer input out-of-range";
    236 	   return false;
    237 	}
    238 
    239 	else
    240 	{
    241 		if (input.length() > 0)
    242 		{
    243 			if (input[0] != '-' && (!isdigit(input[0])))
    244 			{
    245 				msg = "not an integer";
    246 				return false;
    247 			}
    248 			for (unsigned int i=1; i<input.length(); i++)
    249 			{
    250 				if (!isdigit(input[i]))
    251 				{
    252 					msg = "not an integer";
    253 					return false;
    254 				}
    255 			}
    256 
    257 			int tmp = strtol(input.c_str(), NULL, 10);
    258 			if (tmp >= upperLimit)
    259 			{
    260 				if (upperMarker != -1)
    261 				{
    262 					string ts = "";
    263 					ts += input[input.length()-2];
    264 					ts += input[input.length()-1];
    265 					int ti = atoi(ts.c_str());
    266 					if (ti > upperMarker)
    267 					{
    268 						msg = "larger than 32-bit integer";
    269 						return false;
    270 					}
    271 					else
    272 						return true;
    273 				}
    274 				else
    275 				{
    276 					if (tmp == upperLimit)
    277 						return true;
    278 					else
    279 					{
    280 						msg = "larger than upper limit";
    281 						return false;
    282 					}
    283 				}
    284 			}
    285 			else if (tmp <= lowerLimit)
    286 			{
    287 				if (lowerMarker != -1)
    288 				{
    289 					string ts = "";
    290 					ts += input[input.length()-2];
    291 					ts += input[input.length()-1];
    292 					int ti = atoi(ts.c_str());
    293 					if (ti > lowerMarker)
    294 					{
    295 						msg = "smaller than 32-bit integer";
    296 						return false;
    297 					}
    298 					else
    299 						return true;
    300 				}
    301 				else
    302 				{
    303 					if (tmp == lowerLimit)
    304 						return true;
    305 					else
    306 					{
    307 						msg = "smaller than lower limit";
    308 						return false;
    309 					}
    310 				}
    311 			}
    312 			else
    313 				return true;
    314 		}
    315 		msg = "no input";
    316 		return false;
    317 	}
    318 }
    319 
    320 bool CinReader::testDoubleInput (string input)
    321 {
    322 	int dotCount = 0;
    323 	if (input.length() > 0)
    324 	{
    325 		if (input[0] != '-' && (!isdigit(input[0])) && input[0] != '.')
    326 			return false;
    327 		if (input[0] == '.')
    328 			dotCount = 1;
    329 		for (unsigned int i=1; i<input.length(); i++)
    330 		{
    331 			if (input[i] == '.')
    332 			{
    333 				if (dotCount != 0)
    334 					return false;
    335 				else
    336 					++dotCount;
    337 			}
    338 			else if (!isdigit(input[i]))
    339 				return false;
    340 		}
    341 
    342 		return true;
    343 	}
    344 
    345 	return false;
    346 }
    347 
    348 bool CinReader::testCharInput (char c, string range)
    349 {
    350 	bool valid = false;
    351 	for (unsigned int i=0; i<range.length(); i++)
    352 	{
    353 		if (range[i] == c)
    354 		{
    355 			valid = true;
    356 			break;
    357 		}
    358 	}
    359 	return valid;
    360 }
    361 
    362 bool CinReader::testBoolInput (string input)
    363 {
    364 	if (input.length() > 0)
    365 	{
    366 		if (input.length() == 1)
    367 		{
    368 			if (toupper(input[0]) == 'T' || toupper(input[0]) == 'F')
    369 				return true;
    370 			else
    371 				return false;
    372 		}
    373 		else
    374 		{
    375 			if (ignoreCaseCompare(input, "TRUE") ||
    376 			    ignoreCaseCompare(input, "FALSE"))
    377 				return true;
    378 			else
    379 				return false;
    380 		}
    381 	}
    382 	else
    383 		return false;
    384 }
    385 
    386 bool CinReader::getBoolValue (string input)
    387 {
    388 	if (toupper(input[0]) == 'T')
    389 		return true;
    390 	else
    391 		return false;
    392 }
    393 
    394 bool CinReader::ignoreCaseCompare (string input, string comp)
    395 {
    396 	toUpperCase(input);
    397 	if (input == comp)
    398 		return true;
    399 	else
    400 		return false;
    401 }
    402 
    403 void CinReader::toUpperCase (string& s)
    404 {
    405   transform(s.begin(), s.end(),  s.begin(), ::toupper);
    406 }