ReactOS 0.4.15-dev-8039-g69ebfd6
atlconv.h
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS ATL
3 * LICENSE: LGPL-2.0-or-later (https://spdx.org/licenses/LGPL-2.0-or-later)
4 * PURPOSE: String conversion
5 * COPYRIGHT: Copyright 2023 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
6 */
7
8#ifndef __ATLCONV_H__
9#define __ATLCONV_H__
10
11#pragma once
12
13#include "atlbase.h"
14
15namespace ATL
16{
17
18// This class does not own the string
19template <int t_nBufferLength = 128>
21{
22public:
24
25 CA2CAEX(_In_z_ LPCSTR psz) : m_psz(psz) { }
26
27 CA2CAEX(_In_z_ LPCSTR psz, _In_ UINT nCodePage) : m_psz(psz)
28 {
29 UNREFERENCED_PARAMETER(nCodePage);
30 }
31
32 ~CA2CAEX() noexcept { } // There is nothing to free here
33
34 _Ret_z_ operator LPCSTR() const noexcept { return m_psz; }
35
36private:
37 // CA2CAEX is not copyable
38 CA2CAEX(_In_ const CA2CAEX&) noexcept = delete;
39 CA2CAEX& operator=(_In_ const CA2CAEX&) noexcept = delete;
40};
41
42// This class does not own the string
43template <int t_nBufferLength = 128>
45{
46public:
48
49 CW2CWEX(_In_z_ LPCWSTR psz) : m_psz(psz) { }
50
51 CW2CWEX(_In_z_ LPCWSTR psz, _In_ UINT nCodePage) : m_psz(psz)
52 {
53 UNREFERENCED_PARAMETER(nCodePage);
54 }
55
56 ~CW2CWEX() noexcept { } // There is nothing to free here
57
58 _Ret_z_ operator LPCWSTR() const noexcept { return m_psz; }
59
60private:
61 // CW2CWEX is not copyable
62 CW2CWEX(_In_ const CW2CWEX&) noexcept = delete;
63 CW2CWEX& operator=(_In_ const CW2CWEX&) noexcept = delete;
64};
65
66template <int t_nBufferLength = 128>
67class CA2AEX
68{
69public:
71 char m_szBuffer[t_nBufferLength];
72
74 {
75 Init(psz);
76 }
77
78 CA2AEX(_In_z_ LPCSTR psz, _In_ UINT nCodePage)
79 {
80 UNREFERENCED_PARAMETER(nCodePage);
81 Init(psz);
82 }
83
84 ~CA2AEX() noexcept
85 {
86 if (m_psz != m_szBuffer)
87 free(m_psz);
88 }
89
90 _Ret_z_ operator LPSTR() const noexcept
91 {
92 return m_psz;
93 }
94
95private:
96 // CA2AEX is not copyable
97 CA2AEX(_In_ const CA2AEX &) noexcept = delete;
98 CA2AEX& operator=(_In_ const CA2AEX &) noexcept = delete;
99
101 {
102 if (!psz)
103 {
104 m_psz = NULL;
105 m_szBuffer[0] = 0;
106 return;
107 }
108 int cchMax = lstrlenA(psz) + 1;
109 if (cchMax <= t_nBufferLength)
110 {
111#ifdef _STRSAFE_H_INCLUDED_
113#else
115#endif
117 return;
118 }
119
120 m_szBuffer[0] = 0;
121 m_psz = _strdup(psz);
122 if (!m_psz)
124 }
125};
126
127template <int t_nBufferLength = 128>
129{
130public:
132 wchar_t m_szBuffer[t_nBufferLength];
133
135 {
136 Init(psz);
137 }
138
139 CW2WEX(_In_z_ LPCWSTR psz, _In_ UINT nCodePage)
140 {
141 UNREFERENCED_PARAMETER(nCodePage);
142 Init(psz);
143 }
144
145 ~CW2WEX() noexcept
146 {
147 if (m_psz != m_szBuffer)
148 free(m_psz);
149 }
150
151 _Ret_z_ operator LPWSTR() const noexcept
152 {
153 return m_psz;
154 }
155
156private:
157 // CW2WEX is not copyable
158 CW2WEX(_In_ const CW2WEX&) noexcept = delete;
159 CW2WEX& operator=(_In_ const CW2WEX&) noexcept = delete;
160
162 {
163 if (!psz)
164 {
165 m_psz = NULL;
166 m_szBuffer[0] = 0;
167 return;
168 }
169 int cchMax = lstrlenW(psz);
170 if (cchMax <= t_nBufferLength)
171 {
172#ifdef _STRSAFE_H_INCLUDED_
174#else
176#endif
178 return;
179 }
180
181 m_szBuffer[0] = 0;
182 m_psz = _wcsdup(psz);
183 if (!m_psz)
185 }
186};
187
188template <int t_nBufferLength = 128>
190{
191public:
193 wchar_t m_szBuffer[t_nBufferLength];
194
196 {
197 Init(psz, CP_ACP);
198 }
199
200 CA2WEX(_In_z_ LPCSTR psz, _In_ UINT nCodePage)
201 {
202 Init(psz, nCodePage);
203 }
204
205 ~CA2WEX() noexcept
206 {
207 if (m_psz != m_szBuffer)
208 free(m_psz);
209 }
210
211 _Ret_z_ operator LPWSTR() const noexcept
212 {
213 return m_psz;
214 }
215
216private:
217 // CA2WEX is not copyable
218 CA2WEX(_In_ const CA2WEX&) noexcept = delete;
219 CA2WEX& operator=(_In_ const CA2WEX&) noexcept = delete;
220
221 void Init(_In_z_ LPCSTR psz, _In_ UINT nCodePage)
222 {
223 if (!psz)
224 {
225 m_psz = NULL;
226 m_szBuffer[0] = 0;
227 return;
228 }
229
230#if 1
231 int cchMax = lstrlenA(psz) + 1; // This is 3 times faster
232#else
233 int cchMax = MultiByteToWideChar(nCodePage, 0, psz, -1, NULL, 0); // It's slow
234#endif
235 if (cchMax <= (int)_countof(m_szBuffer))
236 {
237 // Use the static buffer
240 }
241 else
242 {
243 // Allocate a new buffer
244 m_szBuffer[0] = 0;
245 m_psz = (LPWSTR)malloc(cchMax * sizeof(WCHAR));
246 if (!m_psz)
248 }
249
250 MultiByteToWideChar(nCodePage, 0, psz, -1, m_psz, cchMax);
251 m_psz[cchMax - 1] = 0;
252 }
253};
254
255template <int t_nBufferLength = 128>
257{
258public:
260 char m_szBuffer[t_nBufferLength];
261
263 {
264 Init(psz, CP_ACP);
265 }
266
267 CW2AEX(_In_z_ LPCWSTR psz, _In_ UINT nCodePage)
268 {
269 Init(psz, nCodePage);
270 }
271
272 ~CW2AEX() noexcept
273 {
274 if (m_psz != m_szBuffer)
275 free(m_psz);
276 }
277
278 _Ret_z_ operator LPSTR() const noexcept
279 {
280 return m_psz;
281 }
282
283private:
284 // CW2AEX is not copyable
285 CW2AEX(_In_ const CW2AEX&) noexcept = delete;
286 CW2AEX& operator=(_In_ const CW2AEX&) noexcept = delete;
287
288 void Init(_In_z_ LPCWSTR psz, _In_ UINT nConvertCodePage)
289 {
290 if (!psz)
291 {
292 m_psz = NULL;
293 m_szBuffer[0] = 0;
294 return;
295 }
296
297 // NOTE: This has a failure.
298 int cchMax = WideCharToMultiByte(nConvertCodePage, 0, psz, -1, NULL, 0, NULL, NULL);
299 if (cchMax <= (int)_countof(m_szBuffer))
300 {
301 // Use the static buffer
304 }
305 else
306 {
307 // Allocate a new buffer
308 m_szBuffer[0] = 0;
309 m_psz = (LPSTR)malloc(cchMax * sizeof(CHAR));
310 if (!m_psz)
312 }
313
314 WideCharToMultiByte(nConvertCodePage, 0, psz, -1, m_psz, cchMax, NULL, NULL);
315 m_psz[cchMax - 1] = 0;
316 }
317};
318
325
326#ifdef UNICODE
327 #define CA2CTEX CA2WEX
328 #define CA2TEX CA2WEX
329 #define CT2AEX CW2AEX
330 #define CT2CAEX CW2AEX
331 #define CT2CWEX CW2CWEX
332 #define CT2WEX CW2WEX
333 #define CW2CTEX CW2CWEX
334 #define CW2CTEX CW2CWEX
335#else
336 #define CA2CTEX CA2CAEX
337 #define CA2TEX CA2AEX
338 #define CT2AEX CA2AEX
339 #define CT2CAEX CA2CAEX
340 #define CT2CWEX CA2WEX
341 #define CT2WEX CA2WEX
342 #define CW2CTEX CW2AEX
343 #define CW2TEX CW2AEX
344#endif
345
346} // namespace ATL
347
348#endif // ndef __ATLCONV_H__
UINT cchMax
#define AtlThrow(x)
Definition: atldef.h:20
CA2AEX(_In_z_ LPCSTR psz)
Definition: atlconv.h:73
~CA2AEX() noexcept
Definition: atlconv.h:84
CA2AEX(_In_z_ LPCSTR psz, _In_ UINT nCodePage)
Definition: atlconv.h:78
char m_szBuffer[t_nBufferLength]
Definition: atlconv.h:71
LPSTR m_psz
Definition: atlconv.h:70
CA2AEX & operator=(_In_ const CA2AEX &) noexcept=delete
CA2AEX(_In_ const CA2AEX &) noexcept=delete
void Init(_In_z_ LPCSTR psz)
Definition: atlconv.h:100
CA2CAEX(_In_ const CA2CAEX &) noexcept=delete
CA2CAEX & operator=(_In_ const CA2CAEX &) noexcept=delete
CA2CAEX(_In_z_ LPCSTR psz, _In_ UINT nCodePage)
Definition: atlconv.h:27
~CA2CAEX() noexcept
Definition: atlconv.h:32
LPCSTR m_psz
Definition: atlconv.h:23
CA2CAEX(_In_z_ LPCSTR psz)
Definition: atlconv.h:25
CA2WEX(_In_z_ LPCSTR psz, _In_ UINT nCodePage)
Definition: atlconv.h:200
LPWSTR m_psz
Definition: atlconv.h:192
CA2WEX(_In_ const CA2WEX &) noexcept=delete
CA2WEX(_In_z_ LPCSTR psz)
Definition: atlconv.h:195
void Init(_In_z_ LPCSTR psz, _In_ UINT nCodePage)
Definition: atlconv.h:221
CA2WEX & operator=(_In_ const CA2WEX &) noexcept=delete
~CA2WEX() noexcept
Definition: atlconv.h:205
wchar_t m_szBuffer[t_nBufferLength]
Definition: atlconv.h:193
char m_szBuffer[t_nBufferLength]
Definition: atlconv.h:260
LPSTR m_psz
Definition: atlconv.h:259
CW2AEX(_In_ const CW2AEX &) noexcept=delete
void Init(_In_z_ LPCWSTR psz, _In_ UINT nConvertCodePage)
Definition: atlconv.h:288
CW2AEX(_In_z_ LPCWSTR psz, _In_ UINT nCodePage)
Definition: atlconv.h:267
CW2AEX & operator=(_In_ const CW2AEX &) noexcept=delete
CW2AEX(_In_z_ LPCWSTR psz)
Definition: atlconv.h:262
~CW2AEX() noexcept
Definition: atlconv.h:272
CW2CWEX & operator=(_In_ const CW2CWEX &) noexcept=delete
CW2CWEX(_In_z_ LPCWSTR psz, _In_ UINT nCodePage)
Definition: atlconv.h:51
~CW2CWEX() noexcept
Definition: atlconv.h:56
CW2CWEX(_In_z_ LPCWSTR psz)
Definition: atlconv.h:49
CW2CWEX(_In_ const CW2CWEX &) noexcept=delete
LPCWSTR m_psz
Definition: atlconv.h:47
CW2WEX(_In_z_ LPCWSTR psz)
Definition: atlconv.h:134
CW2WEX(_In_z_ LPCWSTR psz, _In_ UINT nCodePage)
Definition: atlconv.h:139
LPWSTR m_psz
Definition: atlconv.h:131
void Init(_In_z_ LPCWSTR psz)
Definition: atlconv.h:161
~CW2WEX() noexcept
Definition: atlconv.h:145
CW2WEX & operator=(_In_ const CW2WEX &) noexcept=delete
wchar_t m_szBuffer[t_nBufferLength]
Definition: atlconv.h:132
CW2WEX(_In_ const CW2WEX &) noexcept=delete
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
#define free
Definition: debug_ros.c:5
#define _strdup
Definition: debug_ros.c:7
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
#define CP_ACP
Definition: compat.h:109
#define lstrcpynA
Definition: compat.h:751
#define WideCharToMultiByte
Definition: compat.h:111
#define MultiByteToWideChar
Definition: compat.h:110
#define lstrcpynW
Definition: compat.h:738
#define lstrlenW
Definition: compat.h:750
int WINAPI lstrlenA(LPCSTR lpString)
Definition: lstring.c:145
#define _In_z_
Definition: ms_sal.h:313
#define _In_
Definition: ms_sal.h:308
#define _Ret_z_
Definition: ms_sal.h:524
Definition: rosdlgs.h:6
CW2AEX CW2A
Definition: atlconv.h:320
CA2CAEX CA2CA
Definition: atlconv.h:323
CA2WEX CA2W
Definition: atlconv.h:321
CW2WEX CW2W
Definition: atlconv.h:322
CA2AEX CA2A
Definition: atlconv.h:319
CW2CWEX CW2CW
Definition: atlconv.h:324
unsigned int UINT
Definition: ndis.h:50
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
_Check_return_ _CRTIMP wchar_t *__cdecl _wcsdup(_In_z_ const wchar_t *_Str)
#define _countof(array)
Definition: sndvol32.h:68
STRSAFEAPI StringCchCopyW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:149
STRSAFEAPI StringCchCopyA(STRSAFE_LPSTR pszDest, size_t cchDest, STRSAFE_LPCSTR pszSrc)
Definition: strsafe.h:145
const char * LPCSTR
Definition: xmlstorage.h:183
char * LPSTR
Definition: xmlstorage.h:182
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
char CHAR
Definition: xmlstorage.h:175
#define const
Definition: zconf.h:233