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.
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
)
36 int len
= string
.GetLength();
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);
46 CStringA
CUnicodeUtils::GetUTF8(const CStringA
& string
)
49 int len
= string
.GetLength();
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
);
57 return (CUnicodeUtils::GetUTF8(temp
));
60 CString
CUnicodeUtils::GetUnicode(const CStringA
& string
)
63 int len
= string
.GetLength();
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
);
74 CStringA
CUnicodeUtils::ConvertWCHARStringToUTF8(const CString
& string
)
78 buf
= new char[string
.GetLength()+1];
82 for ( ; i
<string
.GetLength(); ++i
)
84 buf
[i
] = (char)string
.GetAt(i
);
96 std::string
CUnicodeUtils::StdGetUTF8(const wide_string
& wide
)
98 int len
= (int)wide
.size();
100 return std::string();
102 char * narrow
= new char[size
];
103 int ret
= WideCharToMultiByte(CP_UTF8
, 0, wide
.c_str(), len
, narrow
, size
-1, NULL
, NULL
);
105 std::string sRet
= std::string(narrow
);
110 wide_string
CUnicodeUtils::StdGetUnicode(const std::string
& multibyte
)
112 int len
= (int)multibyte
.size();
114 return wide_string();
116 wchar_t * wide
= new wchar_t[size
];
117 int ret
= MultiByteToWideChar(CP_UTF8
, 0, multibyte
.c_str(), len
, wide
, size
- 1);
119 wide_string sRet
= wide_string(wide
);
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
);
131 std::string str
= narrow
;
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
);
141 std::string str
= narrow
;
146 wide_string
MultibyteToWide(const std::string
& multibyte
)
148 size_t length
= multibyte
.length();
150 return wide_string();
152 wchar_t * wide
= new wchar_t[multibyte
.length()*2+2];
154 return wide_string();
155 int ret
= (int)MultiByteToWideChar(CP_ACP
, 0, multibyte
.c_str(), (int)multibyte
.size(), wide
, (int)length
*2 - 1);
157 wide_string str
= wide
;
162 wide_string
UTF8ToWide(const std::string
& multibyte
)
164 size_t length
= multibyte
.length();
166 return wide_string();
168 wchar_t * wide
= new wchar_t[length
*2+2];
170 return wide_string();
171 int ret
= (int)MultiByteToWideChar(CP_UTF8
, 0, multibyte
.c_str(), (int)multibyte
.size(), wide
, (int)length
*2 - 1);
173 wide_string str
= wide
;
178 stdstring
UTF8ToString(const std::string
& string
) {return UTF8ToWide(string
);}
179 std::string
StringToUTF8(const stdstring
& string
) {return WideToUTF8(string
);}
181 stdstring
UTF8ToString(const std::string
& string
) {return WideToMultibyte(UTF8ToWide(string
));}
182 std::string
StringToUTF8(const stdstring
& string
) {return WideToUTF8(MultibyteToWide(string
));}
186 #pragma warning(push)
187 #pragma warning(disable: 4200)
188 struct STRINGRESOURCEIMAGE
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
;
203 BOOL defaultCharUsed
;
207 if (lpBuffer
== NULL
)
210 HRSRC hResource
= FindResourceEx(hInstance
, RT_STRING
, MAKEINTRESOURCE(((uID
>>4)+1)), wLanguage
);
213 //try the default language before giving up!
214 hResource
= FindResource(hInstance
, MAKEINTRESOURCE(((uID
>>4)+1)), RT_STRING
);
218 hGlobal
= LoadResource(hInstance
, hResource
);
221 pImage
= (const STRINGRESOURCEIMAGE
*)::LockResource(hGlobal
);
225 nResourceSize
= ::SizeofResource(hInstance
, hResource
);
226 pImageEnd
= (const STRINGRESOURCEIMAGE
*)(LPBYTE(pImage
)+nResourceSize
);
229 while ((iIndex
> 0) && (pImage
< pImageEnd
))
231 pImage
= (const STRINGRESOURCEIMAGE
*)(LPBYTE(pImage
)+(sizeof(STRINGRESOURCEIMAGE
)+(pImage
->nLength
*sizeof(WCHAR
))));
234 if (pImage
>= pImageEnd
)
236 if (pImage
->nLength
== 0)
239 ret
= pImage
->nLength
;
240 if (ret
> nBufferMax
)
242 wcsncpy_s((wchar_t *)lpBuffer
, nBufferMax
, pImage
->achString
, ret
);
245 ret
= WideCharToMultiByte(CP_ACP
, 0, pImage
->achString
, pImage
->nLength
, (LPSTR
)lpBuffer
, nBufferMax
-1, ".", &defaultCharUsed
);