levelz-c 0.1.0
Loading...
Searching...
No Matches
block.h
1#ifndef LEVELZ_BLOCK_H
2#define LEVELZ_BLOCK_H
3
4#define _BLOCK_PROPERTIES_INIT_CAPACITY 16
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9
10#include "coordinate.h"
11
15typedef struct BlockProperty {
19 char* name;
20
24 char* value;
26
30typedef struct Block {
34 char* name;
35
40
45
50} Block;
51
56Block* createBlock(char* name) {
57 Block* b = (Block*) malloc(sizeof(Block));
58 b->name = name;
59 b->propertyCount = 0;
60 b->propertyCapacity = _BLOCK_PROPERTIES_INIT_CAPACITY;
61 b->properties = (BlockProperty**) malloc(b->propertyCapacity * sizeof(BlockProperty*));
62
63 return b;
64}
65
72char* Block_getProperty(Block* b, char* name) {
73 if (b == 0) return 0;
74 if (name == 0) return 0;
75
76 for (int i = 0; i < b->propertyCount; i++) {
77 if (strcmp(b->properties[i]->name, name) == 0) {
78 return b->properties[i]->value;
79 }
80 }
81
82 return "";
83}
84
91void Block_setProperty(Block* b, char* name, char* value) {
92 if (b == 0) return;
93 if (name == 0) return;
94 if (value == 0) return;
95
96 for (int i = 0; i < b->propertyCount; i++) {
97 if (strcmp(b->properties[i]->name, name) == 0) {
98 b->properties[i]->value = value;
99 return;
100 }
101 }
102
103 BlockProperty* p = (BlockProperty*) malloc(sizeof(BlockProperty));
104 p->name = strcpy((char*) malloc(strlen(name) + 1), name);
105 p->value = strcpy((char*) malloc(strlen(value) + 1), value);
106
107 if (b->propertyCount == b->propertyCapacity) {
108 b->propertyCapacity *= 2;
109 b->properties = (BlockProperty**) realloc(b->properties, b->propertyCapacity * sizeof(BlockProperty*));
110 }
111
112 b->properties[b->propertyCount] = p;
113 b->propertyCount++;
114
115 return;
116}
117
123void Block_removeProperty(Block* b, char* name) {
124 if (b == 0) return;
125 if (name == 0) return;
126 if (b->propertyCount == 0) return;
127
128 for (int i = 0; i < b->propertyCount; i++) {
129 if (strcmp(b->properties[i]->name, name) == 0) {
130 free(b->properties[i]);
131 for (int j = i; j < b->propertyCount - 1; j++) {
132 b->properties[j] = b->properties[j + 1];
133 }
134 b->propertyCount--;
135 return;
136 }
137
138 if (b->propertyCount < b->propertyCapacity / 2) {
139 b->propertyCapacity /= 2;
140 b->properties = (BlockProperty**) realloc(b->properties, b->propertyCapacity * sizeof(BlockProperty*));
141 }
142 }
143}
144
150char* Block_toString(Block* b) {
151 if (b == 0) return 0;
152
153 char* name = (char*) malloc(strlen(b->name) + 1);
154 sprintf(name, "%s", b->name);
155
156 if (b->propertyCount == 0)
157 return name;
158
159 int bytes = strlen(name) + 1;
160 for (int i = 0; i < b->propertyCount; i++) {
161 bytes += strlen(b->properties[i]->name) + strlen(b->properties[i]->value) + 4;
162 }
163
164 char* str = (char*) malloc(bytes);
165 sprintf(str, "%s<", name);
166
167 for (int i = 0; i < b->propertyCount; i++) {
168 sprintf(str, "%s%s=%s", str, b->properties[i]->name, b->properties[i]->value);
169 if (i < b->propertyCount - 1)
170 sprintf(str, "%s, ", str);
171 }
172
173 int l = strlen(str);
174 if (str[l - 1] != '>')
175 sprintf(str, "%s>", str);
176
177 return str;
178}
179
185Block* Block_fromString(const char* str) {
186 if (str == 0) return 0;
187
188 int l = strlen(str);
189 if (l == 0) return 0;
190
191 char* str0 = (char*) malloc(l + 1);
192 strcpy(str0, str);
193
194 char* name = strtok(str0, "<");
195 if (name == 0) {
196 free(str0);
197 return 0;
198 }
199
200 char* name0 = (char*) malloc(strlen(name) + 1);
201 strcpy(name0, name);
202
203 Block* b = createBlock(name0);
204
205 char* properties = strtok(0, "<");
206 if (properties == 0) {
207 free(str0);
208 return b;
209 }
210
211 char* property = strtok(properties, "=,");
212 char* key = 0;
213 char* value = 0;
214 while (property != 0) {
215 if (key == 0) {
216 key = property;
217 } else {
218 int l = strlen(property);
219 value = property;
220
221 if (value[l - 1] == '>') {
222 value[l - 1] = '\0';
223 }
224
225 Block_setProperty(b, key, value);
226
227 key = 0;
228 value = 0;
229 }
230
231 property = strtok(0, "=,");
232 }
233
234 free(str0);
235 return b;
236}
237
253
261LevelObject2D* createLevelObject2D(Block* block, Coordinate2D* coordinate) {
262 LevelObject2D* o = (LevelObject2D*) malloc(sizeof(LevelObject2D));
263 o->block = block;
264 o->coordinate = coordinate;
265 return o;
266}
267
272char* LevelObject2D_toString(LevelObject2D* object) {
273 char* block = Block_toString(object->block);
274 char* coordinate = Coordinate2D_toString(object->coordinate);
275
276 char* str = (char*) malloc(strlen(block) + strlen(coordinate) + 3);
277 sprintf(str, "%s: %s", block, coordinate);
278
279 return str;
280}
281
287LevelObject2D* LevelObject2D_fromString(char* str) {
288 char* blockStr = strtok(str, ":");
289 char* coordinateStr = strtok(0, ":");
290
291 Block* block = Block_fromString(blockStr);
292 Coordinate2D* coordinate = Coordinate2D_fromString(coordinateStr);
293
294 return createLevelObject2D(block, coordinate);
295}
296
312
319LevelObject3D* createLevelObject3D(Block* block, Coordinate3D* coordinate) {
320 LevelObject3D* o = (LevelObject3D*) malloc(sizeof(LevelObject3D));
321 o->block = block;
322 o->coordinate = coordinate;
323 return o;
324}
325
330char* LevelObject3D_toString(LevelObject3D* object) {
331 char* block = Block_toString(object->block);
332 char* coordinate = Coordinate3D_toString(object->coordinate);
333
334 char* str = (char*) malloc(strlen(block) + strlen(coordinate) + 3);
335 sprintf(str, "%s: %s", block, coordinate);
336
337 return str;
338}
339
345LevelObject3D* LevelObject3D_fromString(char* str) {
346 char* blockStr = strtok(str, ":");
347 char* coordinateStr = strtok(0, ":");
348
349 Block* block = Block_fromString(blockStr);
350 Coordinate3D* coordinate = Coordinate3D_fromString(coordinateStr);
351
352 return createLevelObject3D(block, coordinate);
353}
354
355#endif
Definition block.h:15
char * value
Definition block.h:24
char * name
Definition block.h:19
Definition block.h:30
int propertyCapacity
Definition block.h:49
int propertyCount
Definition block.h:44
BlockProperty ** properties
Definition block.h:39
char * name
Definition block.h:34
Definition coordinate.h:11
Definition coordinate.h:108
Definition block.h:241
Block * block
Definition block.h:246
Coordinate2D * coordinate
Definition block.h:251
Definition block.h:300
Block * block
Definition block.h:305
Coordinate3D * coordinate
Definition block.h:310