Permalink
Browse files

Added support for Objective-C

  • Loading branch information...
1 parent f01bb0f commit 9d3db0befd157d4a945f5a91c6fa79cb888edad4 @ehanoc committed Apr 26, 2012
View
@@ -8,6 +8,7 @@ RM := rm -rf
# All of the sources participating in the build are defined here
-include sources.mk
+-include objc/subdir.mk
-include java/subdir.mk
-include subdir.mk
-include objects.mk
View
@@ -0,0 +1,24 @@
+################################################################################
+# Automatically-generated file. Do not edit!
+################################################################################
+
+# Add inputs and outputs from these tool invocations to the build variables
+C_SRCS += \
+../objc/objc_model_write.c
+
+OBJS += \
+./objc/objc_model_write.o
+
+C_DEPS += \
+./objc/objc_model_write.d
+
+
+# Each subdirectory must supply rules for building sources it contributes
+objc/%.o: ../objc/%.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
@@ -13,6 +13,7 @@ EXECUTABLES :=
# Every subdirectory with source files must be described here
SUBDIRS := \
+objc \
. \
java \
View
Binary file not shown.
View
@@ -8,6 +8,7 @@ RM := rm -rf
# All of the sources participating in the build are defined here
-include sources.mk
+-include objc/subdir.mk
-include java/subdir.mk
-include subdir.mk
-include objects.mk
View
@@ -0,0 +1,24 @@
+################################################################################
+# Automatically-generated file. Do not edit!
+################################################################################
+
+# Add inputs and outputs from these tool invocations to the build variables
+C_SRCS += \
+../objc/objc_model_write.c
+
+OBJS += \
+./objc/objc_model_write.o
+
+C_DEPS += \
+./objc/objc_model_write.d
+
+
+# Each subdirectory must supply rules for building sources it contributes
+objc/%.o: ../objc/%.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
@@ -13,6 +13,7 @@ EXECUTABLES :=
# Every subdirectory with source files must be described here
SUBDIRS := \
+objc \
. \
java \
View
@@ -27,9 +27,10 @@ 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");
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");
// char* json = malloc(strlen(json) + 1);
// strncpy(json, json, strlen(json));
// json[strlen(json)] = '\0';
@@ -140,9 +141,18 @@ unsigned int write_json_class_model(char name[], char json[], int language_code,
close_class:
write_getters(implementation_file, header_file, language_code, attributes, attr_types, nr_attributes);
write_setters(implementation_file, header_file, language_code, attributes, attr_types, nr_attributes);
- fprintf(implementation_file, "}");
+
+ if(OBJC == language_code)
+ fprintf(implementation_file, "@end");
+ else
+ fprintf(implementation_file, "@end");
+
fclose(implementation_file);
if (NULL != header_file) {
+ if(OBJC == language_code)
+ fprintf(header_file, "@end");
+ else
+ fprintf(header_file, "@end");
fclose(implementation_file);
}
//free(mutable_json);
View
@@ -12,6 +12,28 @@
#include <ctype.h>
#include "helper_functions.h"
+void preppend_to_file(const char *t, FILE *fp)
+{
+ char* buf;
+ long len = 0;
+
+ fseek(fp,0,SEEK_END);
+ len = ftell(fp);
+ fseek(fp, 0, SEEK_SET);
+
+ buf = (char *)malloc(len);
+ fread(buf,len,1,fp);
+
+ fseek(fp,0,SEEK_SET);
+
+ fprintf(fp, "%s",t);
+ fprintf(fp, "%s",buf);
+
+ fseek(fp,0,SEEK_END);
+
+ free(buf);
+}
+
char* str_plural_to_singular(char str[])
{
// long pos_s_char = str_pos_reverse('s', str, strlen(str), 1);
View
@@ -18,4 +18,6 @@ char find_first_character(char haystack[]);
char* str_plural_to_singular(char str[]);
char* escape_single_quotes(char* json);
+void preppend_to_file(const char *t, FILE *fp);
+
#endif
View
@@ -31,5 +31,9 @@ int get_language_code(char* language)
code = CPP;
}
+ if (strcmp(language, "objc") == 0) {
+ code = OBJC;
+ }
+
return code;
}
View
@@ -15,6 +15,7 @@ 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;
int get_language_code(char* language);
View
4 main.c
@@ -116,14 +116,12 @@ int main(int argc, char * argv[]) {
//fclose(feed_file_stream);
}
//
- //printf("json : %s \n", json);
+ printf("json : %s \n", json);
//json = clean_spaces(json);
json[strlen(json)] = '\0';
-
-
write_json_class_model(name, json, language_code, models_path, class_headers, 0);
free(json);
View
@@ -13,12 +13,15 @@
#include "clever_models.h"
#include "helper_functions.h"
#include "java/java_model_writer.h"
+#include "objc/objc_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) {
+
}
return result;
}
@@ -28,6 +31,8 @@ int write_getters(FILE* implementation_file, FILE* header_file, int language_cod
int result = -1;
if (JAVA == language_code) {
result = java_write_getters(implementation_file, nr_attributes, attributes, attr_types);
+ } else if (OBJC == language_code) {
+
}
return result;
}
@@ -37,6 +42,9 @@ int write_class_header(char class_name[], int16_t language, FILE* implementation
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);
+ }
return result;
}
@@ -50,23 +58,42 @@ int write_attribute(char attribute_name[], int16_t type, int16_t 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);
}
return result;
}
void create_class_files(char name[], char path[], int language_code, char class_headers[] , FILE** implementation, FILE** header) {
- char file_path[255];
+ char file_path[255]; //implementation file
+ char header_file_path[255]; // header, if needed
+
strcpy(file_path, path);
+ strcpy(header_file_path, path);
name[0] = toupper(name[0]);
char* singular_class_name = str_plural_to_singular(name);
strcat(file_path, singular_class_name);
strcat(file_path, ".");
+ strcat(header_file_path, singular_class_name);
+ 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);
+ }
}
*implementation = fopen(file_path, "w+");
@@ -76,11 +103,15 @@ void create_class_files(char name[], char path[], int language_code, char class_
exit(-1);
}
- header = NULL;
fprintf(*implementation, "// Class generated by Clever Models \n");
if (0 != class_headers) {
fprintf(*implementation, "%s\n\n", class_headers);
+
+ if(NULL != *header) {
+ printf("no no no no! \n");
+ fprintf(*header, "%s\n\n", class_headers);
+ }
}
}
View
@@ -0,0 +1,90 @@
+/*
+ * objc_model_write.c
+ *
+ * Created on: Apr 23, 2012
+ * Author: bmartins
+ */
+#include <stdio.h>
+#include "../helper_functions.h"
+#include "../clever_models.h"
+#include "objc_model_writer.h"
+
+int32_t objc_write_class_header(FILE* fp, FILE* header_file, 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(header_file, "#import <Foundation/Foundation.h> \n\n");
+
+ result = fprintf(header_file, "@interface %s : NSObject \n", singular_class_name);
+
+ if(-1 == result)
+ perror("failed writting into header file!");
+
+ fprintf(fp, "#import \"%s.h\" \n\n",class_name);
+ result = fprintf(fp, "@implementation %s \n", singular_class_name);
+
+ if(-1 == result)
+ perror("failed writting into header file!");
+
+ return result;
+}
+
+int32_t objc_write_attribute(FILE* fp, FILE* header_file, 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(header_file, "\t@property (strong) %s* %s; \n", object_type_name,
+ attribute_name);
+ result = fprintf(fp, "\t@synthesize %s; \n", attribute_name);
+
+ char include_str[200];
+ strcat(include_str, "#import \"");
+ strcat(include_str, object_type_name);
+ strcat(include_str, ".h\" \n");
+
+ include_str[199] = '\0';
+
+ preppend_to_file(include_str, header_file);
+ }
+ else if (TYPE_LIST == type) {
+ result = fprintf(header_file, "\t@property (strong) NSArray* %s; \n", object_type_name,
+ attribute_name);
+ fprintf(fp, "\t@synthesize %s; \n", object_type_name,
+ attribute_name);
+ }
+ else if (TYPE_INTEGER == type) {
+ result = fprintf(header_file, "\t@property (strong) NSInteger* %s; \n", attribute_name);
+ result = fprintf(fp, "\t@synthesize %s; \n", attribute_name);
+ } else if (TYPE_STRING == type) {
+ result = fprintf(header_file, "\t@property (strong) NSString* %s; \n", attribute_name);
+ result = fprintf(fp, "\t@synthesize %s; \n", attribute_name);
+ }
+ else if (TYPE_BOOLEAN == type) {
+ result = fprintf(header_file, "\t@property (strong) BOOL %s; \n", attribute_name);
+ result = fprintf(fp, "\t@synthesize %s; \n", attribute_name);
+ } else {
+ result = fprintf(header_file, "\t@property (strong) NSString* %s; \n", attribute_name);
+ result = fprintf(fp, "\t@synthesize %s; \n", attribute_name);
+ }
+
+ free(object_type_name);
+ return result;
+}
+
+int objc_write_getters(FILE* fp, FILE* header_file, int nr_attributes, char attributes[MAX_ATTRIBUTES][MAX_ATTRIBUTE_NAME_LENGTH], int attr_types[])
+{}
+
+int objc_write_setters(FILE* fp, FILE* header_file,int nr_attributes, char attributes[MAX_ATTRIBUTES][MAX_ATTRIBUTE_NAME_LENGTH], int attr_types[])
+{}
+
View
@@ -0,0 +1,20 @@
+/*
+ * objc_model_writer.h
+ *
+ * Created on: Apr 23, 2012
+ * Author: bmartins
+ */
+
+#ifndef OBJC_MODEL_WRITER_H_
+#define OBJC_MODEL_WRITER_H_
+
+#include <stdlib.h>
+#include <stdio.h>
+#include "../constants.h"
+
+int32_t objc_write_class_header(FILE* fp, FILE* header_file, char class_name[]);
+int32_t objc_write_attribute(FILE* fp, FILE* header_file, char attribute_name[], u_int16_t type);
+int objc_write_getters(FILE* fp, FILE* header_file, int nr_attributes, char attributes[MAX_ATTRIBUTES][MAX_ATTRIBUTE_NAME_LENGTH], int attr_types[]);
+int objc_write_setters(FILE* fp, FILE* header_file,int nr_attributes, char attributes[MAX_ATTRIBUTES][MAX_ATTRIBUTE_NAME_LENGTH], int attr_types[]);
+
+#endif /* OBJC_MODEL_WRITER_H_ */

0 comments on commit 9d3db0b

Please sign in to comment.