1 /*
2  * Copyright (c) 2024 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 #include "utils.h"
16 
17 namespace OHOS::MiscServices {
MallocCString(const std::string &origin)18 char* Utils::MallocCString(const std::string &origin)
19 {
20     if (origin.empty()) {
21         return nullptr;
22     }
23     auto len = origin.length() + 1;
24     char *res = static_cast<char *>(malloc(sizeof(char) * len));
25     if (res == nullptr) {
26         return nullptr;
27     }
28     return std::char_traits<char>::copy(res, origin.c_str(), len);
29 }
30 
InputMethodProperty2C(CInputMethodProperty &props, const Property &property)31 void Utils::InputMethodProperty2C(CInputMethodProperty &props, const Property &property)
32 {
33     props.name = Utils::MallocCString(property.name);
34     props.id = Utils::MallocCString(property.id);
35     props.label = Utils::MallocCString(property.label);
36     props.labelId = property.labelId;
37     props.icon = Utils::MallocCString(property.icon);
38     props.iconId = property.iconId;
39 }
40 
C2InputMethodProperty(CInputMethodProperty props)41 Property Utils::C2InputMethodProperty(CInputMethodProperty props)
42 {
43     Property property;
44     property.name = std::string(props.name);
45     property.id = std::string(props.id);
46     property.label = std::string(props.label);
47     property.labelId = props.labelId;
48     property.icon = std::string(props.icon);
49     property.iconId = props.iconId;
50     return property;
51 }
52 
InputMethodSubProperty2C(CInputMethodSubtype &props, const SubProperty &property)53 void Utils::InputMethodSubProperty2C(CInputMethodSubtype &props, const SubProperty &property)
54 {
55     props.name = Utils::MallocCString(property.name);
56     props.id = Utils::MallocCString(property.id);
57     props.label = Utils::MallocCString(property.label);
58     props.labelId = property.labelId;
59     props.icon = Utils::MallocCString(property.icon);
60     props.iconId = property.iconId;
61     props.mode = Utils::MallocCString(property.mode);
62     props.locale = Utils::MallocCString(property.locale);
63     props.language = Utils::MallocCString(property.language);
64 }
65 }