136a3a8d0Sopenharmony_ci/* 236a3a8d0Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 336a3a8d0Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 436a3a8d0Sopenharmony_ci * you may not use this file except in compliance with the License. 536a3a8d0Sopenharmony_ci * You may obtain a copy of the License at 636a3a8d0Sopenharmony_ci * 736a3a8d0Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 836a3a8d0Sopenharmony_ci * 936a3a8d0Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1036a3a8d0Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1136a3a8d0Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1236a3a8d0Sopenharmony_ci * See the License for the specific language governing permissions and 1336a3a8d0Sopenharmony_ci * limitations under the License. 1436a3a8d0Sopenharmony_ci */ 1536a3a8d0Sopenharmony_ci 1636a3a8d0Sopenharmony_ci#include "utils.h" 1736a3a8d0Sopenharmony_ci 1836a3a8d0Sopenharmony_cichar* Utils::MallocCString(const std::string& origin) 1936a3a8d0Sopenharmony_ci{ 2036a3a8d0Sopenharmony_ci if (origin.empty()) { 2136a3a8d0Sopenharmony_ci return nullptr; 2236a3a8d0Sopenharmony_ci } 2336a3a8d0Sopenharmony_ci auto len = origin.length() + 1; 2436a3a8d0Sopenharmony_ci char* res = static_cast<char*>(malloc(sizeof(char) * len)); 2536a3a8d0Sopenharmony_ci if (res == nullptr) { 2636a3a8d0Sopenharmony_ci return nullptr; 2736a3a8d0Sopenharmony_ci } 2836a3a8d0Sopenharmony_ci return std::char_traits<char>::copy(res, origin.c_str(), len); 2936a3a8d0Sopenharmony_ci}