/src/open5gs/lib/sbi/openapi/model/recur_time.c
Line | Count | Source |
1 | | |
2 | | #include <stdlib.h> |
3 | | #include <string.h> |
4 | | #include <stdio.h> |
5 | | #include "recur_time.h" |
6 | | |
7 | | OpenAPI_recur_time_t *OpenAPI_recur_time_create( |
8 | | OpenAPI_valid_time_period_t *recur_time_window, |
9 | | OpenAPI_recur_type_e recur_type, |
10 | | OpenAPI_list_t *recur_month, |
11 | | OpenAPI_list_t *recur_week, |
12 | | OpenAPI_list_t *recur_day, |
13 | | OpenAPI_list_t *recur_date, |
14 | | char *recur_end_time |
15 | | ) |
16 | 0 | { |
17 | 0 | OpenAPI_recur_time_t *recur_time_local_var = ogs_malloc(sizeof(OpenAPI_recur_time_t)); |
18 | 0 | ogs_assert(recur_time_local_var); |
19 | | |
20 | 0 | recur_time_local_var->recur_time_window = recur_time_window; |
21 | 0 | recur_time_local_var->recur_type = recur_type; |
22 | 0 | recur_time_local_var->recur_month = recur_month; |
23 | 0 | recur_time_local_var->recur_week = recur_week; |
24 | 0 | recur_time_local_var->recur_day = recur_day; |
25 | 0 | recur_time_local_var->recur_date = recur_date; |
26 | 0 | recur_time_local_var->recur_end_time = recur_end_time; |
27 | |
|
28 | 0 | return recur_time_local_var; |
29 | 0 | } |
30 | | |
31 | | void OpenAPI_recur_time_free(OpenAPI_recur_time_t *recur_time) |
32 | 0 | { |
33 | 0 | OpenAPI_lnode_t *node = NULL; |
34 | |
|
35 | 0 | if (NULL == recur_time) { |
36 | 0 | return; |
37 | 0 | } |
38 | 0 | if (recur_time->recur_time_window) { |
39 | 0 | OpenAPI_valid_time_period_free(recur_time->recur_time_window); |
40 | 0 | recur_time->recur_time_window = NULL; |
41 | 0 | } |
42 | 0 | if (recur_time->recur_month) { |
43 | 0 | OpenAPI_list_for_each(recur_time->recur_month, node) { |
44 | 0 | ogs_free(node->data); |
45 | 0 | } |
46 | 0 | OpenAPI_list_free(recur_time->recur_month); |
47 | 0 | recur_time->recur_month = NULL; |
48 | 0 | } |
49 | 0 | if (recur_time->recur_week) { |
50 | 0 | OpenAPI_list_for_each(recur_time->recur_week, node) { |
51 | 0 | ogs_free(node->data); |
52 | 0 | } |
53 | 0 | OpenAPI_list_free(recur_time->recur_week); |
54 | 0 | recur_time->recur_week = NULL; |
55 | 0 | } |
56 | 0 | if (recur_time->recur_day) { |
57 | 0 | OpenAPI_list_for_each(recur_time->recur_day, node) { |
58 | 0 | ogs_free(node->data); |
59 | 0 | } |
60 | 0 | OpenAPI_list_free(recur_time->recur_day); |
61 | 0 | recur_time->recur_day = NULL; |
62 | 0 | } |
63 | 0 | if (recur_time->recur_date) { |
64 | 0 | OpenAPI_list_for_each(recur_time->recur_date, node) { |
65 | 0 | ogs_free(node->data); |
66 | 0 | } |
67 | 0 | OpenAPI_list_free(recur_time->recur_date); |
68 | 0 | recur_time->recur_date = NULL; |
69 | 0 | } |
70 | 0 | if (recur_time->recur_end_time) { |
71 | 0 | ogs_free(recur_time->recur_end_time); |
72 | 0 | recur_time->recur_end_time = NULL; |
73 | 0 | } |
74 | 0 | ogs_free(recur_time); |
75 | 0 | } |
76 | | |
77 | | cJSON *OpenAPI_recur_time_convertToJSON(OpenAPI_recur_time_t *recur_time) |
78 | 0 | { |
79 | 0 | cJSON *item = NULL; |
80 | 0 | OpenAPI_lnode_t *node = NULL; |
81 | |
|
82 | 0 | if (recur_time == NULL) { |
83 | 0 | ogs_error("OpenAPI_recur_time_convertToJSON() failed [RecurTime]"); |
84 | 0 | return NULL; |
85 | 0 | } |
86 | | |
87 | 0 | item = cJSON_CreateObject(); |
88 | 0 | if (recur_time->recur_time_window) { |
89 | 0 | cJSON *recur_time_window_local_JSON = OpenAPI_valid_time_period_convertToJSON(recur_time->recur_time_window); |
90 | 0 | if (recur_time_window_local_JSON == NULL) { |
91 | 0 | ogs_error("OpenAPI_recur_time_convertToJSON() failed [recur_time_window]"); |
92 | 0 | goto end; |
93 | 0 | } |
94 | 0 | cJSON_AddItemToObject(item, "recurTimeWindow", recur_time_window_local_JSON); |
95 | 0 | if (item->child == NULL) { |
96 | 0 | ogs_error("OpenAPI_recur_time_convertToJSON() failed [recur_time_window]"); |
97 | 0 | goto end; |
98 | 0 | } |
99 | 0 | } |
100 | | |
101 | 0 | if (recur_time->recur_type != OpenAPI_recur_type_NULL) { |
102 | 0 | if (cJSON_AddStringToObject(item, "recurType", OpenAPI_recur_type_ToString(recur_time->recur_type)) == NULL) { |
103 | 0 | ogs_error("OpenAPI_recur_time_convertToJSON() failed [recur_type]"); |
104 | 0 | goto end; |
105 | 0 | } |
106 | 0 | } |
107 | | |
108 | 0 | if (recur_time->recur_month) { |
109 | 0 | cJSON *recur_monthList = cJSON_AddArrayToObject(item, "recurMonth"); |
110 | 0 | if (recur_monthList == NULL) { |
111 | 0 | ogs_error("OpenAPI_recur_time_convertToJSON() failed [recur_month]"); |
112 | 0 | goto end; |
113 | 0 | } |
114 | 0 | OpenAPI_list_for_each(recur_time->recur_month, node) { |
115 | 0 | if (node->data == NULL) { |
116 | 0 | ogs_error("OpenAPI_recur_time_convertToJSON() failed [recur_month]"); |
117 | 0 | goto end; |
118 | 0 | } |
119 | 0 | if (cJSON_AddNumberToObject(recur_monthList, "", *(double *)node->data) == NULL) { |
120 | 0 | ogs_error("OpenAPI_recur_time_convertToJSON() failed [recur_month]"); |
121 | 0 | goto end; |
122 | 0 | } |
123 | 0 | } |
124 | 0 | } |
125 | | |
126 | 0 | if (recur_time->recur_week) { |
127 | 0 | cJSON *recur_weekList = cJSON_AddArrayToObject(item, "recurWeek"); |
128 | 0 | if (recur_weekList == NULL) { |
129 | 0 | ogs_error("OpenAPI_recur_time_convertToJSON() failed [recur_week]"); |
130 | 0 | goto end; |
131 | 0 | } |
132 | 0 | OpenAPI_list_for_each(recur_time->recur_week, node) { |
133 | 0 | if (node->data == NULL) { |
134 | 0 | ogs_error("OpenAPI_recur_time_convertToJSON() failed [recur_week]"); |
135 | 0 | goto end; |
136 | 0 | } |
137 | 0 | if (cJSON_AddNumberToObject(recur_weekList, "", *(double *)node->data) == NULL) { |
138 | 0 | ogs_error("OpenAPI_recur_time_convertToJSON() failed [recur_week]"); |
139 | 0 | goto end; |
140 | 0 | } |
141 | 0 | } |
142 | 0 | } |
143 | | |
144 | 0 | if (recur_time->recur_day) { |
145 | 0 | cJSON *recur_dayList = cJSON_AddArrayToObject(item, "recurDay"); |
146 | 0 | if (recur_dayList == NULL) { |
147 | 0 | ogs_error("OpenAPI_recur_time_convertToJSON() failed [recur_day]"); |
148 | 0 | goto end; |
149 | 0 | } |
150 | 0 | OpenAPI_list_for_each(recur_time->recur_day, node) { |
151 | 0 | if (node->data == NULL) { |
152 | 0 | ogs_error("OpenAPI_recur_time_convertToJSON() failed [recur_day]"); |
153 | 0 | goto end; |
154 | 0 | } |
155 | 0 | if (cJSON_AddNumberToObject(recur_dayList, "", *(double *)node->data) == NULL) { |
156 | 0 | ogs_error("OpenAPI_recur_time_convertToJSON() failed [recur_day]"); |
157 | 0 | goto end; |
158 | 0 | } |
159 | 0 | } |
160 | 0 | } |
161 | | |
162 | 0 | if (recur_time->recur_date) { |
163 | 0 | cJSON *recur_dateList = cJSON_AddArrayToObject(item, "recurDate"); |
164 | 0 | if (recur_dateList == NULL) { |
165 | 0 | ogs_error("OpenAPI_recur_time_convertToJSON() failed [recur_date]"); |
166 | 0 | goto end; |
167 | 0 | } |
168 | 0 | OpenAPI_list_for_each(recur_time->recur_date, node) { |
169 | 0 | } |
170 | 0 | } |
171 | | |
172 | 0 | if (recur_time->recur_end_time) { |
173 | 0 | if (cJSON_AddStringToObject(item, "recurEndTime", recur_time->recur_end_time) == NULL) { |
174 | 0 | ogs_error("OpenAPI_recur_time_convertToJSON() failed [recur_end_time]"); |
175 | 0 | goto end; |
176 | 0 | } |
177 | 0 | } |
178 | | |
179 | 0 | end: |
180 | 0 | return item; |
181 | 0 | } |
182 | | |
183 | | OpenAPI_recur_time_t *OpenAPI_recur_time_parseFromJSON(cJSON *recur_timeJSON) |
184 | 0 | { |
185 | 0 | OpenAPI_recur_time_t *recur_time_local_var = NULL; |
186 | 0 | OpenAPI_lnode_t *node = NULL; |
187 | 0 | cJSON *recur_time_window = NULL; |
188 | 0 | OpenAPI_valid_time_period_t *recur_time_window_local_nonprim = NULL; |
189 | 0 | cJSON *recur_type = NULL; |
190 | 0 | OpenAPI_recur_type_e recur_typeVariable = 0; |
191 | 0 | cJSON *recur_month = NULL; |
192 | 0 | OpenAPI_list_t *recur_monthList = NULL; |
193 | 0 | cJSON *recur_week = NULL; |
194 | 0 | OpenAPI_list_t *recur_weekList = NULL; |
195 | 0 | cJSON *recur_day = NULL; |
196 | 0 | OpenAPI_list_t *recur_dayList = NULL; |
197 | 0 | cJSON *recur_date = NULL; |
198 | 0 | OpenAPI_list_t *recur_dateList = NULL; |
199 | 0 | cJSON *recur_end_time = NULL; |
200 | 0 | recur_time_window = cJSON_GetObjectItemCaseSensitive(recur_timeJSON, "recurTimeWindow"); |
201 | 0 | if (recur_time_window) { |
202 | 0 | recur_time_window_local_nonprim = OpenAPI_valid_time_period_parseFromJSON(recur_time_window); |
203 | 0 | if (!recur_time_window_local_nonprim) { |
204 | 0 | ogs_error("OpenAPI_valid_time_period_parseFromJSON failed [recur_time_window]"); |
205 | 0 | goto end; |
206 | 0 | } |
207 | 0 | } |
208 | | |
209 | 0 | recur_type = cJSON_GetObjectItemCaseSensitive(recur_timeJSON, "recurType"); |
210 | 0 | if (recur_type) { |
211 | 0 | if (!cJSON_IsString(recur_type)) { |
212 | 0 | ogs_error("OpenAPI_recur_time_parseFromJSON() failed [recur_type]"); |
213 | 0 | goto end; |
214 | 0 | } |
215 | 0 | recur_typeVariable = OpenAPI_recur_type_FromString(recur_type->valuestring); |
216 | 0 | } |
217 | | |
218 | 0 | recur_month = cJSON_GetObjectItemCaseSensitive(recur_timeJSON, "recurMonth"); |
219 | 0 | if (recur_month) { |
220 | 0 | cJSON *recur_month_local = NULL; |
221 | 0 | if (!cJSON_IsArray(recur_month)) { |
222 | 0 | ogs_error("OpenAPI_recur_time_parseFromJSON() failed [recur_month]"); |
223 | 0 | goto end; |
224 | 0 | } |
225 | | |
226 | 0 | recur_monthList = OpenAPI_list_create(); |
227 | |
|
228 | 0 | cJSON_ArrayForEach(recur_month_local, recur_month) { |
229 | 0 | double *localDouble = NULL; |
230 | 0 | int *localInt = NULL; |
231 | 0 | if (!cJSON_IsNumber(recur_month_local)) { |
232 | 0 | ogs_error("OpenAPI_recur_time_parseFromJSON() failed [recur_month]"); |
233 | 0 | goto end; |
234 | 0 | } |
235 | 0 | localDouble = (double *)ogs_calloc(1, sizeof(double)); |
236 | 0 | if (!localDouble) { |
237 | 0 | ogs_error("OpenAPI_recur_time_parseFromJSON() failed [recur_month]"); |
238 | 0 | goto end; |
239 | 0 | } |
240 | 0 | *localDouble = recur_month_local->valuedouble; |
241 | 0 | OpenAPI_list_add(recur_monthList, localDouble); |
242 | 0 | } |
243 | 0 | } |
244 | | |
245 | 0 | recur_week = cJSON_GetObjectItemCaseSensitive(recur_timeJSON, "recurWeek"); |
246 | 0 | if (recur_week) { |
247 | 0 | cJSON *recur_week_local = NULL; |
248 | 0 | if (!cJSON_IsArray(recur_week)) { |
249 | 0 | ogs_error("OpenAPI_recur_time_parseFromJSON() failed [recur_week]"); |
250 | 0 | goto end; |
251 | 0 | } |
252 | | |
253 | 0 | recur_weekList = OpenAPI_list_create(); |
254 | |
|
255 | 0 | cJSON_ArrayForEach(recur_week_local, recur_week) { |
256 | 0 | double *localDouble = NULL; |
257 | 0 | int *localInt = NULL; |
258 | 0 | if (!cJSON_IsNumber(recur_week_local)) { |
259 | 0 | ogs_error("OpenAPI_recur_time_parseFromJSON() failed [recur_week]"); |
260 | 0 | goto end; |
261 | 0 | } |
262 | 0 | localDouble = (double *)ogs_calloc(1, sizeof(double)); |
263 | 0 | if (!localDouble) { |
264 | 0 | ogs_error("OpenAPI_recur_time_parseFromJSON() failed [recur_week]"); |
265 | 0 | goto end; |
266 | 0 | } |
267 | 0 | *localDouble = recur_week_local->valuedouble; |
268 | 0 | OpenAPI_list_add(recur_weekList, localDouble); |
269 | 0 | } |
270 | 0 | } |
271 | | |
272 | 0 | recur_day = cJSON_GetObjectItemCaseSensitive(recur_timeJSON, "recurDay"); |
273 | 0 | if (recur_day) { |
274 | 0 | cJSON *recur_day_local = NULL; |
275 | 0 | if (!cJSON_IsArray(recur_day)) { |
276 | 0 | ogs_error("OpenAPI_recur_time_parseFromJSON() failed [recur_day]"); |
277 | 0 | goto end; |
278 | 0 | } |
279 | | |
280 | 0 | recur_dayList = OpenAPI_list_create(); |
281 | |
|
282 | 0 | cJSON_ArrayForEach(recur_day_local, recur_day) { |
283 | 0 | double *localDouble = NULL; |
284 | 0 | int *localInt = NULL; |
285 | 0 | if (!cJSON_IsNumber(recur_day_local)) { |
286 | 0 | ogs_error("OpenAPI_recur_time_parseFromJSON() failed [recur_day]"); |
287 | 0 | goto end; |
288 | 0 | } |
289 | 0 | localDouble = (double *)ogs_calloc(1, sizeof(double)); |
290 | 0 | if (!localDouble) { |
291 | 0 | ogs_error("OpenAPI_recur_time_parseFromJSON() failed [recur_day]"); |
292 | 0 | goto end; |
293 | 0 | } |
294 | 0 | *localDouble = recur_day_local->valuedouble; |
295 | 0 | OpenAPI_list_add(recur_dayList, localDouble); |
296 | 0 | } |
297 | 0 | } |
298 | | |
299 | 0 | recur_date = cJSON_GetObjectItemCaseSensitive(recur_timeJSON, "recurDate"); |
300 | 0 | if (recur_date) { |
301 | 0 | cJSON *recur_date_local = NULL; |
302 | 0 | if (!cJSON_IsArray(recur_date)) { |
303 | 0 | ogs_error("OpenAPI_recur_time_parseFromJSON() failed [recur_date]"); |
304 | 0 | goto end; |
305 | 0 | } |
306 | | |
307 | 0 | recur_dateList = OpenAPI_list_create(); |
308 | |
|
309 | 0 | cJSON_ArrayForEach(recur_date_local, recur_date) { |
310 | 0 | double *localDouble = NULL; |
311 | 0 | int *localInt = NULL; |
312 | 0 | } |
313 | 0 | } |
314 | | |
315 | 0 | recur_end_time = cJSON_GetObjectItemCaseSensitive(recur_timeJSON, "recurEndTime"); |
316 | 0 | if (recur_end_time) { |
317 | 0 | if (!cJSON_IsString(recur_end_time) && !cJSON_IsNull(recur_end_time)) { |
318 | 0 | ogs_error("OpenAPI_recur_time_parseFromJSON() failed [recur_end_time]"); |
319 | 0 | goto end; |
320 | 0 | } |
321 | 0 | } |
322 | | |
323 | 0 | recur_time_local_var = OpenAPI_recur_time_create ( |
324 | 0 | recur_time_window ? recur_time_window_local_nonprim : NULL, |
325 | 0 | recur_type ? recur_typeVariable : 0, |
326 | 0 | recur_month ? recur_monthList : NULL, |
327 | 0 | recur_week ? recur_weekList : NULL, |
328 | 0 | recur_day ? recur_dayList : NULL, |
329 | 0 | recur_date ? recur_dateList : NULL, |
330 | 0 | recur_end_time && !cJSON_IsNull(recur_end_time) ? ogs_strdup(recur_end_time->valuestring) : NULL |
331 | 0 | ); |
332 | |
|
333 | 0 | return recur_time_local_var; |
334 | 0 | end: |
335 | 0 | if (recur_time_window_local_nonprim) { |
336 | 0 | OpenAPI_valid_time_period_free(recur_time_window_local_nonprim); |
337 | 0 | recur_time_window_local_nonprim = NULL; |
338 | 0 | } |
339 | 0 | if (recur_monthList) { |
340 | 0 | OpenAPI_list_for_each(recur_monthList, node) { |
341 | 0 | ogs_free(node->data); |
342 | 0 | } |
343 | 0 | OpenAPI_list_free(recur_monthList); |
344 | 0 | recur_monthList = NULL; |
345 | 0 | } |
346 | 0 | if (recur_weekList) { |
347 | 0 | OpenAPI_list_for_each(recur_weekList, node) { |
348 | 0 | ogs_free(node->data); |
349 | 0 | } |
350 | 0 | OpenAPI_list_free(recur_weekList); |
351 | 0 | recur_weekList = NULL; |
352 | 0 | } |
353 | 0 | if (recur_dayList) { |
354 | 0 | OpenAPI_list_for_each(recur_dayList, node) { |
355 | 0 | ogs_free(node->data); |
356 | 0 | } |
357 | 0 | OpenAPI_list_free(recur_dayList); |
358 | 0 | recur_dayList = NULL; |
359 | 0 | } |
360 | 0 | if (recur_dateList) { |
361 | 0 | OpenAPI_list_for_each(recur_dateList, node) { |
362 | 0 | ogs_free(node->data); |
363 | 0 | } |
364 | 0 | OpenAPI_list_free(recur_dateList); |
365 | 0 | recur_dateList = NULL; |
366 | 0 | } |
367 | 0 | return NULL; |
368 | 0 | } |
369 | | |
370 | | OpenAPI_recur_time_t *OpenAPI_recur_time_copy(OpenAPI_recur_time_t *dst, OpenAPI_recur_time_t *src) |
371 | 0 | { |
372 | 0 | cJSON *item = NULL; |
373 | 0 | char *content = NULL; |
374 | |
|
375 | 0 | ogs_assert(src); |
376 | 0 | item = OpenAPI_recur_time_convertToJSON(src); |
377 | 0 | if (!item) { |
378 | 0 | ogs_error("OpenAPI_recur_time_convertToJSON() failed"); |
379 | 0 | return NULL; |
380 | 0 | } |
381 | | |
382 | 0 | content = cJSON_Print(item); |
383 | 0 | cJSON_Delete(item); |
384 | |
|
385 | 0 | if (!content) { |
386 | 0 | ogs_error("cJSON_Print() failed"); |
387 | 0 | return NULL; |
388 | 0 | } |
389 | | |
390 | 0 | item = cJSON_Parse(content); |
391 | 0 | ogs_free(content); |
392 | 0 | if (!item) { |
393 | 0 | ogs_error("cJSON_Parse() failed"); |
394 | 0 | return NULL; |
395 | 0 | } |
396 | | |
397 | 0 | OpenAPI_recur_time_free(dst); |
398 | 0 | dst = OpenAPI_recur_time_parseFromJSON(item); |
399 | 0 | cJSON_Delete(item); |
400 | |
|
401 | 0 | return dst; |
402 | 0 | } |
403 | | |