1 /*************************************************************************************
2 This file is a part of CrashRpt library.
4 Copyright (c) 2003, Michael Carruth
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
10 * Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
17 * Neither the name of the author nor the names of its contributors
18 may be used to endorse or promote products derived from this software without
19 specific prior written permission.
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ***************************************************************************************/
34 // Description: String conversion class
35 // Author: zexspectrum
50 for (i
= 0; i
< m_ConvertedStrings
.size(); ++i
)
52 delete [] m_ConvertedStrings
[i
];
56 LPCWSTR
a2w(LPCSTR lpsz
)
61 int count
= MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
, lpsz
, -1, NULL
, 0);
65 void* pBuffer
= (void*) new wchar_t[count
];
66 int result
= MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
, lpsz
, -1, (LPWSTR
)pBuffer
, count
);
73 m_ConvertedStrings
.push_back(pBuffer
);
74 return (LPCWSTR
)pBuffer
;
77 LPCSTR
w2a(LPCWSTR lpsz
)
82 int count
= WideCharToMultiByte(CP_UTF8
, 0, lpsz
, -1, NULL
, 0, NULL
, NULL
);
86 void* pBuffer
= (void*) new char[count
];
87 int result
= WideCharToMultiByte(CP_ACP
, 0, lpsz
, -1, (LPSTR
)pBuffer
, count
, NULL
, NULL
);
94 m_ConvertedStrings
.push_back(pBuffer
);
95 return (LPCSTR
)pBuffer
;
98 // Converts UNICODE little endian string to UNICODE big endian
99 LPCWSTR
w2w_be(LPCWSTR lpsz
, UINT cch
)
104 WCHAR
* pBuffer
= new WCHAR
[cch
+1];
105 for (UINT i
= 0; i
< cch
; ++i
)
108 pBuffer
[i
] = (WCHAR
)MAKEWORD((lpsz
[i
]>>8), (lpsz
[i
]&0xFF));
111 pBuffer
[cch
] = 0; // Zero terminator
113 m_ConvertedStrings
.push_back((void*)pBuffer
);
114 return (LPCWSTR
)pBuffer
;
117 LPCSTR
a2utf8(LPCSTR lpsz
)
122 // 1. Convert input ANSI string to widechar using
123 // MultiByteToWideChar(CP_ACP, ...) function (CP_ACP
124 // is current Windows system Ansi code page)
126 // Calculate required buffer size
127 int count
= MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
, lpsz
, -1, NULL
, 0);
131 // Convert ANSI->UNICODE
132 wchar_t* pBuffer
= new wchar_t[count
];
133 int result
= MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
, lpsz
, -1, (LPWSTR
)pBuffer
, count
);
140 // 2. Convert output widechar string from previous call to
141 // UTF-8 using WideCharToMultiByte(CP_UTF8, ...) function
143 LPCSTR pszResult
= (LPCSTR
)w2utf8(pBuffer
);
148 LPCSTR
w2utf8(LPCWSTR lpsz
)
153 // Calculate required buffer size
154 int count
= WideCharToMultiByte(CP_UTF8
, 0, lpsz
, -1, NULL
, 0, NULL
, NULL
);
160 // Convert UNICODE->UTF8
161 LPSTR pBuffer
= new char[count
];
162 int result
= WideCharToMultiByte(CP_UTF8
, 0, lpsz
, -1, (LPSTR
)pBuffer
, count
, NULL
, NULL
);
169 m_ConvertedStrings
.push_back(pBuffer
);
170 return (LPCSTR
)pBuffer
;
173 LPCWSTR
utf82w(LPCSTR lpsz
)
178 // Calculate required buffer size
179 int count
= MultiByteToWideChar(CP_UTF8
, 0, lpsz
, -1, NULL
, 0);
185 // Convert UNICODE->UTF8
186 LPWSTR pBuffer
= new wchar_t[count
];
187 int result
= MultiByteToWideChar(CP_UTF8
, 0, lpsz
, -1, (LPWSTR
)pBuffer
, count
);
194 m_ConvertedStrings
.push_back(pBuffer
);
195 return (LPCWSTR
)pBuffer
;
198 LPCWSTR
utf82w(LPCSTR pStr
, UINT cch
)
203 // Calculate required buffer size
204 int count
= MultiByteToWideChar(CP_UTF8
, 0, pStr
, cch
, NULL
, 0);
210 // Convert UNICODE->UTF8
211 LPWSTR pBuffer
= new wchar_t[count
+1];
212 int result
= MultiByteToWideChar(CP_UTF8
, 0, pStr
, cch
, (LPWSTR
)pBuffer
, count
);
222 m_ConvertedStrings
.push_back(pBuffer
);
223 return (LPCWSTR
)pBuffer
;
226 LPCSTR
utf82a(LPCSTR lpsz
)
228 return w2a(utf82w(lpsz
));
231 LPCTSTR
utf82t(LPCSTR lpsz
)
240 LPCSTR
t2a(LPCTSTR lpsz
)
249 LPCWSTR
t2w(LPCTSTR lpsz
)
258 LPCTSTR
a2t(LPCSTR lpsz
)
267 LPCTSTR
w2t(LPCWSTR lpsz
)
276 LPCSTR
t2utf8(LPCTSTR lpsz
)
286 std::vector
<void*> m_ConvertedStrings
;