Skip to content

Commit

Permalink
[C] Resolve some issues from #1473 (#1497)
Browse files Browse the repository at this point in the history
* change folder structure for external and added list as primitive type

* modified gitignore for C generator

* change include statement as per directory structure

* added projectname variable for using in cmakelists

* modified cmakefile:compile files irrespective of testfiles and add support for make install

* updated readme with how to compile and how to use

* CMakeLists change curl to 7.58.0

* fix indentation

* add remove as reserved word
  • Loading branch information
zhemant authored and wing328 committed Dec 6, 2018
1 parent 32d228c commit df19c13
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 85 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,7 @@ samples/client/petstore/javascript/package-lock.json

# elm
samples/client/petstore/elm/index.html

# C
samples/client/petstore/c/build
samples/client/petstore/c/*.so
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.*;
import org.openapitools.codegen.utils.ModelUtils;
Expand All @@ -38,7 +40,10 @@
public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(CLibcurlClientCodegen.class);

public static final String PROJECT_NAME = "projectName";
protected String moduleName;
protected String projectName;
protected static final String defaultProjectName = "openapi_client";
protected String specFolder = "spec";
protected String libFolder = "lib";
protected String apiDocPath = "docs/";
Expand Down Expand Up @@ -93,6 +98,7 @@ public CLibcurlClientCodegen() {
"int",
"long",
"register",
"remove",
"restrict",
"return",
"short",
Expand Down Expand Up @@ -135,6 +141,7 @@ public CLibcurlClientCodegen() {
languageSpecificPrimitives.add("FILE");
languageSpecificPrimitives.add("Object");
languageSpecificPrimitives.add("list_t*");
languageSpecificPrimitives.add("list");

typeMapping.put("string", "char");
typeMapping.put("char", "char");
Expand Down Expand Up @@ -200,8 +207,8 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("list.h.mustache", "include", "list.h"));
// external folder
supportingFiles.add(new SupportingFile("cJSON.licence.mustache", "external", "cJSON.licence"));
supportingFiles.add(new SupportingFile("cJSON.c.mustache", "external" + File.separator + "src", "cJSON.c"));
supportingFiles.add(new SupportingFile("cJSON.h.mustache", "external" + File.separator + "include", "cJSON.h"));
supportingFiles.add(new SupportingFile("cJSON.c.mustache", "external", "cJSON.c"));
supportingFiles.add(new SupportingFile("cJSON.h.mustache", "external", "cJSON.h"));

}

Expand Down Expand Up @@ -500,7 +507,7 @@ public String toApiImport(String name) {

@Override
public String toModelImport(String name) {
return "#include \"" + name + ".h\"";
return "#include \"" +"../model/" + name + ".h\"";
}

@Override
Expand Down Expand Up @@ -567,6 +574,21 @@ public void setParameterExampleValue(CodegenParameter p) {
p.example = example;
}

@Override
public void preprocessOpenAPI(OpenAPI openAPI) {
if (openAPI.getInfo() != null) {
Info info = openAPI.getInfo();
setProjectName((escapeText(info.getTitle())));
} else {
setProjectName(defaultProjectName);
}
additionalProperties.put(PROJECT_NAME, projectName);
}

public void setProjectName(String projectName) {
this.projectName = underscore(projectName.toLowerCase(Locale.ROOT));
}

@Override
public boolean shouldOverwrite(String filename) {
// skip spec file as the file might have been updated with new test cases
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
cmake_minimum_required (VERSION 2.6)
project (CGenerator)

file(GLOB SRC_C src/*.c)
#file(GLOB UNIT_TESTS_C unit-tests/*.c)
#file(GLOB UNIT_TEST_C unit-test/*.c)
file(GLOB MODEL_C model/*.c)
file(GLOB API_C api/*.c)
file(GLOB EXTERNAL_SRC_C external/src/*.c)
set(ALL_SRC_LIST ${SRC_C} ${UNIT_TESTS_C} ${UNIT_TEST_C} ${MODEL_C} ${API_C})
cmake_policy(SET CMP0063 NEW)

set(CMAKE_C_VISIBILITY_PRESET default)
set(CMAKE_CXX_VISIBILITY_PRESET default)
set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF)
set(CMAKE_BUILD_TYPE Debug)

include(CTest)
include_directories(include)
include_directories(external/include)
include_directories(model)
include_directories(api)
set(pkgName "{{projectName}}")

find_program(VALGRIND valgrind)
if(VALGRIND)
Expand All @@ -23,54 +17,57 @@ if(VALGRIND)
set(VALGRIND_LIST "")
endif()

find_package(CURL 7.61.1 REQUIRED)
find_package(CURL 7.58.0 REQUIRED)
if(CURL_FOUND)
include_directories(${CURL_INCLUDE_DIR})
set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} ${CURL_LIBRARIES} )
else(CURL_FOUND)
message(FATAL_ERROR "Could not find the CURL library and development files.")
endif()

# comment out below as auto-generated test file is not supported at the moment
#foreach(ELEMENT ${UNIT_TESTS_C})
# get_filename_component(ELEMENT_NAME ${ELEMENT} NAME_WE)
# string(REGEX REPLACE "\\.c$" "" ELEMENT_REPLACED ${ELEMENT_NAME})
# set(EXE_NAME unit-${ELEMENT_REPLACED})
# add_executable(${EXE_NAME} ${ELEMENT} ${SRC_C} ${MODEL_C} ${API_C} ${EXTERNAL_SRC_C})
# target_link_libraries(${EXE_NAME} ${CURL_LIBRARIES})
# add_test(NAME ${EXE_NAME} COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME})
#
# if(VALGRIND)
# set(memcheck_command "${CMAKE_MEMORYCHECK_COMMAND} ${CMAKE_MEMORYCHECK_COMMAND_OPTIONS}")
# separate_arguments(memcheck_command)
#
# add_test(
# NAME valgrind-test-${ELEMENT_REPLACED}
# COMMAND ${memcheck_command} ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME}
# WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
# )
# endif()
#endforeach()
#
#foreach(ELEMENT ${UNIT_TEST_C})
#get_filename_component(ELEMENT_NAME ${ELEMENT} NAME_WE)
#string(REGEX REPLACE "\\.c$" "" ELEMENT_REPLACED ${ELEMENT_NAME})
#set(EXE_NAME unit-${ELEMENT_REPLACED})
#add_executable(${EXE_NAME} ${ELEMENT} ${SRC_C} ${MODEL_C} ${API_C} ${EXTERNAL_SRC_C})
#target_link_libraries(${EXE_NAME} ${CURL_LIBRARIES})
#add_test(NAME ${EXE_NAME} COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME})
#
#if(VALGRIND)
#set(memcheck_command "${CMAKE_MEMORYCHECK_COMMAND} ${CMAKE_MEMORYCHECK_COMMAND_OPTIONS}")
#separate_arguments(memcheck_command)
#
#add_test(
#NAME valgrind-test-${ELEMENT_REPLACED}
#COMMAND ${memcheck_command} ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME}
#WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
#)
#endif()
#endforeach()
set(SRCS
src/list.c
src/apiKey.c
src/apiClient.c
external/cJSON.c
{{#models}}
{{#model}}
model/{{classname}}.c
{{/model}}
{{/models}}
{{#apiInfo}}
{{#apis}}
{{#operations}}
api/{{classname}}.c
{{/operations}}
{{/apis}}
{{/apiInfo}}

)

set(HDRS
include/apiClient.h
include/list.h
include/keyValuePair.h
external/cJSON.h
{{#models}}
{{#model}}
model/{{classname}}.h
{{/model}}
{{/models}}
{{#apiInfo}}
{{#apis}}
{{#operations}}
api/{{classname}}.h
{{/operations}}
{{/apis}}
{{/apiInfo}}

)

add_library(${pkgName} SHARED ${SRCS} ${HDRS})
target_link_libraries(${pkgName} ${CURL_LIBRARIES} )
install(TARGETS ${pkgName} DESTINATION ${CMAKE_INSTALL_PREFIX})

#For common coding standard (code beautifier/pretty printing)
find_program(UNCRUSTIFY uncrustify)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@ For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}})
{{/infoUrl}}

## Installation
You'll need the `curl 7.61.1` package in order to build the API. To have code formatted nicely you also need to have uncrustify version 0.67.
You'll need the `curl 7.58.0` package in order to build the API. To have code formatted nicely you also need to have uncrustify version 0.67.

### Prerequisites
Install the `curl 7.61.1` package with the following command on Linux.
# Prerequisites

## Install the `curl 7.58.0` package with the following command on Linux.
```bash
sudo apt remove curl
wget http://curl.haxx.se/download/curl-7.61.1.tar.gz
tar -xvf curl-7.61.1.tar.gz
cd curl-7.61.1/
wget http://curl.haxx.se/download/curl-7.58.0.tar.gz
tar -xvf curl-7.58.0.tar.gz
cd curl-7.58.0/
./configure
make
sudo make install
```
Install the `uncrustify 0.67` package with the following command on Linux.
## Install the `uncrustify 0.67` package with the following command on Linux.
```bash
git clone https://github.com/uncrustify/uncrustify.git
cd uncrustify
Expand All @@ -38,6 +39,27 @@ make
sudo make install
```

## Compile the sample:
```bash
mkdir build
cd build
// To install library to specific location use following command
cmake -DCMAKE_INSTALL_PREFIX=/pathtolocaiton ..
// for normal install use following command
cmake ..
make
sudo make install
```
## How to use compiled library
Considering the test/source code which uses the API is written in main.c(respective api include is written and all objects necessary are defined)

To compile main.c use following command
-L - locaiton of the library(not required if cmake with normal installation is performed)
-l library name
```bash
gcc main.c -L. -lpetstore -o main
```


## Author

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "apiClient.h"
#include "cJSON.h"
#include "keyValuePair.h"
{{#imports}}{{{import}}}
{{/imports}}
#include "{{classname}}.h"


#define MAX_BUFFER_LENGTH 4096
#define intToStr(dst, src) \
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <stdlib.h>
#include <stdio.h>
#include "apiClient.h"
#include "cJSON.h"
#include "../include/apiClient.h"
#include "../include/list.h"
#include "../external/cJSON.h"
#include "../include/keyValuePair.h"
{{#imports}}{{{import}}}
{{/imports}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "apiClient.h"
#include "keyValuePair.h"
#include "../include/apiClient.h"

size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#ifndef INCLUDE_API_CLIENT_H
#define INCLUDE_API_CLIENT_H

#include "list.h"
#include "../include/list.h"
#include "../include/keyValuePair.h"

typedef int bool;
#define true 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <stdlib.h>
#include <string.h>
#include "keyValuePair.h"
#include "../include/keyValuePair.h"

keyValuePair_t *keyValuePair_create(char *key, void *value) {
keyValuePair_t *keyValuePair = malloc(sizeof(keyValuePair_t));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef _keyValuePair_H_
#define _keyValuePair_H_

#include<string.h>

typedef struct keyValuePair_t {
Expand All @@ -8,3 +11,5 @@ typedef struct keyValuePair_t {
keyValuePair_t *keyValuePair_create(char *key, void *value);

void keyValuePair_free(keyValuePair_t *keyValuePair);

#endif /* _keyValuePair_H_ */
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
#include <stdlib.h>
#include <stdio.h>

#include "cJSON.h"
#include "list.h"

#include "../include/list.h"
static listEntry_t *listEntry_create(void *data) {
listEntry_t *createdListEntry = malloc(sizeof(listEntry_t));
if(createdListEntry == NULL) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#ifndef INCLUDE_LIST_H
#define INCLUDE_LIST_H

#include "cJSON.h"
#include "../external/cJSON.h"
#include "../include/list.h"

typedef struct list_t list_t;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "cJSON.h"
#include "list.h"
#include "keyValuePair.h"
#include "{{classname}}.h"
{{#imports}}
#include "{{{.}}}.h"
{{/imports}}


{{#vars}}
{{#isEnum}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#define _{{classname}}_H_

#include <string.h>
#include "cJSON.h"
#include "../external/cJSON.h"
#include "../include/list.h"
#include "../include/keyValuePair.h"
{{#imports}}
#include "{{{.}}}.h"
{{/imports}}
Expand Down

0 comments on commit df19c13

Please sign in to comment.