• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
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 
16 #include "StringHelper.h"
17 #include <windows.h>
18 #include "securec.h"
19 #include "PreviewerEngineLog.h"
20 
StringToUtf8(const std::string & str)21 std::string StringHelper::StringToUtf8(const std::string& str)
22 {
23     int doubles = 2;
24     int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
25     if (nwLen < 1) {
26         ELOG("MultiByteToWideChar failed.");
27         return str;
28     }
29     wchar_t* pwBuf = new wchar_t[nwLen + 1];
30     ZeroMemory(pwBuf, (nwLen + 1) * doubles);
31     ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);
32     int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
33     if (nLen < 1) {
34         delete[]pwBuf;
35         ELOG("WideCharToMultiByte failed.");
36         return str;
37     }
38     char* pBuf = new char[nLen + 1];
39     ZeroMemory(pBuf, nLen + 1);
40     ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
41     std::string retStr(pBuf);
42     delete[]pwBuf;
43     delete[]pBuf;
44     pwBuf = NULL;
45     pBuf = NULL;
46     return retStr;
47 }
48 
Utf8ToString(const std::string & str)49 std::string StringHelper::Utf8ToString(const std::string& str)
50 {
51     int doubles = 2;
52     int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
53     int wLenAdd = nwLen + 1;
54     if (wLenAdd <= 1) {
55         return str;
56     }
57     wchar_t* pwBuf = new wchar_t[wLenAdd];
58     int doubleLen = wLenAdd * doubles;
59     if (EOK != memset_s(pwBuf, sizeof(*pwBuf) * doubles, 0, doubleLen)) {
60         delete []pwBuf;
61         ELOG("pwBuf memset_s failed.");
62         return str;
63     }
64     MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);
65     int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
66     int lenAdd = nLen + 1;
67     if (lenAdd <= 1) {
68         delete []pwBuf;
69         return str;
70     }
71     char* pBuf = new char[lenAdd];
72     if (EOK != memset_s(pBuf, lenAdd, 0, lenAdd)) {
73         delete []pBuf;
74         ELOG("pBuf memset_s failed.");
75         return str;
76     }
77     WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
78     std::string retStr = pBuf;
79     delete []pBuf;
80     delete []pwBuf;
81     pBuf = NULL;
82     pwBuf = NULL;
83     return retStr;
84 }