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 <cstdint> 16 #include "i18n_ffi.h" 17 #include "i18n_struct.h" 18 #include "locale_config.h" 19 20 namespace OHOS { 21 namespace Global { 22 namespace I18n { 23 extern "C" 24 { MallocCString(const std::string & origin)25char* MallocCString(const std::string& origin) 26 { 27 if (origin.empty()) { 28 return nullptr; 29 } 30 auto length = origin.length() + 1; 31 char* res = static_cast<char*>(malloc(sizeof(char) * length)); 32 if (res == nullptr) { 33 return nullptr; 34 } 35 return std::char_traits<char>::copy(res, origin.c_str(), length); 36 } 37 VectorStringToCArr(const std::vector<std::string> & vectorString)38CArrStr VectorStringToCArr(const std::vector<std::string>& vectorString) 39 { 40 CArrStr strArray; 41 strArray.length = static_cast<int64_t>(vectorString.size()); 42 strArray.data = static_cast<char**>(malloc(strArray.length * sizeof(char*))); 43 if (strArray.data == nullptr) { 44 return CArrStr{0}; 45 } 46 for (int64_t i = 0; i < strArray.length; i++) { 47 strArray.data[i] = MallocCString(vectorString[i]); 48 } 49 return strArray; 50 } 51 FfiOHOSIsRTL(char * locale)52bool FfiOHOSIsRTL(char* locale) 53 { 54 bool isRTL = LocaleConfig::IsRTL(std::string(locale)); 55 return isRTL; 56 } 57 } 58 } // namespace I18n 59 } // namespace Global 60 } // namespace OHOS