1 // Copyright 2022 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/core/util/tchar.h"
16
17 #include <grpc/support/port_platform.h>
18
19 #ifdef GPR_WINDOWS
20
21 namespace grpc_core {
22
23 #if defined UNICODE || defined _UNICODE
CharToTchar(std::string input)24 TcharString CharToTchar(std::string input) {
25 int needed = MultiByteToWideChar(CP_UTF8, 0, input.c_str(), -1, NULL, 0);
26 if (needed <= 0) return TcharString();
27 TcharString ret(needed, L'\0');
28 MultiByteToWideChar(CP_UTF8, 0, input.c_str(), -1,
29 const_cast<LPTSTR>(ret.data()), needed);
30 return ret;
31 }
32
TcharToChar(TcharString input)33 std::string TcharToChar(TcharString input) {
34 int needed =
35 WideCharToMultiByte(CP_UTF8, 0, input.c_str(), -1, NULL, 0, NULL, NULL);
36 if (needed <= 0) return std::string();
37 std::string ret(needed, '\0');
38 WideCharToMultiByte(CP_UTF8, 0, input.c_str(), -1,
39 const_cast<LPSTR>(ret.data()), needed, NULL, NULL);
40 return ret;
41 }
42 #else
43 TcharString CharToTchar(std::string input) { return input; }
44 std::string TcharToChar(TcharString input) { return input; }
45 #endif
46
47 } // namespace grpc_core
48
49 #endif
50