home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 308_01 / string.hpp < prev    next >
C/C++ Source or Header  |  1990-09-20  |  3KB  |  103 lines

  1.  
  2. /************************************************************************
  3.     Title:        C++ Dynamic String Class Header;
  4.     Filename:    String.hpp;
  5.     Date:        9/2/90;
  6.     Version:    1.0;
  7.     Requires:    string.cpp;
  8.     
  9.     Author:        Michael Kelly
  10.                     254 Gold Street
  11.                     Boston, MA 02127
  12.             Copyright 1990;
  13.  
  14.     COPYRIGHT:    This code may not be commercially distributed
  15.             without prior arrangement with the author.  It
  16.             may be used by programmers, without royalty, for
  17.             their personal programs and for "one of a kind"
  18.             or "custom" applications, provided that said
  19.             programmers assume all liability concerning
  20.             same.
  21. ************************************************************************/
  22.  
  23.  
  24. #if !defined(STRING_HPP)
  25. #define STRING_HPP
  26.  
  27. #include <string.h>
  28.  
  29. class String  {
  30.     int   len;
  31.     int   siz;
  32.     char  *str;
  33.  
  34.   public:            // constructors
  35.     String(int str_siz);
  36.     String(String &string);
  37.     String(char *cstr);
  38.                 // destructor
  39.     ~String(void);
  40.  
  41.   friend String& operator + (char *cstr, String &string);
  42.  
  43.     String& operator =     (String &string);
  44.     String& operator =     (char *cstr);
  45.     String& operator +     (String &string);
  46.     String& operator +     (char *cstr);
  47.     String& operator += (String &string);
  48.     String& operator += (char *cstr);
  49.     char& operator []    (int index)
  50.     { 
  51.         if(index < 0)
  52.         return *str;
  53.         else if(index >= siz)
  54.         return *(str + (siz - 1));
  55.         else
  56.         return *(str + index); 
  57.     }
  58.     const char* operator *     (void)
  59.     { return (const char *)str; }
  60.  
  61.   friend int operator <  (char *cstr, String &string);
  62.   friend int operator <= (char *cstr, String &string);
  63.   friend int operator >  (char *cstr, String &string);
  64.   friend int operator >= (char *cstr, String &string);
  65.   friend int operator == (char *cstr, String &string);
  66.   friend int operator != (char *cstr, String &string);
  67.  
  68.     int operator < (String &string)
  69.     { return(strcmp(str, string.str) < 0); }
  70.     int operator < (char *cstr)
  71.     { return(strcmp(str, cstr) < 0); }
  72.     int operator <= (String &string)
  73.     { return(strcmp(str, string.str) < 1); }
  74.     int operator <= (char *cstr)
  75.     { return(strcmp(str, cstr) < 1); }
  76.     int operator > (String &string)
  77.     { return(strcmp(str, string.str) > 0); }
  78.     int operator > (char *cstr)
  79.     { return(strcmp(str, cstr) > 0); }
  80.     int operator >= (String &string)
  81.     { return(strcmp(str, string.str) > -1); }
  82.     int operator >= (char *cstr)
  83.     { return(strcmp(str, cstr) > -1); }
  84.     int operator == (String &string)
  85.     { return !(strcmp(str, string.str)); }
  86.     int operator == (char *cstr)
  87.     { return !(strcmp(str, cstr)); }
  88.     int operator != (String &string)
  89.     { return(strcmp(str, string.str) != 0); }
  90.     int operator != (char *cstr)
  91.     { return(strcmp(str, cstr) != 0); }
  92.     int length(void)
  93.     { return len; }
  94.     int length(int new_len)
  95.     { return (new_len < siz) ? len = new_len : len; }
  96.     int size(void)
  97.     { return siz; }
  98.     int shrink(int new_size);
  99.  
  100. };
  101.  
  102. #endif
  103.