-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.c
More file actions
212 lines (181 loc) · 6.42 KB
/
Copy pathArray.c
File metadata and controls
212 lines (181 loc) · 6.42 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
#include "Array.h"
#include "types.h"
#include "Object.h"
#include <stdlib.h>
Array create_array_from_stack(size_t size, struct Element* arr) {
return (Array){.size=size, .capacity=size, .a=arr};
}
Array create_array(size_t size) {
struct Element* arr = malloc(sizeof(struct Element) * size);
return (Array){.size=size, .capacity=size, .a=arr};
}
Array create_empty_array() {
struct Element* arr = malloc(sizeof(struct Element) * ARRAY_INIT_CAPACITY);
return (Array){.size=0, .capacity=ARRAY_INIT_CAPACITY, .a=arr};
}
void delete_array(Array array) {
for (size_t i = 0; i < array.size; ++i) {
if (array.a[i].type == JSON_TYPE_OBJECT)
delete_object(*(struct Object*)array.a[i].value);
else if (array.a[i].type == JSON_TYPE_STRING)
delete_string(*(String*)array.a[i].value);
else if (array.a[i].type == JSON_TYPE_ARRAY)
delete_array(*(Array*)array.a[i].value);
free(array.a[i].value);
}
if (array.a != NULL)
free(array.a);
}
struct Element copy_array_element(struct Element element) {
struct Element copy_element = element;
void* value = NULL;
switch(element.type) {
case JSON_TYPE_OBJECT: {
struct Object* x = malloc(sizeof(struct Object));
*x = copy_object(*(struct Object*)element.value);
value = (void*)x;
break;
}
case JSON_TYPE_CHAR: {
char* x = malloc(sizeof(char));
*x = *(char*)element.value;
value = (void*)x;
break;
}
case JSON_TYPE_INTEGER: {
int32_t* x = malloc(sizeof(int32_t));
*x = *(int32_t*)element.value;
value = (void*)x;
break;
}
case JSON_TYPE_LONG: {
int64_t* x = malloc(sizeof(int64_t));
*x = *(int64_t*)element.value;
value = (void*)x;
break;
}
case JSON_TYPE_ARRAY: {
Array* x = malloc(sizeof(Array));
*x = copy_array(*(Array*)element.value);
value = (void*)x;
break;
}
case JSON_TYPE_STRING: {
String* x = malloc(sizeof(String));
*x = copy_string(*(String*)element.value);
value = (void*)x;
break;
}
default:
fprintf(stderr, "[ERROR]: while copying array got NAT Type or others.\n");
break;
}
copy_element.value = value;
return copy_element;
}
Array copy_array(Array other) {
struct Element* arr = malloc(sizeof(struct Element) * other.capacity);
Array new_array = other;
new_array.a = arr;
for (size_t i = 0; i < other.size; ++i) {
arr[i] = copy_array_element(other.a[i]);
}
return new_array;
}
struct Element get_struct_array_element(Array* array, size_t index) {
if (index >= array->size) {
fprintf(stderr, "Array overbound access tried. Aborting\n");
exit(1);
}
return array->a[index];
}
char get_char_element(Array *array, size_t index) {
struct Element el = get_struct_array_element(array, index);
return *(char*)el.value;
}
int32_t get_int32_t_element(Array *array, size_t index) {
struct Element el = get_struct_array_element(array, index);
return *(int32_t*)el.value;
}
int64_t get_int64_t_element(Array *array, size_t index) {
struct Element el = get_struct_array_element(array, index);
return *(int64_t*)el.value;
}
String get_string_element(Array *array, size_t index) {
struct Element el = get_struct_array_element(array, index);
return *(String*)el.value;
}
struct Object get_object_element(Array *array, size_t index) {
struct Element el = get_struct_array_element(array, index);
return *(struct Object*)el.value;
}
Array get_array_element(Array* array, size_t index) {
struct Element el = get_struct_array_element(array, index);
return *(Array*)el.value;
}
void array_push_back(Array *array, enum JSONType type, void* value) {
if (array->size + 1 > array->capacity) {
array->capacity *= ARRAY_M_FACTOR;
array->a = realloc(array->a, array->capacity);
}
if (array->a == NULL) {
fprintf(stderr, "[ERROR]: Buy more RAM lol\n");
exit(1);
}
array->a[array->size++] = (struct Element){type, value};
}
void array_char_push_back(Array *array, char _x) {
char* x = malloc(sizeof(char));
*x = _x;
array_push_back(array, JSON_TYPE_CHAR, (void*)x);
}
void array_int32_t_push_back(Array *array, int32_t _x) {
int32_t* x = malloc(sizeof(int32_t));
*x = _x;
array_push_back(array, JSON_TYPE_INTEGER, (void*)x);
}
void array_int64_t_push_back(Array *array, int64_t _x) {
int64_t* x = malloc(sizeof(int64_t));
*x = _x;
array_push_back(array, JSON_TYPE_LONG, (void*)x);
}
void array_string_push_back(Array *array, String _x) {
String* x = malloc(sizeof(String));
*x = copy_string(_x);
array_push_back(array, JSON_TYPE_STRING, (void*)x);
}
void array_object_push_back(Array *array, struct Object _x) {
struct Object* x = malloc(sizeof(struct Object));
*x = copy_object(_x);
array_push_back(array, JSON_TYPE_OBJECT, (void*)x);
}
void array_array_push_back(Array *array, Array _x) {
Array* x = malloc(sizeof(Array));
*x = copy_array(_x);
array_push_back(array, JSON_TYPE_OBJECT, (void*)x);
}
void set_struct_array_element(Array* array, size_t index, enum JSONType type, void* value) {
if (index >= array->size) {
fprintf(stderr, "[ERROR]: Array overbound access detected. Aborting.\n");
exit(1);
}
array->a[index] = copy_array_element((struct Element){type, value});
}
void set_char_element(Array *array, size_t index, char x) {
set_struct_array_element(array, index, JSON_TYPE_CHAR, (void*)&x);
}
void set_int32_t_element(Array *array, size_t index, int32_t x) {
set_struct_array_element(array, index, JSON_TYPE_INTEGER, (void*)&x);
}
void set_int64_t_element(Array *array, size_t index, int64_t x) {
set_struct_array_element(array, index, JSON_TYPE_LONG, (void*)&x);
}
void set_string_element(Array *array, size_t index, String x) {
set_struct_array_element(array, index, JSON_TYPE_STRING, (void*)&x);
}
void set_object_element(Array *array, size_t index, struct Object x) {
set_struct_array_element(array, index, JSON_TYPE_OBJECT, (void*)&x);
}
void set_array_element(Array* array, size_t index, Array x) {
set_struct_array_element(array, index, JSON_TYPE_ARRAY, (void*)&x);
}