-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.c
More file actions
229 lines (171 loc) · 6.12 KB
/
object.c
File metadata and controls
229 lines (171 loc) · 6.12 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "object.h"
#include "utils.h"
struct ObjectType {
ObjectTypeToStringFunction * toStringFunction;
ObjectTypeCompareFunction * compareFunction;
ObjectTypeDestructor * destructor;
int nestedTypesSize;
ObjectType ** nestedTypes;
int numProperties;
void ** properties;
};
static char* CHAR_TO_STRING(ObjectType * objectType, void * value);
static int CHAR_COMPARE(ObjectType * objectType, void * value1, void * value2);
static void CHAR_DESTRUCTOR(ObjectType * objectType, void * value);
static char* STRING_TO_STRING(ObjectType * objectType, void * value);
static int STRING_COMPARE(ObjectType * objectType, void * value1, void * value2);
static void STRING_DESTRUCTOR(ObjectType * objectType, void * value);
static char* INT_TO_STRING(ObjectType * objectType, void * value);
static int INT_COMPARE(ObjectType * objectType, void * value1, void * value2);
static void INT_DESTRUCTOR(ObjectType * objectType, void * value);
ObjectType CHAR_OBJECT = {CHAR_TO_STRING, CHAR_COMPARE, CHAR_DESTRUCTOR, 0, 0, 0, 0};
ObjectType STRING_OBJECT = {STRING_TO_STRING, STRING_COMPARE, STRING_DESTRUCTOR, 0, 0, 0, 0};
ObjectType INT_OBJECT = {INT_TO_STRING, INT_COMPARE, INT_DESTRUCTOR, 0, 0, 0, 0};
static ObjectType * NULL_TYPE;
ObjectType * objectTypeInit(ObjectTypeToStringFunction * toStringFunction, ObjectTypeCompareFunction * compareFunction, ObjectTypeDestructor * destructor) {
return objectTypePropertiesNestedInit(toStringFunction, compareFunction, destructor, 0, 0);
}
ObjectType * objectTypePropertiesNestedInit(ObjectTypeToStringFunction * toStringFunction, ObjectTypeCompareFunction * compareFunction, ObjectTypeDestructor * destructor, int nestedTypesSize, int numProperties, ...) {
ObjectType * objType = malloc(sizeof(ObjectType));
objType->toStringFunction = toStringFunction;
objType->compareFunction = compareFunction;
objType->destructor = destructor;
objType->nestedTypesSize = nestedTypesSize;
objType->numProperties = numProperties;
va_list argList;
va_start( argList, numProperties);
if(nestedTypesSize > 0) {
objType->nestedTypes = malloc(sizeof(ObjectType*) * nestedTypesSize);
for( int i = 0; i < nestedTypesSize; i++ )
*(objType->nestedTypes + i) = va_arg( argList, ObjectType * );
} else{
objType->nestedTypes = 0;
}
if(numProperties > 0) {
objType->properties = malloc(sizeof(void *) * numProperties);
for( int i = 0; i < nestedTypesSize; i++ )
*(objType->properties + i) = va_arg( argList, void * );
} else{
objType->properties = 0;
}
va_end( argList );
return objType;
}
void objectTypeDestroyTypeOnly(ObjectType * objectType) {
if (objectType->nestedTypes)
free(objectType->nestedTypes);
if (objectType->properties)
free(objectType->properties);
free(objectType);
}
void objectTypeDestroyValue(ObjectType * objectType, void * value) {
if(objectType->destructor)
objectType->destructor(objectType, value);
}
void objectTypePrintValue(ObjectType * objectType, void * value) {
if(objectType->toStringFunction) {
char * valueString = objectType->toStringFunction(objectType, value);
printf("%s", valueString);
free(valueString);
}
}
char* objectTypeToString(ObjectType * objectType, void * value) {
if(objectType->toStringFunction)
return objectType->toStringFunction(objectType, value);
return 0;
}
ObjectType * objectTypeGetNestedType(ObjectType * objectType, int index) {
int size = objectType->nestedTypesSize;
if(index >= 0 && index < size)
return objectType->nestedTypes[index];
return 0;
}
void * objectTypeGetProperty(ObjectType * objectType, int index) {
int size = objectType->numProperties;
if(index >= 0 && index < size)
return objectType->properties[index];
return 0;
}
int objectTypeCompare(ObjectType * objectType, void * value1, void * value2) {
if(objectType->compareFunction)
return objectType->compareFunction(objectType, value1, value2);
else
return 0;
}
int objectTypeGetNestedTypesSize(ObjectType * objectType) {
return objectType->nestedTypesSize;
}
int objectTypeGetPropertiesSize(ObjectType * objectType) {
return objectType->numProperties;
}
bool objectTypeHasDestructor(ObjectType * objectType) {
return objectType->destructor? true : false;
}
bool objectTypeHasCompareFunction(ObjectType * objectType) {
return objectType->compareFunction? true : false;
}
bool objectTypeHasToStringFunction(ObjectType * objectType) {
return objectType->toStringFunction? true : false;
}
ObjectType * objectTypeNULL() {
if(!NULL_TYPE)
NULL_TYPE = objectTypeInit(0, 0, 0);
return NULL_TYPE;
}
static char* CHAR_TO_STRING(ObjectType * objectType, void * value){
char * temp = malloc(sizeof(char)*2);
*temp = *(char *)value;
*(temp+1) = '\0';
return temp;
}
static int CHAR_COMPARE(ObjectType * objectType, void * value1, void * value2){
int charValue1 = *(char *)value1;
int charValue2 = *(char *)value2;
if(charValue1 < charValue2)
return -1;
else if (charValue1 > charValue2)
return 1;
return 0;
}
static void CHAR_DESTRUCTOR(ObjectType * objectType, void * value) {
free(value);
}
static char* STRING_TO_STRING(ObjectType * objectType, void * value){
char * stringValue = (char*) value;
char c = *stringValue;
int size = getStringSize(stringValue);
char * temp = malloc(sizeof(char) * (size+1));
for(int i = 0; i < size; i++)
*(temp+i) = *(stringValue+i);
*(temp+size) = '\0';
return temp;
}
static int STRING_COMPARE(ObjectType * objectType, void * value1, void * value2){
char * string1 = (char *)value1;
char * string2 = (char *)value2;
return strcmp(string1, string2);
}
static void STRING_DESTRUCTOR(ObjectType * objectType, void * value) {
free(value);
}
static char* INT_TO_STRING(ObjectType * objectType, void * value){
int intValue = *(int *)value;
char * temp = intToString(intValue);
return temp;
}
static int INT_COMPARE(ObjectType * objectType, void * value1, void * value2){
int charValue1 = *(int *)value1;
int charValue2 = *(int *)value2;
if(charValue1 < charValue2)
return -1;
else if (charValue1 > charValue2)
return 1;
return 0;
}
static void INT_DESTRUCTOR(ObjectType * objectType, void * value) {
free(value);
}