/* String2.cpp */ #include /* std I/O */ #include /* for string manipulation */ #include "String2.h" /* class file */ /* Prototypes of auxiliary functions. Note: If skip is true, non-allowable leading characters are skipped If end-of-file is encountered while skipping characters, the empty string is returned. If the number of allowable characters exceeds MAX_CHARS, the rest are read and discarded. */ /*********************************************************************** * GetAlphaNum() -- take in input if it's an alpha or numeric char * * * * Parameters * * bool skip -- read note above for true and false operations * * char letters[] -- the letters array to populate * * Returns * * void * * Precondition * * NONE * * Postcondition * * If the number of allowable characters exceeds MAX_CHARS, the * * remaining allowable characters have been read and discarded. * ***********************************************************************/ void GetAlphaNum(bool skip, char letters[]); // Post: letters array contains only alphabetic characters and numbers. /*********************************************************************** * GetTilNew() -- get everything up to the newline char * * * * Parameters * * bool skip -- read note above for true and false operations * * char letters[] -- the letters array to populate * * Returns * * void * * Precondition * * NONE * * Postcondition * * If the number of allowable characters exceeds MAX_CHARS, the * * remaining allowable characters have been read and discarded. * ***********************************************************************/ void GetTilNew(bool skip, char letters[]); // Post: letters array contains everything up to the newline character. /*********************************************************************** * GetAlphaNumFile() -- get alphanum characters from the file * * * * Parameters * * bool skip -- read note on skip above * * char letters[] -- letters array * * ifstream inFile -- infilestream inFile * * Returns * * void * * Precondition * * inFile exists * * Postcondition * * If the number of allowable characters exceeds maxCHARS, the * * remaining allowable characters have been read and discarded. * ***********************************************************************/ void GetAlphaNumFile(bool skip, char letters[], ifstream &inFile); // Post: letters array contains only alphabetic characters and numbers. /*********************************************************************** * GetTilNewFile() -- get until newline from the file * * * * Parameters * * bool skip -- read note on skip above * * char letters[] -- letters array * * ifstream inFile -- infilestream inFile * * Returns * * void * * Precondition * * inFile exists * * Postcondition * * If the number of allowable characters exceeds maxCHARS, the * * remaining allowable characters have been read and discarded. * ***********************************************************************/ void GetTilNewFile(bool skip, char letters[], ifstream &inFile); // Post: letters array contains everything up to the newline character. /* NOT IMPLEMENTED YET void GetAlpha(bool skip, char letters[]); // Post: letters array contains only alphabetic characters. void GetNonWhite(bool skip, char letters[]); // Post: letters array contains only non-white space characters. void GetAlphaFile(bool skip, char letters[], ifstream& inFile); // Post: letters array contains only alphabetic characters. void GetNonWhiteFile(bool skip, char letters[], ifstream& inFile); // Post: letters array contains only non-white space characters. */ /*********************************************************************** * operator=() -- copies otherString into letters * * * * Parameters * * StrType otherString -- the passed string object * * Returns * * void -- remember letters will now equal otherString * * Precondition * * NONE * * Postcondition * * letters will contain otherString after assignment * ***********************************************************************/ void StrType::operator=(StrType otherString) { strcpy(letters, otherString.letters); } /*********************************************************************** * GetString() -- used to get the input string * * * * Parameters * * bool skip -- read note above for true and false operations * * InType charsAllowed -- what sorts of characters we will take * * Returns * * void * * Precondition * * NONE * * Postcondition * * If the number of allowable characters exceeds MAX_CHARS, the * * remaining allowable characters have been read and discarded. * ***********************************************************************/ void StrType::GetString(const bool skip, InType charsAllowed) { switch (charsAllowed) { case ALPHA_NUM : GetAlphaNum(skip, letters); break; // case ALPHA : GetAlpha(skip, letters); // break; // case NON_WHITE : GetNonWhite(skip, letters); // break; case NOT_NEW : GetTilNew(skip, letters); break; } } /*********************************************************************** * GetAlphaNum() -- take in input if it's an alpha or numeric char * * * * Parameters * * bool skip -- read note above for true and false operations * * char letters[] -- the letters array to populate * * Returns * * void * * Precondition * * NONE * * Postcondition * * If the number of allowable characters exceeds MAX_CHARS, the * * remaining allowable characters have been read and discarded. * ***********************************************************************/ void GetAlphaNum(bool skip, char letters[]) { char letter; /* the input letter */ int count = 0; /* make count start @ 0 */ if (skip) /* skip non-alphanum */ { cin.get(letter); /* get the first letter */ while (!isalnum(letter) && cin) /* loop until get anphnum */ cin.get(letter); /* get one at a time */ } else /* or just get the letter */ cin.get(letter); /* we don't care */ if (!cin || !isalnum(letter)) /* if letter !alphanum empty */ { letters[0] = '\0'; /* empty letters array */ } else /* if it is alphanum */ { do /* loop */ { letters[count] = letter; /* copy letter to letters */ count++; /* next */ cin.get(letter); /* get another */ } while (isalnum(letter) && cin && (count < MAX_CHARS)); /* break out when we get everything or letter is !alphanum */ letters[count] = '\0'; /* make the last val NULL */ /* Skip extra characters if necessary. */ if (count == MAX_CHARS) do { cin.get(letter); } while (isalnum(letter) && cin); } } /*********************************************************************** * GetTilNew() -- get everything up to the newline char * * * * Parameters * * bool skip -- read note above for true and false operations * * char letters[] -- the letters array to populate * * Returns * * void * * Precondition * * NONE * * Postcondition * * If the number of allowable characters exceeds MAX_CHARS, the * * remaining allowable characters have been read and discarded. * ***********************************************************************/ void GetTilNew(bool skip, char letters[]) { char letter; /* input letter */ int count = 0; /* start count @ 0 */ if (skip) /* is we want to skip '\n' */ { cin.get(letter); /* get the letter */ while ((letter == '\n') && cin) /* loop while we are '\n' */ cin.get(letter); /* one at a time */ } else /* don't care of it's '\n' */ cin.get(letter); /* just get first char */ if (!cin || letter == '\n') /* if '\n' empty letters */ { letters[0] = '\0'; } else /* otherwise go here */ { do /* loop */ { letters[count] = letter; /* copy letter to letters */ count++; /* next */ cin.get(letter); /* get another */ } while ((letter != '\n') && cin && (count < MAX_CHARS)); /* loop while letters is not full or '\n' */ letters[count] = '\0'; /* end letters with NULL */ /* Skip extra characters if necessary. */ if (count == MAX_CHARS) do { cin.get(letter); } while ((letter == '\n') && cin); } } /*********************************************************************** * GetStringFile() -- used to read in from input file * * * * Parameters * * bool skip -- read note above for true and false operations * * InType charsAllowed -- what sorts of characters we will take * * ifstream inFile -- file handle for reading * * Returns * * void * * Precondition * * inFile exists * * Postcondition * * If the number of allowable characters exceeds maxCHARS, the * * remaining allowable characters have been read and discarded. * ***********************************************************************/ void StrType::GetStringFile(const bool skip, InType charsAllowed, ifstream &inFile) { switch (charsAllowed) { case ALPHA_NUM : GetAlphaNumFile(skip, letters, inFile); break; // case ALPHA : GetAlphaFile(skip, letters, inFile); // break; // case NON_WHITE : GetNonWhiteFile(skip, letters, inFile); // break; case NOT_NEW : GetTilNewFile(skip, letters, inFile); break; } } /*********************************************************************** * GetAlphaNumFile() -- get alphanum characters from the file * * * * Parameters * * bool skip -- read note on skip above * * char letters[] -- letters array * * ifstream inFile -- infilestream inFile * * Returns * * void * * Precondition * * inFile exists * * Postcondition * * If the number of allowable characters exceeds maxCHARS, the * * remaining allowable characters have been read and discarded. * ***********************************************************************/ void GetAlphaNumFile(bool skip, char letters[], ifstream &inFile) { char letter; /* input letter */ int count = 0; /* start count @ 0 */ if (skip) /* skip alphanum chars */ { inFile.get(letter); /* get letter from file */ while (!isalnum(letter) && inFile) /* loop until it's alphanum */ inFile.get(letter); } else /* just grab the char */ inFile.get(letter); /* get first char */ if (!inFile || !isalnum(letter)) /* if it's !alphanum */ { letters[0] = '\0'; /* make letters empty */ } else /* if letter is alphanum */ { do /* loopit */ { letters[count] = letter; /* copy letter to letters */ count++; /* next letter */ inFile.get(letter); /* get the next letter */ } while (isalnum(letter) && inFile && (count < MAX_CHARS)); /* loop while letter is alphanum and we still have space */ letters[count] = '\0'; /* end letters array w/NULL */ /* Skip extra characters if necessary. */ if (count == MAX_CHARS) do { inFile.get(letter); } while (isalnum(letter) && inFile); } } /*********************************************************************** * GetTilNewFile() -- get until newline from the file * * * * Parameters * * bool skip -- read note on skip above * * char letters[] -- letters array * * ifstream inFile -- infilestream inFile * * Returns * * void * * Precondition * * inFile exists * * Postcondition * * If the number of allowable characters exceeds maxCHARS, the * * remaining allowable characters have been read and discarded. * ***********************************************************************/ void GetTilNewFile(bool skip, char letters[], ifstream& inFile) { char letter; /* input letter */ int count = 0; /* count start @ 0 */ if (skip) /* skip leading new line */ { inFile.get(letter); /* get a letter */ while ((letter == '\n') && inFile) /* loop while letter is nl */ inFile.get(letter); /* get letter again */ } else /* just grab it */ inFile.get(letter); /* first character */ if (!inFile || letter == '\n') /* if nl empty letters */ { letters[0] = '\0'; /* empty letters */ } else /* if !nl go here */ { do /* loop man loop */ { letters[count] = letter; /* copy letter to letters */ count++; /* next */ inFile.get(letter); /* get another letter */ } while ((letter != '\n') && inFile && (count < MAX_CHARS)); /* while letter does !equal nl and letters is not full */ letters[count] = '\0'; /* end letter in NULL */ /* Skip extra characters if necessary. */ if (count == MAX_CHARS) do { inFile.get(letter); } while ((letter == '\n') && inFile); } } /*********************************************************************** * CopyString() -- This will copy the contents of letters into a * * StrType that is pass by reference. * * * * Parameters * * StrType newString -- copies letters into newString.letters * * Returns * * void -- remember it is pass by reference * * Precondition * * you will want something in letters * * Postcondition * * newString will contain the same thing as letters. * ***********************************************************************/ void StrType::CopyString(StrType& newString) { strcpy(newString.letters, letters); } /*********************************************************************** * StrType() -- Default constructor used to initialize letters (empty) * * * * Parameters * * void * * Returns * * void * * Precondition * * NONE * * Postcondition * * Current position is prior to list. * ***********************************************************************/ StrType::StrType() { letters[0] = '\0'; } /*********************************************************************** * operator<() -- if letters is less then otherString * * * * Parameters * * StrType otherString -- the passed string object * * Returns * * bool -- returns true if less then otherString else false * * Precondition * * you will want something in letters * * Postcondition * * returns true if less then otherString else false * ***********************************************************************/ bool StrType::operator<(StrType otherString) { int result; /* compare the two strings and then present true or false */ result = strcmp(letters, otherString.letters); if (result < 0) return true; else return false; } /*********************************************************************** * operator==() -- if letters is equal to otherString * * * * Parameters * * StrType otherString -- the passed string object * * Returns * * bool -- returns true if equal to otherString else false * * Precondition * * You will want something in letters * * Postcondition * * returns true if equal to otherString else false * ***********************************************************************/ bool StrType::operator==(StrType otherString) { int result; /* compare the two strings and then present true or false */ result = strcmp(letters, otherString.letters); if (result == 0) return true; else return false; } /*********************************************************************** * PrintToScreen() -- this will print contents letters * * * * Parameters * * bool newLine -- if true we print a newline first else opposite * * Returns * * void * * Precondition * * You will want something in letters * * Postcondition * * letters will be printed to screen. * ***********************************************************************/ void StrType::PrintToScreen(const bool newLine) const { if (newLine) cout << endl; cout << letters; } /*********************************************************************** * PrintToFile() -- This will print the contents of letters to outFile * * * * Parameters * * bool newLine -- if true we print a newline first else opposite * * ofstream outFile -- file handle to print letters into * * Returns * * void * * Precondition * * You will want something in letters * * Postcondition * * letters will be printed to outFile * ***********************************************************************/ void StrType::PrintToFile(const bool newLine, ofstream& outFile) const { if (newLine) outFile << endl; outFile << letters; } /*********************************************************************** * MakeEmpty() -- used to empty letters * * * * Parameters * * void * * Returns * * void * * Precondition * * NONE * * Postcondition * * Current position is prior to list. * ***********************************************************************/ void StrType::MakeEmpty() { letters[0] = '\0'; } /*********************************************************************** * LengthIs() -- figure out the length of letters * * * * Parameters * * void * * Returns * * int -- returns the numeric length of letters like strlen() * * Precondition * * something should be in letters * * Postcondition * * returns the numeric length of letters * ***********************************************************************/ int StrType::LengthIs() { return strlen(letters); } /*********************************************************************** * EqualtoCharArray() -- if input string is the same as letters * * * * Parameters * * char carray[] -- character array to compare with letters * * Returns * * bool -- returns true of equal false if !equal * * Precondition * * letters should be assigned a value * * Postcondition * * a true or false will be returned * ***********************************************************************/ bool StrType::EqualtoCharArray(char carray[]) { int result; /* compare the two strings and then present true or false */ result = strcmp(letters, carray); if (result == 0) return true; else return false; } /*********************************************************************** * SetString() -- used to copy carray into letters array * * * * Parameters * * char carray[] -- takes a character array as input * * Returns * * void * * Precondition * * NONE * * Postcondition * * letters will equal carray * ***********************************************************************/ void StrType::SetString(char carray[]) { if (strlen(carray) < MAX_CHARS) strcpy(letters, carray); }