Skip to content
Browse files

Support for C# Pojos added. Fixed wrgon character Bug when closing cl…

…asses
  • Loading branch information...
1 parent 1ac2203 commit 09137f5ee855480ce9af2b0a56d1076f63418ada @BrunoAlexandreMendesMartins committed Jun 12, 2012
Showing with 284 additions and 39 deletions.
  1. BIN Debug/cm
  2. +24 −0 Debug/cs/subdir.mk
  3. +1 −0 Debug/makefile
  4. +1 −0 Debug/sources.mk
  5. +5 −3 README
  6. BIN Release/cm
  7. +24 −0 Release/cs/subdir.mk
  8. +1 −0 Release/makefile
  9. +1 −0 Release/sources.mk
  10. +3 −3 clever_models.c
  11. +131 −0 cs/cs_model_writer.c
  12. +21 −0 cs/cs_model_writer.h
  13. +7 −5 languages.h
  14. +65 −28 model_builder.c
View
BIN Debug/cm
Binary file not shown.
View
24 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 ' '
+
+
View
1 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
View
1 Debug/sources.mk
@@ -16,4 +16,5 @@ SUBDIRS := \
objc \
. \
java \
+cs \
View
8 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
+-l' objc
+or
+-l csharp
View
BIN Release/cm
Binary file not shown.
View
24 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 ' '
+
+
View
1 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
View
1 Release/sources.mk
@@ -16,4 +16,5 @@ SUBDIRS := \
objc \
. \
java \
+cs \
View
6 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);
View
131 cs/cs_model_writer.c
@@ -0,0 +1,131 @@
+/*
+ * cs_model_writer.c
+ *
+ * Created on: Jun 12, 2012
+ * Author: bmartins
+ */
+
+#include <stdio.h>
+#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;
+}
View
21 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 <stdlib.h>
+#include <stdio.h>
+#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_ */
View
12 languages.h
@@ -11,11 +11,13 @@
#include <stdlib.h>
-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);
View
93 model_builder.c
@@ -14,36 +14,57 @@
#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;
}
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+");

0 comments on commit 09137f5

Please sign in to comment.
Something went wrong with that request. Please try again.