////////
//
//  Russell's Utils.cc package   7/18/2002
//
//

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>


//
//  Get a string [str] of length maxLength
//
void getString(char * query, char * str, int maxLength)
{
	cout << query;							// ask query
	
	cin >> ws;								// remove all the white spaces
	cin.getline(str, maxLength);			// read characters until \n MAXSTR
}
    

//
//  reads an integer from the keyboard,
//  uses error checking to ensure value is between minValue and maxValue
//
int getInt(char askStr[], int minValue, int maxValue)
{
	int mVar;
	bool inputOK = false;
	
	while (! inputOK) { 
	
		cout << askStr;						// ask query
		cin >> mVar;						// get input
		
		if ( (mVar >= minValue && mVar <= maxValue) )
			inputOK = true;					// input OK, exit loop
		else {
			cout << "Bad input: please enter a value between "
			     << minValue << " and " << maxValue << endl;
			cin.clear();					// clear error [see  cin.fail();]
			cin.ignore(80, '\n');			// empty buffer
		}
	}
	
	return mVar;
}


//
//  ensures int is within bounds
//
int ensureValues(char * valueName, int& value, int min, int max)
{
	if ( ! (value >= min && value <= max) ) {	// Illegal val
												
		cout << "Illegal value passed for " << valueName;
		cout << ", which was passed as: " << value << endl;
		cout << "( min: " << min << "   max: " << max << " )" << endl;
		
		if (value < min) {
			value = min;						// cap min
		}
		
		if (value > max) {
			value = max;						// cap max
		}
		
		return -1;
	}
	
	return 0;
}


//
//  convert base
//
void writeBase(int n, int base)
{
	if (n >= base) {
		writeBase( (n / base) , base );
	}
	
	switch(n % base)
	{
		case 0:  cout << '0'; break;
		case 1:  cout << '1'; break;
		case 2:  cout << '2'; break;
		case 3:  cout << '3'; break;
		case 4:  cout << '4'; break;
		case 5:  cout << '5'; break;
		case 6:  cout << '6'; break;
		case 7:  cout << '7'; break;
		case 8:  cout << '8'; break;
		case 9:  cout << '9'; break;
		case 10: cout << 'A'; break;
		case 11: cout << 'B'; break;
		case 12: cout << 'C'; break;
		case 13: cout << 'D'; break;
		case 14: cout << 'D'; break;
		case 15: cout << 'F'; break;
	}
	// ...or cout << char( n + '0' ) for val between 0-9
}


//
//  random seed according to time
//
void randomizer()
{
	time_t      timeval;
	
	time(&timeval);
	srand(timeval);
}


//
//  random generator
//
int randomInt(int min, int max)
{
	static bool seed = false;
	int range = (max+1) - min;
	    
	if (! seed) {
		randomizer();
		seed = true;
	}
	
    return (int) (( rand() / (double) RAND_MAX ) * range + min);
}


//
//  print the date/time
//
void myTime()
{
	time_t      timeval;
	
	time(&timeval);
	cout << ctime(&timeval) << endl;
	cout << timeval << endl;
}