home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / string.zip / STRINGS.CPP < prev    next >
Text File  |  1994-04-19  |  8KB  |  209 lines

  1. /*----------------------------------------------------------------------------*/
  2. /*  (c) 1994 Larry Morley                                                     */
  3. /*----------------------------------------------------------------------------*/
  4. /*  Module     : strings.cpp                            Date: 04 April 1994   */
  5. /*                                                                            */
  6. /*  Description: A string class that automatically sizes itself based on the  */
  7. /*               data assigned to it or provided when an object of the class  */
  8. /*               type is created.  Automatically adjusts string length for    */
  9. /*               concatenation.  Looks to other functions just like an        */
  10. /*               ordinary char * via a conversion function.                   */
  11. /*                                                                            */
  12. /*----------------------------------------------------------------------------*/
  13. /*                                                                            */
  14. /*  To contact the author, send an e-mail message to 73670,563 or a regular   */
  15. /*  letter / note to:                                                         */
  16. /*                                                                            */
  17. /*  Larry Morley / Enlightened Computer Solutions                             */
  18. /*  6909 Custer #2503                                                         */
  19. /*  Plano, TX 75023                                                           */
  20. /*                                                                            */
  21. /*  Please let me know if you're using this code, and your suggestions,       */
  22. /*  questions, etc.  Thanks.                                                  */
  23. /*                                                                            */
  24. /*----------------------------------------------------------------------------*/
  25.  
  26. #include <stdio.h>
  27. #include <string.h>
  28.  
  29. /*----------------------------------------------------------------------------*/
  30.  
  31. int main(void);
  32.  
  33. /*----------------------------------------------------------------------------*/
  34. // Note that there's no need to override the assignment operator;
  35. // since there's a constructor that takes a char * as an argument,
  36. // the compiler automatically supplies the conversion from char *
  37. // to String, by creating an interim string from the char *, and a
  38. // new string through the constructor as a result of the assignment.
  39.  
  40. class String
  41. {
  42.    private:
  43.  
  44.       // The actual string text
  45.       char *szText;
  46.  
  47.    public:
  48.  
  49.       // Constructor for string literals,
  50.       // char * strings, etc.
  51.       String(char *initText)
  52.       {
  53.          // szText <- address if enough memory
  54.          // is available or NULL if out of memory.
  55.          szText = new char[strlen(initText)+1];
  56.          // Make sure we got the memory
  57.          if (szText)
  58.             strcpy(szText,initText);
  59.       };
  60.  
  61.       //---------------------------------------------------
  62.  
  63.       // Constructor for a string of
  64.       // a certain length
  65.       String(int size)
  66.       {
  67.          // szText <- address if enough memory
  68.          // is available or NULL if out of memory.
  69.          szText = new char [size];
  70.          // Make sure we got the memory
  71.          // Terminate the string at the
  72.          // first element (empty string)
  73.          if (szText)
  74.             szText[0] = (char)0;
  75.       };
  76.  
  77.       //---------------------------------------------------
  78.  
  79.       ~String()
  80.       {
  81.          // If there's a string, delete it
  82.          if (szText)
  83.             delete[] szText;
  84.       };
  85.  
  86.       //---------------------------------------------------
  87.  
  88.       // Conversion function - makes class objects
  89.       // function and look like a normal char *s
  90.       operator char *()
  91.       {
  92.          return szText;
  93.       };
  94.  
  95.       //---------------------------------------------------
  96.  
  97.       // Overload addition operator
  98.       // to mean concatenation
  99.       String operator+(String szStringB)
  100.       {
  101.          // Create a temporary string
  102.          char *p = new char[strlen(szText)+strlen(szStringB)+1];
  103.          // Make sure we got the memory
  104.          if (p)
  105.          {
  106.             strcpy(p,szText);
  107.             // Delete the old string
  108.             delete[] szText;
  109.             // Copy the original string into the
  110.             // temporary string
  111.             szText = p;
  112.             // Do the concatenation
  113.             strcat(szText,szStringB);
  114.             // Delete the temporary free store
  115.             delete[] p;
  116.             // Return the new value of szText
  117.             return szText;
  118.          }
  119.          // Out of memory - return NULL
  120.          return (String)0;
  121.       };
  122.  
  123.       //---------------------------------------------------
  124.  
  125.       // Do the same for "+=".  Since the copy constructor
  126.       // will automatically handle the assignment, all
  127.       // we need to do here is the concatenation; e.g.,
  128.       // the "+=" operator is equivalent to the "+" operator
  129.       String operator+=(String szStringB)
  130.       {
  131.          return String::operator+(szStringB);
  132.       };
  133.  
  134.       //***************************************************
  135.       // Note that you could build in some handy functions
  136.       // like these, and not interfere with anything
  137.  
  138.       int Length()
  139.       {
  140.          return strlen(szText);
  141.       };
  142.  
  143.       //---------------------------------------------------
  144.  
  145.       int IsEqual(String szStringB)
  146.       {
  147.          return (!strcmp(szText,szStringB));
  148.       };
  149.  
  150.       //***************************************************
  151.  
  152.       // And, you can overload the equality operator
  153.       String operator==(String szStringB)
  154.       {
  155.          return IsEqual(szStringB);
  156.       };
  157.  
  158.       //---------------------------------------------------
  159. };
  160.  
  161. /*----------------------------------------------------------------------------*/
  162. // Try the String class out...
  163.  
  164. main()
  165. {
  166.    String StringA = "This is a string";
  167.    char * StringB = ", alright.";
  168.    String StringC = "  You bet.";
  169.  
  170.    //============================================
  171.    // Test the concatenation operator ("+")
  172.    printf("StringA = StringA + StringB;\n");
  173.    printf("\"%s\" = \"%s\" + \"%s\" ;\n",StringA,StringA,StringB);
  174.    StringA = StringA + StringB;
  175.    printf("StringA: \"%s\".\n\n",StringA);
  176.  
  177.    //============================================
  178.    // Test the add-and-assign operator ("+=")
  179.    printf("StringA += StringC;\n");
  180.    printf("\"%s\" += \"%s\" ;\n",StringA,StringC);
  181.    StringA += StringC;
  182.    printf("StringA: \"%s\".\n\n",StringA);
  183.  
  184.    //============================================
  185.    // Test the length function (String::Length())
  186.    printf("Length of StringA: %d Characters.\n\n",StringA.Length());
  187.  
  188.    //============================================
  189.    // Test the equality function (String::IsEqual())
  190.    printf("StringA (\"%s\") == StringB (\"%s\") ? %s.\n\n",
  191.       StringA,
  192.       StringB,
  193.       StringA.IsEqual(StringB) ? "Yes"
  194.                                : "No");
  195.  
  196.    //============================================
  197.    // Test the equality operator ("==")
  198.    printf("StringA (\"%s\") == StringA (\"%s\") ? %s.\n",
  199.       StringA,
  200.       StringA,
  201.       StringA == StringA ? "Yes" : "No");
  202.  
  203.    printf("**************************************\n");
  204.  
  205.    return 0;
  206. };
  207.  
  208. /*----------------------------------------------------------------------------*/
  209.