#if !defined HAD_INPUT_OUTPUT_H #define HAD_INPUT_OUTPUT_H #include #include "hstring.h" #include "hadexitcodes.h" #define INPUT_LENGTH 80 class HadInput { public: HadInput() : inputLine(' ',INPUT_LENGTH),quit("quit"),endPrompt(": "),decidePrompt(" (y,n)? ") { pInput=0; pNext=0; pPrevious=0; mode='s'; inputDefault='t'; inputId=inputDefault; delimiter=" ,=\t"; } ~HadInput(){delete pInput;} //prompt for filename, create ifstream object: pointer pInput, //open file int selectFile(); //select input source (keyboard, file, database etc.) stored in inputId. //returns HQUIT if 'q' = quit was selected //returns HFAILURE if source unknown. //resets input source to inputDefault, if selection failes (unknown file name) int selectSource(); //mode defines whether or not command file content and prompt //content is displayed void selectMode(); void setPrompt(const HString & newPrompt) { endPrompt=newPrompt; return; } ifstream * pfile() {return pInput;} //obtains the next token from the input line //skipping (i-1) input strings int next(int i=1) { while(i--) { pPrevious=pNext; currentString=inputLine.token(pNext,delimiter); } return HSUCCESS; } int previous() { if(pNext) { pNext=pPrevious; return HSUCCESS; } return HFAILURE; } //obtains the first token from inputLine //returns the current string HString & string() {return currentString;} //returns the current string and moves to the next string //skipping (i-1) input strings HString string(int i) { HString returnString=currentString; next(i); return returnString; } //converts the current word into an integer number int intNumber() {return int(currentString);} //converts the current word into an integer number //moves to the next string, skipping (i-1) strings int intNumber(int i) { int returnValue=int(currentString); next(i); return returnValue; } //converts the current string into an integer number float floatNumber() {return float(currentString);} float floatNumber(int i) { float returnValue=float(currentString); next(i); return returnValue; } //reads a line from an input stream into an input buffer. //Obtains the first string 'currentString' from the buffer. //string separators can be whitespace char's or ',' //In case of terminal input it prompts the prompt string //returns HQUIT, if input is "q" or "quit" int read(const HString & prompt); int read() {return read(empty);} //Store the input line 'command'. //Like read but input is the program int command(const HString & command); //prints a prompt int prompt(const HString & prompt); //decides a question answered by yes (true) or no (false) int decide(const HString &question); //allows to check which input source is in use char source(void) {return inputId;} //used to reset the inputId, if it was overwritten by intermediate use void source(char id) {inputId=id;return;} //modify the default delimiter void setDelimiter(const HString & s) {delimiter=s;} void setDelimiter(char * c) {delimiter=c;} protected: //Stores the content of inputLine within a stringstream buffer //Extracts the first string from the stringstream buffer and stores //it in in currentString int store(); //gets a line from input source (used by read and decide functions) int newLine(const HString & prompt); char inputId; char inputDefault; char oldInputId; char mode; HString quit; HString inputLine; HString currentString; char * pNext; char * pPrevious; ifstream * pInput; ifstream * pOldInput; HString empty; HString endPrompt; HString decidePrompt; HString delimiter; }; #endif