home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / CTECHAPP.ZIP / STRINGS.ZIP / STR.HPP < prev   
C/C++ Source or Header  |  1990-03-25  |  4KB  |  146 lines

  1. //  Header:     Str  (Dynamic Strings)
  2. //  Version:    3.10
  3. //
  4. //  Language:   C++ 2.0
  5. //  Environ:    Any
  6. //
  7. //  Purpose:    Provides a general dynamic string class.
  8. //
  9. //  Written by: Scott Robert Ladd
  10.  
  11. #if !defined(__STRING_HPP)
  12. #define __STRING_HPP 1
  13.  
  14. enum StrCompVal  {SC_LESS, SC_EQUAL, SC_GREATER, SC_ERROR};
  15. enum StrCompMode {SM_SENSITIVE, SM_IGNORE};
  16. enum StrError    {SE_ALLOC, SE_TOO_LONG};
  17.  
  18. class String
  19.     {
  20.     private:
  21.         // instance variables
  22.         unsigned int Siz;    // allocated size
  23.         unsigned int Len;    // current length
  24.         char * Txt;          // pointer to text
  25.  
  26.         // class constant
  27.         static unsigned int AllocIncr;
  28.  
  29.         // pointer to exception handler
  30.         static void (*ErrorHandler)(StrError err);
  31.  
  32.         // private method used to shrink string to minimum allocation
  33.         void Shrink();
  34.  
  35.     public:
  36.         // constructor
  37.         String();
  38.         String(const String & str);
  39.         String(const char * cstr);
  40.         String(char fillCh, unsigned int count);
  41.  
  42.         // destructor
  43.         ~String();
  44.  
  45.         // value return methods
  46.         unsigned int Length();
  47.         unsigned int Size();
  48.  
  49.         // Assign an exception handler
  50.         static void SetErrorHandler(void (* userHandler)(StrError));
  51.  
  52.         // create a c-string from String method
  53.         operator const char * ();
  54.  
  55.         // assignment method
  56.         void operator = (const String & str);
  57.  
  58.         // concatenation methods
  59.         friend String operator + (const String & str1, const String & str2);
  60.         void operator += (const String & str);
  61.  
  62.         // comparison methods
  63.         int operator <  (const String & str);
  64.         int operator >  (const String & str);
  65.         int operator <= (const String & str);
  66.         int operator >= (const String & str);
  67.         int operator == (const String & str);
  68.         int operator != (const String & str);
  69.  
  70.         StrCompVal Compare(const String & str, StrCompMode caseChk= SM_IGNORE);
  71.  
  72.         // substring search methods
  73.         int Find(const String & str, unsigned int & pos,
  74.                  StrCompMode caseChk = SM_IGNORE);
  75.  
  76.         // substring deletion method
  77.         void Delete(unsigned int pos, unsigned int count);
  78.  
  79.         // substring insertion methods
  80.         void Insert(unsigned int pos, char ch);
  81.         void Insert(unsigned int pos, const String & str);
  82.  
  83.         // substring retrieval method
  84.         String SubStr(unsigned int start, unsigned int count);
  85.  
  86.         // character retrieval method
  87.         char operator [] (unsigned int pos);
  88.  
  89.         // case-modification methods
  90.         String ToUpper();
  91.         String ToLower();
  92.     };
  93.  
  94. // value return methods
  95. inline unsigned int String::Length()
  96.     {
  97.     return Len;
  98.     }
  99.  
  100. inline unsigned int String::Size()
  101.     {
  102.     return Siz;
  103.     }
  104.  
  105. // comparison methods
  106. inline int String::operator <  (const String & str)
  107.     {
  108.     return (Compare(str) == SC_LESS);
  109.     }
  110.  
  111. inline int String::operator >  (const String & str)
  112.     {
  113.     return (Compare(str) == SC_GREATER);
  114.     }
  115.  
  116. inline int String::operator <= (const String & str)
  117.     {
  118.     return (Compare(str) != SC_GREATER);
  119.     }
  120.  
  121. inline int String::operator >= (const String & str)
  122.     {
  123.     return (Compare(str) != SC_LESS);
  124.     }
  125.  
  126. inline int String::operator == (const String & str)
  127.     {
  128.     return (Compare(str) == SC_EQUAL);
  129.     }
  130.  
  131. inline int String::operator != (const String & str)
  132.     {
  133.     return (Compare(str) != SC_EQUAL);
  134.     }
  135.  
  136. // character retrieval method
  137. inline char String::operator [] (unsigned int pos)
  138.     {
  139.     if (pos >= Len)
  140.         return '\x00';
  141.     else
  142.         return Txt[pos];
  143.     }
  144.  
  145. #endif
  146.