| RecursivePowerFunction.h | String.h | StringReverse.h | CSoundManager.h | CSoundManager.cpp |
String.h
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //    Name    : String.h
  3. //
  4. //    Date    : 2008.04.08
  5. //
  6. //    Purpose : Simple functionality in a string class.
  7. ///////////////////////////////////////////////////////////////////////////////
  8. class String
  9. {
  10. public:
  11.         // Constructor
  12.         String(void): m_szCharacters(NULL) {}
  13.         // Destructor
  14.         ~String(void)
  15.         {
  16.                 if (NULL != m_szCharacters)
  17.                         delete [] m_szCharacters;
  18.         }
  19.  
  20.         ///////////////////////////////////////////////////////////////////////////////
  21.         //    Name     : operator=
  22.         //
  23.         //    Date     : 2008.04.08
  24.         //
  25.         //    Purpose  : Set the string from another string.
  26.         ///////////////////////////////////////////////////////////////////////////////
  27.         String & operator=(const String &rightString)
  28.         {
  29.                 if (this == &rightString)
  30.                         return *this;
  31.  
  32.                 int rightSize = static_cast<int>(strlen(rightString.m_szCharacters));
  33.  
  34.                 if (m_szCharacters)
  35.                         delete [] m_szCharacters;
  36.  
  37.                 m_szCharacters = new char[rightSize+1];
  38.                 strcpy(m_szCharacters, rightString.m_szCharacters);
  39.  
  40.                 return *this;
  41.         }
  42.         String & operator=(const char * rightString)
  43.         {
  44.                 int rightSize = static_cast<int>(strlen(rightString));
  45.  
  46.                 if (m_szCharacters)
  47.                         delete [] m_szCharacters;
  48.  
  49.                 m_szCharacters = new char[rightSize+1];
  50.                 strcpy(m_szCharacters, rightString);
  51.  
  52.                 return *this;
  53.         }
  54.  
  55.  
  56.         ///////////////////////////////////////////////////////////////////////////////
  57.         //    Name     : operator==
  58.         //
  59.         //    Date     : 2008.04.08
  60.         //
  61.         //    Purpose  : Check the string for equality.
  62.         ///////////////////////////////////////////////////////////////////////////////
  63.         bool operator==(const String &rightString)
  64.         {
  65.                 if (this == &rightString)
  66.                         return true;
  67.  
  68.                 int rightSize = static_cast<int>(strlen(rightString.m_szCharacters));
  69.  
  70.                 if (strlen(m_szCharacters) != rightSize)
  71.                         return false;
  72.  
  73.                 for (int i = 0; i < rightSize; i++)
  74.                         if (m_szCharacters[i] != rightString.m_szCharacters[i])
  75.                                 return false;
  76.  
  77.                 return true;
  78.         }
  79.  
  80.  
  81.         ///////////////////////////////////////////////////////////////////////////////
  82.         //    Name     : operator+=
  83.         //
  84.         //    Date     : 2008.04.08
  85.         //
  86.         //    Purpose  : String concatenation overloaded into the += operator.
  87.         ///////////////////////////////////////////////////////////////////////////////
  88.         String & operator+=(const String &rightString)
  89.         {
  90.                 int StringSize = static_cast<int>(strlen(m_szCharacters) + strlen(rightString.m_szCharacters) + 1);
  91.                 char* newString = new char[StringSize];
  92.  
  93.                 if (m_szCharacters)
  94.                 {
  95.                         strcpy(newString, m_szCharacters);
  96.                         delete [] m_szCharacters;
  97.                 }
  98.  
  99.                 strcat(newString, rightString.m_szCharacters);
  100.                 m_szCharacters = newString;
  101.  
  102.                 return *this;
  103.         }
  104.  
  105.  
  106.         ///////////////////////////////////////////////////////////////////////////////
  107.         //    Name     : AsCString
  108.         //
  109.         //    Date     : 2008.04.08
  110.         //
  111.         //    Purpose  : Return the string as a C style string (char array).
  112.         ///////////////////////////////////////////////////////////////////////////////
  113.         const char* AsCString(void)
  114.         {
  115.                 return m_szCharacters;
  116.         }
  117.  
  118. private:
  119.         char * m_szCharacters;
  120. };