fixed the ContextMenuStash.png screenshot
[TortoiseGit.git] / src / Utils / strconv.h
blob374795fa5ee7bfc04e8841c88598ebbe454ab4c4
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 UINT i;
106 for(i=0; i<cch; i++)
108 // Swap bytes
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)
120 if(lpsz==NULL)
121 return NULL;
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);
129 if(count==0)
130 return NULL;
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);
135 if(result==0)
137 delete [] pBuffer;
138 return NULL;
141 // 2. Convert output widechar string from previous call to
142 // UTF-8 using WideCharToMultiByte(CP_UTF8, ...) function
144 LPCSTR pszResult = (LPCSTR)w2utf8(pBuffer);
145 delete [] pBuffer;
146 return pszResult;
149 LPCSTR w2utf8(LPCWSTR lpsz)
151 if(lpsz==NULL)
152 return NULL;
154 // Calculate required buffer size
155 int count = WideCharToMultiByte(CP_UTF8, 0, lpsz, -1, NULL, 0, NULL, NULL);
156 if(count==0)
158 return 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);
164 if(result==0)
166 delete [] pBuffer;
167 return NULL;
170 m_ConvertedStrings.push_back(pBuffer);
171 return (LPCSTR)pBuffer;
174 LPCWSTR utf82w(LPCSTR lpsz)
176 if(lpsz==NULL)
177 return NULL;
179 // Calculate required buffer size
180 int count = MultiByteToWideChar(CP_UTF8, 0, lpsz, -1, NULL, 0);
181 if(count==0)
183 return NULL;
186 // Convert UNICODE->UTF8
187 LPWSTR pBuffer = new wchar_t[count];
188 int result = MultiByteToWideChar(CP_UTF8, 0, lpsz, -1, (LPWSTR)pBuffer, count);
189 if(result==0)
191 delete [] pBuffer;
192 return NULL;
195 m_ConvertedStrings.push_back(pBuffer);
196 return (LPCWSTR)pBuffer;
199 LPCWSTR utf82w(LPCSTR pStr, UINT cch)
201 if(pStr==NULL)
202 return NULL;
204 // Calculate required buffer size
205 int count = MultiByteToWideChar(CP_UTF8, 0, pStr, cch, NULL, 0);
206 if(count==0)
208 return NULL;
211 // Convert UNICODE->UTF8
212 LPWSTR pBuffer = new wchar_t[count+1];
213 int result = MultiByteToWideChar(CP_UTF8, 0, pStr, cch, (LPWSTR)pBuffer, count);
214 if(result==0)
216 delete [] pBuffer;
217 return NULL;
220 // Zero-terminate
221 pBuffer[count]=0;
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)
234 #ifdef UNICODE
235 return utf82w(lpsz);
236 #else
237 return utf82a(lpsz);
238 #endif
241 LPCSTR t2a(LPCTSTR lpsz)
243 #ifdef UNICODE
244 return w2a(lpsz);
245 #else
246 return lpsz;
247 #endif
250 LPCWSTR t2w(LPCTSTR lpsz)
252 #ifdef UNICODE
253 return lpsz;
254 #else
255 return a2w(lpsz);
256 #endif
259 LPCTSTR a2t(LPCSTR lpsz)
261 #ifdef UNICODE
262 return a2w(lpsz);
263 #else
264 return lpsz;
265 #endif
268 LPCTSTR w2t(LPCWSTR lpsz)
270 #ifdef UNICODE
271 return lpsz;
272 #else
273 return w2a(lpsz);
274 #endif
277 LPCSTR t2utf8(LPCTSTR lpsz)
279 #ifdef UNICODE
280 return w2utf8(lpsz);
281 #else
282 return a2utf8(lpsz);
283 #endif
286 private:
287 std::vector<void*> m_ConvertedStrings;
290 #endif //_STRCONV_H