/* * Copyright (c) 2002 by Cliff Green. All rights reserved. Individual files * may be covered by other copyrights (as noted in the file itself). * * Redistribution and use in source and binary forms are permitted * provided that this entire copyright notice is duplicated in all such * copies. * * This software is provided "as is" and without any expressed or implied * warranties, including, without limitation, the implied warranties of * merchantibility and fitness for any particular purpose. */ //---------------------------------------------------------------------- // Source file: word.cpp // Written by: Cliff Green, 2002 // Compiler: Metrowerks CodeWarrior Pro 6, g++ 3.0.4 // History: // Modified: Mar. 5, 2002 // By: Cliff Green // Comments: See header file comments. //---------------------------------------------------------------------- // This commentheader supports documentation tools such as Doc++ and // Doxygen: http://www.doxygen.org/ //---------------------------------------------------------------------- /// Word class and string utility implementations, uses soundex.h functionality. /** * */ extern "C" { #include "soundex.h" } #include "word.h" #include #include #include // Utility function to count the number of alpha chars in a C string int strlen_alpha (char const* s) { int cnt(0); while (*s != '\0') { if (std::isalpha(*s)) { ++cnt; } ++s; } return cnt; } // Utility function to copy alpha-only chars, downshifted char* strcpy_alpha (char* dest, char const* source) { char* sav(dest); while (*source != '\0') { if (std::isalpha(*source)) { *dest = std::tolower(*source); ++dest; } ++source; } *dest = '\0'; return sav; } // Word constructor Word::Word (char const* initStr) : mLength(0), mWordPtr(0) { initWord(strlen_alpha(initStr), initStr); } // utility initialization function for the Word class void Word::initWord (int len, char const* initStr) { mLength = len; mWordPtr = new char[mLength + 1]; // throw exception if free store exhausted strcpy_alpha(mWordPtr, initStr); if (mLength == 0 || soundex (mWordPtr, mSoundexValue) == 0) { mSoundexValue[0] = '\0'; // empty mSoundexValue string so strcmps will work } } void Word::debugPrint () const { std::cout << " Str: " << mWordPtr << " Len: " << mLength << " Sdx: " << mSoundexValue << std::endl; } std::ostream& Word::streamOut (std::ostream& os) const { return (os << " " << mWordPtr << " "); } bool Word::isEqual (Word const& rhs) const { if (mLength != rhs.mLength) { return false; } return (std::strcmp(mWordPtr, rhs.mWordPtr) == 0 ? true : false); } Word& Word::operator= (Word const& rhs) { // copy assignment op overloaded if (this != &rhs) { // check for pathological case delete [] mWordPtr; initWord (rhs.mLength, rhs.mWordPtr); } return *this; } std::ostream& operator<< (std::ostream& os, Word const& rhs) { return rhs.streamOut(os); }