String.h
- ///////////////////////////////////////////////////////////////////////////////
- // Name : String.h
- //
- // Date : 2008.04.08
- //
- // Purpose : Simple functionality in a string class.
- ///////////////////////////////////////////////////////////////////////////////
- class String
- {
- public:
- // Constructor
- String(void): m_szCharacters(NULL) {}
- // Destructor
- ~String(void)
- {
- if (NULL != m_szCharacters)
- delete [] m_szCharacters;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // Name : operator=
- //
- // Date : 2008.04.08
- //
- // Purpose : Set the string from another string.
- ///////////////////////////////////////////////////////////////////////////////
- String & operator=(const String &rightString)
- {
- if (this == &rightString)
- return *this;
- int rightSize = static_cast<int>(strlen(rightString.m_szCharacters));
- if (m_szCharacters)
- delete [] m_szCharacters;
- m_szCharacters = new char[rightSize+1];
- strcpy(m_szCharacters, rightString.m_szCharacters);
- return *this;
- }
- String & operator=(const char * rightString)
- {
- int rightSize = static_cast<int>(strlen(rightString));
- if (m_szCharacters)
- delete [] m_szCharacters;
- m_szCharacters = new char[rightSize+1];
- strcpy(m_szCharacters, rightString);
- return *this;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // Name : operator==
- //
- // Date : 2008.04.08
- //
- // Purpose : Check the string for equality.
- ///////////////////////////////////////////////////////////////////////////////
- bool operator==(const String &rightString)
- {
- if (this == &rightString)
- return true;
- int rightSize = static_cast<int>(strlen(rightString.m_szCharacters));
- if (strlen(m_szCharacters) != rightSize)
- return false;
- for (int i = 0; i < rightSize; i++)
- if (m_szCharacters[i] != rightString.m_szCharacters[i])
- return false;
- return true;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // Name : operator+=
- //
- // Date : 2008.04.08
- //
- // Purpose : String concatenation overloaded into the += operator.
- ///////////////////////////////////////////////////////////////////////////////
- String & operator+=(const String &rightString)
- {
- int StringSize = static_cast<int>(strlen(m_szCharacters) + strlen(rightString.m_szCharacters) + 1);
- char* newString = new char[StringSize];
- if (m_szCharacters)
- {
- strcpy(newString, m_szCharacters);
- delete [] m_szCharacters;
- }
- strcat(newString, rightString.m_szCharacters);
- m_szCharacters = newString;
- return *this;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // Name : AsCString
- //
- // Date : 2008.04.08
- //
- // Purpose : Return the string as a C style string (char array).
- ///////////////////////////////////////////////////////////////////////////////
- const char* AsCString(void)
- {
- return m_szCharacters;
- }
- private:
- char * m_szCharacters;
- };