9#include "levelz/coordinate.h"
10#include "levelz/block.h"
11#include "levelz/level.h"
12#include "levelz/matrix.h"
17const char* LEVELZ_HEADER_END =
"---";
22const char* LEVELZ_END =
"end";
26char* __trim(
const char* str) {
27 char* str0 = (
char*) malloc(strlen(str) + 1);
31 while (isspace(str0[i])) i++;
33 int j = strlen(str0) - 1;
34 while (isspace(str0[j])) j--;
36 char* str1 = (
char*) malloc(j - i + 2);
37 strncpy(str1, str0 + i, j - i + 1);
38 str1[j - i + 1] =
'\0';
48 char* line = input[index];
51 LevelHeader* header = LevelHeader_fromString(__trim(line));
57 headers[index] = header;
58 headers[index + 1] = 0;
68 char* trimmed = __trim(input);
69 char* str0 = (
char*) malloc(strlen(trimmed) + 1);
70 strcpy(str0, trimmed);
76 while (*end && *end !=
'*') end++;
77 char* token = (
char*) malloc(end - start + 1);
78 strncpy(token, start, end - start);
79 token[end - start] =
'\0';
81 if (token[0] ==
'(') {
88 int size = CoordinateMatrix2D_size(matrix);
91 points = CoordinateMatrix2D_coordinates(matrix);
94 while (points[length] != 0) length++;
98 Coordinate2D** matrixPoints = CoordinateMatrix2D_coordinates(matrix);
99 for (
int i = 0; i < size; i++) {
100 points[length + i] = matrixPoints[i];
103 points[length + size] = 0;
119 while (points[length] != 0) length++;
123 points[length] = point;
124 points[length + 1] = 0;
128 start = (*end) ? end + 1 : end;
138 char* trimmed = __trim(input);
139 char* str0 = (
char*) malloc(strlen(trimmed) + 1);
140 strcpy(str0, trimmed);
146 while (*end && *end !=
'*') end++;
147 char* token = (
char*) malloc(end - start + 1);
148 strncpy(token, start, end - start);
149 token[end - start] =
'\0';
151 if (token[0] ==
'(') {
158 int size = CoordinateMatrix3D_size(matrix);
161 points = CoordinateMatrix3D_coordinates(matrix);
164 while (points[length] != 0) length++;
168 Coordinate3D** matrixPoints = CoordinateMatrix3D_coordinates(matrix);
169 for (
int i = 0; i < size; i++) {
170 points[length + i] = matrixPoints[i];
173 points[length + size] = 0;
189 while (points[length] != 0) length++;
193 points[length] = point;
194 points[length + 1] = 0;
198 start = (*end) ? end + 1 : end;
212 char* str0 = (
char*) malloc(strlen(input) + 1);
215 char* blockStr = __trim(strtok(str0,
":"));
216 char* coordinateStr = __trim(strtok(0,
":"));
218 Block* block = Block_fromString(blockStr);
219 Coordinate2D** coordinates = __read2DPoints(coordinateStr);
222 line->coordinates = coordinates;
236 char* str0 = (
char*) malloc(strlen(input) + 1);
239 char* blockStr = __trim(strtok(str0,
":"));
240 char* coordinateStr = __trim(strtok(0,
":"));
242 Block* block = Block_fromString(blockStr);
243 Coordinate3D** coordinates = __read3DPoints(coordinateStr);
245 line->coordinates = coordinates;
258Level2D* readLevel2D(
const char* str) {
261 char* str0 = (
char*) malloc(strlen(str) + 1);
264 char* token = strtok(str0,
"\n");
266 if (strcmp(token, LEVELZ_HEADER_END) == 0) {
270 LevelHeader* header = LevelHeader_fromString(__trim(token));
271 if (header->
name ==
"type" && header->
value !=
"2") {
276 Level2D_addHeader(level, header->
name, header->
value);
278 if (header->
name ==
"spawn") {
280 level->
spawn = spawn;
283 token = strtok(0,
"\n");
286 token = strtok(0,
"\n");
288 if (strcmp(token, LEVELZ_END) == 0) {
294 for (
int i = 0; i <
sizeof(line->coordinates) /
sizeof(
Coordinate2D); i++) {
295 LevelObject2D* obj = createLevelObject2D(line->block, line->coordinates[i]);
296 Level2D_addBlock(level, obj);
299 token = strtok(0,
"\n");
311Level3D* readLevel3D(
const char* str) {
312 Level3D* level = createLevel3D(createCoordinate3D(0, 0, 0));
314 char* str0 = (
char*) malloc(strlen(str) + 1);
317 char* token = strtok(str0,
"\n");
319 if (strcmp(token, LEVELZ_HEADER_END) == 0) {
323 LevelHeader* header = LevelHeader_fromString(__trim(token));
324 if (header->
name ==
"type" && header->
value !=
"3") {
329 Level3D_addHeader(level, header->
name, header->
value);
331 if (header->
name ==
"spawn") {
333 level->
spawn = spawn;
336 token = strtok(0,
"\n");
339 token = strtok(0,
"\n");
341 if (strcmp(token, LEVELZ_END) == 0) {
347 for (
int i = 0; i <
sizeof(line->coordinates) /
sizeof(
Coordinate3D); i++) {
348 LevelObject3D* obj = createLevelObject3D(line->block, line->coordinates[i]);
349 Level3D_addBlock(level, obj);
352 token = strtok(0,
"\n");
364Level2D* parseFile2D(
const char* path) {
365 FILE* file = fopen(path,
"r");
366 if (file == 0)
return 0;
368 fseek(file, 0, SEEK_END);
369 long length = ftell(file);
370 fseek(file, 0, SEEK_SET);
372 char* buffer = (
char*) malloc(length);
373 fread(buffer, 1, length, file);
376 Level2D* level = readLevel2D(buffer);
387Level3D* parseFile3D(
const char* path) {
388 FILE* file = fopen(path,
"r");
389 if (file == 0)
return 0;
391 fseek(file, 0, SEEK_END);
392 long length = ftell(file);
393 fseek(file, 0, SEEK_SET);
395 char* buffer = (
char*) malloc(length);
396 fread(buffer, 1, length, file);
399 Level3D* level = readLevel3D(buffer);
Definition coordinate.h:11
Definition coordinate.h:108
Coordinate2D * spawn
Definition level.h:88
Coordinate3D * spawn
Definition level.h:367