*.js files: keep the style consistent
[TortoiseGit.git] / src / Utils / strconv.h
blob31c0b3364f3c2e8db1a26f1baa75e9944256e2b4
1 /*************************************************************************************
2 This file is a part of CrashRpt library.
4 Copyright (c) 2003, Michael Carruth
5 All rights reserved.
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 ***************************************************************************************/
33 // File: strconv.h
34 // Description: String conversion class
35 // Author: zexspectrum
36 // Date: 2009-2010
38 #ifndef _STRCONV_H
39 #define _STRCONV_H
41 #include <vector>
43 class strconv_t
45 public:
46 strconv_t(){}
47 ~strconv_t()
49 unsigned i;
50 for (i = 0; i < m_ConvertedStrings.size(); ++i)
52 delete [] m_ConvertedStrings[i];
56 LPCWSTR a2w(LPCSTR lpsz)
58 if(lpsz==NULL)
59 return NULL;
61 int count = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpsz, -1, NULL, 0);
62 if(count==0)
63 return NULL;
65 void* pBuffer = (void*) new wchar_t[count];
66 int result = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpsz, -1, (LPWSTR)pBuffer, count);
67 if(result==0)
69 delete [] pBuffer;
70 return NULL;
73 m_ConvertedStrings.push_back(pBuffer);
74 return (LPCWSTR)pBuffer;
77 LPCSTR w2a(LPCWSTR lpsz)
79 if(lpsz==NULL)
80 return NULL;
82 int count = WideCharToMultiByte(CP_UTF8, 0, lpsz, -1, NULL, 0, NULL, NULL);
83 if(count==0)
84 return NULL;
86 void* pBuffer = (void*) new char[count];
87 int result = WideCharToMultiByte(CP_ACP, 0, lpsz, -1, (LPSTR)pBuffer, count, NULL, NULL);
88 if(result==0)
90 delete [] pBuffer;
91 return 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)
101 if(lpsz==NULL)
102 return NULL;
104 WCHAR* pBuffer = new WCHAR[cch+1];
105 for (UINT i = 0; i < cch; ++i)
107 // Swap bytes
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)
119 if(lpsz==NULL)
120 return NULL;
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);
128 if(count==0)
129 return NULL;
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);
134 if(result==0)
136 delete [] pBuffer;
137 return NULL;
140 // 2. Convert output widechar string from previous call to
141 // UTF-8 using WideCharToMultiByte(CP_UTF8, ...) function
143 LPCSTR pszResult = (LPCSTR)w2utf8(pBuffer);
144 delete [] pBuffer;
145 return pszResult;
148 LPCSTR w2utf8(LPCWSTR lpsz)
150 if(lpsz==NULL)
151 return NULL;
153 // Calculate required buffer size
154 int count = WideCharToMultiByte(CP_UTF8, 0, lpsz, -1, NULL, 0, NULL, NULL);
155 if(count==0)
157 return 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);
163 if(result==0)
165 delete [] pBuffer;
166 return NULL;
169 m_ConvertedStrings.push_back(pBuffer);
170 return (LPCSTR)pBuffer;
173 LPCWSTR utf82w(LPCSTR lpsz)
175 if(lpsz==NULL)
176 return NULL;
178 // Calculate required buffer size
179 int count = MultiByteToWideChar(CP_UTF8, 0, lpsz, -1, NULL, 0);
180 if(count==0)
182 return NULL;
185 // Convert UNICODE->UTF8
186 LPWSTR pBuffer = new wchar_t[count];
187 int result = MultiByteToWideChar(CP_UTF8, 0, lpsz, -1, (LPWSTR)pBuffer, count);
188 if(result==0)
190 delete [] pBuffer;
191 return NULL;
194 m_ConvertedStrings.push_back(pBuffer);
195 return (LPCWSTR)pBuffer;
198 LPCWSTR utf82w(LPCSTR pStr, UINT cch)
200 if(pStr==NULL)
201 return NULL;
203 // Calculate required buffer size
204 int count = MultiByteToWideChar(CP_UTF8, 0, pStr, cch, NULL, 0);
205 if(count==0)
207 return NULL;
210 // Convert UNICODE->UTF8
211 LPWSTR pBuffer = new wchar_t[count+1];
212 int result = MultiByteToWideChar(CP_UTF8, 0, pStr, cch, (LPWSTR)pBuffer, count);
213 if(result==0)
215 delete [] pBuffer;
216 return NULL;
219 // Zero-terminate
220 pBuffer[count]=0;
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)
233 #ifdef UNICODE
234 return utf82w(lpsz);
235 #else
236 return utf82a(lpsz);
237 #endif
240 LPCSTR t2a(LPCTSTR lpsz)
242 #ifdef UNICODE
243 return w2a(lpsz);
244 #else
245 return lpsz;
246 #endif
249 LPCWSTR t2w(LPCTSTR lpsz)
251 #ifdef UNICODE
252 return lpsz;
253 #else
254 return a2w(lpsz);
255 #endif
258 LPCTSTR a2t(LPCSTR lpsz)
260 #ifdef UNICODE
261 return a2w(lpsz);
262 #else
263 return lpsz;
264 #endif
267 LPCTSTR w2t(LPCWSTR lpsz)
269 #ifdef UNICODE
270 return lpsz;
271 #else
272 return w2a(lpsz);
273 #endif
276 LPCSTR t2utf8(LPCTSTR lpsz)
278 #ifdef UNICODE
279 return w2utf8(lpsz);
280 #else
281 return a2utf8(lpsz);
282 #endif
285 private:
286 std::vector<void*> m_ConvertedStrings;
289 #endif //_STRCONV_H