home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 58 / pcpp58a.iso / extras / quake 3 source / Q3A_ToolSource.exe / Main / str.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-02  |  3.9 KB  |  193 lines

  1. #ifndef __STR__
  2. #define __STR__
  3. //
  4. // class Str
  5. // loose replacement for CString from MFC
  6. //
  7. //#include "cmdlib.h"
  8. #include <string.h>
  9.  
  10. char* __StrDup(char* pStr);
  11. char* __StrDup(const char* pStr);
  12.  
  13.  
  14.  
  15. static char *g_pStrWork = NULL;
  16.  
  17. class Str
  18. {
  19. protected:
  20.   bool m_bIgnoreCase;
  21.   char *m_pStr;
  22.  
  23. public:
  24.   Str()
  25.   {
  26.     m_bIgnoreCase = true;
  27.     m_pStr = NULL;
  28.   }
  29.  
  30.   Str(char *p)
  31.   {
  32.     m_bIgnoreCase = true;
  33.     m_pStr = __StrDup(p);
  34.   }
  35.  
  36.   Str(const char *p)
  37.   {
  38.     m_bIgnoreCase = true;
  39.     m_pStr = __StrDup(p);
  40.   }
  41.  
  42.   void Deallocate()
  43.   {
  44.     delete []m_pStr;
  45.     m_pStr = NULL;
  46.   }
  47.  
  48.   void Allocate(int n)
  49.   {
  50.     Deallocate();
  51.     m_pStr = new char[n];
  52.   }
  53.  
  54.   const char* GetBuffer()
  55.   {
  56.     return m_pStr;
  57.   }
  58.  
  59.   void MakeEmpty()
  60.   {
  61.     Deallocate();
  62.     m_pStr = __StrDup("");
  63.   }
  64.  
  65.   ~Str()
  66.   {
  67.     Deallocate();
  68.     delete []g_pStrWork;
  69.     g_pStrWork = NULL;
  70.   }
  71.  
  72.   void MakeLower()
  73.   {
  74.     if (m_pStr)
  75.     {
  76.       strlwr(m_pStr);
  77.     }
  78.   }
  79.  
  80.   int Find(const char *p)
  81.   {
  82.     char *pf = strstr(m_pStr, p);
  83.     return (pf) ? (pf - m_pStr) : -1;
  84.   }
  85.  
  86.   int GetLength()
  87.   {
  88.     return (m_pStr) ? strlen(m_pStr) : 0;
  89.   }
  90.  
  91.   const char* Left(int n)
  92.   {
  93.     delete []g_pStrWork;
  94.     if (n > 0)
  95.     {
  96.       g_pStrWork = new char[n+1];
  97.       strncpy(g_pStrWork, m_pStr, n);
  98.     }
  99.     else
  100.     {
  101.       g_pStrWork = "";
  102.       g_pStrWork = new char[1];
  103.       g_pStrWork[0] = '\0';
  104.     }
  105.     return g_pStrWork;
  106.   }
  107.  
  108.   const char* Right(int n)
  109.   {
  110.     delete []g_pStrWork;
  111.     if (n > 0)
  112.     {
  113.       g_pStrWork = new char[n+1];
  114.       int nStart = GetLength() - n;
  115.       strncpy(g_pStrWork, &m_pStr[nStart], n);
  116.       g_pStrWork[n] = '\0';
  117.     }
  118.     else
  119.     {
  120.       g_pStrWork = new char[1];
  121.       g_pStrWork[0] = '\0';
  122.     }
  123.     return g_pStrWork;
  124.   }
  125.  
  126.  
  127.   char& operator *() { return *m_pStr; }
  128.   char& operator *() const { return *const_cast<Str*>(this)->m_pStr; }
  129.   operator void*() { return m_pStr; }
  130.   operator char*() { return m_pStr; }
  131.   operator const char*(){ return reinterpret_cast<const char*>(m_pStr); }
  132.   operator unsigned char*() { return reinterpret_cast<unsigned char*>(m_pStr); }
  133.   operator const unsigned char*() { return reinterpret_cast<const unsigned char*>(m_pStr); }
  134.   Str& operator =(const Str& rhs)
  135.   {
  136.     if (&rhs != this)
  137.     {
  138.       delete[] m_pStr;
  139.       m_pStr = __StrDup(rhs.m_pStr);
  140.     }
  141.     return *this;
  142.   }
  143.   
  144.   Str& operator =(const char* pStr)
  145.   {
  146.     if (m_pStr != pStr)
  147.     {
  148.       delete[] m_pStr;
  149.       m_pStr = __StrDup(pStr);
  150.     }
  151.     return *this;
  152.   }
  153.  
  154.   Str& operator +=(const char *pStr)
  155.   {
  156.     if (pStr)
  157.     {
  158.       if (m_pStr)
  159.       {
  160.         char *p = new char[strlen(m_pStr) + strlen(pStr) + 1];
  161.         strcpy(p, m_pStr);
  162.         strcat(p, pStr);
  163.         delete m_pStr;
  164.         m_pStr = p;
  165.       }
  166.       else
  167.       {
  168.         m_pStr = __StrDup(pStr);
  169.       }
  170.     }
  171.     return *this;
  172.   }
  173.   
  174.   Str& operator +=(const char c)
  175.   {
  176.     return operator+=(&c);
  177.   }
  178.  
  179.  
  180.   bool operator ==(const Str& rhs) const { return (m_bIgnoreCase) ? stricmp(m_pStr, rhs.m_pStr) == 0 : strcmp(m_pStr, rhs.m_pStr) == 0; }
  181.   bool operator ==(char* pStr) const { return (m_bIgnoreCase) ? stricmp(m_pStr, pStr) == 0 : strcmp(m_pStr, pStr) == 0; }
  182.   bool operator ==(const char* pStr) const { return (m_bIgnoreCase) ? stricmp(m_pStr, pStr) == 0 : strcmp(m_pStr, pStr) == 0; }
  183.   bool operator !=(Str& rhs) const { return (m_bIgnoreCase) ? stricmp(m_pStr, rhs.m_pStr) != 0 : strcmp(m_pStr, rhs.m_pStr) != 0; }
  184.   bool operator !=(char* pStr) const { return (m_bIgnoreCase) ? stricmp(m_pStr, pStr) != 0 : strcmp(m_pStr, pStr) != 0; }
  185.   bool operator !=(const char* pStr) const { return (m_bIgnoreCase) ? stricmp(m_pStr, pStr) != 0 : strcmp(m_pStr, pStr) != 0; }
  186.   char& operator [](int nIndex) { return m_pStr[nIndex]; }
  187.   char& operator [](int nIndex) const { return m_pStr[nIndex]; }
  188.      
  189. };
  190.  
  191.  
  192.  
  193. #endif