122736c2fSopenharmony_ci/*
222736c2fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
322736c2fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
422736c2fSopenharmony_ci * you may not use this file except in compliance with the License.
522736c2fSopenharmony_ci * You may obtain a copy of the License at
622736c2fSopenharmony_ci *
722736c2fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
822736c2fSopenharmony_ci *
922736c2fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1022736c2fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1122736c2fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1222736c2fSopenharmony_ci * See the License for the specific language governing permissions and
1322736c2fSopenharmony_ci * limitations under the License.
1422736c2fSopenharmony_ci */
1522736c2fSopenharmony_ci#include "utils.h"
1622736c2fSopenharmony_ci
1722736c2fSopenharmony_cinamespace OHOS::MiscServices {
1822736c2fSopenharmony_cichar* Utils::MallocCString(const std::string &origin)
1922736c2fSopenharmony_ci{
2022736c2fSopenharmony_ci    if (origin.empty()) {
2122736c2fSopenharmony_ci        return nullptr;
2222736c2fSopenharmony_ci    }
2322736c2fSopenharmony_ci    auto len = origin.length() + 1;
2422736c2fSopenharmony_ci    char *res = static_cast<char *>(malloc(sizeof(char) * len));
2522736c2fSopenharmony_ci    if (res == nullptr) {
2622736c2fSopenharmony_ci        return nullptr;
2722736c2fSopenharmony_ci    }
2822736c2fSopenharmony_ci    return std::char_traits<char>::copy(res, origin.c_str(), len);
2922736c2fSopenharmony_ci}
3022736c2fSopenharmony_ci
3122736c2fSopenharmony_civoid Utils::InputMethodProperty2C(CInputMethodProperty &props, const Property &property)
3222736c2fSopenharmony_ci{
3322736c2fSopenharmony_ci    props.name = Utils::MallocCString(property.name);
3422736c2fSopenharmony_ci    props.id = Utils::MallocCString(property.id);
3522736c2fSopenharmony_ci    props.label = Utils::MallocCString(property.label);
3622736c2fSopenharmony_ci    props.labelId = property.labelId;
3722736c2fSopenharmony_ci    props.icon = Utils::MallocCString(property.icon);
3822736c2fSopenharmony_ci    props.iconId = property.iconId;
3922736c2fSopenharmony_ci}
4022736c2fSopenharmony_ci
4122736c2fSopenharmony_ciProperty Utils::C2InputMethodProperty(CInputMethodProperty props)
4222736c2fSopenharmony_ci{
4322736c2fSopenharmony_ci    Property property;
4422736c2fSopenharmony_ci    property.name = std::string(props.name);
4522736c2fSopenharmony_ci    property.id = std::string(props.id);
4622736c2fSopenharmony_ci    property.label = std::string(props.label);
4722736c2fSopenharmony_ci    property.labelId = props.labelId;
4822736c2fSopenharmony_ci    property.icon = std::string(props.icon);
4922736c2fSopenharmony_ci    property.iconId = props.iconId;
5022736c2fSopenharmony_ci    return property;
5122736c2fSopenharmony_ci}
5222736c2fSopenharmony_ci
5322736c2fSopenharmony_civoid Utils::InputMethodSubProperty2C(CInputMethodSubtype &props, const SubProperty &property)
5422736c2fSopenharmony_ci{
5522736c2fSopenharmony_ci    props.name = Utils::MallocCString(property.name);
5622736c2fSopenharmony_ci    props.id = Utils::MallocCString(property.id);
5722736c2fSopenharmony_ci    props.label = Utils::MallocCString(property.label);
5822736c2fSopenharmony_ci    props.labelId = property.labelId;
5922736c2fSopenharmony_ci    props.icon = Utils::MallocCString(property.icon);
6022736c2fSopenharmony_ci    props.iconId = property.iconId;
6122736c2fSopenharmony_ci    props.mode = Utils::MallocCString(property.mode);
6222736c2fSopenharmony_ci    props.locale = Utils::MallocCString(property.locale);
6322736c2fSopenharmony_ci    props.language = Utils::MallocCString(property.language);
6422736c2fSopenharmony_ci}
6522736c2fSopenharmony_ci}