Open Chinese Convert  1.1.0
A project for conversion between Traditional and Simplified Chinese
TestUtilsUTF8.hpp
1 /*
2  * Open Chinese Convert
3  *
4  * Copyright 2020 BYVoid <byvoid@byvoid.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 
21 #include <string>
22 
23 namespace opencc {
24 
25 #if defined(_MSC_VER) && _MSC_VER > 1310
26 // Visual C++ 2005 and later require the source files in UTF-8, and all strings
27 // to be encoded as wchar_t otherwise the strings will be converted into the
28 // local multibyte encoding and cause errors. To use a wchar_t as UTF-8, these
29 // strings then need to be convert back to UTF-8. This function is just a rough
30 // example of how to do this.
31 #include <Windows.h>
32 #define utf8(str) ConvertToUTF8(L##str)
33 std::string ConvertToUTF8(const wchar_t* pStr) {
34  static char szBuf[1024];
35  WideCharToMultiByte(CP_UTF8, 0, pStr, -1, szBuf, sizeof(szBuf), NULL, NULL);
36  return szBuf;
37 }
38 
39 #else // if defined(_MSC_VER) && _MSC_VER > 1310
40 // Visual C++ 2003 and gcc will use the string literals as is, so the files
41 // should be saved as UTF-8. gcc requires the files to not have a UTF-8 BOM.
42 #define utf8(str) std::string(str)
43 #endif // if defined(_MSC_VER) && _MSC_VER > 1310
44 
45 } // namespace opencc