• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <vector>
22 #include "i18n_hilog.h"
23 #include "i18n_struct.h"
24 #include "i18n_phone_number_format_ffi.h"
25 #include "i18n_phone_number_format_impl.h"
26 #include <utility>
27 
28 namespace OHOS {
29 namespace Global {
30 namespace I18n {
31 using namespace OHOS::FFI;
32 using namespace OHOS::HiviewDFX;
33 extern "C"
34 {
FfiI18nPhoneNumberFormatConstructor(char * country,char * formatType)35     int64_t FfiI18nPhoneNumberFormatConstructor(char* country, char* formatType)
36     {
37         std::map<std::string, std::string> options;
38         if (formatType != nullptr) {
39             options.insert(std::make_pair("type", std::string(formatType)));
40         }
41         std::unique_ptr<PhoneNumberFormat> phoneNumberFormatInstance
42             = PhoneNumberFormat::CreateInstance(country, options);
43         if (phoneNumberFormatInstance == nullptr) {
44             HILOG_ERROR_I18N("Create PhoneNumberFormat fail");
45             return -1;
46         }
47         auto ffiI18nPhoneNumberFormatInstance
48             = FFIData::Create<FfiI18nPhoneNumberFormat>(std::move(phoneNumberFormatInstance));
49         if (ffiI18nPhoneNumberFormatInstance == nullptr) {
50             HILOG_ERROR_I18N("Create FfiI18nPhoneNumberFormat fail");
51             return -1;
52         }
53         return ffiI18nPhoneNumberFormatInstance->GetID();
54     }
55 
FfiI18nPhoneNumberFormatFormat(int64_t remoteDataID,char * number)56     char* FfiI18nPhoneNumberFormatFormat(int64_t remoteDataID, char* number)
57     {
58         auto format = FFIData::GetData<FfiI18nPhoneNumberFormat>(remoteDataID);
59         if (!format) {
60             HILOG_ERROR_I18N("The FfiI18nPhoneNumberFormat instance is nullptr");
61             return nullptr;
62         }
63         return format->format(std::string(number));
64     }
FfiI18nPhoneNumberFormatIsValidNumber(int64_t remoteDataID,char * number)65     bool FfiI18nPhoneNumberFormatIsValidNumber(int64_t remoteDataID, char* number)
66     {
67         auto format = FFIData::GetData<FfiI18nPhoneNumberFormat>(remoteDataID);
68         if (!format) {
69             HILOG_ERROR_I18N("The FfiI18nPhoneNumberFormat instance is nullptr");
70             return false;
71         }
72         return format->isValidNumber(std::string(number));
73     }
FfiI18nPhoneNumberFormatGetLocationName(int64_t remoteDataID,char * number,char * locale)74     char* FfiI18nPhoneNumberFormatGetLocationName(int64_t remoteDataID, char* number, char* locale)
75     {
76         auto format = FFIData::GetData<FfiI18nPhoneNumberFormat>(remoteDataID);
77         if (!format) {
78             HILOG_ERROR_I18N("The FfiI18nPhoneNumberFormat instance is nullptr");
79             return nullptr;
80         }
81         return format->getLocationName(std::string(number), std::string(locale));
82     }
83 }
84 }  // namespace I18n
85 }  // namespace Global
86 }  // namespace OHOS