1 // TortoiseSVN - a Windows shell extension for easy version control
\r
3 // Copyright (C) 2003-2006, 2008 - TortoiseSVN
\r
5 // This program is free software; you can redistribute it and/or
\r
6 // modify it under the terms of the GNU General Public License
\r
7 // as published by the Free Software Foundation; either version 2
\r
8 // of the License, or (at your option) any later version.
\r
10 // This program is distributed in the hope that it will be useful,
\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 // GNU General Public License for more details.
\r
15 // You should have received a copy of the GNU General Public License
\r
16 // along with this program; if not, write to the Free Software Foundation,
\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\r
20 #include "unicodeutils.h"
\r
22 CUnicodeUtils::CUnicodeUtils(void)
\r
26 CUnicodeUtils::~CUnicodeUtils(void)
\r
30 #if defined(_MFC_VER) || defined(CSTRING_AVAILABLE)
\r
32 CStringA CUnicodeUtils::GetUTF8(const CStringW& string)
\r
36 int len = string.GetLength();
\r
39 buf = retVal.GetBuffer(len*4 + 1);
\r
40 // SecureZeroMemory(buf, (string.GetLength()*4 + 1)*sizeof(char));
\r
41 int lengthIncTerminator = WideCharToMultiByte(CP_UTF8, 0, string, -1, buf, len*4, NULL, NULL);
\r
42 retVal.ReleaseBuffer(lengthIncTerminator-1);
\r
46 CStringA CUnicodeUtils::GetUTF8(const CStringA& string)
\r
49 int len = string.GetLength();
\r
52 buf = new WCHAR[len*4 + 1];
\r
53 SecureZeroMemory(buf, (len*4 + 1)*sizeof(WCHAR));
\r
54 MultiByteToWideChar(CP_ACP, 0, string, -1, buf, len*4);
\r
55 CStringW temp = CStringW(buf);
\r
57 return (CUnicodeUtils::GetUTF8(temp));
\r
60 CString CUnicodeUtils::GetUnicode(const CStringA& string)
\r
63 int len = string.GetLength();
\r
66 buf = new WCHAR[len*4 + 1];
\r
67 SecureZeroMemory(buf, (len*4 + 1)*sizeof(WCHAR));
\r
68 MultiByteToWideChar(CP_UTF8, 0, string, -1, buf, len*4);
\r
69 CString ret = CString(buf);
\r
74 CStringA CUnicodeUtils::ConvertWCHARStringToUTF8(const CString& string)
\r
78 buf = new char[string.GetLength()+1];
\r
82 for ( ; i<string.GetLength(); ++i)
\r
84 buf[i] = (char)string.GetAt(i);
\r
87 sRet = CStringA(buf);
\r
96 std::string CUnicodeUtils::StdGetUTF8(const wide_string& wide)
\r
98 int len = (int)wide.size();
\r
100 return std::string();
\r
102 char * narrow = new char[size];
\r
103 int ret = WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), len, narrow, size-1, NULL, NULL);
\r
105 std::string sRet = std::string(narrow);
\r
110 wide_string CUnicodeUtils::StdGetUnicode(const std::string& multibyte)
\r
112 int len = (int)multibyte.size();
\r
114 return wide_string();
\r
116 wchar_t * wide = new wchar_t[size];
\r
117 int ret = MultiByteToWideChar(CP_UTF8, 0, multibyte.c_str(), len, wide, size - 1);
\r
119 wide_string sRet = wide_string(wide);
\r
125 std::string WideToMultibyte(const wide_string& wide)
\r
127 char * narrow = new char[wide.length()*3+2];
\r
128 BOOL defaultCharUsed;
\r
129 int ret = (int)WideCharToMultiByte(CP_ACP, 0, wide.c_str(), (int)wide.size(), narrow, (int)wide.length()*3 - 1, ".", &defaultCharUsed);
\r
131 std::string str = narrow;
\r
136 std::string WideToUTF8(const wide_string& wide)
\r
138 char * narrow = new char[wide.length()*3+2];
\r
139 int ret = (int)WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), (int)wide.size(), narrow, (int)wide.length()*3 - 1, NULL, NULL);
\r
141 std::string str = narrow;
\r
146 wide_string MultibyteToWide(const std::string& multibyte)
\r
148 size_t length = multibyte.length();
\r
150 return wide_string();
\r
152 wchar_t * wide = new wchar_t[multibyte.length()*2+2];
\r
154 return wide_string();
\r
155 int ret = (int)MultiByteToWideChar(CP_ACP, 0, multibyte.c_str(), (int)multibyte.size(), wide, (int)length*2 - 1);
\r
157 wide_string str = wide;
\r
162 wide_string UTF8ToWide(const std::string& multibyte)
\r
164 size_t length = multibyte.length();
\r
166 return wide_string();
\r
168 wchar_t * wide = new wchar_t[length*2+2];
\r
170 return wide_string();
\r
171 int ret = (int)MultiByteToWideChar(CP_UTF8, 0, multibyte.c_str(), (int)multibyte.size(), wide, (int)length*2 - 1);
\r
173 wide_string str = wide;
\r
178 stdstring UTF8ToString(const std::string& string) {return UTF8ToWide(string);}
\r
179 std::string StringToUTF8(const stdstring& string) {return WideToUTF8(string);}
\r
181 stdstring UTF8ToString(const std::string& string) {return WideToMultibyte(UTF8ToWide(string));}
\r
182 std::string StringToUTF8(const stdstring& string) {return WideToUTF8(MultibyteToWide(string));}
\r
186 #pragma warning(push)
\r
187 #pragma warning(disable: 4200)
\r
188 struct STRINGRESOURCEIMAGE
\r
193 #pragma warning(pop) // C4200
\r
195 int LoadStringEx(HINSTANCE hInstance, UINT uID, LPTSTR lpBuffer, int nBufferMax, WORD wLanguage)
\r
197 const STRINGRESOURCEIMAGE* pImage;
\r
198 const STRINGRESOURCEIMAGE* pImageEnd;
\r
199 ULONG nResourceSize;
\r
203 BOOL defaultCharUsed;
\r
207 if (lpBuffer == NULL)
\r
210 HRSRC hResource = FindResourceEx(hInstance, RT_STRING, MAKEINTRESOURCE(((uID>>4)+1)), wLanguage);
\r
213 //try the default language before giving up!
\r
214 hResource = FindResource(hInstance, MAKEINTRESOURCE(((uID>>4)+1)), RT_STRING);
\r
218 hGlobal = LoadResource(hInstance, hResource);
\r
221 pImage = (const STRINGRESOURCEIMAGE*)::LockResource(hGlobal);
\r
225 nResourceSize = ::SizeofResource(hInstance, hResource);
\r
226 pImageEnd = (const STRINGRESOURCEIMAGE*)(LPBYTE(pImage)+nResourceSize);
\r
227 iIndex = uID&0x000f;
\r
229 while ((iIndex > 0) && (pImage < pImageEnd))
\r
231 pImage = (const STRINGRESOURCEIMAGE*)(LPBYTE(pImage)+(sizeof(STRINGRESOURCEIMAGE)+(pImage->nLength*sizeof(WCHAR))));
\r
234 if (pImage >= pImageEnd)
\r
236 if (pImage->nLength == 0)
\r
239 ret = pImage->nLength;
\r
240 if (ret > nBufferMax)
\r
242 wcsncpy_s((wchar_t *)lpBuffer, nBufferMax, pImage->achString, ret);
\r
245 ret = WideCharToMultiByte(CP_ACP, 0, pImage->achString, pImage->nLength, (LPSTR)lpBuffer, nBufferMax-1, ".", &defaultCharUsed);
\r