• 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 "i18n_holiday_ffi.h"
17 #include "i18n_struct.h"
18 #include "ffi_remote_data.h"
19 #include "i18n_ffi.h"
20 #include "i18n_holiday_impl.h"
21 #include "i18n_hilog.h"
22 
23 namespace OHOS {
24 namespace Global {
25 namespace I18n {
26 using namespace OHOS::FFI;
27 #define HOLIDAY_OPT_SUCCESS 0
28 #define HOLIDAY_OPT_FAILED (-1)
29 extern "C" {
FfiOHOSHolidayManagerImplConstructor(char * icsPath)30 int64_t FfiOHOSHolidayManagerImplConstructor(char* icsPath)
31 {
32     auto instance = FFIData::Create<CHolidayManager>(icsPath);
33     if (instance == nullptr) {
34         HILOG_ERROR_I18N("Create CHolidayManager fail");
35         return -1;
36     }
37     return instance->GetID();
38 }
39 
FfiOHOSHolidayManagerIsHoliday(int64_t id,CDate date)40 bool FfiOHOSHolidayManagerIsHoliday(int64_t id, CDate date)
41 {
42     auto instance = FFIData::GetData<CHolidayManager>(id);
43     if (!instance) {
44         HILOG_ERROR_I18N("The CHolidayManager instance is nullptr");
45         return false;
46     }
47     if (date.isNull) {
48         return instance->isHoliday();
49     } else {
50         return instance->isHoliday(date.year, date.month, date.day);
51     }
52 }
53 
HolidayLocalNames2Cj(const std::vector<HolidayLocalName> & localNames,HolidayLocalNameArr & arr)54 static int32_t HolidayLocalNames2Cj(const std::vector<HolidayLocalName>& localNames, HolidayLocalNameArr &arr)
55 {
56     int64_t size = static_cast<int64_t>(localNames.size());
57     if (size > 0) {
58         arr.head = static_cast<CHolidayLocalName *>(malloc(sizeof(CHolidayLocalName) * size));
59         if (arr.head == nullptr) {
60             return HOLIDAY_OPT_FAILED;
61         }
62         arr.size = size;
63 
64         uint32_t idx = 0;
65         for (const auto& each : localNames) {
66             CHolidayLocalName info;
67             info.language = MallocCString(each.language);
68             info.name = MallocCString(each.name);
69             arr.head[idx] = info;
70             idx++;
71         }
72     } else {
73         arr.head = nullptr;
74         arr.size = 0;
75     }
76 
77     return HOLIDAY_OPT_SUCCESS;
78 }
79 
FreeHolidayLocalNameArr(HolidayLocalNameArr & arr)80 static void FreeHolidayLocalNameArr(HolidayLocalNameArr &arr)
81 {
82     for (int64_t index = 0; index < arr.size; index++) {
83         free(arr.head[index].language);
84         free(arr.head[index].name);
85     }
86 }
87 
HolidayInfo2Cj(const std::vector<HolidayInfoItem> & items,HolidayInfoItemArr & arr)88 static int32_t HolidayInfo2Cj(const std::vector<HolidayInfoItem>& items, HolidayInfoItemArr &arr)
89 {
90     int64_t size = static_cast<int64_t>(items.size());
91     if (size <= 0) {
92         arr.head = nullptr;
93         arr.size = 0;
94         return HOLIDAY_OPT_SUCCESS;
95     }
96     arr.head = static_cast<CHolidayInfoItem*>(malloc(sizeof(CHolidayInfoItem) * size));
97     if (arr.head == nullptr) {
98         return HOLIDAY_OPT_FAILED;
99     }
100     uint32_t idx = 0;
101     for (const auto& each : items) {
102         CHolidayInfoItem info;
103         info.baseName = MallocCString(each.baseName);
104         info.day = each.day;
105         info.month = each.month;
106         info.year = each.year;
107 
108         if (HolidayLocalNames2Cj(each.localNames, info.localNames) != HOLIDAY_OPT_SUCCESS) {
109             for (uint32_t index = 0; index < idx; index++) {
110                 free(arr.head[index].baseName);
111                 FreeHolidayLocalNameArr(arr.head[index].localNames);
112                 free(arr.head[index].localNames.head);
113             }
114             free(info.baseName);
115             free(arr.head);
116             return HOLIDAY_OPT_FAILED;
117         }
118 
119         arr.head[idx] = info;
120         idx++;
121     }
122     arr.size = idx;
123     return HOLIDAY_OPT_SUCCESS;
124 }
125 
FfiOHOSHolidayManagerGetHolidayInfoItemArray(int64_t id,int32_t year)126 HolidayInfoItemArr FfiOHOSHolidayManagerGetHolidayInfoItemArray(int64_t id, int32_t year)
127 {
128     auto instance = FFIData::GetData<CHolidayManager>(id);
129     std::vector<HolidayInfoItem> result;
130     HolidayInfoItemArr infos = { 0, nullptr };
131     if (year == -1) {
132         result = instance->getHolidayInfoItemArray();
133     } else {
134         result = instance->getHolidayInfoItemArray(year);
135     }
136 
137     HolidayInfo2Cj(result, infos);
138     return infos;
139 }
140 }
141 }  // namespace I18n
142 }  // namespace Global
143 }  // namespace OHOS
144