17c804472Sopenharmony_ci/*
27c804472Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
37c804472Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
47c804472Sopenharmony_ci * you may not use this file except in compliance with the License.
57c804472Sopenharmony_ci * You may obtain a copy of the License at
67c804472Sopenharmony_ci *
77c804472Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
87c804472Sopenharmony_ci *
97c804472Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
107c804472Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
117c804472Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127c804472Sopenharmony_ci * See the License for the specific language governing permissions and
137c804472Sopenharmony_ci * limitations under the License.
147c804472Sopenharmony_ci */
157c804472Sopenharmony_ci
167c804472Sopenharmony_ci#include "StringHelper.h"
177c804472Sopenharmony_ci#include <windows.h>
187c804472Sopenharmony_ci#include "securec.h"
197c804472Sopenharmony_ci#include "PreviewerEngineLog.h"
207c804472Sopenharmony_ci
217c804472Sopenharmony_cistd::string StringHelper::StringToUtf8(const std::string& str)
227c804472Sopenharmony_ci{
237c804472Sopenharmony_ci    int doubles = 2;
247c804472Sopenharmony_ci    int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
257c804472Sopenharmony_ci    if (nwLen < 1) {
267c804472Sopenharmony_ci        ELOG("MultiByteToWideChar failed.");
277c804472Sopenharmony_ci        return str;
287c804472Sopenharmony_ci    }
297c804472Sopenharmony_ci    wchar_t* pwBuf = new(std::nothrow) wchar_t[nwLen + 1];
307c804472Sopenharmony_ci    if (!pwBuf) {
317c804472Sopenharmony_ci        ELOG("Memory allocation failed : pwBuf.");
327c804472Sopenharmony_ci        return str;
337c804472Sopenharmony_ci    }
347c804472Sopenharmony_ci    ZeroMemory(pwBuf, (nwLen + 1) * doubles);
357c804472Sopenharmony_ci    ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);
367c804472Sopenharmony_ci    int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
377c804472Sopenharmony_ci    if (nLen < 1) {
387c804472Sopenharmony_ci        delete[] pwBuf;
397c804472Sopenharmony_ci        ELOG("WideCharToMultiByte failed.");
407c804472Sopenharmony_ci        return str;
417c804472Sopenharmony_ci    }
427c804472Sopenharmony_ci    char* pBuf = new(std::nothrow) char[nLen + 1];
437c804472Sopenharmony_ci    if (!pBuf) {
447c804472Sopenharmony_ci        ELOG("Memory allocation failed : pBuf.");
457c804472Sopenharmony_ci        return str;
467c804472Sopenharmony_ci    }
477c804472Sopenharmony_ci    ZeroMemory(pBuf, nLen + 1);
487c804472Sopenharmony_ci    ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
497c804472Sopenharmony_ci    std::string retStr(pBuf);
507c804472Sopenharmony_ci    delete[] pwBuf;
517c804472Sopenharmony_ci    delete[] pBuf;
527c804472Sopenharmony_ci    pwBuf = NULL;
537c804472Sopenharmony_ci    pBuf = NULL;
547c804472Sopenharmony_ci    return retStr;
557c804472Sopenharmony_ci}
567c804472Sopenharmony_ci
577c804472Sopenharmony_cistd::string StringHelper::Utf8ToString(const std::string& str)
587c804472Sopenharmony_ci{
597c804472Sopenharmony_ci    int doubles = 2;
607c804472Sopenharmony_ci    int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
617c804472Sopenharmony_ci    int wLenAdd = nwLen + 1;
627c804472Sopenharmony_ci    if (wLenAdd <= 1) {
637c804472Sopenharmony_ci        return str;
647c804472Sopenharmony_ci    }
657c804472Sopenharmony_ci    wchar_t* pwBuf = new(std::nothrow) wchar_t[wLenAdd];
667c804472Sopenharmony_ci    if (!pwBuf) {
677c804472Sopenharmony_ci        ELOG("Memory allocation failed : pwBuf.");
687c804472Sopenharmony_ci        return str;
697c804472Sopenharmony_ci    }
707c804472Sopenharmony_ci    int doubleLen = wLenAdd * doubles;
717c804472Sopenharmony_ci    if (EOK != memset_s(pwBuf, sizeof(*pwBuf) * doubles, 0, doubleLen)) {
727c804472Sopenharmony_ci        delete []pwBuf;
737c804472Sopenharmony_ci        ELOG("pwBuf memset_s failed.");
747c804472Sopenharmony_ci        return str;
757c804472Sopenharmony_ci    }
767c804472Sopenharmony_ci    MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);
777c804472Sopenharmony_ci    int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
787c804472Sopenharmony_ci    int lenAdd = nLen + 1;
797c804472Sopenharmony_ci    if (lenAdd <= 1) {
807c804472Sopenharmony_ci        delete []pwBuf;
817c804472Sopenharmony_ci        return str;
827c804472Sopenharmony_ci    }
837c804472Sopenharmony_ci    char* pBuf = new(std::nothrow) char[lenAdd];
847c804472Sopenharmony_ci    if (!pBuf) {
857c804472Sopenharmony_ci        ELOG("Memory allocation failed : pBuf.");
867c804472Sopenharmony_ci        return str;
877c804472Sopenharmony_ci    }
887c804472Sopenharmony_ci    if (EOK != memset_s(pBuf, lenAdd, 0, lenAdd)) {
897c804472Sopenharmony_ci        delete []pBuf;
907c804472Sopenharmony_ci        ELOG("pBuf memset_s failed.");
917c804472Sopenharmony_ci        return str;
927c804472Sopenharmony_ci    }
937c804472Sopenharmony_ci    WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
947c804472Sopenharmony_ci    std::string retStr = pBuf;
957c804472Sopenharmony_ci    delete []pBuf;
967c804472Sopenharmony_ci    delete []pwBuf;
977c804472Sopenharmony_ci    pBuf = NULL;
987c804472Sopenharmony_ci    pwBuf = NULL;
997c804472Sopenharmony_ci    return retStr;
1007c804472Sopenharmony_ci}