• 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(std::nothrow) wchar_t[nwLen + 1];
30     if (!pwBuf) {
31         ELOG("Memory allocation failed : pwBuf.");
32         return str;
33     }
34     ZeroMemory(pwBuf, (nwLen + 1) * doubles);
35     ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);
36     int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
37     if (nLen < 1) {
38         delete[] pwBuf;
39         ELOG("WideCharToMultiByte failed.");
40         return str;
41     }
42     char* pBuf = new(std::nothrow) char[nLen + 1];
43     if (!pBuf) {
44         ELOG("Memory allocation failed : pBuf.");
45         return str;
46     }
47     ZeroMemory(pBuf, nLen + 1);
48     ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
49     std::string retStr(pBuf);
50     delete[] pwBuf;
51     delete[] pBuf;
52     pwBuf = NULL;
53     pBuf = NULL;
54     return retStr;
55 }
56 
Utf8ToString(const std::string & str)57 std::string StringHelper::Utf8ToString(const std::string& str)
58 {
59     int doubles = 2;
60     int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
61     int wLenAdd = nwLen + 1;
62     if (wLenAdd <= 1) {
63         return str;
64     }
65     wchar_t* pwBuf = new(std::nothrow) wchar_t[wLenAdd];
66     if (!pwBuf) {
67         ELOG("Memory allocation failed : pwBuf.");
68         return str;
69     }
70     int doubleLen = wLenAdd * doubles;
71     if (EOK != memset_s(pwBuf, sizeof(*pwBuf) * doubles, 0, doubleLen)) {
72         delete []pwBuf;
73         ELOG("pwBuf memset_s failed.");
74         return str;
75     }
76     MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);
77     int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
78     int lenAdd = nLen + 1;
79     if (lenAdd <= 1) {
80         delete []pwBuf;
81         return str;
82     }
83     char* pBuf = new(std::nothrow) char[lenAdd];
84     if (!pBuf) {
85         ELOG("Memory allocation failed : pBuf.");
86         return str;
87     }
88     if (EOK != memset_s(pBuf, lenAdd, 0, lenAdd)) {
89         delete []pBuf;
90         ELOG("pBuf memset_s failed.");
91         return str;
92     }
93     WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
94     std::string retStr = pBuf;
95     delete []pBuf;
96     delete []pwBuf;
97     pBuf = NULL;
98     pwBuf = NULL;
99     return retStr;
100 }