//implementation file for library of auxiliary input output functions -- myio.cc

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

#include "myio.h"

//**************************************************************************
// function to clear the cin stream up to & including the next new line
//**************************************************************************
void clrStream() {

   while (cin.peek() != '\n') cin.ignore();  // ignore all characters
   cin.ignore();                             // ignore the new line
}

//**************************************************************************
// function to get a character response from the user and use the default
// response if the user just hits the return key
//
// parameter usage -- defalt imports the default char to use 
//**************************************************************************
char getResponse(char defalt) {

   char ch = cin.get();
   if (ch == '\n') ch = defalt;    // use the default character
   else clrStream();               // clear the CR and any garbage entered
   return ch;
}


//**************************************************************************
// function to obtain a valid integer from the user
//
// parameter usage -- i exports the integer obtained
//**************************************************************************
void getValid(int& i) {

   while (true) {
      cin >> i;
      if (cin.good() || cin.eof()) break; // valid int or eof so exit
      cout << "Invalid integer entered!! Reenter -- ";
      cin.clear();   // clear the error flag
      clrStream();   // clear bad data from the stream
   }
}


//**************************************************************************
// function to obtain a valid real number from the user
//
// parameter usage --  d exports the real number obtained
//**************************************************************************
void getValid(double& d) {

   while (true) {
      cin >> d;
      if (cin.good() || cin.eof()) break; // valid real or eof so exit
      cout << "Invalid real entered!! Reenter -- ";
      cin.clear();   // clear the error flag
      clrStream();   // clear bad data from the stream
   }
}


//**************************************************************************
// function to attach a file to an input stream
//
// parameter usage :
//          iStr -- exports the stream that the file was attached to
//      filename -- imports the name of the file to be opened and
//                  exports the name of the file that was opened
//**************************************************************************
void openIn(ifstream& iStr, string& filename) {

   iStr.open(filename.c_str());
   while (!iStr) {                                  // unable to open file
      iStr.clear();                                 //  so clear error flag
      cout << "Unable to open file!!! " << filename //  complain
	   << ".  Re-enter filename --  ";
      getline(cin,filename);                        //  get replacement filename
      iStr.open(filename.c_str());                  //  and try to open it
   }
}


//**************************************************************************
// function to attach a file to an output stream
//
// parameter usage :
//          oStr -- exports the stream that the file was attached to
//      filename -- imports the name of the file to be opened and
//                  exports the name of the file that was opened
//**************************************************************************
void openOut(ofstream& oStr, string& filename) {

   oStr.open(filename.c_str());
   while (!oStr) {                                  // unable to open file
      oStr.clear();                                 //  so clear error flag
      cout << "Unable to open file!!! " << filename //  complain
	   << ".  Re-enter filename --  ";
      getline(cin,filename);                        //  get replacement filename
      oStr.open(filename.c_str());                  //  and try to open it
   }
}