Try to enable show merged file
[TortoiseGit.git] / src / Utils / UnicodeUtils.cpp
blobef63004e9d05ac7c2a61677797f9043625f82f66
1 // TortoiseSVN - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2006, 2008 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "StdAfx.h"
20 #include "unicodeutils.h"
22 CUnicodeUtils::CUnicodeUtils(void)
26 CUnicodeUtils::~CUnicodeUtils(void)
30 #if defined(_MFC_VER) || defined(CSTRING_AVAILABLE)
32 CStringA CUnicodeUtils::GetUTF8(const CStringW& string)
34 char * buf;
35 CStringA retVal;
36 int len = string.GetLength();
37 if (len==0)
38 return retVal;
39 buf = retVal.GetBuffer(len*4 + 1);
40 // SecureZeroMemory(buf, (string.GetLength()*4 + 1)*sizeof(char));
41 int lengthIncTerminator = WideCharToMultiByte(CP_UTF8, 0, string, -1, buf, len*4, NULL, NULL);
42 retVal.ReleaseBuffer(lengthIncTerminator-1);
43 return retVal;
46 CStringA CUnicodeUtils::GetUTF8(const CStringA& string)
48 WCHAR * buf;
49 int len = string.GetLength();
50 if (len==0)
51 return CStringA();
52 buf = new WCHAR[len*4 + 1];
53 SecureZeroMemory(buf, (len*4 + 1)*sizeof(WCHAR));
54 MultiByteToWideChar(CP_ACP, 0, string, -1, buf, len*4);
55 CStringW temp = CStringW(buf);
56 delete [] buf;
57 return (CUnicodeUtils::GetUTF8(temp));
60 CString CUnicodeUtils::GetUnicode(const CStringA& string)
62 WCHAR * buf;
63 int len = string.GetLength();
64 if (len==0)
65 return CString();
66 buf = new WCHAR[len*4 + 1];
67 SecureZeroMemory(buf, (len*4 + 1)*sizeof(WCHAR));
68 MultiByteToWideChar(CP_UTF8, 0, string, -1, buf, len*4);
69 CString ret = CString(buf);
70 delete [] buf;
71 return ret;
74 CStringA CUnicodeUtils::ConvertWCHARStringToUTF8(const CString& string)
76 CStringA sRet;
77 char * buf;
78 buf = new char[string.GetLength()+1];
79 if (buf)
81 int i=0;
82 for ( ; i<string.GetLength(); ++i)
84 buf[i] = (char)string.GetAt(i);
86 buf[i] = 0;
87 sRet = CStringA(buf);
88 delete [] buf;
90 return sRet;
93 #endif //_MFC_VER
95 #ifdef UNICODE
96 std::string CUnicodeUtils::StdGetUTF8(const wide_string& wide)
98 int len = (int)wide.size();
99 if (len==0)
100 return std::string();
101 int size = len*4;
102 char * narrow = new char[size];
103 int ret = WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), len, narrow, size-1, NULL, NULL);
104 narrow[ret] = 0;
105 std::string sRet = std::string(narrow);
106 delete [] narrow;
107 return sRet;
110 wide_string CUnicodeUtils::StdGetUnicode(const std::string& multibyte)
112 int len = (int)multibyte.size();
113 if (len==0)
114 return wide_string();
115 int size = len*4;
116 wchar_t * wide = new wchar_t[size];
117 int ret = MultiByteToWideChar(CP_UTF8, 0, multibyte.c_str(), len, wide, size - 1);
118 wide[ret] = 0;
119 wide_string sRet = wide_string(wide);
120 delete [] wide;
121 return sRet;
123 #endif
125 std::string WideToMultibyte(const wide_string& wide)
127 char * narrow = new char[wide.length()*3+2];
128 BOOL defaultCharUsed;
129 int ret = (int)WideCharToMultiByte(CP_ACP, 0, wide.c_str(), (int)wide.size(), narrow, (int)wide.length()*3 - 1, ".", &defaultCharUsed);
130 narrow[ret] = 0;
131 std::string str = narrow;
132 delete[] narrow;
133 return str;
136 std::string WideToUTF8(const wide_string& wide)
138 char * narrow = new char[wide.length()*3+2];
139 int ret = (int)WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), (int)wide.size(), narrow, (int)wide.length()*3 - 1, NULL, NULL);
140 narrow[ret] = 0;
141 std::string str = narrow;
142 delete[] narrow;
143 return str;
146 wide_string MultibyteToWide(const std::string& multibyte)
148 size_t length = multibyte.length();
149 if (length == 0)
150 return wide_string();
152 wchar_t * wide = new wchar_t[multibyte.length()*2+2];
153 if (wide == NULL)
154 return wide_string();
155 int ret = (int)MultiByteToWideChar(CP_ACP, 0, multibyte.c_str(), (int)multibyte.size(), wide, (int)length*2 - 1);
156 wide[ret] = 0;
157 wide_string str = wide;
158 delete[] wide;
159 return str;
162 wide_string UTF8ToWide(const std::string& multibyte)
164 size_t length = multibyte.length();
165 if (length == 0)
166 return wide_string();
168 wchar_t * wide = new wchar_t[length*2+2];
169 if (wide == NULL)
170 return wide_string();
171 int ret = (int)MultiByteToWideChar(CP_UTF8, 0, multibyte.c_str(), (int)multibyte.size(), wide, (int)length*2 - 1);
172 wide[ret] = 0;
173 wide_string str = wide;
174 delete[] wide;
175 return str;
177 #ifdef UNICODE
178 stdstring UTF8ToString(const std::string& string) {return UTF8ToWide(string);}
179 std::string StringToUTF8(const stdstring& string) {return WideToUTF8(string);}
180 #else
181 stdstring UTF8ToString(const std::string& string) {return WideToMultibyte(UTF8ToWide(string));}
182 std::string StringToUTF8(const stdstring& string) {return WideToUTF8(MultibyteToWide(string));}
183 #endif
186 #pragma warning(push)
187 #pragma warning(disable: 4200)
188 struct STRINGRESOURCEIMAGE
190 WORD nLength;
191 WCHAR achString[];
193 #pragma warning(pop) // C4200
195 int LoadStringEx(HINSTANCE hInstance, UINT uID, LPTSTR lpBuffer, int nBufferMax, WORD wLanguage)
197 const STRINGRESOURCEIMAGE* pImage;
198 const STRINGRESOURCEIMAGE* pImageEnd;
199 ULONG nResourceSize;
200 HGLOBAL hGlobal;
201 UINT iIndex;
202 #ifndef UNICODE
203 BOOL defaultCharUsed;
204 #endif
205 int ret;
207 if (lpBuffer == NULL)
208 return 0;
209 lpBuffer[0] = 0;
210 HRSRC hResource = FindResourceEx(hInstance, RT_STRING, MAKEINTRESOURCE(((uID>>4)+1)), wLanguage);
211 if (!hResource)
213 //try the default language before giving up!
214 hResource = FindResource(hInstance, MAKEINTRESOURCE(((uID>>4)+1)), RT_STRING);
215 if (!hResource)
216 return 0;
218 hGlobal = LoadResource(hInstance, hResource);
219 if (!hGlobal)
220 return 0;
221 pImage = (const STRINGRESOURCEIMAGE*)::LockResource(hGlobal);
222 if(!pImage)
223 return 0;
225 nResourceSize = ::SizeofResource(hInstance, hResource);
226 pImageEnd = (const STRINGRESOURCEIMAGE*)(LPBYTE(pImage)+nResourceSize);
227 iIndex = uID&0x000f;
229 while ((iIndex > 0) && (pImage < pImageEnd))
231 pImage = (const STRINGRESOURCEIMAGE*)(LPBYTE(pImage)+(sizeof(STRINGRESOURCEIMAGE)+(pImage->nLength*sizeof(WCHAR))));
232 iIndex--;
234 if (pImage >= pImageEnd)
235 return 0;
236 if (pImage->nLength == 0)
237 return 0;
238 #ifdef UNICODE
239 ret = pImage->nLength;
240 if (ret > nBufferMax)
241 ret = nBufferMax;
242 wcsncpy_s((wchar_t *)lpBuffer, nBufferMax, pImage->achString, ret);
243 lpBuffer[ret] = 0;
244 #else
245 ret = WideCharToMultiByte(CP_ACP, 0, pImage->achString, pImage->nLength, (LPSTR)lpBuffer, nBufferMax-1, ".", &defaultCharUsed);
246 lpBuffer[ret] = 0;
247 #endif
248 return ret;