/* StrType.cpp */ #include /* std I/O */ #include /* for file I/O */ #include /* for string functions */ #include /* for isalnum and others */ using namespace std; /* for ISO/ANSI compliance */ #include "StrType.h" /* class definition file */ /*********************************************************************** * StrType() -- Default constructor used to initialize letters (empty) * * * * Parameters * * void * * Returns * * void * * Precondition * * NONE * * Postcondition * * Current position is prior to list. * ***********************************************************************/ StrType::StrType() { letters = (char *)malloc(MAX_BUFF); /* make letters that new size */ if(letters == NULL) /* check if heap is full */ { fprintf(stderr, "Heap full\n"); exit(1); /* exit if true */ } buffSize = MAX_BUFF; /* set the buffer size */ tok = false; /* no token */ } /*********************************************************************** * ~StrType() -- Default destructor used to clean up letters * * * * Parameters * * void * * Returns * * void * * Precondition * * NONE * * Postcondition * * dynamic string is removed from heap * ***********************************************************************/ StrType::~StrType() { free(letters); /* letters is removed */ letters = NULL; /* just to be clean */ } /*********************************************************************** * StrType() -- copy constructor used to deep copy * * * * Parameters * * the original StrType * * Returns * * NONE * * Precondition * * NONE * * Postcondition * * a deep copy of performed * ***********************************************************************/ StrType::StrType(const StrType &anotherStrType) { buffSize = anotherStrType.buffSize; /* set the buffer size */ tok = anotherStrType.tok; /* copy token */ letters = (char *)malloc(buffSize); /* make letters that new size */ if(letters == NULL) /* check if heap is full */ { fprintf(stderr, "Heap full\n"); exit(1); /* exit if true */ } /* copy other letters into new letters */ strncpy(letters, anotherStrType.letters, buffSize); } /*********************************************************************** * MakeEmpty() -- used to empty letters * * * * Parameters * * void * * Returns * * void * * Precondition * * NONE * * Postcondition * * Current position is prior to list. * ***********************************************************************/ void StrType::MakeEmpty() { free(letters); /* letters is removed */ letters = (char *)malloc(MAX_BUFF); /* and one more is made */ if(letters == NULL) /* check if heap is full */ { fprintf(stderr, "Heap full\n"); exit(1); /* exit if true */ } letters[0] = '\0'; /* empty letters array */ buffSize = MAX_BUFF; /* set the buffer size */ tok = false; /* no token */ } /*********************************************************************** * Grow() -- Grow the letters array * * * * Parameters * * void * * Returns * * bool - if the new malloc was a success * * Precondition * * NONE * * Postcondition * * letters is now larger * ***********************************************************************/ bool StrType::Grow() { char *tempPtr; /* a temp pointer to delete */ tempPtr = letters; /* make tempPtr == letters */ buffSize = buffSize + MAX_BUFF; /* construct new size */ letters = (char *)malloc(buffSize); /* make letters that new size */ if(letters == NULL) /* check if heap is full */ { fprintf(stderr, "Heap full\n"); return(false); /* exit if it is */ } strncpy(letters, tempPtr, buffSize); /* copy old letters to new */ free(tempPtr); /* free up memory */ tempPtr = NULL; /* and make it clear */ return(true); /* all is well that ends well */ } /*********************************************************************** * Element() -- get the element of array at num location * * * * Parameters * * int -- the number of the array to return * * Returns * * char -- the element in that part of array * * Precondition * * NONE * * Postcondition * * the element in num location is returned * ***********************************************************************/ char StrType::Element(int num) { /* so long as we are not out of bounds */ if(num < buffSize) { return(letters[num]); } else { return('\0'); } } /*********************************************************************** * 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) >= buffSize) { Grow(); } strncpy(letters, carray, buffSize); } /*********************************************************************** * 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 * * letters will contain input string * ***********************************************************************/ void StrType::GetString(const bool skip, InType charsAllowed) { switch (charsAllowed) { case ALPHA_NUM : GetAlphaNum(skip); break; case NOT_NEW : GetTilNew(skip); break; /* case ALPHA : GetAlpha(skip); break; case NON_WHITE : GetNonWhite(skip); break; */ } } /*********************************************************************** * 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 * * letters will contain input string * ***********************************************************************/ void StrType::GetStringFile(const bool skip, InType charsAllowed, ifstream &inFile) { switch (charsAllowed) { case ALPHA_NUM : GetAlphaNumFile(skip, inFile); break; case NOT_NEW : GetTilNewFile(skip, inFile); break; /* case ALPHA : GetAlphaFile(skip, inFile); break; case NON_WHITE : GetNonWhiteFile(skip, inFile); break; */ } } /*********************************************************************** * 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) { if (LengthIs() >= newString.buffSize) { newString.Grow(); } strncpy(newString.letters, letters, newString.buffSize); } /*********************************************************************** * 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 { cout << letters; if (newLine) { cout << endl; } } /*********************************************************************** * 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 { outFile << letters; if (newLine) { outFile << endl; } } /*********************************************************************** * 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); } } /*********************************************************************** * Tok() -- this is a string tokenizer similar to strtok() * * * * Parameters * * char[] -- token to use * * Returns * * char* -- pointer to the next substring pointed to * * Precondition * * something should be in letters * * Postcondition * * returns a substring * ***********************************************************************/ char* StrType::Tok(char token[]) { if(tok) { return(strtok(NULL, token)); } else { tok = true; return(strtok(letters, token)); } } /*********************************************************************** * 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)); } /*********************************************************************** * SizeOf() -- return the max size of array * * * * Parameters * * void * * Returns * * int -- returns the numeric size of letters like sizeof() * * Precondition * * none * * Postcondition * * returns the numeric size of letters * ***********************************************************************/ int StrType::SizeOf() { return(buffSize); } /*********************************************************************** * 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); } } /*********************************************************************** * 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) { if(otherString.LengthIs() >= buffSize) { Grow(); } strncpy(letters, otherString.letters, buffSize); } /*********************************************************************** * operator=() -- copies carray into letters * * * * Parameters * * char[] -- the char array to copy into letters * * Returns * * void -- remember letters will now equal otherString * * Precondition * * NONE * * Postcondition * * letters will contain carray after assignment * ***********************************************************************/ void StrType::operator=(char carray[]) { if(strlen(carray) >= buffSize) { Grow(); } strncpy(letters, carray, buffSize); } /*********************************************************************** * GetAlphaNum() -- take in input if it's an alpha or numeric char * * * * Parameters * * bool skip -- read note above for true and false operations * * Returns * * void * * Precondition * * NONE * * Postcondition * * letters will contain input string * ***********************************************************************/ void StrType::GetAlphaNum(bool skip) { 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 alphanum */ { 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 */ { MakeEmpty(); /* empty letters array */ } else /* if it is alphanum */ { do /* loop */ { if(count >= buffSize) /* if letters is full Grow() */ { Grow(); } letters[count] = letter; /* copy letter to letters */ count++; /* next */ cin.get(letter); /* get another */ } while(isalnum(letter) && cin); /* break out when we get everything or letter is !alphanum */ if(count >= buffSize) /* if letters is full Grow() */ { Grow(); } letters[count] = '\0'; /* make the last val NULL */ } } /*********************************************************************** * GetTilNew() -- get everything up to the newline char * * * * Parameters * * bool skip -- read note above for true and false operations * * Returns * * void * * Precondition * * NONE * * Postcondition * * letters will contain input string * ***********************************************************************/ void StrType::GetTilNew(bool skip) { 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 */ { MakeEmpty(); /* empty letters array */ } else /* otherwise go here */ { do /* loop */ { if(count >= buffSize) /* if letters is full Grow() */ { Grow(); } letters[count] = letter; /* copy letter to letters */ count++; /* next */ cin.get(letter); /* get another */ }while((letter != '\n') && cin); /* loop while letters is not full or '\n' */ if(count >= buffSize) /* if letters is full Grow() */ { Grow(); } letters[count] = '\0'; /* end letters with NULL */ } } /*********************************************************************** * GetAlphaNumFile() -- get alphanum characters from the file * * * * Parameters * * bool skip -- read note on skip above * * ifstream inFile -- infilestream inFile * * Returns * * void * * Precondition * * inFile exists * * Postcondition * * letters will contain input string * ***********************************************************************/ void StrType::GetAlphaNumFile(bool skip, 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 */ { MakeEmpty(); /* empty letters array */ } else /* if letter is alphanum */ { do /* loopit */ { if(count >= buffSize) /* if letters is full Grow() */ { Grow(); } letters[count] = letter; /* copy letter to letters */ count++; /* next letter */ inFile.get(letter); /* get the next letter */ } while(isalnum(letter) && inFile); /* loop while letter is alphanum and we still have space */ if(count >= buffSize) /* if letters is full Grow() */ { Grow(); } letters[count] = '\0'; /* end letters array w/NULL */ } } /*********************************************************************** * GetTilNewFile() -- get until newline from the file * * * * Parameters * * bool skip -- read note on skip above * * ifstream inFile -- infilestream inFile * * Returns * * void * * Precondition * * inFile exists * * Postcondition * * letters will contain input string * ***********************************************************************/ void StrType::GetTilNewFile(bool skip, 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 */ { MakeEmpty(); /* empty letters array */ } else /* if !nl go here */ { do /* loop man loop */ { if(count >= buffSize) /* if letters is full Grow() */ { Grow(); } letters[count] = letter; /* copy letter to letters */ count++; /* next */ inFile.get(letter); /* get another letter */ } while((letter != '\n') && inFile); /* while letter does !equal nl and letters is not full */ if(count >= buffSize) /* if letters is full Grow() */ { Grow(); } letters[count] = '\0'; /* end letter in NULL */ } }