home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 14 / MA_Cover_14.iso / source / c / pegase_src / stringclass.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-07  |  6.8 KB  |  200 lines

  1. #ifndef _STRING_CLASS_HPP
  2. #define _STRING_CLASS_HPP 1
  3.  
  4. /*
  5. **
  6. ** StringClass.hpp
  7. **
  8. ** (c) 1998 Didier Levet
  9. **
  10. ** String classes
  11. **
  12. ** $Revision: 1.4 $
  13. ** $State: Exp $
  14. ** $Date: 1998/11/23 14:38:42 $
  15. **
  16. ** $Log: StringClass.hpp $
  17. ** Revision 1.4  1998/11/23 14:38:42  kakace
  18. ** Reworked CString and CStrArray classes
  19. **
  20. ** Revision 1.3  1998/11/22 14:57:35  kakace
  21. ** Class CStrArray now uses class CString and seems to work fine
  22. **
  23. ** Revision 1.2  1998/11/21 20:58:09  kakace
  24. ** Class CString seems to be ok now.
  25. **
  26. ** Revision 1.1  1998/11/11 16:34:56  kakace
  27. ** Initial revision
  28. **
  29. **
  30. */
  31.  
  32.  
  33. /// Includes
  34.  
  35. #ifndef  _INCLUDE_FSTREAM_H
  36. #include <fstream.h>
  37. #endif
  38.  
  39. #ifndef  EXEC_TYPES_H
  40. #include <exec/types.h>
  41. #endif
  42.  
  43. ///
  44.  
  45.  
  46. #ifndef STRING
  47. typedef const char *STRING;
  48. #endif
  49.  
  50.  
  51. //----------------------------------------------------------------------------------------------------
  52. //========================================== Class CString ===========================================
  53. //----------------------------------------------------------------------------------------------------
  54.  
  55. class CString
  56. {
  57.     public:
  58.  
  59.         enum ST_EMPTY  {EMPTY};
  60.         enum ST_UNUSED {UNUSED};
  61.  
  62.         // Constructors and destructor.
  63.  
  64.         CString()                                   {release();}
  65.         CString(STRING str, ULONG minLength = 0);
  66.         CString(CString& str)                       {adopt(str);}
  67.         ~CString()                                  {freebuff();}
  68.  
  69.         // Accessors.
  70.  
  71.         ULONG length() const                        {return iLength;}
  72.         ULONG buffsize() const                      {return iBuffLen;}
  73.         void  update()                              {iLength = stringlen(pcString);}
  74.         void  release()                             {pcString = NULL; iLength = iBuffLen = 0;}
  75.         ULONG extend(ULONG newSize);
  76.  
  77.         STRING   string() const                     {return pcString;}
  78.         CString *clone(const CString& str)          {duplicate(str.pcString, str.iLength); return this;}
  79.  
  80.         void    adopt(CString &str)
  81.                 {
  82.                     pcString = str.pcString;
  83.                     iLength  = str.iLength;
  84.                     iBuffLen = str.iBuffLen;
  85.                     str.release();
  86.                 }
  87.  
  88.         // Operators.
  89.  
  90.         operator char *() const                     {return pcString;}
  91.         int operator !() const                      {return (pcString == NULL);}
  92.  
  93.         CString& operator =(const CString& str)
  94.                  {
  95.                      if (&str != this)
  96.                          duplicate(str.pcString, str.iLength);
  97.                      return *this;
  98.                  }
  99.  
  100.         CString& operator = (STRING str)            {duplicate(str, stringlen(str)); return *this;}
  101.         CString& operator +=(const CString& str)    {concat(str.pcString, str.iLength); return *this;}
  102.         CString& operator +=(STRING str)            {concat(str, stringlen(str)); return *this;}
  103.         CString& operator + (const CString& str)    {return *this += (const CString& ) str;}
  104.  
  105.         int operator > (const CString& str) const;
  106.         int operator !=(const CString& str) const;
  107.  
  108.         int operator !=(ST_EMPTY f) const           {return (iLength > 0);}
  109.         int operator !=(ST_UNUSED f) const          {return (pcString != NULL);}
  110.  
  111.         int operator >=(const CString& str) const   {return !((const CString&) str > (const CString&) *this);}
  112.         int operator < (const CString& str) const   {return  ((const CString&) str > (const CString&) *this);}
  113.         int operator <=(const CString& str) const   {return !((const CString&) *this > (const CString&) str);}
  114.         int operator ==(const CString& str) const   {return !((const CString&) *this != (const CString&) str);}
  115.  
  116.         int operator ==(ST_EMPTY f) const           {return (iLength == 0);}
  117.         int operator ==(ST_UNUSED f) const          {return (pcString == NULL);}
  118.  
  119.         friend ostream& operator << (ostream& os, const CString& str) {return os << (str.string() ? str.string() : "\0");}
  120.  
  121.     protected:
  122.  
  123.         char   *pcString;           // Pointer to the string buffer. NULL == No buffer allocated.
  124.         ULONG   iLength;            // String's length. ZERO = No string held or empty string.
  125.         ULONG   iBuffLen;           // Buffer's length. ZERO = No buffer allocated.
  126.  
  127.         // Protected function members.
  128.  
  129.         static ULONG stringlen(const char s[]);
  130.         static ULONG stringcopy(STRING source, char *dest);
  131.  
  132.         void  newstring(STRING str, ULONG size);
  133.         void  concat(STRING str, ULONG len);
  134.         void  duplicate(STRING str, ULONG len);
  135.         void  freebuff()        { delete [] pcString; }
  136. };
  137.  
  138.  
  139. //----------------------------------------------------------------------------------------------------
  140. //========================================= Class CStrArray ==========================================
  141. //----------------------------------------------------------------------------------------------------
  142. //
  143. //  This class is used to hold string arrays on an exclusive manner. Once an object is created, it
  144. // can't be referenced several times. This helps to avoid multiple buffer deallocations.
  145.  
  146.  
  147. class CStrArray
  148. {
  149.     public:
  150.  
  151.         void release()          {voStringArray = NULL; iNumEntries = iCurrentIndex = 0;}
  152.  
  153.         CStrArray()             {release();}
  154.         ~CStrArray()            {freebuff();}
  155.  
  156.         // Copy constructor and assignement operator.
  157.  
  158.         CStrArray(CStrArray& array) {AdoptArray(array);}
  159.  
  160.         CStrArray& operator =(CStrArray& array);
  161.         CStrArray& operator =(STRING *array);
  162.         CString&   operator [] (int i) const    { return voStringArray[i]; }
  163.  
  164.         // Misc.
  165.  
  166.         LONG     CurrentIndex() const           {return iCurrentIndex;}
  167.         int      Add(STRING str)                {return adopt(str);}
  168.         int      adopt(CString& str);
  169.         int      extend(ULONG size = 10);
  170.  
  171.         // Conditionnal operators.
  172.  
  173.         operator int() const                    {return (int) voStringArray;}
  174.         int operator !() const                  {return !((int) voStringArray);}
  175.  
  176.     private:
  177.  
  178.         CString *voStringArray;             // Array pointer.
  179.         ULONG    iNumEntries;               // Array size.
  180.         LONG     iCurrentIndex;             // Index of the next entry.
  181.  
  182.         void    cleanup()                   {freebuff(); release();}
  183.         void    freebuff()                  { delete [] voStringArray; }
  184.         void    newstring(CString& str)     { voStringArray[iCurrentIndex++].adopt(str); }
  185.  
  186.         void    AdoptArray(CStrArray& array)
  187.                 {
  188.                     voStringArray = array.voStringArray;
  189.                     iNumEntries   = array.iNumEntries;
  190.                     iCurrentIndex = array.iCurrentIndex;
  191.                     array.release();
  192.                 }
  193. };
  194.  
  195.  
  196. //----------------------------------------------------------------------------------------------------
  197.  
  198. #endif  // _STRING_CLASS_HPP
  199.  
  200.