home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 May / PCFMay2001.iso / Xenon / C++ / FreeCommandLineTools.exe / Include / chstring.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  10.2 KB  |  260 lines

  1. #pragma option push -b -a8 -pc -A- /*P_O_Push*/
  2. //***************************************************************************
  3. //
  4. //  Copyright (c) 1997-1999 Microsoft Corporation
  5. //
  6. //  CHSTRING.h
  7. //
  8. //  Purpose: Utility library version of MFC CString
  9. //
  10. //***************************************************************************
  11.  
  12. #if _MSC_VER > 1000
  13. #pragma once
  14. #endif
  15.  
  16. #ifndef _CHSTRING_H
  17. #define _CHSTRING_H
  18.  
  19. /////////////////////////////////////////////////////////////////////////////
  20. #include <windows.h>
  21. #include <limits.h>
  22. #include <tchar.h>
  23. #include <polarity.h>
  24. #pragma warning( disable : 4290 ) // Ignore 'C++ Exception Specification ignored'
  25. #include <ProvExce.h>
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28.  
  29. struct _DOUBLE  { BYTE doubleBits[sizeof(double)]; };
  30.  
  31. void POLARITY WINAPI SetCHStringResourceHandle(HINSTANCE handle);
  32.  
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CHString formatting
  35. /////////////////////////////////////////////////////////////////////////////
  36. #define TCHAR_ARG   WCHAR
  37. #define WCHAR_ARG   WCHAR
  38. #define CHAR_ARG    char
  39.  
  40. #if defined(_68K_) || defined(_X86_)
  41.     #define DOUBLE_ARG  _DOUBLE
  42. #else
  43.     #define DOUBLE_ARG  double
  44. #endif
  45.  
  46. struct CHStringData
  47. {
  48.     long nRefs;
  49.     int nDataLength;
  50.     int nAllocLength;
  51.  
  52.     WCHAR* data()
  53.     {
  54.         return (WCHAR*)(this+1); 
  55.     }
  56. };
  57.  
  58. /////////////////////////////////////////////////////////////////////////////
  59. class POLARITY CHString
  60. {
  61.     protected:
  62.  
  63.         LPWSTR m_pchData;               // pointer to ref counted string data
  64.  
  65.     protected:
  66.  
  67.                                         // implementation helpers
  68.  
  69.         CHStringData* GetData() const;  // returns data pointer
  70.         void Init();
  71.         void AllocCopy(CHString& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const throw ( CHeap_Exception ) ;
  72.         void AllocBuffer(int nLen) throw ( CHeap_Exception ) ;
  73.         void AssignCopy(int nSrcLen, LPCWSTR lpszSrcData) throw ( CHeap_Exception ) ;
  74.         void ConcatCopy(int nSrc1Len, LPCWSTR lpszSrc1Data, int nSrc2Len, LPCWSTR lpszSrc2Data) throw ( CHeap_Exception ) ;
  75.         void ConcatInPlace(int nSrcLen, LPCWSTR lpszSrcData);
  76.         void CopyBeforeWrite() throw ( CHeap_Exception ) ;
  77.         void AllocBeforeWrite(int nLen) throw ( CHeap_Exception ) ;
  78.         void Release();
  79.         static void WINAPI Release(CHStringData* pData);
  80.         static inline int WINAPI SafeStrlen(LPCWSTR lpsz)    { return (lpsz == NULL) ? 0 : wcslen(lpsz); }
  81.  
  82.         // Helper function used to load resource into lpszBuf buffer.
  83.         int LoadStringW(UINT nID, LPWSTR lpszBuf, UINT nMaxBuf) throw ( CHeap_Exception ) ;
  84.  
  85.     public:
  86.  
  87. // Constructors/Destruction
  88.  
  89.         CHString();
  90.         CHString(const CHString& stringSrc);
  91.         CHString(WCHAR ch, int nRepeat = 1) throw ( CHeap_Exception ) ;
  92.         CHString(LPCSTR lpsz) throw ( CHeap_Exception ) ;
  93.         CHString(LPCWSTR lpsz) throw ( CHeap_Exception ) ;
  94.         CHString(LPCWSTR lpch, int nLength) throw ( CHeap_Exception ) ;
  95.         inline CHString(const unsigned char* lpsz)    { Init(); *this = (LPCSTR)lpsz; }
  96.  
  97.         ~CHString();
  98.  
  99. // Functions
  100.  
  101.         void SetAt(int nIndex, WCHAR ch) throw ( CHeap_Exception ) ;
  102.         void Empty();    
  103.  
  104.         // inlines
  105.  
  106.         inline int GetLength() const { return GetData()->nDataLength; }
  107.         inline BOOL IsEmpty() const  { return GetData()->nDataLength == 0; }
  108.  
  109. #if (!defined DEBUG && !defined _DEBUG)
  110.         inline WCHAR GetAt(int nIndex) const{ return m_pchData[nIndex]; }
  111.         inline WCHAR operator[](int nIndex) const{    return m_pchData[nIndex]; }
  112. #else
  113.         WCHAR GetAt(int nIndex) const;
  114.         WCHAR operator[](int nIndex) const;
  115. #endif
  116.         inline operator LPCWSTR() const     { return m_pchData; }
  117.         inline int GetAllocLength() const        { return GetData()->nAllocLength; }
  118.  
  119. // overloaded assignment
  120.  
  121.         const CHString& operator=(const CHString& stringSrc) throw ( CHeap_Exception ) ;
  122.         const CHString& operator=(WCHAR ch) throw ( CHeap_Exception ) ;
  123.         const CHString& operator=(LPCSTR lpsz) throw ( CHeap_Exception ) ;
  124.         const CHString& operator=(LPCWSTR lpsz) throw ( CHeap_Exception ) ;
  125.         inline const CHString& operator=(const unsigned char* lpsz) throw ( CHeap_Exception ) { *this = (LPCSTR)lpsz; return *this; }
  126.         inline const CHString& operator=(CHString *p) throw ( CHeap_Exception ) { *this = *p; return *this; }
  127.         inline const CHString& operator=(char ch) throw ( CHeap_Exception ) { *this = (WCHAR)ch; return *this; }        
  128.         
  129.         inline const CHString& CHString::operator+=(char ch) throw ( CHeap_Exception ) { *this += (WCHAR)ch; return *this; }
  130.         friend inline CHString  operator+(const CHString& string, char ch) throw ( CHeap_Exception ) { return string + (WCHAR)ch; }
  131.         friend inline CHString  operator+(char ch, const CHString& string) throw ( CHeap_Exception ) { return (WCHAR)ch + string; }
  132.  
  133.         const CHString& operator+=(const CHString& string) throw ( CHeap_Exception ) ;
  134.         const CHString& operator+=(WCHAR ch) throw ( CHeap_Exception ) ;
  135.         const CHString& operator+=(LPCWSTR lpsz) throw ( CHeap_Exception ) ;
  136.  
  137.         friend CHString POLARITY WINAPI operator+(const CHString& string1,  const CHString& string2) throw ( CHeap_Exception ) ;
  138.         friend CHString POLARITY WINAPI operator+(const CHString& string, WCHAR ch) throw ( CHeap_Exception ) ;
  139.         friend CHString POLARITY WINAPI operator+(WCHAR ch, const CHString& string) throw ( CHeap_Exception ) ;
  140.         friend CHString POLARITY WINAPI operator+(const CHString& string, LPCWSTR lpsz) throw ( CHeap_Exception ) ;
  141.         friend CHString POLARITY WINAPI operator+(LPCWSTR lpsz, const CHString& string) throw ( CHeap_Exception ) ;
  142.  
  143. // string comparison
  144.  
  145.         int Compare(LPCWSTR lpsz) const;
  146.  
  147.         inline int CompareNoCase(LPCWSTR lpsz) const
  148.         {
  149.             // ignore case
  150.  
  151.             return _wcsicmp(m_pchData, lpsz); 
  152.  
  153.         }   // MBCS/Unicode aware
  154.  
  155.         inline int Collate(LPCWSTR lpsz) const
  156.         {  
  157.             // NLS aware
  158.             // CHString::Collate is often slower than Compare but is MBSC/Unicode
  159.             // aware as well as locale-sensitive with respect to sort order.
  160.  
  161.             return wcscoll(m_pchData, lpsz); 
  162.  
  163.         }   // locale sensitive
  164.  
  165. // Load string from resource file.
  166.  
  167.         BOOL LoadStringW(UINT nID) throw ( CHeap_Exception ) ;
  168.  
  169. // Access to string implementation buffer as "C" character array
  170.  
  171.         LPWSTR GetBuffer(int nMinBufLength) throw ( CHeap_Exception ) ;
  172.         void ReleaseBuffer(int nNewLength = -1) throw ( CHeap_Exception ) ;
  173.         LPWSTR GetBufferSetLength(int nNewLength) throw ( CHeap_Exception ) ;
  174.         void FreeExtra() throw ( CHeap_Exception ) ;
  175.  
  176. // Use LockBuffer/UnlockBuffer to turn refcounting off
  177.  
  178.         LPWSTR LockBuffer() ;
  179.         void UnlockBuffer();
  180.  
  181. // searching (return starting index, or -1 if not found)
  182. // look for a single character match
  183.  
  184.         int Find(WCHAR ch) const;               // like "C" strchr
  185.         int FindOneOf(LPCWSTR lpszCharSet) const;
  186.         int ReverseFind(WCHAR ch) const;
  187.  
  188. // look for a specific sub-string
  189.  
  190.         int Find(LPCWSTR lpszSub) const;        // like "C" strstr
  191.  
  192. // upper/lower/reverse conversion
  193.  
  194.         void MakeUpper() throw ( CHeap_Exception ) ;
  195.         void MakeLower() throw ( CHeap_Exception ) ;
  196.         void MakeReverse() throw ( CHeap_Exception ) ;
  197.  
  198. // simple sub-string extraction
  199.  
  200.         CHString Mid(int nFirst, int nCount) const throw ( CHeap_Exception ) ;
  201.         CHString Mid(int nFirst) const throw ( CHeap_Exception ) ;
  202.         CHString Left(int nCount) const throw ( CHeap_Exception ) ;
  203.         CHString Right(int nCount) const throw ( CHeap_Exception ) ;
  204.  
  205.         CHString SpanIncluding(LPCWSTR lpszCharSet) const throw ( CHeap_Exception ) ;
  206.         CHString SpanExcluding(LPCWSTR lpszCharSet) const throw ( CHeap_Exception ) ;
  207.  
  208. // trimming whitespace (either side)
  209.  
  210.         void TrimRight() throw ( CHeap_Exception ) ;
  211.         void TrimLeft() throw ( CHeap_Exception ) ;
  212.     
  213. // printf-like formatting using passed string
  214.         void __cdecl Format(LPCWSTR lpszFormat, ...) throw ( CHeap_Exception ) ;
  215.         void FormatV(LPCWSTR lpszFormat, va_list argList);
  216.  
  217. // printf-like formatting using referenced string resource
  218.         void __cdecl Format(UINT nFormatID, ...) throw ( CHeap_Exception ) ;
  219.  
  220. // format using FormatMessage API on passed string
  221.         // Warning: if you pass string inserts to this function, they must
  222.         // be LPCSTRs on Win9x and LPCWSTRs on NT.
  223.         void __cdecl FormatMessageW(LPCWSTR lpszFormat, ...) throw ( CHeap_Exception ) ;
  224.  
  225. // format using FormatMessage API on referenced string resource
  226.         // Warning: if you pass string inserts to this function, they must
  227.         // be LPCSTRs on Win9x and LPCWSTRs on NT.
  228.         void __cdecl FormatMessageW(UINT nFormatID, ...) throw ( CHeap_Exception ) ;
  229.  
  230.  
  231. #ifndef _NO_BSTR_SUPPORT
  232.  
  233.         // OLE BSTR support (use for OLE automation)
  234.  
  235.         BSTR AllocSysString() const throw ( CHeap_Exception ) ;
  236. #endif
  237.  
  238. };
  239.  
  240. inline BOOL operator==(const CHString& s1, const CHString& s2)  { return s1.Compare(s2) == 0; }
  241. inline BOOL operator==(const CHString& s1, LPCWSTR s2)          { return s1.Compare(s2) == 0; }
  242.  
  243. inline BOOL operator!=(const CHString& s1, const CHString& s2)    { return s1.Compare(s2) != 0; }
  244. inline BOOL operator!=(const CHString& s1, LPCWSTR s2)          { return s1.Compare(s2) != 0; }
  245.  
  246. inline BOOL operator<(const CHString& s1, const CHString& s2)   { return s1.Compare(s2) < 0; }
  247. inline BOOL operator<(const CHString& s1, LPCWSTR s2)           { return s1.Compare(s2) < 0; }
  248.  
  249. inline BOOL operator>(const CHString& s1, const CHString& s2)    { return s1.Compare(s2) > 0; }
  250. inline BOOL operator>(const CHString& s1, LPCWSTR s2)           { return s1.Compare(s2) > 0; }
  251.  
  252. inline BOOL operator<=(const CHString& s1, const CHString& s2)    { return s1.Compare(s2) <= 0; }
  253. inline BOOL operator<=(const CHString& s1, LPCWSTR s2)          { return s1.Compare(s2) <= 0; }
  254.  
  255. inline BOOL operator>=(const CHString& s1, const CHString& s2)    { return s1.Compare(s2) >= 0; }
  256. inline BOOL operator>=(const CHString& s1, LPCWSTR s2)          { return s1.Compare(s2) >= 0; }
  257.  
  258. #endif
  259. #pragma option pop /*P_O_Pop*/
  260.