-
Notifications
You must be signed in to change notification settings - Fork 9
/
model_builder.c
155 lines (126 loc) · 3.64 KB
/
model_builder.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// model_builder.c
// CleverModels
//
// Created by Bruno Martins on 3/29/12.
// Copyright (c) 2012. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include "languages.h"
#include "model_builder.h"
#include "clever_models.h"
#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;
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;
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;
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;
}
int write_attribute(char attribute_name[], int16_t type, int16_t language,
FILE* implementation_file, FILE* header_file) {
int result = -1;
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;
}
void create_class_files(char name[], char path[], int language_code, char class_headers[] , FILE** implementation, FILE** header) {
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;
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+");
if (NULL == *implementation) {
perror("failed creating implementation file \n");
exit(-1);
}
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);
}
}
}