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