/src/open5gs/lib/sbi/openapi/model/flows.c
Line | Count | Source |
1 | | |
2 | | #include <stdlib.h> |
3 | | #include <string.h> |
4 | | #include <stdio.h> |
5 | | #include "flows.h" |
6 | | |
7 | | OpenAPI_flows_t *OpenAPI_flows_create( |
8 | | OpenAPI_list_t *cont_vers, |
9 | | OpenAPI_list_t *f_nums, |
10 | | int med_comp_n |
11 | | ) |
12 | 1.00k | { |
13 | 1.00k | OpenAPI_flows_t *flows_local_var = ogs_malloc(sizeof(OpenAPI_flows_t)); |
14 | 1.00k | ogs_assert(flows_local_var); |
15 | | |
16 | 1.00k | flows_local_var->cont_vers = cont_vers; |
17 | 1.00k | flows_local_var->f_nums = f_nums; |
18 | 1.00k | flows_local_var->med_comp_n = med_comp_n; |
19 | | |
20 | 1.00k | return flows_local_var; |
21 | 1.00k | } |
22 | | |
23 | | void OpenAPI_flows_free(OpenAPI_flows_t *flows) |
24 | 1.00k | { |
25 | 1.00k | OpenAPI_lnode_t *node = NULL; |
26 | | |
27 | 1.00k | if (NULL == flows) { |
28 | 0 | return; |
29 | 0 | } |
30 | 1.00k | if (flows->cont_vers) { |
31 | 0 | OpenAPI_list_for_each(flows->cont_vers, node) { |
32 | 0 | ogs_free(node->data); |
33 | 0 | } |
34 | 0 | OpenAPI_list_free(flows->cont_vers); |
35 | 0 | flows->cont_vers = NULL; |
36 | 0 | } |
37 | 1.00k | if (flows->f_nums) { |
38 | 492 | OpenAPI_list_for_each(flows->f_nums, node) { |
39 | 492 | ogs_free(node->data); |
40 | 492 | } |
41 | 364 | OpenAPI_list_free(flows->f_nums); |
42 | 364 | flows->f_nums = NULL; |
43 | 364 | } |
44 | 1.00k | ogs_free(flows); |
45 | 1.00k | } |
46 | | |
47 | | cJSON *OpenAPI_flows_convertToJSON(OpenAPI_flows_t *flows) |
48 | 0 | { |
49 | 0 | cJSON *item = NULL; |
50 | 0 | OpenAPI_lnode_t *node = NULL; |
51 | |
|
52 | 0 | if (flows == NULL) { |
53 | 0 | ogs_error("OpenAPI_flows_convertToJSON() failed [Flows]"); |
54 | 0 | return NULL; |
55 | 0 | } |
56 | | |
57 | 0 | item = cJSON_CreateObject(); |
58 | 0 | if (flows->cont_vers) { |
59 | 0 | cJSON *cont_versList = cJSON_AddArrayToObject(item, "contVers"); |
60 | 0 | if (cont_versList == NULL) { |
61 | 0 | ogs_error("OpenAPI_flows_convertToJSON() failed [cont_vers]"); |
62 | 0 | goto end; |
63 | 0 | } |
64 | 0 | OpenAPI_list_for_each(flows->cont_vers, node) { |
65 | 0 | if (node->data == NULL) { |
66 | 0 | ogs_error("OpenAPI_flows_convertToJSON() failed [cont_vers]"); |
67 | 0 | goto end; |
68 | 0 | } |
69 | 0 | if (cJSON_AddNumberToObject(cont_versList, "", *(double *)node->data) == NULL) { |
70 | 0 | ogs_error("OpenAPI_flows_convertToJSON() failed [cont_vers]"); |
71 | 0 | goto end; |
72 | 0 | } |
73 | 0 | } |
74 | 0 | } |
75 | | |
76 | 0 | if (flows->f_nums) { |
77 | 0 | cJSON *f_numsList = cJSON_AddArrayToObject(item, "fNums"); |
78 | 0 | if (f_numsList == NULL) { |
79 | 0 | ogs_error("OpenAPI_flows_convertToJSON() failed [f_nums]"); |
80 | 0 | goto end; |
81 | 0 | } |
82 | 0 | OpenAPI_list_for_each(flows->f_nums, node) { |
83 | 0 | if (node->data == NULL) { |
84 | 0 | ogs_error("OpenAPI_flows_convertToJSON() failed [f_nums]"); |
85 | 0 | goto end; |
86 | 0 | } |
87 | 0 | if (cJSON_AddNumberToObject(f_numsList, "", *(double *)node->data) == NULL) { |
88 | 0 | ogs_error("OpenAPI_flows_convertToJSON() failed [f_nums]"); |
89 | 0 | goto end; |
90 | 0 | } |
91 | 0 | } |
92 | 0 | } |
93 | | |
94 | 0 | if (cJSON_AddNumberToObject(item, "medCompN", flows->med_comp_n) == NULL) { |
95 | 0 | ogs_error("OpenAPI_flows_convertToJSON() failed [med_comp_n]"); |
96 | 0 | goto end; |
97 | 0 | } |
98 | | |
99 | 0 | end: |
100 | 0 | return item; |
101 | 0 | } |
102 | | |
103 | | OpenAPI_flows_t *OpenAPI_flows_parseFromJSON(cJSON *flowsJSON) |
104 | 1.04k | { |
105 | 1.04k | OpenAPI_flows_t *flows_local_var = NULL; |
106 | 1.04k | OpenAPI_lnode_t *node = NULL; |
107 | 1.04k | cJSON *cont_vers = NULL; |
108 | 1.04k | OpenAPI_list_t *cont_versList = NULL; |
109 | 1.04k | cJSON *f_nums = NULL; |
110 | 1.04k | OpenAPI_list_t *f_numsList = NULL; |
111 | 1.04k | cJSON *med_comp_n = NULL; |
112 | 1.04k | cont_vers = cJSON_GetObjectItemCaseSensitive(flowsJSON, "contVers"); |
113 | 1.04k | if (cont_vers) { |
114 | 4 | cJSON *cont_vers_local = NULL; |
115 | 4 | if (!cJSON_IsArray(cont_vers)) { |
116 | 1 | ogs_error("OpenAPI_flows_parseFromJSON() failed [cont_vers]"); |
117 | 1 | goto end; |
118 | 1 | } |
119 | | |
120 | 3 | cont_versList = OpenAPI_list_create(); |
121 | | |
122 | 3 | cJSON_ArrayForEach(cont_vers_local, cont_vers) { |
123 | 2 | double *localDouble = NULL; |
124 | 2 | int *localInt = NULL; |
125 | 2 | if (!cJSON_IsNumber(cont_vers_local)) { |
126 | 1 | ogs_error("OpenAPI_flows_parseFromJSON() failed [cont_vers]"); |
127 | 1 | goto end; |
128 | 1 | } |
129 | 1 | localDouble = (double *)ogs_calloc(1, sizeof(double)); |
130 | 1 | if (!localDouble) { |
131 | 0 | ogs_error("OpenAPI_flows_parseFromJSON() failed [cont_vers]"); |
132 | 0 | goto end; |
133 | 0 | } |
134 | 1 | *localDouble = cont_vers_local->valuedouble; |
135 | 1 | OpenAPI_list_add(cont_versList, localDouble); |
136 | 1 | } |
137 | 3 | } |
138 | | |
139 | 1.04k | f_nums = cJSON_GetObjectItemCaseSensitive(flowsJSON, "fNums"); |
140 | 1.04k | if (f_nums) { |
141 | 377 | cJSON *f_nums_local = NULL; |
142 | 377 | if (!cJSON_IsArray(f_nums)) { |
143 | 1 | ogs_error("OpenAPI_flows_parseFromJSON() failed [f_nums]"); |
144 | 1 | goto end; |
145 | 1 | } |
146 | | |
147 | 376 | f_numsList = OpenAPI_list_create(); |
148 | | |
149 | 834 | cJSON_ArrayForEach(f_nums_local, f_nums) { |
150 | 834 | double *localDouble = NULL; |
151 | 834 | int *localInt = NULL; |
152 | 834 | if (!cJSON_IsNumber(f_nums_local)) { |
153 | 1 | ogs_error("OpenAPI_flows_parseFromJSON() failed [f_nums]"); |
154 | 1 | goto end; |
155 | 1 | } |
156 | 833 | localDouble = (double *)ogs_calloc(1, sizeof(double)); |
157 | 833 | if (!localDouble) { |
158 | 0 | ogs_error("OpenAPI_flows_parseFromJSON() failed [f_nums]"); |
159 | 0 | goto end; |
160 | 0 | } |
161 | 833 | *localDouble = f_nums_local->valuedouble; |
162 | 833 | OpenAPI_list_add(f_numsList, localDouble); |
163 | 833 | } |
164 | 376 | } |
165 | | |
166 | 1.04k | med_comp_n = cJSON_GetObjectItemCaseSensitive(flowsJSON, "medCompN"); |
167 | 1.04k | if (!med_comp_n) { |
168 | 33 | ogs_error("OpenAPI_flows_parseFromJSON() failed [med_comp_n]"); |
169 | 33 | goto end; |
170 | 33 | } |
171 | 1.00k | if (!cJSON_IsNumber(med_comp_n)) { |
172 | 1 | ogs_error("OpenAPI_flows_parseFromJSON() failed [med_comp_n]"); |
173 | 1 | goto end; |
174 | 1 | } |
175 | | |
176 | 1.00k | flows_local_var = OpenAPI_flows_create ( |
177 | 1.00k | cont_vers ? cont_versList : NULL, |
178 | 1.00k | f_nums ? f_numsList : NULL, |
179 | | |
180 | 1.00k | med_comp_n->valuedouble |
181 | 1.00k | ); |
182 | | |
183 | 1.00k | return flows_local_var; |
184 | 38 | end: |
185 | 38 | if (cont_versList) { |
186 | 3 | OpenAPI_list_for_each(cont_versList, node) { |
187 | 1 | ogs_free(node->data); |
188 | 1 | } |
189 | 3 | OpenAPI_list_free(cont_versList); |
190 | 3 | cont_versList = NULL; |
191 | 3 | } |
192 | 38 | if (f_numsList) { |
193 | 341 | OpenAPI_list_for_each(f_numsList, node) { |
194 | 341 | ogs_free(node->data); |
195 | 341 | } |
196 | 12 | OpenAPI_list_free(f_numsList); |
197 | 12 | f_numsList = NULL; |
198 | 12 | } |
199 | 38 | return NULL; |
200 | 1.00k | } |
201 | | |
202 | | OpenAPI_flows_t *OpenAPI_flows_copy(OpenAPI_flows_t *dst, OpenAPI_flows_t *src) |
203 | 0 | { |
204 | 0 | cJSON *item = NULL; |
205 | 0 | char *content = NULL; |
206 | |
|
207 | 0 | ogs_assert(src); |
208 | 0 | item = OpenAPI_flows_convertToJSON(src); |
209 | 0 | if (!item) { |
210 | 0 | ogs_error("OpenAPI_flows_convertToJSON() failed"); |
211 | 0 | return NULL; |
212 | 0 | } |
213 | | |
214 | 0 | content = cJSON_Print(item); |
215 | 0 | cJSON_Delete(item); |
216 | |
|
217 | 0 | if (!content) { |
218 | 0 | ogs_error("cJSON_Print() failed"); |
219 | 0 | return NULL; |
220 | 0 | } |
221 | | |
222 | 0 | item = cJSON_Parse(content); |
223 | 0 | ogs_free(content); |
224 | 0 | if (!item) { |
225 | 0 | ogs_error("cJSON_Parse() failed"); |
226 | 0 | return NULL; |
227 | 0 | } |
228 | | |
229 | 0 | OpenAPI_flows_free(dst); |
230 | 0 | dst = OpenAPI_flows_parseFromJSON(item); |
231 | 0 | cJSON_Delete(item); |
232 | |
|
233 | 0 | return dst; |
234 | 0 | } |
235 | | |