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 16 #include <cstdint> 17 #include <cstdlib> 18 #include <chrono> 19 #include <string> 20 #include <map> 21 #include "i18n_struct.h" 22 #include "i18n_hilog.h" 23 #include "i18n_index_util_ffi.h" 24 #include "i18n_index_util_impl.h" 25 #include "index_util.h" 26 #include <utility> 27 28 namespace OHOS { 29 namespace Global { 30 namespace I18n { 31 using namespace OHOS::FFI; 32 extern "C" 33 { FfiI18nIndexUtilConstructor(char * locale)34 int64_t FfiI18nIndexUtilConstructor(char* locale) 35 { 36 std::unique_ptr<IndexUtil> indexUtil_ = std::make_unique<IndexUtil>(std::string(locale)); 37 if (indexUtil_ == nullptr) { 38 HILOG_ERROR_I18N("Create IndexUtil fail"); 39 return -1; 40 } 41 auto ffiIndexUtilInstance = FFIData::Create<FfiI18nIndexUtil>(std::move(indexUtil_)); 42 if (ffiIndexUtilInstance == nullptr) { 43 HILOG_ERROR_I18N("Create FfiI18nIndexUtil fail"); 44 return -1; 45 } 46 return ffiIndexUtilInstance -> GetID(); 47 } 48 FfiI18nIndexUtilAddLocale(int64_t remoteDataID,char * locale)49 void FfiI18nIndexUtilAddLocale(int64_t remoteDataID, char* locale) 50 { 51 auto ffiIndexUtilInstance = FFIData::GetData<FfiI18nIndexUtil>(remoteDataID); 52 if (!ffiIndexUtilInstance) { 53 HILOG_ERROR_I18N("The FfiI18nIndexUtil instance is nullptr"); 54 return; 55 } 56 ffiIndexUtilInstance->addLocale(std::string(locale)); 57 } 58 FfiI18nIndexUtilGetIndex(int64_t remoteDataID,char * text)59 char* FfiI18nIndexUtilGetIndex(int64_t remoteDataID, char* text) 60 { 61 auto ffiIndexUtilInstance = FFIData::GetData<FfiI18nIndexUtil>(remoteDataID); 62 if (!ffiIndexUtilInstance) { 63 HILOG_ERROR_I18N("The FfiI18nIndexUtil instance is nullptr"); 64 return nullptr; 65 } 66 return ffiIndexUtilInstance->getIndex(std::string(text)); 67 } 68 FfiI18nIndexUtilGetIndexList(int64_t remoteDataID)69 CArrStr FfiI18nIndexUtilGetIndexList(int64_t remoteDataID) 70 { 71 auto ffiIndexUtilInstance = FFIData::GetData<FfiI18nIndexUtil>(remoteDataID); 72 if (!ffiIndexUtilInstance) { 73 HILOG_ERROR_I18N("The FfiI18nIndexUtil instance is nullptr"); 74 return { nullptr, 0 }; 75 } 76 return ffiIndexUtilInstance->getIndexList(); 77 } 78 } 79 } // namespace I18n 80 } // namespace Global 81 } // namespace OHOS