home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / SAMPLES / ICLCC / TOYSTRNG.H < prev    next >
C/C++ Source or Header  |  1993-05-07  |  3KB  |  93 lines

  1. /******************************************************************************/
  2. /*                                                                            */
  3. /* COPYRIGHT:                                                                 */
  4. /* ----------                                                                 */
  5. /* Copyright (C) International Business Machines Corp., 1991,1992.            */
  6. /*                                                                            */
  7. /* DISCLAIMER OF WARRANTIES:                                                  */
  8. /* -------------------------                                                  */
  9. /* The following [enclosed] code is sample code created by IBM                */
  10. /* Corporation.  This sample code is not part of any standard IBM product     */
  11. /* and is provided to you solely for the purpose of assisting you in the      */
  12. /* development of your applications.  The code is provided "AS IS",           */
  13. /* without warranty of any kind.  IBM shall not be liable for any damages     */
  14. /* arising out of your use of the sample code, even if they have been         */
  15. /* advised of the possibility of such damages.                                */
  16. /*                                                                            */
  17. /******************************************************************************/
  18. /*-------------------------------------------------------------*\
  19. |  toystrng.h  -  ToyString Class for use with coding examples. |
  20. |                                                               |
  21. |  Note:  ToyString is kept as simple as possible to not        |
  22. |         obscure the principles with details.                  |
  23. |         If you are using the Collection Classes with OS/2,    |
  24. |         you might want to use the String Class from the base  |
  25. |         class library for real.                               |
  26. \*-------------------------------------------------------------*/
  27. #ifndef _TOYSTRNG_H_
  28.    #define _TOYSTRNG_H_
  29.  
  30.    #include <string.h>
  31.    #include <iostream.h>
  32.                                 // Definition of Boolean:
  33.    #include <iglobals.h>
  34.  
  35. class ToyString {
  36.  
  37.      char* txt;
  38.      int   lng;
  39.  
  40.   public:
  41.  
  42.      ToyString (char* atxt)  {
  43.         init(atxt);
  44.      }
  45.  
  46.      ToyString (ToyString const& aTS)  {
  47.         init(aTS.txt);
  48.      }
  49.  
  50.      ToyString& operator= (ToyString const& aTS)  {
  51.         if (& aTS != this)  {
  52.            deInit();
  53.            init(aTS.txt);
  54.         }
  55.         return *this;
  56.      }
  57.  
  58.      ~ToyString () { deInit(); }
  59.  
  60.      Boolean operator== (ToyString const& aTS) const  {
  61.         return strcmp(txt, aTS.txt) == 0;
  62.      }
  63.  
  64.      Boolean operator== (char* acp) const  {
  65.         return strcmp(txt, acp) == 0;
  66.      }
  67.  
  68.      Boolean operator!= (ToyString const& aTS) const  {
  69.         return strcmp(txt, aTS.txt) != 0;
  70.      }
  71.  
  72.      char* const& text() const {return txt;}
  73.  
  74.      int const& length() const {return lng;}
  75.  
  76.      friend ostream& operator<< (ostream& os, ToyString const& aTS) {
  77.         return os << aTS.txt;
  78.      }
  79.  
  80.   protected:
  81.  
  82.     inline void init (char* const& atxt)  {
  83.        lng = strlen(atxt);
  84.        txt = new char[lng + 1];
  85.        strcpy(txt, atxt);
  86.     }
  87.  
  88.     inline void deInit() {
  89.     delete[] txt;  }
  90.  
  91. };
  92. #endif
  93.