home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / OSL_INC.PAK / USTRING.H < prev   
C/C++ Source or Header  |  1995-08-29  |  7KB  |  219 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectSupport
  3. // (C) Copyright 1994 by Borland International, All Rights Reserved
  4. //
  5. //  TString, TUstring - localized name support
  6. //  TRegItem, TRegList - associative array of localizable string parameters
  7. //----------------------------------------------------------------------------
  8. #if !defined(OSL_USTRING_H)
  9. #define OSL_USTRING_H
  10.  
  11. #if !defined(OSL_DEFS_H)
  12. # if !defined(INC_OLE2)
  13. #   define INC_OLE2        // Locale needs Ole2 when 16bit, let osl/defs get it
  14. # endif
  15. # include <osl/defs.h>
  16. #elif !defined(__OLE2_H) && !defined(_OLE2_H_)
  17. # if defined(BI_PLAT_WIN16)
  18. #   include <ole2.h>       // Get Win16 Ole2 headers now if we missed above
  19. #   if !defined(__DISPATCH_H)
  20. #     include <dispatch.h>
  21. #   endif
  22. #   if !defined(__OLENLS_H)
  23. #     include <olenls.h>
  24. #   endif
  25. # else
  26. #   error Win32 Ole2 headers cannot be included after Windows.h, define INC_OLE2 first
  27. # endif
  28. #endif
  29. #if !defined(BI_PLAT_MSW)
  30. # error Locale classes are only supported under MSW
  31. #endif
  32. #if !defined(OSL_LOCALE_H)
  33. # include <osl/locale.h>
  34. #endif
  35. #if !defined(__COMMDLG_H)
  36. # include <commdlg.h>   // only for OFN_xxx defines
  37. #endif
  38. #if !defined(__CSTRING_H)
  39. # include <cstring.h>
  40. #endif
  41.  
  42. //----------------------------------------------------------------------------
  43. // OLECHAR & BSTR is char under Win16, and wchar_t under Win32
  44. //
  45. #if !defined(BI_PLAT_WIN16)
  46. # define BI_OLECHAR_WIDE    // OLECHAR is wide (wchar_t)
  47. # define BI_HAS_WCHAR       // platform has wide char functions
  48. #endif
  49.  
  50. //----------------------------------------------------------------------------
  51. // TUString class - privately used by TString to manage string pointers
  52. //   This is a reference counted union of various string representatons
  53. //   Constructors/destructors are private to enforce reference count model
  54. //   Create functions are used to facilitate rapid allocation schemes
  55. //   Null pointers are never stored; instead a static null object is ref'd
  56. //
  57. class TUString {
  58.   public:
  59.     static TUString* Create(const char far* str);
  60.     static TUString* Create(char* str);
  61. #if defined(BI_HAS_WCHAR)
  62.     static TUString* Create(const wchar_t* str);
  63.     static TUString* Create(wchar_t* str);
  64. #endif
  65.     static TUString* Create(BSTR str, bool loan, TLangId lang = 0);
  66.     static TUString* Create(const string& str);
  67.  
  68.     TUString* Assign(const TUString& s);
  69.     TUString* Assign(const string& s);
  70.     TUString* Assign(const char far* s);
  71.     TUString* Assign(char* s);
  72. #if defined(BI_HAS_WCHAR)
  73.     TUString* Assign(const wchar_t* s);
  74.     TUString* Assign(wchar_t* s);
  75. #endif
  76.     TUString* Assign(BSTR str, TLangId lang);
  77.  
  78.     operator const char far*() const;
  79.     operator char*();
  80. #if defined(BI_HAS_WCHAR)
  81.     operator const wchar_t*() const;
  82.     operator wchar_t*();
  83. #endif
  84.  
  85.     TUString& operator ++();    // preincrement operator only
  86.     TUString& operator --();    // predecrement operator only
  87.  
  88.     int Length() const;         // return appropriate string length
  89.     bool IsNull() const;        // is the string a null string?
  90.  
  91.     TLangId Lang;
  92.     void RevokeBstr(BSTR s);    // used to restore if Created with loan==true
  93.     void ReleaseBstr(BSTR s);   // used to unhook  if Created with loan==true
  94.  
  95. #if defined(BI_HAS_WCHAR)
  96.     static wchar_t* ConvertAtoW(const char* src, size_t len = -1);
  97.     static char* ConvertWtoA(const wchar_t* src, size_t len = -1);
  98. #endif
  99.  
  100.   private:
  101.     TUString(const char far& str);
  102.     TUString(char& str);
  103. #if defined(BI_HAS_WCHAR)
  104.     TUString(const wchar_t& str);
  105.     TUString(wchar_t& str);
  106. #endif
  107.     TUString(BSTR str, bool loan, TLangId lang);
  108.     TUString(const string& str);
  109.  
  110.    ~TUString() {Free();}
  111.     void Free();
  112.  
  113.     char*    ChangeToCopy();
  114. #if defined(BI_HAS_WCHAR)
  115.     wchar_t* ChangeToWCopy();
  116. #endif
  117.  
  118.     enum TKind {
  119.       isNull=0, isConst, isCopy, isWConst, isWCopy, isBstr, isExtBstr, isString
  120.     } Kind;
  121.     int RefCnt;
  122.     union {
  123.       const char far* Const;  // Passed-in string, NOT owned here, read-only
  124.       char* Copy;             // Local copy, must be deleted, read-write
  125. #if defined(BI_HAS_WCHAR)
  126.       const wchar_t* WConst;  // Unicode version of Const (Win32)
  127.       wchar_t* WCopy;         // Unicode version of Copy (Win32)
  128. #endif
  129.       BSTR Bstr;              // Copy of pointer, owned here
  130.       TStringRef* String;     // Placeholder for string:: object
  131.     };
  132.  
  133.     static TUString Null;     // null TString references this
  134.     TUString() : Kind(isNull),Const(0),RefCnt(1),Lang(0) {} // for Null object
  135. };
  136.  
  137. //----------------------------------------------------------------------------
  138. // TString class - reference to reference counted string object TUString
  139. //   Lightweight reference object consisting of a pointer to actual object
  140. //   Facilitates copying and assignment with minimal string reallocations
  141. //
  142. class TString {
  143.   public:
  144.     TString(const char far* s = 0) : S(TUString::Create(s)) {}
  145. #if defined(BI_HAS_WCHAR)
  146.     TString(const wchar_t* s)      : S(TUString::Create(s)) {}
  147. #endif
  148. //!CQ no non-const? UString always assumes [W]Const union member
  149.     TString(BSTR s, bool loan)     : S(TUString::Create(s, loan)) {}
  150.     TString(const string& s)       : S(TUString::Create(s)) {}
  151.     TString(TUString* s)           : S(s) {}
  152.     TString(const TString& src)    : S(src.S) {++*S;}
  153.  
  154.    ~TString() {--*S;}
  155.  
  156.     int Length() const {return S->Length();}
  157.     bool IsNull() const {return S->IsNull();}
  158.  
  159.     TString& operator =(const TString& s)  {S = S->Assign(*s.S); return *this;}
  160.     TString& operator =(const string& s)   {S = S->Assign(s); return *this;}
  161.     TString& operator =(const char far* s) {S = S->Assign(s); return *this;}
  162.     TString& operator =(char* s)           {S = S->Assign(s); return *this;}
  163. #if defined(BI_HAS_WCHAR)
  164.     TString& operator =(const wchar_t* s)  {S = S->Assign(s); return *this;}
  165.     TString& operator =(wchar_t* s)        {S = S->Assign(s); return *this;}
  166. #endif
  167.  
  168.     operator const char far*() const {return S->operator const char far*();}
  169.     operator char*()           {return S->operator char*();}
  170. #if defined(BI_HAS_WCHAR)
  171.     operator const wchar_t*() const  {return S->operator const wchar_t*();}
  172.     operator wchar_t*()        {return S->operator wchar_t*();}
  173. #endif
  174.  
  175.     TLangId GetLangId() {return S->Lang;}
  176.     void    SetLangId(TLangId id) {S->Lang = id;}
  177.  
  178.   protected:
  179.     TUString* S;
  180. };
  181.  
  182. // Provide ANSI to Wide conversion when OLE requires wide chars
  183. // Allocate a unicode BSTR from an ANSI char*
  184. //
  185. #if defined(BI_OLECHAR_WIDE)
  186. # define OleStr(s) TString(s)
  187. # define OleText(s) L##s
  188.   inline BSTR SysAllocString(const char far* str) {
  189.     return ::SysAllocString((const wchar_t*)TString(str));
  190.   }
  191. #else
  192. # define OleStr(s) s
  193. # define OleText(s) s
  194. #endif
  195.  
  196. //----------------------------------------------------------------------------
  197. // Inlines
  198.  
  199. inline TUString& TUString::operator ++()
  200. {
  201.   ++RefCnt;
  202.   return *this;
  203. }
  204.  
  205. inline TUString& TUString::operator --()
  206. {
  207.   if (--RefCnt != 0)
  208.     return *this;
  209.   delete this;
  210.   return Null;
  211. }
  212.  
  213. inline bool TUString::IsNull() const
  214. {
  215.   return Kind == isNull;
  216. }
  217.  
  218. #endif  // OSL_USTRING_H
  219.