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];
109 pBuffer
[i
] = (WCHAR
)MAKEWORD((lpsz
[i
]>>8), (lpsz
[i
]&0xFF));
112 pBuffer
[cch
] = 0; // Zero terminator
114 m_ConvertedStrings
.push_back((void*)pBuffer
);
115 return (LPCWSTR
)pBuffer
;
118 LPCSTR
a2utf8(LPCSTR lpsz
)
123 // 1. Convert input ANSI string to widechar using
124 // MultiByteToWideChar(CP_ACP, ...) function (CP_ACP
125 // is current Windows system Ansi code page)
127 // Calculate required buffer size
128 int count
= MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
, lpsz
, -1, NULL
, 0);
132 // Convert ANSI->UNICODE
133 wchar_t* pBuffer
= new wchar_t[count
];
134 int result
= MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
, lpsz
, -1, (LPWSTR
)pBuffer
, count
);
141 // 2. Convert output widechar string from previous call to
142 // UTF-8 using WideCharToMultiByte(CP_UTF8, ...) function
144 LPCSTR pszResult
= (LPCSTR
)w2utf8(pBuffer
);
149 LPCSTR
w2utf8(LPCWSTR lpsz
)
154 // Calculate required buffer size
155 int count
= WideCharToMultiByte(CP_UTF8
, 0, lpsz
, -1, NULL
, 0, NULL
, NULL
);
161 // Convert UNICODE->UTF8
162 LPSTR pBuffer
= new char[count
];
163 int result
= WideCharToMultiByte(CP_UTF8
, 0, lpsz
, -1, (LPSTR
)pBuffer
, count
, NULL
, NULL
);
170 m_ConvertedStrings
.push_back(pBuffer
);
171 return (LPCSTR
)pBuffer
;
174 LPCWSTR
utf82w(LPCSTR lpsz
)
179 // Calculate required buffer size
180 int count
= MultiByteToWideChar(CP_UTF8
, 0, lpsz
, -1, NULL
, 0);
186 // Convert UNICODE->UTF8
187 LPWSTR pBuffer
= new wchar_t[count
];
188 int result
= MultiByteToWideChar(CP_UTF8
, 0, lpsz
, -1, (LPWSTR
)pBuffer
, count
);
195 m_ConvertedStrings
.push_back(pBuffer
);
196 return (LPCWSTR
)pBuffer
;
199 LPCWSTR
utf82w(LPCSTR pStr
, UINT cch
)
204 // Calculate required buffer size
205 int count
= MultiByteToWideChar(CP_UTF8
, 0, pStr
, cch
, NULL
, 0);
211 // Convert UNICODE->UTF8
212 LPWSTR pBuffer
= new wchar_t[count
+1];
213 int result
= MultiByteToWideChar(CP_UTF8
, 0, pStr
, cch
, (LPWSTR
)pBuffer
, count
);
223 m_ConvertedStrings
.push_back(pBuffer
);
224 return (LPCWSTR
)pBuffer
;
227 LPCSTR
utf82a(LPCSTR lpsz
)
229 return w2a(utf82w(lpsz
));
232 LPCTSTR
utf82t(LPCSTR lpsz
)
241 LPCSTR
t2a(LPCTSTR lpsz
)
250 LPCWSTR
t2w(LPCTSTR lpsz
)
259 LPCTSTR
a2t(LPCSTR lpsz
)
268 LPCTSTR
w2t(LPCWSTR lpsz
)
277 LPCSTR
t2utf8(LPCTSTR lpsz
)
287 std::vector
<void*> m_ConvertedStrings
;