1*32b1fd08SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*32b1fd08SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*32b1fd08SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*32b1fd08SAndrew Rist  * distributed with this work for additional information
6*32b1fd08SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*32b1fd08SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*32b1fd08SAndrew Rist  * "License"); you may not use this file except in compliance
9*32b1fd08SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*32b1fd08SAndrew Rist  *
11*32b1fd08SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*32b1fd08SAndrew Rist  *
13*32b1fd08SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*32b1fd08SAndrew Rist  * software distributed under the License is distributed on an
15*32b1fd08SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*32b1fd08SAndrew Rist  * KIND, either express or implied.  See the License for the
17*32b1fd08SAndrew Rist  * specific language governing permissions and limitations
18*32b1fd08SAndrew Rist  * under the License.
19*32b1fd08SAndrew Rist  *
20*32b1fd08SAndrew Rist  *************************************************************/
21*32b1fd08SAndrew Rist 
22*32b1fd08SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "stringconverter.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #ifdef _MSC_VER
27cdf0e10cSrcweir #pragma warning(push, 1) /* disable warnings within system headers */
28cdf0e10cSrcweir #endif
29cdf0e10cSrcweir #include <windows.h>
30cdf0e10cSrcweir #ifdef _MSC_VER
31cdf0e10cSrcweir #pragma warning(pop)
32cdf0e10cSrcweir #endif
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <malloc.h>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir /** Convert a Unicode string to an ANSI string based on CP_ACP
37cdf0e10cSrcweir */
UnicodeToAnsiString(const std::wstring & UniString)38cdf0e10cSrcweir std::string UnicodeToAnsiString(const std::wstring& UniString)
39cdf0e10cSrcweir {
40cdf0e10cSrcweir     int len = WideCharToMultiByte(
41cdf0e10cSrcweir 		CP_ACP, 0, UniString.c_str(), -1, 0, 0, 0, 0);
42cdf0e10cSrcweir 
43cdf0e10cSrcweir 	char* buff = reinterpret_cast<char*>(_alloca(len));
44cdf0e10cSrcweir 
45cdf0e10cSrcweir 	WideCharToMultiByte(
46cdf0e10cSrcweir 		CP_ACP, 0, UniString.c_str(), -1, buff, len, 0, 0);
47cdf0e10cSrcweir 
48cdf0e10cSrcweir 	return std::string(buff);
49cdf0e10cSrcweir }
50cdf0e10cSrcweir 
51cdf0e10cSrcweir /** Convert an ANSI string to unicode based on CP_ACP
52cdf0e10cSrcweir */
AnsiToUnicodeString(const std::string & AnsiString)53cdf0e10cSrcweir std::wstring AnsiToUnicodeString(const std::string& AnsiString)
54cdf0e10cSrcweir {
55cdf0e10cSrcweir     int len = MultiByteToWideChar(
56cdf0e10cSrcweir 		CP_ACP, 0, AnsiString.c_str(), -1, 0, 0);
57cdf0e10cSrcweir 
58cdf0e10cSrcweir 	wchar_t* buff = reinterpret_cast<wchar_t*>(_alloca(len * sizeof(wchar_t)));
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 	MultiByteToWideChar(
61cdf0e10cSrcweir 		CP_ACP, 0, AnsiString.c_str(), -1, buff, len);
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 	return std::wstring(buff);
64cdf0e10cSrcweir }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 
67