From e12b5998ae1dd5e20e7839261dce5de94f837369 Mon Sep 17 00:00:00 2001 From: Hemant Zope Date: Mon, 27 Aug 2018 15:36:57 +0200 Subject: [PATCH 1/6] Add string replace function for C generator --- .../src/main/resources/C-libcurl/api.mustache | 2 +- .../resources/C-libcurl/apiClient.c.mustache | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache index f96800649d61..95084586df06 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache @@ -31,7 +31,7 @@ {{#pathParams}} // TODO path parameter {{paramName}} ({{baseName}}) not yet supported // TODO base path = {{{basePath}}} - replace_str(localVarPath, "{" + "{{baseName}}" + "}", {{paramName}})// TODO need to revise + str_replace(localVarPath,{{baseName}}, {{paramName}}) {{/pathParams}} {{#headerParams}} diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache index 58a593e28f27..50ede26153c1 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache @@ -274,4 +274,30 @@ size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp) { *(char **) userp = strdup(buffer); return size * nmemb; +} + +int str_replace(char *in, char* toreplace, char* replaceby ){ + int i,mark,copy_len=0; + char* copy; + int in_len=strlen(in); + + for(i=0;i<(in_len-strlen(toreplace));i++){ + if(memcmp(in+i,toreplace,strlen(toreplace)) == 0){ + mark = i; + copy = malloc(in_len+strlen(replaceby)); + memcpy(copy,in,mark-1); + copy_len=mark-1; + memcpy(copy+copy_len,replaceby,strlen(replaceby)); + copy_len += strlen(replaceby); + memcpy(copy+copy_len,in+mark+strlen(toreplace)+1,in_len-mark-1-strlen(toreplace)); + copy_len += in_len-mark-1-strlen(toreplace); + } + } + if(!copy_len) + goto error; + free(in); + memcpy(in,copy,copy_len); + return 1; +error: + return 0; } \ No newline at end of file From ea7ca2d80fac1dc80c4ee4b15568ded8ea589500 Mon Sep 17 00:00:00 2001 From: Hemant Zope Date: Mon, 27 Aug 2018 20:11:31 +0200 Subject: [PATCH 2/6] Fixed replacement for variable only --- .../src/main/resources/C-libcurl/api.mustache | 6 ++++- .../resources/C-libcurl/apiClient.c.mustache | 27 +++++++++---------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache index 95084586df06..4e5451fbc648 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache @@ -7,6 +7,8 @@ {{/imports}} #define MAX_BUFFER_LENGTH 4096 +#define vstr(s) str(s) +#define str(s) #s {{#operations}} {{#operation}} @@ -31,7 +33,9 @@ {{#pathParams}} // TODO path parameter {{paramName}} ({{baseName}}) not yet supported // TODO base path = {{{basePath}}} - str_replace(localVarPath,{{baseName}}, {{paramName}}) + char* baseNameMod=malloc(strlen({{baseName}})+2); //baseNameMod free not yet implemented + snprintf(baseNameMod,strlen(baseName)+3,"%s%s%s","{",{{baseName}},"}"); + str_replace(localVarPath,baseNameMod, vstr({{paramName}})); {{/pathParams}} {{#headerParams}} diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache index 50ede26153c1..688b3dd6f5c6 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache @@ -280,21 +280,20 @@ int str_replace(char *in, char* toreplace, char* replaceby ){ int i,mark,copy_len=0; char* copy; int in_len=strlen(in); - - for(i=0;i<(in_len-strlen(toreplace));i++){ - if(memcmp(in+i,toreplace,strlen(toreplace)) == 0){ - mark = i; - copy = malloc(in_len+strlen(replaceby)); - memcpy(copy,in,mark-1); - copy_len=mark-1; - memcpy(copy+copy_len,replaceby,strlen(replaceby)); - copy_len += strlen(replaceby); - memcpy(copy+copy_len,in+mark+strlen(toreplace)+1,in_len-mark-1-strlen(toreplace)); - copy_len += in_len-mark-1-strlen(toreplace); + for(i=0;i<(in_len-strlen(toreplace));i++){ + if(memcmp(in+i,toreplace,strlen(toreplace)) == 0){ + mark = i; + copy = malloc(in_len+strlen(replaceby)); + memcpy(copy,in,mark); + copy_len=mark; + memcpy(copy+copy_len,replaceby,strlen(replaceby)); + copy_len += strlen(replaceby); + memcpy(copy+copy_len,in+mark+strlen(toreplace),in_len-mark-strlen(toreplace)+1); + copy_len += in_len-mark-strlen(toreplace)+1; + } } - } - if(!copy_len) - goto error; + if(!copy_len) + goto error; free(in); memcpy(in,copy,copy_len); return 1; From 26fb08751d415fecfe79c2d4ff64a5c82a44604d Mon Sep 17 00:00:00 2001 From: Hemant Zope Date: Tue, 28 Aug 2018 16:26:23 +0200 Subject: [PATCH 3/6] Fixed problem for different datatypes of paramName --- .../src/main/resources/C-libcurl/api.mustache | 76 ++- .../resources/C-libcurl/apiClient.c.mustache | 557 +++++++++--------- 2 files changed, 335 insertions(+), 298 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache index 4e5451fbc648..3dee44a380a0 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache @@ -7,8 +7,11 @@ {{/imports}} #define MAX_BUFFER_LENGTH 4096 -#define vstr(s) str(s) -#define str(s) #s +#define int_to_str(dst,src) \ + do {\ + char dst[64];\ + snprintf(dst,64,"%ld",(long int)(src));\ +}while(0) {{#operations}} {{#operation}} @@ -21,21 +24,30 @@ // {{/notes}} {{#returnType}}{{{.}}}_t{{/returnType}}{{^returnType}}void{{/returnType}} *{{{classname}}}_{{{operationId}}}(apiClient_t *apiClient{{#allParams}}, {{{dataType}}} {{paramName}}{{/allParams}}) { - list_t *localVarQueryParameters, - list_t *localVarHeaderParameters, - list_t *localVarFormParameters, - char *localVarBodyParameters, + list_t *localVarQueryParameters, + list_t *localVarHeaderParameters, + list_t *localVarFormParameters, + char *localVarBodyParameters, // create the path - char *localVarPath = malloc(MAX_BUFFER_LENGTH); - snprintf(localVarPath, MAX_BUFFER_LENGTH, "{{{path}}}"); + char *localVarPath = malloc(MAX_BUFFER_LENGTH); + snprintf(localVarPath, MAX_BUFFER_LENGTH, "{{{path}}}"); {{#pathParams}} // TODO path parameter {{paramName}} ({{baseName}}) not yet supported // TODO base path = {{{basePath}}} char* baseNameMod=malloc(strlen({{baseName}})+2); //baseNameMod free not yet implemented snprintf(baseNameMod,strlen(baseName)+3,"%s%s%s","{",{{baseName}},"}"); - str_replace(localVarPath,baseNameMod, vstr({{paramName}})); + {{#paramName}} + {{#isLong}} + char buff[64]; + int_to_str(buf,{{paramName}}); + str_replace(localVarPath,baseNameMod,buff); + {{/isLong}} + {{#isString}} + str_replace(localVarPath,baseNameMod,{{paramName}}); + {{/isString}} + {{/paramName}} {{/pathParams}} {{#headerParams}} @@ -61,25 +73,25 @@ localVarBodyParameters = cJSON_Print({{{paramName}}}JSONObject); {{/bodyParam}} - apiClient_invoke(apiClient, - "pet", - localVarPath, - localVarQueryParameters, - localVarHeaderParameters, - localVarFormParameters, - localVarBodyParameters, - "{{{httpMethod}}}"); + apiClient_invoke(apiClient, + "pet", + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarBodyParameters, + "{{{httpMethod}}}"); - free(apiClient->dataReceived); - {{#allParams}} - {{^bodyParam}} - free({{{paramName}}}String); - {{/bodyParam}} - {{#bodyParam}} + free(apiClient->dataReceived); + {{#allParams}} + {{^bodyParam}} + free({{{paramName}}}String); + {{/bodyParam}} + {{#bodyParam}} free(localVarBodyParameters); - cJSON_Delete() - {{/bodyParam}} - {{/allParams}} + cJSON_Delete() + {{/bodyParam}} + {{/allParams}} {{#returnType}} localVar{{{returnType}}} = {{complexType}}_parseFromJSON(apiClient->dataReceived); if(localVar{{{returnType}}} == NULL) { @@ -89,13 +101,15 @@ cJSON_Delete(jsonObject); } - return localVar{{{returnType}}}; - {{/returnType}} + return localVar{{{returnType}}}; + {{/returnType}} {{^returnType}} - return; - {{/returnType}} + return; + {{/returnType}} } {{/operation}} -{{/operations}} \ No newline at end of file +{{/operations}} + + diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache index 688b3dd6f5c6..6c3359c0d213 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache @@ -8,295 +8,318 @@ size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp); apiClient_t *apiClient_create() { - curl_global_init(CURL_GLOBAL_ALL); - apiClient_t *apiClient = malloc(sizeof(apiClient_t)); - apiClient->basePath = "http://petstore.swagger.io:80/v2/"; - #ifdef BASIC_AUTH - apiClient->username = NULL; - apiClient->password = NULL; - #endif // BASIC_AUTH - #ifdef OAUTH2 - apiClient->accessToken = NULL; - #endif // OAUTH2 - return apiClient; + curl_global_init(CURL_GLOBAL_ALL); + apiClient_t *apiClient = malloc(sizeof(apiClient_t)); + apiClient->basePath = "http://petstore.swagger.io:80/v2/"; + #ifdef BASIC_AUTH + apiClient->username = NULL; + apiClient->password = NULL; + #endif // BASIC_AUTH + #ifdef OAUTH2 + apiClient->accessToken = NULL; + #endif // OAUTH2 + return apiClient; } void apiClient_free(apiClient_t *apiClient) { - free(apiClient); - curl_global_cleanup(); + free(apiClient); + curl_global_cleanup(); } void replaceSpaceWithPlus(char *stringToProcess) { - for(int i = 0; i < strlen(stringToProcess); i++) { - if(stringToProcess[i] == ' ') { - stringToProcess[i] = '+'; - } - } + for(int i = 0; i < strlen(stringToProcess); i++) { + if(stringToProcess[i] == ' ') { + stringToProcess[i] = '+'; + } + } } -char *assembleTargetUrl(char *basePath, - char *operationName, - char *operationParameter, - list_t *queryParameters) { - int neededBufferSizeForQueryParameters = 0; - listEntry_t *listEntry; - - if(queryParameters != NULL) { - list_ForEach(listEntry, queryParameters) { - keyValuePair_t *pair = listEntry->data; - neededBufferSizeForQueryParameters += - strlen(pair->key) + strlen(pair->value); - } - - neededBufferSizeForQueryParameters += - (queryParameters->count * 2); // each keyValuePair is separated by a = and a & except the last, but this makes up for the ? at the beginning - } - - int operationParameterLength = 0; - int basePathLength = strlen(basePath); - bool slashNeedsToBeAppendedToBasePath = false; - - if(operationParameter != NULL) { - operationParameterLength = (1 + strlen(operationParameter)); - } - if(basePath[strlen(basePath) - 1] != '/') { - slashNeedsToBeAppendedToBasePath = true; - basePathLength++; - } - - char *targetUrl = - malloc(strlen( - operationName) + neededBufferSizeForQueryParameters + basePathLength + operationParameterLength + 1 - ); - strcpy(targetUrl, basePath); - if(slashNeedsToBeAppendedToBasePath) { - strcat(targetUrl, "/"); - } - strcat(targetUrl, operationName); - if(operationParameter != NULL) { - strcat(targetUrl, "/"); - strcat(targetUrl, operationParameter); - } - - if(queryParameters != NULL) { - strcat(targetUrl, "?"); - list_ForEach(listEntry, queryParameters) { - keyValuePair_t *pair = listEntry->data; - replaceSpaceWithPlus(pair->key); - strcat(targetUrl, pair->key); - strcat(targetUrl, "="); - replaceSpaceWithPlus(pair->value); - strcat(targetUrl, pair->value); - if(listEntry->nextListEntry != NULL) { - strcat(targetUrl, "&"); - } - } - } - - return targetUrl; +char *assembleTargetUrl(char *basePath, + char *operationName, + char *operationParameter, + list_t *queryParameters) { + int neededBufferSizeForQueryParameters = 0; + listEntry_t *listEntry; + + if(queryParameters != NULL) { + list_ForEach(listEntry, queryParameters) { + keyValuePair_t *pair = listEntry->data; + neededBufferSizeForQueryParameters += + strlen(pair->key) + strlen(pair->value); + } + + neededBufferSizeForQueryParameters += + (queryParameters->count * 2); // each keyValuePair is separated by a = and a & except the last, but this makes up for the ? at the beginning + } + + int operationParameterLength = 0; + int basePathLength = strlen(basePath); + bool slashNeedsToBeAppendedToBasePath = false; + + if(operationParameter != NULL) { + operationParameterLength = (1 + strlen(operationParameter)); + } + if(basePath[strlen(basePath) - 1] != '/') { + slashNeedsToBeAppendedToBasePath = true; + basePathLength++; + } + + char *targetUrl = + malloc(strlen( + operationName) + neededBufferSizeForQueryParameters + basePathLength + operationParameterLength + 1 + ); + strcpy(targetUrl, basePath); + if(slashNeedsToBeAppendedToBasePath) { + strcat(targetUrl, "/"); + } + strcat(targetUrl, operationName); + if(operationParameter != NULL) { + strcat(targetUrl, "/"); + strcat(targetUrl, operationParameter); + } + + if(queryParameters != NULL) { + strcat(targetUrl, "?"); + list_ForEach(listEntry, queryParameters) { + keyValuePair_t *pair = listEntry->data; + replaceSpaceWithPlus(pair->key); + strcat(targetUrl, pair->key); + strcat(targetUrl, "="); + replaceSpaceWithPlus(pair->value); + strcat(targetUrl, pair->value); + if(listEntry->nextListEntry != NULL) { + strcat(targetUrl, "&"); + } + } + } + + return targetUrl; } char *assembleHeaderField(char *key, char *value) { - char *header = malloc(strlen(key) + strlen(value) + 3); + char *header = malloc(strlen(key) + strlen(value) + 3); - strcpy(header, key), - strcat(header, ": "); - strcat(header, value); + strcpy(header, key), + strcat(header, ": "); + strcat(header, value); - return header; + return header; } void postData(CURL *handle, char *bodyParameters) { - curl_easy_setopt(handle, CURLOPT_POSTFIELDS, bodyParameters); - curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE_LARGE, - strlen(bodyParameters)); + curl_easy_setopt(handle, CURLOPT_POSTFIELDS, bodyParameters); + curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE_LARGE, + strlen(bodyParameters)); } -void apiClient_invoke(apiClient_t *apiClient, - char *operationName, - char *operationParameter, - list_t *queryParameters, - list_t *headerParameters, - list_t *formParameters, - char *bodyParameters, - char *requestType) { - CURL *handle = curl_easy_init(); - CURLcode res; - - if(handle) { - listEntry_t *listEntry; - curl_mime *mime = NULL; - struct curl_slist *headers = NULL; - - headers = - curl_slist_append(headers, "accept: application/json"); - headers = curl_slist_append(headers, - "Content-Type: application/json"); - if(requestType != NULL) { - curl_easy_setopt(handle, - CURLOPT_CUSTOMREQUEST, - requestType); - } - if(formParameters != NULL) { - mime = curl_mime_init(handle); - - list_ForEach(listEntry, formParameters) { - keyValuePair_t *keyValuePair = listEntry->data; - if((keyValuePair->key != NULL) && - (keyValuePair->value != NULL) ) - { - curl_mimepart *part = curl_mime_addpart( - mime); - curl_mime_data(part, - keyValuePair->key, - CURL_ZERO_TERMINATED); - curl_mime_name(part, - keyValuePair->value); - } - } - - curl_easy_setopt(handle, CURLOPT_MIMEPOST, mime); - } - - list_ForEach(listEntry, headerParameters) { - keyValuePair_t *keyValuePair = listEntry->data; - if((keyValuePair->key != NULL) && - (keyValuePair->value != NULL) ) - { - char *headerValueToWrite = - assembleHeaderField( - keyValuePair->key, - keyValuePair->value); - curl_slist_append(headers, headerValueToWrite); - free(headerValueToWrite); - } - } - // this would only be generated for apiKey authentication - #ifdef API_KEY - list_ForEach(listEntry, apiClient->apiKeys) { - keyValuePair_t *apiKey = listEntry->data; - if((apiKey->key != NULL) && - (apiKey->value != NULL) ) - { - char *headerValueToWrite = - assembleHeaderField( - apiKey->key, - apiKey->value); - curl_slist_append(headers, headerValueToWrite); - free(headerValueToWrite); - } - } - #endif // API_KEY - - char *targetUrl = - assembleTargetUrl(apiClient->basePath, - operationName, - operationParameter, - queryParameters); - - curl_easy_setopt(handle, CURLOPT_URL, targetUrl); - curl_easy_setopt(handle, - CURLOPT_WRITEFUNCTION, - writeDataCallback); - curl_easy_setopt(handle, - CURLOPT_WRITEDATA, - &apiClient->dataReceived); - curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers); - - // this would only be generated for OAuth2 authentication - #ifdef OAUTH2 - if(apiClient->accessToken != NULL) { - // curl_easy_setopt(handle, CURLOPT_HTTPAUTH, CURLAUTH_BEARER); - curl_easy_setopt(handle, - CURLOPT_XOAUTH2_BEARER, - apiClient->accessToken); - } - #endif - - - // this would only be generated for basic authentication: - #ifdef BASIC_AUTH - char *authenticationToken; - - if((apiClient->username != NULL) && - (apiClient->password != NULL) ) - { - authenticationToken = malloc(strlen( - apiClient->username) + - strlen( - apiClient->password) + - 2); - sprintf(authenticationToken, - "%s:%s", - apiClient->username, - apiClient->password); - - curl_easy_setopt(handle, - CURLOPT_HTTPAUTH, - CURLAUTH_BASIC); - curl_easy_setopt(handle, - CURLOPT_USERPWD, - authenticationToken); - } - - #endif // BASIC_AUTH - - if(bodyParameters != NULL) { - postData(handle, bodyParameters); - } - - res = curl_easy_perform(handle); - - curl_slist_free_all(headers); - - free(targetUrl); - - if(res != CURLE_OK) { - fprintf(stderr, "curl_easy_perform() failed: %s\n", - curl_easy_strerror(res)); - } - #ifdef BASIC_AUTH - if((apiClient->username != NULL) && - (apiClient->password != NULL) ) - { - free(authenticationToken); - } - #endif // BASIC_AUTH - curl_easy_cleanup(handle); - if(formParameters != NULL) { - curl_mime_free(mime); - } - } +void apiClient_invoke(apiClient_t *apiClient, + char *operationName, + char *operationParameter, + list_t *queryParameters, + list_t *headerParameters, + list_t *formParameters, + char *bodyParameters, + char *requestType) { + CURL *handle = curl_easy_init(); + CURLcode res; + + if(handle) { + listEntry_t *listEntry; + curl_mime *mime = NULL; + struct curl_slist *headers = NULL; + + headers = + curl_slist_append(headers, "accept: application/json"); + headers = curl_slist_append(headers, + "Content-Type: application/json"); + if(requestType != NULL) { + curl_easy_setopt(handle, + CURLOPT_CUSTOMREQUEST, + requestType); + } + if(formParameters != NULL) { + mime = curl_mime_init(handle); + + list_ForEach(listEntry, formParameters) { + keyValuePair_t *keyValuePair = listEntry->data; + if((keyValuePair->key != NULL) && + (keyValuePair->value != NULL) ) + { + curl_mimepart *part = curl_mime_addpart( + mime); + curl_mime_data(part, + keyValuePair->key, + CURL_ZERO_TERMINATED); + curl_mime_name(part, + keyValuePair->value); + } + } + + curl_easy_setopt(handle, CURLOPT_MIMEPOST, mime); + } + + list_ForEach(listEntry, headerParameters) { + keyValuePair_t *keyValuePair = listEntry->data; + if((keyValuePair->key != NULL) && + (keyValuePair->value != NULL) ) + { + char *headerValueToWrite = + assembleHeaderField( + keyValuePair->key, + keyValuePair->value); + curl_slist_append(headers, headerValueToWrite); + free(headerValueToWrite); + } + } + // this would only be generated for apiKey authentication + #ifdef API_KEY + list_ForEach(listEntry, apiClient->apiKeys) { + keyValuePair_t *apiKey = listEntry->data; + if((apiKey->key != NULL) && + (apiKey->value != NULL) ) + { + char *headerValueToWrite = + assembleHeaderField( + apiKey->key, + apiKey->value); + curl_slist_append(headers, headerValueToWrite); + free(headerValueToWrite); + } + } + #endif // API_KEY + + char *targetUrl = + assembleTargetUrl(apiClient->basePath, + operationName, + operationParameter, + queryParameters); + + curl_easy_setopt(handle, CURLOPT_URL, targetUrl); + curl_easy_setopt(handle, + CURLOPT_WRITEFUNCTION, + writeDataCallback); + curl_easy_setopt(handle, + CURLOPT_WRITEDATA, + &apiClient->dataReceived); + curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers); + + // this would only be generated for OAuth2 authentication + #ifdef OAUTH2 + if(apiClient->accessToken != NULL) { + // curl_easy_setopt(handle, CURLOPT_HTTPAUTH, CURLAUTH_BEARER); + curl_easy_setopt(handle, + CURLOPT_XOAUTH2_BEARER, + apiClient->accessToken); + } + #endif + + + // this would only be generated for basic authentication: + #ifdef BASIC_AUTH + char *authenticationToken; + + if((apiClient->username != NULL) && + (apiClient->password != NULL) ) + { + authenticationToken = malloc(strlen( + apiClient->username) + + strlen( + apiClient->password) + + 2); + sprintf(authenticationToken, + "%s:%s", + apiClient->username, + apiClient->password); + + curl_easy_setopt(handle, + CURLOPT_HTTPAUTH, + CURLAUTH_BASIC); + curl_easy_setopt(handle, + CURLOPT_USERPWD, + authenticationToken); + } + + #endif // BASIC_AUTH + + if(bodyParameters != NULL) { + postData(handle, bodyParameters); + } + + res = curl_easy_perform(handle); + + curl_slist_free_all(headers); + + free(targetUrl); + + if(res != CURLE_OK) { + fprintf(stderr, "curl_easy_perform() failed: %s\n", + curl_easy_strerror(res)); + } + #ifdef BASIC_AUTH + if((apiClient->username != NULL) && + (apiClient->password != NULL) ) + { + free(authenticationToken); + } + #endif // BASIC_AUTH + curl_easy_cleanup(handle); + if(formParameters != NULL) { + curl_mime_free(mime); + } + } } size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp) { - *(char **) userp = strdup(buffer); + *(char **) userp = strdup(buffer); - return size * nmemb; + return size * nmemb; } -int str_replace(char *in, char* toreplace, char* replaceby ){ - int i,mark,copy_len=0; - char* copy; - int in_len=strlen(in); - for(i=0;i<(in_len-strlen(toreplace));i++){ - if(memcmp(in+i,toreplace,strlen(toreplace)) == 0){ - mark = i; - copy = malloc(in_len+strlen(replaceby)); - memcpy(copy,in,mark); - copy_len=mark; - memcpy(copy+copy_len,replaceby,strlen(replaceby)); - copy_len += strlen(replaceby); - memcpy(copy+copy_len,in+mark+strlen(toreplace),in_len-mark-strlen(toreplace)+1); - copy_len += in_len-mark-strlen(toreplace)+1; - } - } - if(!copy_len) - goto error; - free(in); - memcpy(in,copy,copy_len); - return 1; -error: - return 0; -} \ No newline at end of file +char *str_replace(char *orig, char *rep, char *with) { + char *result; // the return string + char *ins; // the next insert point + char *tmp; // varies + int len_rep; // length of rep (the string to remove) + int len_with; // length of with (the string to replace rep with) + int len_front; // distance between rep and end of last rep + int count; // number of replacements + + // sanity checks and initialization + if (!orig || !rep) + return NULL; + len_rep = strlen(rep); + if (len_rep == 0) + return NULL; // empty rep causes infinite loop during count + if (!with) + with = ""; + len_with = strlen(with); + + // count the number of replacements needed + ins = orig; + for (count = 0; tmp = strstr(ins, rep); ++count) { + ins = tmp + len_rep; + } + + tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1); + + if (!result) + return NULL; + + // first time through the loop, all the variable are set correctly + // from here on, + // tmp points to the end of the result string + // ins points to the next occurrence of rep in orig + // orig points to the remainder of orig after "end of rep" + while (count--) { + ins = strstr(orig, rep); + len_front = ins - orig; + tmp = strncpy(tmp, orig, len_front) + len_front; + tmp = strcpy(tmp, with) + len_with; + orig += len_front + len_rep; // move to next "end of rep" + } + strcpy(tmp, orig); + return result; +} + + From 1734e728f1c565ef8248b93f9a412aab79f9dd3b Mon Sep 17 00:00:00 2001 From: Hemant Zope Date: Tue, 28 Aug 2018 18:21:43 +0200 Subject: [PATCH 4/6] store return value of modified path --- .../src/main/resources/C-libcurl/api.mustache | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache index 3dee44a380a0..accd338cf2cf 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache @@ -38,14 +38,15 @@ // TODO base path = {{{basePath}}} char* baseNameMod=malloc(strlen({{baseName}})+2); //baseNameMod free not yet implemented snprintf(baseNameMod,strlen(baseName)+3,"%s%s%s","{",{{baseName}},"}"); + char *localVarPathMod = malloc(MAX_BUFFER_LENGTH); {{#paramName}} {{#isLong}} char buff[64]; int_to_str(buf,{{paramName}}); - str_replace(localVarPath,baseNameMod,buff); + localVarPathMod = str_replace(localVarPath,baseNameMod,buff); {{/isLong}} {{#isString}} - str_replace(localVarPath,baseNameMod,{{paramName}}); + localVarPathMod = str_replace(localVarPath,baseNameMod,{{paramName}}); {{/isString}} {{/paramName}} {{/pathParams}} From f4f3b07eecf7d32b2c21de9132d1b22f9e43ab3c Mon Sep 17 00:00:00 2001 From: Hemant Zope Date: Wed, 29 Aug 2018 11:22:50 +0200 Subject: [PATCH 5/6] set str_replace variable to be same as original variable. --- .../src/main/resources/C-libcurl/api.mustache | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache index accd338cf2cf..87e15222e61f 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache @@ -38,15 +38,14 @@ // TODO base path = {{{basePath}}} char* baseNameMod=malloc(strlen({{baseName}})+2); //baseNameMod free not yet implemented snprintf(baseNameMod,strlen(baseName)+3,"%s%s%s","{",{{baseName}},"}"); - char *localVarPathMod = malloc(MAX_BUFFER_LENGTH); {{#paramName}} {{#isLong}} char buff[64]; int_to_str(buf,{{paramName}}); - localVarPathMod = str_replace(localVarPath,baseNameMod,buff); + localVarPath = str_replace(localVarPath,baseNameMod,buff); {{/isLong}} {{#isString}} - localVarPathMod = str_replace(localVarPath,baseNameMod,{{paramName}}); + localVarPath = str_replace(localVarPath,baseNameMod,{{paramName}}); {{/isString}} {{/paramName}} {{/pathParams}} From f4ea6ce072a0ded1288c00b9097b9058886c57ef Mon Sep 17 00:00:00 2001 From: Hemant Zope Date: Mon, 3 Sep 2018 12:49:18 +0200 Subject: [PATCH 6/6] [C] Fixed coding style issues --- .../src/main/resources/C-libcurl/api.mustache | 14 +++++----- .../resources/C-libcurl/apiClient.c.mustache | 26 +++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache index 87e15222e61f..660f1286239f 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache @@ -7,10 +7,10 @@ {{/imports}} #define MAX_BUFFER_LENGTH 4096 -#define int_to_str(dst,src) \ +#define intToStr(dst, src) \ do {\ char dst[64];\ - snprintf(dst,64,"%ld",(long int)(src));\ + snprintf(dst, 64, "%ld", (long int)(src));\ }while(0) {{#operations}} @@ -36,16 +36,16 @@ {{#pathParams}} // TODO path parameter {{paramName}} ({{baseName}}) not yet supported // TODO base path = {{{basePath}}} - char* baseNameMod=malloc(strlen({{baseName}})+2); //baseNameMod free not yet implemented - snprintf(baseNameMod,strlen(baseName)+3,"%s%s%s","{",{{baseName}},"}"); + char* baseNameMod = malloc(strlen({{baseName}})+2); //baseNameMod free not yet implemented + snprintf(baseNameMod, strlen(baseName)+3, "%s%s%s", "{", {{baseName}}, "}"); {{#paramName}} {{#isLong}} char buff[64]; - int_to_str(buf,{{paramName}}); - localVarPath = str_replace(localVarPath,baseNameMod,buff); + intToStr(buf, {{paramName}}); + localVarPath = strReplace(localVarPath, baseNameMod, buff); {{/isLong}} {{#isString}} - localVarPath = str_replace(localVarPath,baseNameMod,{{paramName}}); + localVarPath = strReplace(localVarPath, baseNameMod, {{paramName}}); {{/isString}} {{/paramName}} {{/pathParams}} diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache index 6c3359c0d213..9cd6e1b32397 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache @@ -276,32 +276,32 @@ size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp) { return size * nmemb; } -char *str_replace(char *orig, char *rep, char *with) { +char *strReplace(char *orig, char *rep, char *with) { char *result; // the return string char *ins; // the next insert point char *tmp; // varies - int len_rep; // length of rep (the string to remove) - int len_with; // length of with (the string to replace rep with) - int len_front; // distance between rep and end of last rep + int lenRep; // length of rep (the string to remove) + int lenWith; // length of with (the string to replace rep with) + int lenFront; // distance between rep and end of last rep int count; // number of replacements // sanity checks and initialization if (!orig || !rep) return NULL; - len_rep = strlen(rep); - if (len_rep == 0) + lenRep = strlen(rep); + if (lenRep == 0) return NULL; // empty rep causes infinite loop during count if (!with) with = ""; - len_with = strlen(with); + lenWith = strlen(with); // count the number of replacements needed ins = orig; for (count = 0; tmp = strstr(ins, rep); ++count) { - ins = tmp + len_rep; + ins = tmp + lenRep; } - tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1); + tmp = result = malloc(strlen(orig) + (lenWith - lenRep) * count + 1); if (!result) return NULL; @@ -313,10 +313,10 @@ char *str_replace(char *orig, char *rep, char *with) { // orig points to the remainder of orig after "end of rep" while (count--) { ins = strstr(orig, rep); - len_front = ins - orig; - tmp = strncpy(tmp, orig, len_front) + len_front; - tmp = strcpy(tmp, with) + len_with; - orig += len_front + len_rep; // move to next "end of rep" + lenFront = ins - orig; + tmp = strncpy(tmp, orig, lenFront) + lenFront; + tmp = strcpy(tmp, with) + lenWith; + orig += lenFront + lenRep; // move to next "end of rep" } strcpy(tmp, orig); return result;