home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / CL187A.ZIP / EXAMP301.CPP < prev    next >
C/C++ Source or Header  |  1994-03-15  |  2KB  |  96 lines

  1. // Container Lite (CL v 1.87a)
  2. // (C) Copyright John Webster Small 1994
  3. // All rights reserved
  4. // examp301.cpp -- link with cl.obj
  5.  
  6. // #define CL_NO_TEMPLATES
  7.  
  8. #include "cl.h"
  9.  
  10.  
  11. class String {
  12.  
  13.     char *str;
  14.     size_t len;
  15.     int cmp(const String& cs) const;
  16.  
  17. public:
  18.  
  19.     String(const char * s = (const char *)0)
  20.     {
  21.         str = (s? len = strlen(s), strdup(s)
  22.             : (char *)(len = 0));
  23.     }
  24.     String(const String& s)
  25.     {
  26.         str = (((len = s.len) != 0)?
  27.             strdup(s.str) : (char *)0);
  28.     }
  29.     ~String() { delete str; }
  30.     String& operator=(const String& s)
  31.     {
  32.         delete str;
  33.         str = (((len = s.len) != 0)?
  34.             strdup(s.str) : (char *)0);
  35.         return *this;
  36.     }
  37.     inline int operator==(const String& cs) const
  38.         { return !cmp(cs); }
  39.     inline int operator> (const String& cs) const
  40.         { return (cmp(cs) > 0); }
  41.     operator const char *()  { return str; }
  42.     friend ostream& operator<<
  43.         (ostream& os, String& s)
  44.     { return os << &s.str; }
  45.     friend istream& operator>>
  46.         (istream& is, String& s)
  47.     {
  48.         is >> &s.str;
  49.         s.len = (s.str? strlen(s.str) : 0);
  50.         return is;
  51.     }
  52. };
  53.  
  54. int String::cmp(const String& cs) const
  55. {
  56.     if (!str)
  57.         if (!cs.str)
  58.             return 0;
  59.         else
  60.             return -1;
  61.     else
  62.         if (!cs.str)
  63.             return 1;
  64.         else
  65.             return strcmp(str,cs.str);
  66. }
  67.  
  68.  
  69. #ifdef CL_NO_TEMPLATES
  70.     #define    ITEM     String
  71.     #define    CL_WELL_ENDOWED
  72.     #define    CL       CL_String
  73.     #include  "cl.hf"
  74. #else
  75.     CL_WELL_ENDOWED(String)
  76.     #define    CL_String CL<String>
  77. #endif
  78.  
  79. #define StrFile "strings.tmp"
  80.  
  81. main()
  82. {
  83.     CL_String cS(CL_ANDS);
  84.     String s("can be");
  85.     cS.insNew(&s);
  86.     cS.ins(new String("tamed!"));
  87.     cS.insQ(new String("Now strings"));
  88.     cS.sort();
  89.     cS.save(StrFile);
  90.     cS.allClr();
  91.     cS.load(StrFile);
  92.     while (cS.popDelAsg(&s))
  93.         cout << (const char *)s << endl;
  94.     return 0;
  95. }
  96.