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';
49 char* line = input[index];
52 LevelHeader* header = LevelHeader_fromString(__trim(line));
58 headers[index] = header;
59 headers[index + 1] = 0;
69 char* trimmed = __trim(input);
70 char* str0 = (
char*) malloc(strlen(trimmed) + 1);
71 strcpy(str0, trimmed);
77 while (*end && *end !=
'*') end++;
78 char* token = (
char*) malloc(end - start + 1);
79 strncpy(token, start, end - start);
80 token[end - start] =
'\0';
82 if (token[0] ==
'(') {
89 int size = CoordinateMatrix2D_size(matrix);
92 points = CoordinateMatrix2D_coordinates(matrix);
95 while (points[length] != 0) length++;
99 Coordinate2D** matrixPoints = CoordinateMatrix2D_coordinates(matrix);
100 for (
int i = 0; i < size; i++) {
101 points[length + i] = matrixPoints[i];
104 points[length + size] = 0;
120 while (points[length] != 0) length++;
124 points[length] = point;
125 points[length + 1] = 0;
129 start = (*end) ? end + 1 : end;
139 char* trimmed = __trim(input);
140 char* str0 = (
char*) malloc(strlen(trimmed) + 1);
141 strcpy(str0, trimmed);
147 while (*end && *end !=
'*') end++;
148 char* token = (
char*) malloc(end - start + 1);
149 strncpy(token, start, end - start);
150 token[end - start] =
'\0';
152 if (token[0] ==
'(') {
159 int size = CoordinateMatrix3D_size(matrix);
162 points = CoordinateMatrix3D_coordinates(matrix);
165 while (points[length] != 0) length++;
169 Coordinate3D** matrixPoints = CoordinateMatrix3D_coordinates(matrix);
170 for (
int i = 0; i < size; i++) {
171 points[length + i] = matrixPoints[i];
174 points[length + size] = 0;
190 while (points[length] != 0) length++;
194 points[length] = point;
195 points[length + 1] = 0;
199 start = (*end) ? end + 1 : end;
214 char* str0 = (
char*) malloc(strlen(input) + 1);
217 char* blockStr = __trim(strtok(str0,
":"));
218 char* coordinateStr = __trim(strtok(0,
":"));
220 Block* block = Block_fromString(blockStr);
221 Coordinate2D** coordinates = __read2DPoints(coordinateStr);
224 line->coordinates = coordinates;
238 char* str0 = (
char*) malloc(strlen(input) + 1);
241 char* blockStr = __trim(strtok(str0,
":"));
242 char* coordinateStr = __trim(strtok(0,
":"));
244 Block* block = Block_fromString(blockStr);
245 Coordinate3D** coordinates = __read3DPoints(coordinateStr);
247 line->coordinates = coordinates;
260Level2D* readLevel2D(
const char* str) {
263 char* str0 = (
char*) malloc(strlen(str) + 1);
266 char* token = strtok(str0,
"\n");
268 if (strcmp(token, LEVELZ_HEADER_END) == 0) {
272 LevelHeader* header = LevelHeader_fromString(__trim(token));
273 if (header->
name ==
"type" && header->
value !=
"2") {
278 Level2D_addHeader(level, header->
name, header->
value);
280 if (header->
name ==
"spawn") {
282 level->
spawn = spawn;
285 token = strtok(0,
"\n");
288 token = strtok(0,
"\n");
290 if (strcmp(token, LEVELZ_END) == 0) {
296 for (
int i = 0; i <
sizeof(line->coordinates) /
sizeof(
Coordinate2D); i++) {
297 LevelObject2D* obj = createLevelObject2D(line->block, line->coordinates[i]);
298 Level2D_addBlock(level, obj);
301 token = strtok(0,
"\n");
313Level3D* readLevel3D(
const char* str) {
314 Level3D* level = createLevel3D(createCoordinate3D(0, 0, 0));
316 char* str0 = (
char*) malloc(strlen(str) + 1);
319 char* token = strtok(str0,
"\n");
321 if (strcmp(token, LEVELZ_HEADER_END) == 0) {
325 LevelHeader* header = LevelHeader_fromString(__trim(token));
326 if (header->
name ==
"type" && header->
value !=
"3") {
331 Level3D_addHeader(level, header->
name, header->
value);
333 if (header->
name ==
"spawn") {
335 level->
spawn = spawn;
338 token = strtok(0,
"\n");
341 token = strtok(0,
"\n");
343 if (strcmp(token, LEVELZ_END) == 0) {
349 for (
int i = 0; i <
sizeof(line->coordinates) /
sizeof(
Coordinate3D); i++) {
350 LevelObject3D* obj = createLevelObject3D(line->block, line->coordinates[i]);
351 Level3D_addBlock(level, obj);
354 token = strtok(0,
"\n");
366Level2D* parseFile2D(
const char* path) {
367 FILE* file = fopen(path,
"r");
368 if (file == 0)
return 0;
370 fseek(file, 0, SEEK_END);
371 long length = ftell(file);
372 fseek(file, 0, SEEK_SET);
374 char* buffer = (
char*) malloc(length);
375 fread(buffer, 1, length, file);
378 Level2D* level = readLevel2D(buffer);
389Level3D* parseFile3D(
const char* path) {
390 FILE* file = fopen(path,
"r");
391 if (file == 0)
return 0;
393 fseek(file, 0, SEEK_END);
394 long length = ftell(file);
395 fseek(file, 0, SEEK_SET);
397 char* buffer = (
char*) malloc(length);
398 fread(buffer, 1, length, file);
401 Level3D* level = readLevel3D(buffer);
Definition coordinate.h:11
Definition coordinate.h:108
Coordinate2D * spawn
Definition level.h:88
Coordinate3D * spawn
Definition level.h:401