Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cWinString.h
Go to the documentation of this file.
1 //
5 //
6 
7 #ifndef _INC_cWinString_H
8 #define _INC_cWinString_H
9 #ifndef NO_PRAGMA_ONCE
10 #pragma once
11 #endif
12 
13 #include "../GrayLibBase.h"
14 #include "GrayCore/include/StrU.h"
18 #ifdef _WIN32
20 // #include <oaidl.h>
21 #include <oleauto.h>
22 #endif
23 
24 
25 namespace GrayLib
26 {
28 
29 #if defined(_WIN32) && ! defined(_MFC_VER)
30  class cWinString
31  {
36 
37  public:
38  BSTR m_str;
39  public:
40  cWinString()
41  : m_str(nullptr)
42  {
43  }
44  cWinString(const wchar_t* pSrc)
45  {
46  if (pSrc == nullptr)
47  {
48  m_str = nullptr;
49  return;
50  }
51  m_str = ::SysAllocString(pSrc);
52  }
53  cWinString(StrLen_t nLenChars, const wchar_t* pSrc)
54  {
55  if (pSrc == nullptr)
56  {
57  m_str = nullptr;
58  return;
59  }
60  m_str = ::SysAllocStringLen(pSrc, nLenChars);
61  }
62  cWinString(const char* pSrc)
63  {
64  if (pSrc == nullptr)
65  {
66  m_str = nullptr;
67  return;
68  }
69  // DONT use ::SysAllocStringByteLen( pSrc, StrT::Len(pSrc)); does not convert to unicode.
70  StrLen_t iLenChars = StrU::UTF8toUNICODELen(pSrc) + 1; // estimate size first. + null
71  m_str = ::SysAllocStringLen(nullptr, iLenChars);
72  StrU::UTF8toUNICODE(m_str, iLenChars, pSrc);
73  }
74  cWinString(const cWinString& src)
75  {
76  m_str = src.MakeCopy();
77  }
78  ~cWinString()
79  {
80  ::SysFreeString(m_str);
81  }
82 
83  const cWinString& operator=(const cWinString& src)
84  {
85  if (m_str != src.m_str)
86  {
87  ::SysFreeString(m_str);
88  m_str = src.MakeCopy();
89  }
90  return *this;
91  }
92 
93  const cWinString& operator=(const wchar_t* pSrc)
94  {
95  if (pSrc != m_str)
96  {
97  ::SysFreeString(m_str);
98  if (pSrc != nullptr)
99  m_str = ::SysAllocString(pSrc);
100  else
101  m_str = nullptr;
102  }
103  return *this;
104  }
105 
106  StrLen_t Length() const throw()
107  {
110 #if defined (_WIN32)
111  return (m_str == nullptr) ? 0 : ::SysStringLen(m_str);
112 #endif
113  }
114  size_t ByteLength() const throw()
115  {
118 #if defined (_WIN32)
119  return (m_str == nullptr) ? 0 : ::SysStringByteLen(m_str);
120 #endif
121  }
122  StrLen_t get_Length() const throw()
123  {
125  return Length();
126  }
127  const wchar_t* get_CPtr() const
128  {
129  return m_str;
130  }
131  operator BSTR() const throw()
132  {
133  return m_str;
134  }
135  BSTR MakeCopy() const throw()
136  {
138  if (m_str == nullptr)
139  return nullptr;
140  // just do a binary copy.
141  return ::SysAllocStringByteLen((const char*)m_str, ::SysStringByteLen(m_str));
142  }
143  BSTR Copy() const throw()
144  {
145  return MakeCopy();
146  }
147  BSTR* ref_BStr() throw()
148  {
149  // A pointer to the string used as an output argument to a function call.
150  Empty();
151  return &m_str;
152  }
153  BSTR* operator&() throw()
154  {
156  return ref_BStr();
157  }
158  void Attach(BSTR src) throw()
159  {
161  if (m_str != src)
162  {
163  ::SysFreeString(m_str);
164  m_str = src;
165  }
166  }
167  BSTR Detach() throw()
168  {
170  BSTR s = m_str;
171  m_str = nullptr;
172  return s;
173  }
174  void Empty() throw()
175  {
176  ::SysFreeString(m_str);
177  m_str = nullptr;
178  }
179  HRESULT CopyTo(VARIANT* pvarDest) const throw()
180  {
183  HRESULT hRes = E_POINTER;
184  if (pvarDest != nullptr)
185  {
186  pvarDest->vt = VT_BSTR;
187  pvarDest->bstrVal = MakeCopy();
188  if (pvarDest->bstrVal == nullptr && m_str != nullptr)
189  hRes = E_OUTOFMEMORY;
190  else
191  hRes = S_OK;
192  }
193  return hRes;
194  }
195  HRESULT WriteToStream(IStream* pStream) throw()
196  {
197  ASSERT(pStream != nullptr);
198  if (pStream == nullptr)
199  return E_INVALIDARG;
200 
201  ULONG cb;
202  ULONG cbStrLen = ULONG(m_str ? ::SysStringByteLen(m_str) + sizeof(OLECHAR) : 0);
203  HRESULT hRes = pStream->Write((void*)&cbStrLen, sizeof(cbStrLen), &cb);
204  if (FAILED(hRes))
205  return hRes;
206  return cbStrLen ? pStream->Write((void*)m_str, cbStrLen, &cb) : S_OK;
207  }
208  HRESULT ReadFromStream(IStream* pStream) throw()
209  {
210  ASSERT(pStream != nullptr);
211  if (pStream == nullptr)
212  return E_INVALIDARG;
213 
214  ASSERT(m_str == nullptr); // should be empty
215  Empty();
216 
217  ULONG cbStrLen = 0;
218  HRESULT hRes = pStream->Read((void*)&cbStrLen, sizeof(cbStrLen), nullptr);
219  if ((hRes == S_OK) && (cbStrLen != 0))
220  {
221  // subtract size for terminating '\0' which we wrote out
222  // since SysAllocStringByteLen over-allocates for the '\0'
223  m_str = ::SysAllocStringByteLen(nullptr, cbStrLen - sizeof(OLECHAR));
224  if (m_str == nullptr)
225  hRes = E_OUTOFMEMORY;
226  else
227  hRes = pStream->Read((void*)m_str, cbStrLen, nullptr);
228  // If SysAllocStringByteLen or IStream::Read failed, reset seek
229  // pointer to start of BSTR size.
230  if (hRes != S_OK)
231  {
232  LARGE_INTEGER nOffset;
233  nOffset.QuadPart = -(static_cast<LONGLONG>(sizeof(cbStrLen)));
234  pStream->Seek(nOffset, STREAM_SEEK_CUR, nullptr);
235  }
236  }
237  if (hRes == S_FALSE)
238  hRes = HRESULT_WIN32_C(ERROR_READ_FAULT);
239  return hRes;
240  }
241 
243  };
244 #endif // _MFC_VER
245 
246 #ifdef _MFC_VER // using MFC.
247  typedef CComBSTR cWinString;
248 #elif defined(_WIN32)
249  typedef cWinString CComBSTR;
250 #else
252  // typedef cStringW CComBSTR;
253 #endif // _WIN32
254 };
255 #endif // _INC_cWinString_H
#define HRESULT_WIN32_C(x)
a constant LSTATUS/error_status_t with no check, unlike HRESULT_FROM_WIN32()
Definition: HResult.h:79
#define FAILED(x)
Definition: HResult.h:30
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
void Empty()
Definition: cString.h:193
const THIS_t & operator=(const THIS_t &s)
Definition: cString.h:515
const wchar_t * get_CPtr() const noexcept
Definition: cString.h:419
Definition: cMesh.h:22
UNITTEST2_PREDEF(cQuadtree)
cStringW cWinString
Definition: cWinString.h:251
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
const cDebugSourceLine & src
Definition: cDebugAssert.h:51
cUInt64 operator&(const cUInt64 &roUI64_1, const cUInt64 &roUI64_2)
Definition: cUInt64.h:384
static StrLen_t __stdcall UTF8toUNICODELen(const char *pInp, StrLen_t iSizeInpBytes=k_StrLen_UNK)
Definition: StrU.cpp:220
static StrLen_t __stdcall UTF8toUNICODE(wchar_t &wChar, const char *pInp, StrLen_t iSizeInpBytes)
Definition: StrU.cpp:133