Hemant Zope 3be4902444 [C] C generator refactored (#2463)
* New modified model header and body mustache for c client generator

* remove uncrustify from cmake as it is used during code generation, also remove valgrind as it is not used

* add function to encode and decode binary data

* update model mustache

* update api body and header mustache for handling all types of parameters

* update model mustache with variable names and address few more issues to generate working codes

* updated api body and header mustaches with support for various new parameters and fix some issues as per new changes in code flow structure

* update apiClient header and body mustache as per new modifications for handling binary data and few more stuff

* updated samples generated by new modified mustache

* update handling of file and binary data type to binary_t

* update samples with recent commit on master regarding c-generator

* update cmakelist which was ignored by .openapi-generator-ignore, cleanup external folder

* update CMakeList mustache to show how to use compiled libary to compile source files

* update samples with new cmake

* Add comments explaining what each command is doing inshort

* remove freeing of base path as it is not memory allocated

* update samples to free apiclient object when the requirement is over

* add missing cJSON delete to fix memory not freed bugs

* use uncrustify to beautify manual written test code
2019-04-15 11:27:34 +08:00

109 lines
2.2 KiB
C

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "api_response.h"
api_response_t *api_response_create(int code, char *type, char *message) {
api_response_t *api_response_local_var = malloc(sizeof(api_response_t));
if(!api_response_local_var) {
return NULL;
}
api_response_local_var->code = code;
api_response_local_var->type = type;
api_response_local_var->message = message;
return api_response_local_var;
}
void api_response_free(api_response_t *api_response) {
listEntry_t *listEntry;
free(api_response->type);
free(api_response->message);
free(api_response);
}
cJSON *api_response_convertToJSON(api_response_t *api_response) {
cJSON *item = cJSON_CreateObject();
// api_response->code
if(api_response->code) {
if(cJSON_AddNumberToObject(item, "code",
api_response->code) == NULL)
{
goto fail; // Numeric
}
}
// api_response->type
if(api_response->type) {
if(cJSON_AddStringToObject(item, "type",
api_response->type) == NULL)
{
goto fail; // String
}
}
// api_response->message
if(api_response->message) {
if(cJSON_AddStringToObject(item, "message",
api_response->message) == NULL)
{
goto fail; // String
}
}
return item;
fail:
if(item) {
cJSON_Delete(item);
}
return NULL;
}
api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON) {
api_response_t *api_response_local_var = NULL;
// api_response->code
cJSON *code =
cJSON_GetObjectItemCaseSensitive(api_responseJSON, "code");
if(code) {
if(!cJSON_IsNumber(code)) {
goto end; // Numeric
}
}
// api_response->type
cJSON *type =
cJSON_GetObjectItemCaseSensitive(api_responseJSON, "type");
if(type) {
if(!cJSON_IsString(type)) {
goto end; // String
}
}
// api_response->message
cJSON *message = cJSON_GetObjectItemCaseSensitive(api_responseJSON,
"message");
if(message) {
if(!cJSON_IsString(message)) {
goto end; // String
}
}
api_response_local_var = api_response_create(
code ? code->valuedouble : 0,
type ? strdup(type->valuestring) : NULL,
message ? strdup(message->valuestring) : NULL
);
return api_response_local_var;
end:
return NULL;
}