/*----------------------------------------------------------------------------*/
/*  (c) 1994 Larry Morley                                                     */
/*----------------------------------------------------------------------------*/
/*  Module     : strings.cpp                            Date: 04 April 1994   */
/*                                                                            */
/*  Description: A string class that automatically sizes itself based on the  */
/*               data assigned to it or provided when an object of the class  */
/*               type is created.  Automatically adjusts string length for    */
/*               concatenation.  Looks to other functions just like an        */
/*               ordinary char * via a conversion function.                   */
/*                                                                            */
/*----------------------------------------------------------------------------*/
/*                                                                            */
/*  To contact the author, send an e-mail message to 73670,563 or a regular   */
/*  letter / note to:                                                         */
/*                                                                            */
/*  Larry Morley / Enlightened Computer Solutions                             */
/*  6909 Custer #2503                                                         */
/*  Plano, TX 75023                                                           */
/*                                                                            */
/*  Please let me know if you're using this code, and your suggestions,       */
/*  questions, etc.  Thanks.                                                  */
/*                                                                            */
/*----------------------------------------------------------------------------*/

#include <stdio.h>
#include <string.h>

/*----------------------------------------------------------------------------*/

int main(void);

/*----------------------------------------------------------------------------*/
// Note that there's no need to override the assignment operator;
// since there's a constructor that takes a char * as an argument,
// the compiler automatically supplies the conversion from char *
// to String, by creating an interim string from the char *, and a
// new string through the constructor as a result of the assignment.

class String
{
   private:

      // The actual string text
      char *szText;

   public:

      // Constructor for string literals,
      // char * strings, etc.
      String(char *initText)
      {
         // szText <- address if enough memory
         // is available or NULL if out of memory.
         szText = new char[strlen(initText)+1];
         // Make sure we got the memory
         if (szText)
            strcpy(szText,initText);
      };

      //---------------------------------------------------

      // Constructor for a string of
      // a certain length
      String(int size)
      {
         // szText <- address if enough memory
         // is available or NULL if out of memory.
         szText = new char [size];
         // Make sure we got the memory
         // Terminate the string at the
         // first element (empty string)
         if (szText)
            szText[0] = (char)0;
      };

      //---------------------------------------------------

      ~String()
      {
         // If there's a string, delete it
         if (szText)
            delete[] szText;
      };

      //---------------------------------------------------

      // Conversion function - makes class objects
      // function and look like a normal char *s
      operator char *()
      {
         return szText;
      };

      //---------------------------------------------------

      // Overload addition operator
      // to mean concatenation
      String operator+(String szStringB)
      {
         // Create a temporary string
         char *p = new char[strlen(szText)+strlen(szStringB)+1];
         // Make sure we got the memory
         if (p)
         {
            strcpy(p,szText);
            // Delete the old string
            delete[] szText;
            // Copy the original string into the
            // temporary string
            szText = p;
            // Do the concatenation
            strcat(szText,szStringB);
            // Return the new value of szText
            return szText;
         }
         // Out of memory - return NULL
         return (String)0;
      };

};

/*----------------------------------------------------------------------------*/
// Try the String class out...

main()
{
   String StringA = "This is a string";
   char * StringB = ", alright.";

   printf("Before concatenation:\n");
   printf("            StringA = %s\n",StringA);
   printf("            StringB = %s\n",StringB);

   StringA = StringA + StringB;

   printf("After concatenation :\n");
   printf("            StringA = %s\n",StringA);
   printf("            StringB = %s\n",StringB);

   return 0;
};

/*----------------------------------------------------------------------------*/
