File: | model/pet.c |
Warning: | line 256, column 19 Potential memory leak |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | #include <stdlib.h> | |||
2 | #include <string.h> | |||
3 | #include <stdio.h> | |||
4 | #include "pet.h" | |||
5 | ||||
6 | ||||
7 | char* statuspet_ToString(openapi_petstore_pet_STATUS_e status) { | |||
8 | char* statusArray[] = { "NULL", "available", "pending", "sold" }; | |||
9 | return statusArray[status]; | |||
10 | } | |||
11 | ||||
12 | openapi_petstore_pet_STATUS_e statuspet_FromString(char* status){ | |||
13 | int stringToReturn = 0; | |||
14 | char *statusArray[] = { "NULL", "available", "pending", "sold" }; | |||
15 | size_t sizeofArray = sizeof(statusArray) / sizeof(statusArray[0]); | |||
16 | while(stringToReturn < sizeofArray) { | |||
17 | if(strcmp(status, statusArray[stringToReturn]) == 0) { | |||
18 | return stringToReturn; | |||
19 | } | |||
20 | stringToReturn++; | |||
21 | } | |||
22 | return 0; | |||
23 | } | |||
24 | ||||
25 | pet_t *pet_create( | |||
26 | long id, | |||
27 | category_t *category, | |||
28 | char *name, | |||
29 | list_t *photo_urls, | |||
30 | list_t *tags, | |||
31 | openapi_petstore_pet_STATUS_e status | |||
32 | ) { | |||
33 | pet_t *pet_local_var = malloc(sizeof(pet_t)); | |||
34 | if (!pet_local_var) { | |||
35 | return NULL((void*)0); | |||
36 | } | |||
37 | pet_local_var->id = id; | |||
38 | pet_local_var->category = category; | |||
39 | pet_local_var->name = name; | |||
40 | pet_local_var->photo_urls = photo_urls; | |||
41 | pet_local_var->tags = tags; | |||
42 | pet_local_var->status = status; | |||
43 | ||||
44 | return pet_local_var; | |||
45 | } | |||
46 | ||||
47 | ||||
48 | void pet_free(pet_t *pet) { | |||
49 | if(NULL((void*)0) == pet){ | |||
50 | return ; | |||
51 | } | |||
52 | listEntry_t *listEntry; | |||
53 | if (pet->category) { | |||
54 | category_free(pet->category); | |||
55 | pet->category = NULL((void*)0); | |||
56 | } | |||
57 | if (pet->name) { | |||
58 | free(pet->name); | |||
59 | pet->name = NULL((void*)0); | |||
60 | } | |||
61 | if (pet->photo_urls) { | |||
62 | list_ForEach(listEntry, pet->photo_urls)for(listEntry = (pet->photo_urls != ((void*)0)) ? (pet-> photo_urls)->firstEntry : ((void*)0); listEntry != ((void* )0); listEntry = listEntry->nextListEntry) { | |||
63 | free(listEntry->data); | |||
64 | } | |||
65 | list_free(pet->photo_urls); | |||
66 | pet->photo_urls = NULL((void*)0); | |||
67 | } | |||
68 | if (pet->tags) { | |||
69 | list_ForEach(listEntry, pet->tags)for(listEntry = (pet->tags != ((void*)0)) ? (pet->tags) ->firstEntry : ((void*)0); listEntry != ((void*)0); listEntry = listEntry->nextListEntry) { | |||
70 | tag_free(listEntry->data); | |||
71 | } | |||
72 | list_free(pet->tags); | |||
73 | pet->tags = NULL((void*)0); | |||
74 | } | |||
75 | free(pet); | |||
76 | } | |||
77 | ||||
78 | cJSON *pet_convertToJSON(pet_t *pet) { | |||
79 | cJSON *item = cJSON_CreateObject(); | |||
80 | ||||
81 | // pet->id | |||
82 | if(pet->id) { | |||
83 | if(cJSON_AddNumberToObject(item, "id", pet->id) == NULL((void*)0)) { | |||
84 | goto fail; //Numeric | |||
85 | } | |||
86 | } | |||
87 | ||||
88 | ||||
89 | // pet->category | |||
90 | if(pet->category) { | |||
91 | cJSON *category_local_JSON = category_convertToJSON(pet->category); | |||
92 | if(category_local_JSON == NULL((void*)0)) { | |||
93 | goto fail; //model | |||
94 | } | |||
95 | cJSON_AddItemToObject(item, "category", category_local_JSON); | |||
96 | if(item->child == NULL((void*)0)) { | |||
97 | goto fail; | |||
98 | } | |||
99 | } | |||
100 | ||||
101 | ||||
102 | // pet->name | |||
103 | if (!pet->name) { | |||
104 | goto fail; | |||
105 | } | |||
106 | ||||
107 | if(cJSON_AddStringToObject(item, "name", pet->name) == NULL((void*)0)) { | |||
108 | goto fail; //String | |||
109 | } | |||
110 | ||||
111 | ||||
112 | // pet->photo_urls | |||
113 | if (!pet->photo_urls) { | |||
114 | goto fail; | |||
115 | } | |||
116 | ||||
117 | cJSON *photo_urls = cJSON_AddArrayToObject(item, "photoUrls"); | |||
118 | if(photo_urls == NULL((void*)0)) { | |||
119 | goto fail; //primitive container | |||
120 | } | |||
121 | ||||
122 | listEntry_t *photo_urlsListEntry; | |||
123 | list_ForEach(photo_urlsListEntry, pet->photo_urls)for(photo_urlsListEntry = (pet->photo_urls != ((void*)0)) ? (pet->photo_urls)->firstEntry : ((void*)0); photo_urlsListEntry != ((void*)0); photo_urlsListEntry = photo_urlsListEntry-> nextListEntry) { | |||
124 | if(cJSON_AddStringToObject(photo_urls, "", (char*)photo_urlsListEntry->data) == NULL((void*)0)) | |||
125 | { | |||
126 | goto fail; | |||
127 | } | |||
128 | } | |||
129 | ||||
130 | ||||
131 | // pet->tags | |||
132 | if(pet->tags) { | |||
133 | cJSON *tags = cJSON_AddArrayToObject(item, "tags"); | |||
134 | if(tags == NULL((void*)0)) { | |||
135 | goto fail; //nonprimitive container | |||
136 | } | |||
137 | ||||
138 | listEntry_t *tagsListEntry; | |||
139 | if (pet->tags) { | |||
140 | list_ForEach(tagsListEntry, pet->tags)for(tagsListEntry = (pet->tags != ((void*)0)) ? (pet->tags )->firstEntry : ((void*)0); tagsListEntry != ((void*)0); tagsListEntry = tagsListEntry->nextListEntry) { | |||
141 | cJSON *itemLocal = tag_convertToJSON(tagsListEntry->data); | |||
142 | if(itemLocal == NULL((void*)0)) { | |||
143 | goto fail; | |||
144 | } | |||
145 | cJSON_AddItemToArray(tags, itemLocal); | |||
146 | } | |||
147 | } | |||
148 | } | |||
149 | ||||
150 | ||||
151 | // pet->status | |||
152 | ||||
153 | if(cJSON_AddStringToObject(item, "status", statuspet_ToString(pet->status)) == NULL((void*)0)) | |||
154 | { | |||
155 | goto fail; //Enum | |||
156 | } | |||
157 | ||||
158 | ||||
159 | return item; | |||
160 | fail: | |||
161 | if (item) { | |||
162 | cJSON_Delete(item); | |||
163 | } | |||
164 | return NULL((void*)0); | |||
165 | } | |||
166 | ||||
167 | pet_t *pet_parseFromJSON(cJSON *petJSON){ | |||
168 | ||||
169 | pet_t *pet_local_var = NULL((void*)0); | |||
170 | ||||
171 | // pet->id | |||
172 | cJSON *id = cJSON_GetObjectItemCaseSensitive(petJSON, "id"); | |||
173 | if (id) { | |||
| ||||
174 | if(!cJSON_IsNumber(id)) | |||
175 | { | |||
176 | goto end; //Numeric | |||
177 | } | |||
178 | } | |||
179 | ||||
180 | // pet->category | |||
181 | cJSON *category = cJSON_GetObjectItemCaseSensitive(petJSON, "category"); | |||
182 | category_t *category_local_nonprim = NULL((void*)0); | |||
183 | if (category) { | |||
184 | category_local_nonprim = category_parseFromJSON(category); //nonprimitive | |||
185 | } | |||
186 | ||||
187 | // pet->name | |||
188 | cJSON *name = cJSON_GetObjectItemCaseSensitive(petJSON, "name"); | |||
189 | if (!name) { | |||
190 | goto end; | |||
191 | } | |||
192 | ||||
193 | ||||
194 | if(!cJSON_IsString(name)) | |||
195 | { | |||
196 | goto end; //String | |||
197 | } | |||
198 | ||||
199 | // pet->photo_urls | |||
200 | cJSON *photo_urls = cJSON_GetObjectItemCaseSensitive(petJSON, "photoUrls"); | |||
201 | if (!photo_urls) { | |||
202 | goto end; | |||
203 | } | |||
204 | ||||
205 | list_t *photo_urlsList; | |||
206 | ||||
207 | cJSON *photo_urls_local; | |||
208 | if(!cJSON_IsArray(photo_urls)) { | |||
209 | goto end;//primitive container | |||
210 | } | |||
211 | photo_urlsList = list_create(); | |||
212 | ||||
213 |
)->child : ((void*)0); photo_urls_local != ((void*)0); photo_urls_local = photo_urls_local->next) | |||
214 | { | |||
215 | if(!cJSON_IsString(photo_urls_local)) | |||
216 | { | |||
217 | goto end; | |||
218 | } | |||
219 | list_addElement(photo_urlsList , strdup(photo_urls_local->valuestring)); | |||
220 | } | |||
221 | ||||
222 | // pet->tags | |||
223 | cJSON *tags = cJSON_GetObjectItemCaseSensitive(petJSON, "tags"); | |||
224 | list_t *tagsList; | |||
225 | if (tags) { | |||
226 | cJSON *tags_local_nonprimitive; | |||
227 | if(!cJSON_IsArray(tags)){ | |||
228 | goto end; //nonprimitive container | |||
229 | } | |||
230 | ||||
231 | tagsList = list_create(); | |||
232 | ||||
233 | cJSON_ArrayForEach(tags_local_nonprimitive,tags )for(tags_local_nonprimitive = (tags != ((void*)0)) ? (tags)-> child : ((void*)0); tags_local_nonprimitive != ((void*)0); tags_local_nonprimitive = tags_local_nonprimitive->next) | |||
234 | { | |||
235 | if(!cJSON_IsObject(tags_local_nonprimitive)){ | |||
236 | goto end; | |||
237 | } | |||
238 | tag_t *tagsItem = tag_parseFromJSON(tags_local_nonprimitive); | |||
239 | ||||
240 | list_addElement(tagsList, tagsItem); | |||
241 | } | |||
242 | } | |||
243 | ||||
244 | // pet->status | |||
245 | cJSON *status = cJSON_GetObjectItemCaseSensitive(petJSON, "status"); | |||
246 | openapi_petstore_pet_STATUS_e statusVariable; | |||
247 | if (status) { | |||
248 | if(!cJSON_IsString(status)) | |||
249 | { | |||
250 | goto end; //Enum | |||
251 | } | |||
252 | statusVariable = statuspet_FromString(status->valuestring); | |||
253 | } | |||
254 | ||||
255 | ||||
256 | pet_local_var = pet_create ( | |||
| ||||
257 |
| |||
258 |
| |||
259 | strdup(name->valuestring), | |||
260 | photo_urlsList, | |||
261 |
| |||
262 |
| |||
263 | ); | |||
264 | ||||
265 | return pet_local_var; | |||
266 | end: | |||
267 | return NULL((void*)0); | |||
268 | ||||
269 | } |