diff --git a/Debug/cm b/Debug/cm new file mode 100755 index 0000000..d568009 Binary files /dev/null and b/Debug/cm differ diff --git a/Debug/cs/subdir.mk b/Debug/cs/subdir.mk new file mode 100644 index 0000000..5ad1b7c --- /dev/null +++ b/Debug/cs/subdir.mk @@ -0,0 +1,24 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +# Add inputs and outputs from these tool invocations to the build variables +C_SRCS += \ +../cs/cs_model_writer.c + +OBJS += \ +./cs/cs_model_writer.o + +C_DEPS += \ +./cs/cs_model_writer.d + + +# Each subdirectory must supply rules for building sources it contributes +cs/%.o: ../cs/%.c + @echo 'Building file: $<' + @echo 'Invoking: Cross GCC Compiler' + gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + diff --git a/Debug/makefile b/Debug/makefile index e80f949..629c98a 100644 --- a/Debug/makefile +++ b/Debug/makefile @@ -10,6 +10,7 @@ RM := rm -rf -include sources.mk -include objc/subdir.mk -include java/subdir.mk +-include cs/subdir.mk -include subdir.mk -include objects.mk diff --git a/Debug/sources.mk b/Debug/sources.mk index 90be6ec..32b6b18 100644 --- a/Debug/sources.mk +++ b/Debug/sources.mk @@ -16,4 +16,5 @@ SUBDIRS := \ objc \ . \ java \ +cs \ diff --git a/README b/README index cdd8aa9..4c101a7 100644 --- a/README +++ b/README @@ -1,8 +1,8 @@ Clever Models -Small tool to generate model classes from Restful Feeds. ( JSON & XML (planned) ) . +Small tool to generate POJOs model classes from Feeds. ( JSON ) . -Current initial Support, Java models from JSON Feeds. +Current Support, Java, C# & Objective-C models from JSON Feeds. Install : @@ -24,4 +24,6 @@ Supported Language arguments -l java or --l objc \ No newline at end of file +-l' objc +or +-l csharp \ No newline at end of file diff --git a/Release/cm b/Release/cm index 57f997f..ec5ee13 100755 Binary files a/Release/cm and b/Release/cm differ diff --git a/Release/cs/subdir.mk b/Release/cs/subdir.mk new file mode 100644 index 0000000..f082666 --- /dev/null +++ b/Release/cs/subdir.mk @@ -0,0 +1,24 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +# Add inputs and outputs from these tool invocations to the build variables +C_SRCS += \ +../cs/cs_model_writer.c + +OBJS += \ +./cs/cs_model_writer.o + +C_DEPS += \ +./cs/cs_model_writer.d + + +# Each subdirectory must supply rules for building sources it contributes +cs/%.o: ../cs/%.c + @echo 'Building file: $<' + @echo 'Invoking: Cross GCC Compiler' + gcc -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + diff --git a/Release/makefile b/Release/makefile index e80f949..629c98a 100644 --- a/Release/makefile +++ b/Release/makefile @@ -10,6 +10,7 @@ RM := rm -rf -include sources.mk -include objc/subdir.mk -include java/subdir.mk +-include cs/subdir.mk -include subdir.mk -include objects.mk diff --git a/Release/sources.mk b/Release/sources.mk index 90be6ec..32b6b18 100644 --- a/Release/sources.mk +++ b/Release/sources.mk @@ -16,4 +16,5 @@ SUBDIRS := \ objc \ . \ java \ +cs \ diff --git a/clever_models.c b/clever_models.c index 1364f74..9a9f6c9 100644 --- a/clever_models.c +++ b/clever_models.c @@ -27,7 +27,7 @@ unsigned int write_json_class_model(char name[], char json[], int language_code, FILE* implementation_file = 0; FILE* header_file = 0; - printf("writting class files ! \n"); + printf("writting class files ! language_code : %d \n", language_code); create_class_files(name, path, language_code, class_headers, &implementation_file, &header_file); write_class_header(name, language_code, implementation_file, header_file); printf("wrote class files ! \n"); @@ -145,14 +145,14 @@ unsigned int write_json_class_model(char name[], char json[], int language_code, if(OBJC == language_code) fprintf(implementation_file, "@end"); else - fprintf(implementation_file, "@end"); + fprintf(implementation_file, "}"); fclose(implementation_file); if (NULL != header_file) { if(OBJC == language_code) fprintf(header_file, "@end"); else - fprintf(header_file, "@end"); + fprintf(header_file, "}"); fclose(implementation_file); } //free(mutable_json); diff --git a/cs/cs_model_writer.c b/cs/cs_model_writer.c new file mode 100644 index 0000000..19ca8bd --- /dev/null +++ b/cs/cs_model_writer.c @@ -0,0 +1,131 @@ +/* + * cs_model_writer.c + * + * Created on: Jun 12, 2012 + * Author: bmartins + */ + +#include +#include "cs_model_writer.h" +#include "../helper_functions.h" +#include "../clever_models.h" + +int32_t cs_write_class_header(FILE* fp, char class_name[]) { + int32_t result = -1; + class_name[0] = toupper(class_name[0]); + char* singular_class_name = str_plural_to_singular(class_name); + fprintf(fp, "\n using System;"); + fprintf(fp, "\n using System.Collections.Generic;"); + result = fprintf(fp, "\n public class %s { \n", singular_class_name); + return result; +} + +int32_t cs_write_attribute(FILE* fp, char attribute_name[], u_int16_t type) { + u_int32_t result = -1; + + char c = toupper(attribute_name[0]); + + char* object_type_name = malloc(strlen(attribute_name)); + strncpy(object_type_name, attribute_name, strlen(attribute_name)); + object_type_name[strlen(attribute_name)] = '\0'; + + *object_type_name = c; + object_type_name = str_plural_to_singular(object_type_name); + + if (TYPE_OBJECT == type) + result = fprintf(fp, "\tprivate %s %s; \n", object_type_name, + attribute_name); + else if (TYPE_LIST == type) + result = fprintf(fp, "\tprivate List<%s> %s; \n", object_type_name, + attribute_name); + else if (TYPE_INTEGER == type) + result = fprintf(fp, "\tprivate int %s; \n", attribute_name); + else if (TYPE_STRING == type) + result = fprintf(fp, "\tprivate string %s; \n", attribute_name); + else if (TYPE_BOOLEAN == type) + result = fprintf(fp, "\tprivate bool %s; \n", attribute_name); + else + result = fprintf(fp, "\tprivate string %s; \n", attribute_name); + + + free(object_type_name); + return result; +} + +int cs_write_getters(FILE* fp, int nr_attributes, char attributes[50][100], int attr_types[]) { + int i; + for (i = 0; i < nr_attributes; ++i) { + + char* uppercase_attribute_name = malloc(sizeof(attributes[i])); + strncpy(uppercase_attribute_name, attributes[i], sizeof(attributes[i])); + uppercase_attribute_name[strlen(uppercase_attribute_name)] = '\0'; + uppercase_attribute_name[0] = toupper(uppercase_attribute_name[0]); + + char* object_type_name = malloc(sizeof(uppercase_attribute_name)); + strncpy(object_type_name, uppercase_attribute_name, sizeof(uppercase_attribute_name)); + object_type_name[strlen(uppercase_attribute_name)] = '\0'; + + object_type_name = str_plural_to_singular(object_type_name); + + if (TYPE_OBJECT == attr_types[i]) + fprintf(fp, "\n\tpublic %s get%s() { \n", object_type_name, uppercase_attribute_name); + else if (TYPE_LIST == attr_types[i]) + fprintf(fp, "\n\tpublic List<%s> get%s() { \n", object_type_name, uppercase_attribute_name); + else if (TYPE_INTEGER == attr_types[i]) + fprintf(fp, "\n\tpublic int get%s() { \n", uppercase_attribute_name); + else if (TYPE_STRING == attr_types[i]) + fprintf(fp, "\n\tpublic string get%s() { \n", uppercase_attribute_name); + else if (TYPE_BOOLEAN == attr_types[i]) + fprintf(fp, "\n\tpublic bool get%s() { \n", uppercase_attribute_name); + else + fprintf(fp, "\n\tpublic string get%s() { \n", uppercase_attribute_name); + + + + fprintf(fp, "\t\t return %s; \n", attributes[i]); + fprintf(fp, "\t} \n"); + + free(object_type_name); + free(uppercase_attribute_name); + } + + return i; +} + +int cs_write_setters(FILE* fp, int nr_attributes, char attributes[50][100], int attr_types[]) { + int i; + for (i = 0; i < nr_attributes; ++i) { + + char* uppercase_attribute_name = malloc(sizeof(attributes[i])); + strncpy(uppercase_attribute_name, attributes[i], sizeof(attributes[i])); + uppercase_attribute_name[strlen(uppercase_attribute_name)] = '\0'; + uppercase_attribute_name[0] = toupper(uppercase_attribute_name[0]); + + char* object_type_name = malloc(sizeof(uppercase_attribute_name)); + strncpy(object_type_name, uppercase_attribute_name, sizeof(uppercase_attribute_name)); + object_type_name[strlen(uppercase_attribute_name)] = '\0'; + + object_type_name = str_plural_to_singular(object_type_name); + + if (TYPE_OBJECT == attr_types[i]) + fprintf(fp, "\n\tpublic void set%s(%s %s) { \n", uppercase_attribute_name, object_type_name, attributes[i]); + else if (TYPE_LIST == attr_types[i]) + fprintf(fp, "\n\tpublic void set%s(List<%s> %s) { \n", uppercase_attribute_name, object_type_name, attributes[i]); + else if (TYPE_INTEGER == attr_types[i]) + fprintf(fp, "\n\tpublic void set%s(int %s) { \n", uppercase_attribute_name, attributes[i]); + else if (TYPE_STRING == attr_types[i]) + fprintf(fp, "\n\tpublic void set%s(string %s) { \n", uppercase_attribute_name, attributes[i]); + else if (TYPE_BOOLEAN == attr_types[i]) + fprintf(fp, "\n\tpublic void set%s(bool %s) { \n", uppercase_attribute_name, attributes[i]); + else + fprintf(fp, "\n\tpublic void set%s(string %s) { \n", uppercase_attribute_name, attributes[i]); + + fprintf(fp, "\t\t this.%s = %s; \n", attributes[i], attributes[i]); + fprintf(fp, "\t} \n"); + + free(object_type_name); + free(uppercase_attribute_name); + } + + return i; +} diff --git a/cs/cs_model_writer.h b/cs/cs_model_writer.h new file mode 100644 index 0000000..3b3b748 --- /dev/null +++ b/cs/cs_model_writer.h @@ -0,0 +1,21 @@ +/* + * cs_model_writer.h + * + * Created on: Jun 12, 2012 + * Author: bmartins + */ + +#ifndef CS_MODEL_WRITER_H_ +#define CS_MODEL_WRITER_H_ + +#include +#include +#include "../constants.h" + +int32_t cs_write_class_header(FILE* fp, char class_name[]); +int32_t cs_write_attribute(FILE* fp, char attribute_name[], u_int16_t type); +int cs_write_getters(FILE* fp, int nr_attributes, char attributes[MAX_ATTRIBUTES][MAX_ATTRIBUTE_NAME_LENGTH], int attr_types[]); +int cs_write_setters(FILE* fp, int nr_attributes, char attributes[MAX_ATTRIBUTES][MAX_ATTRIBUTE_NAME_LENGTH], int attr_types[]); + + +#endif /* CS_MODEL_WRITER_H_ */ diff --git a/languages.h b/languages.h index 05534ec..cfb1e75 100644 --- a/languages.h +++ b/languages.h @@ -11,11 +11,13 @@ #include -const static int JAVA = 0; -const static int PHP = 1; -const static int CSHARP = 2; -const static int CPP = 3; -const static int OBJC = 4; +enum { + JAVA = 0, + PHP = 1, + CSHARP = 2, + CPP = 3, + OBJC = 4 +}; int get_language_code(char* language); diff --git a/model_builder.c b/model_builder.c index e32d393..8ff2e4c 100644 --- a/model_builder.c +++ b/model_builder.c @@ -14,14 +14,20 @@ #include "helper_functions.h" #include "java/java_model_writer.h" #include "objc/objc_model_writer.h" +#include "cs/cs_model_writer.h" int write_setters(FILE* implementation_file, FILE* header_file, int language_code, char attributes[MAX_ATTRIBUTES][MAX_ATTRIBUTE_NAME_LENGTH], int attr_types[], int nr_attributes) { int result = -1; - if (JAVA == language_code) { - result = java_write_setters(implementation_file, nr_attributes, attributes, attr_types); - } else if (OBJC == language_code) { - + switch (language_code) { + case JAVA: + result = java_write_setters(implementation_file, nr_attributes, attributes, attr_types); + break; + case CSHARP: + result = cs_write_setters(implementation_file, nr_attributes, attributes, attr_types); + break; + default: + break; } return result; } @@ -29,21 +35,36 @@ int write_setters(FILE* implementation_file, FILE* header_file, int language_cod int write_getters(FILE* implementation_file, FILE* header_file, int language_code, char attributes[MAX_ATTRIBUTES][MAX_ATTRIBUTE_NAME_LENGTH], int attr_types[], int nr_attributes) { int result = -1; - if (JAVA == language_code) { - result = java_write_getters(implementation_file, nr_attributes, attributes, attr_types); - } else if (OBJC == language_code) { + switch (language_code) { + case JAVA: + result = java_write_getters(implementation_file, nr_attributes, attributes, attr_types); + break; + case CSHARP: + result = cs_write_getters(implementation_file, nr_attributes, attributes, attr_types); + break; + default: + break; } + return result; } int write_class_header(char class_name[], int16_t language, FILE* implementation_file, FILE* header_file) { int result = -1; - if (JAVA == language) - result = java_write_class_header(implementation_file, class_name); - else if (OBJC == language) { - result = objc_write_class_header(implementation_file, header_file, class_name); + switch (language) { + case JAVA: + result = java_write_class_header(implementation_file, class_name); + break; + case OBJC: + result = objc_write_class_header(implementation_file, header_file, class_name); + break; + case CSHARP: + result = cs_write_class_header(implementation_file, class_name); + break; + default: + break; } return result; @@ -54,12 +75,18 @@ int write_attribute(char attribute_name[], int16_t type, int16_t language, int result = -1; - if (JAVA == language) { - - result = java_write_attribute(implementation_file, attribute_name, - type); - } else if (OBJC == language) { - result = objc_write_attribute(implementation_file, header_file, attribute_name, type); + switch (language) { + case JAVA: + result = java_write_attribute(implementation_file, attribute_name, type); + break; + case OBJC: + result = objc_write_attribute(implementation_file, header_file, attribute_name, type); + break; + case CSHARP: + result = cs_write_attribute(implementation_file, attribute_name, type); + break; + default: + break; } return result; @@ -82,18 +109,28 @@ void create_class_files(char name[], char path[], int language_code, char class_ strcat(header_file_path, "."); *header = NULL; - if (JAVA == language_code) { - strcat(file_path, "java"); - } else if(OBJC == language_code) { - strcat(file_path, "m"); - strcat(header_file_path, "h"); - - *header = fopen(header_file_path, "w+"); - if(NULL == *header) { - perror("failed in creating header file \n"); - exit(-1); - } + switch (language_code) { + case JAVA: + strcat(file_path, "java"); + break; + + case OBJC: + strcat(file_path, "m"); + strcat(header_file_path, "h"); + + *header = fopen(header_file_path, "w+"); + if (NULL == *header) { + perror("failed in creating header file \n"); + exit(-1); + } + break; + + case CSHARP: + strcat(file_path, "cs"); + break; + default: + break; } *implementation = fopen(file_path, "w+");