MWCapture SDK Linux  3.3.1.LAST_SVN_COMMIT_NUM
StringUtils.h
1 #ifndef __STRING_UTILS_H__
2 #define __STRING_UTILS_H__
3 
5 {
6 public:
7  CAutoConvertString(const WCHAR *pwszString, UINT uCodePage = CP_ACP) {
8  m_pwszString = const_cast<WCHAR *>(pwszString);
9  m_pszString = NULL;
10  m_pvAlloc = NULL;
11  m_uCodePage = uCodePage;
12  }
13 
14  CAutoConvertString(const char *pszString, UINT uCodePage = CP_ACP) {
15  m_pszString = const_cast<char *>(pszString);
16  m_pwszString = NULL;
17  m_pvAlloc = NULL;
18  m_uCodePage = uCodePage;
19  }
20 
21  virtual ~CAutoConvertString() {
22  if (NULL != m_pvAlloc) {
23  free(m_pvAlloc);
24  m_pvAlloc = NULL;
25  }
26  }
27 
28 public:
29  void Detach() {
30  m_pszString = NULL;
31  m_pwszString = NULL;
32  m_pvAlloc = NULL;
33  }
34 
35 public:
36  operator char *() {
37  if (NULL == m_pszString) {
38  int nLen = WideCharToMultiByte(m_uCodePage, 0, m_pwszString, -1,
39  NULL, 0, NULL, NULL);
40  if (nLen == 0)
41  return NULL;
42 
43  m_pszString = (char *)malloc(nLen);
44  WideCharToMultiByte(m_uCodePage, 0, m_pwszString, -1,
45  m_pszString, nLen, NULL, NULL);
46 
47  m_pvAlloc = m_pszString;
48  }
49 
50  return m_pszString;
51  }
52 
53  operator WCHAR *() {
54  if (NULL == m_pwszString) {
55  int nLen = MultiByteToWideChar(m_uCodePage, 0, m_pszString, -1,
56  NULL, 0);
57  if (nLen == 0)
58  return NULL;
59 
60  m_pwszString = (WCHAR *)malloc(nLen * sizeof(WCHAR));
61  MultiByteToWideChar(m_uCodePage, 0, m_pszString, -1, m_pwszString, nLen);
62 
63  m_pvAlloc = m_pwszString;
64  }
65 
66  return m_pwszString;
67  }
68 
69 protected:
70  WCHAR * m_pwszString;
71  char * m_pszString;
72  void * m_pvAlloc;
73  UINT m_uCodePage;
74 };
75 
76 #endif //__STRING_UTILS_H__
Definition: StringUtils.h:4