Skip to content

Commit 3f05778

Browse files
committed
[0.1.1] Add Comments Support
1 parent 3bf829d commit 3f05778

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.16)
22

33
project(levelz-cpp
44
LANGUAGES C CXX
5-
VERSION 0.1.0
5+
VERSION 0.1.1
66
DESCRIPTION "C/C++ implementation of the LevelZ File Format"
77
HOMEPAGE_URL "https://github.com/LevelZ-File/cpp-bindings"
88
)

include/levelz.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <string>
22
#include <vector>
33
#include <fstream>
4+
#include <sstream>
45
#include <cstdio>
56
#include <unordered_map>
67
#include <algorithm>
@@ -249,14 +250,19 @@ namespace LevelZ {
249250

250251
std::vector<LevelObject> blocks;
251252
for (std::string& line : parts[1]) {
253+
if (line[0] == '#') continue;
252254
if (line == END) break;
253255

256+
int ci = line.find('#');
257+
std::string line0 = ci == std::string::npos ? line : line.substr(0, ci);
258+
line0.erase(line0.find_last_not_of(" \n\r\t") + 1);
259+
254260
if (is2D) {
255-
std::pair<Block, std::vector<Coordinate2D>> pair = read2DLine(line);
261+
std::pair<Block, std::vector<Coordinate2D>> pair = read2DLine(line0);
256262
for (Coordinate2D& point : pair.second)
257263
blocks.push_back(LevelObject(pair.first, point));
258264
} else {
259-
std::pair<Block, std::vector<Coordinate3D>> pair = read3DLine(line);
265+
std::pair<Block, std::vector<Coordinate3D>> pair = read3DLine(line0);
260266
for (Coordinate3D& point : pair.second)
261267
blocks.push_back(LevelObject(pair.first, point));
262268
}

test/src/level.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,32 @@ int main() {
1919

2020
// File Parsing
2121
std::vector<std::string> l4v = {
22-
"@type 2",
23-
"@scroll horizontal-right",
24-
"@spawn [-2, 4]",
25-
"---",
26-
"grass: [0, 0]",
27-
"stone: [0, 1]*[0, 2]"
22+
"@type 2",
23+
"@scroll horizontal-right",
24+
"@spawn [-2, 4]",
25+
"---",
26+
"grass: [0, 0]",
27+
"stone: [0, 1]*[0, 2]"
2828
};
2929
Level2D l4 = static_cast<Level2D>(LevelZ::parseLines(l4v));
3030
r |= assert(l4.scroll() == Scroll::HORIZONTAL_RIGHT);
3131
r |= assert(l4.spawn == Coordinate2D(-2, 4));
3232
r |= assert(l4.blocks().size() == 3);
3333

34+
std::vector<std::string> l5v = {
35+
"@type 2",
36+
"@spawn [-10, 4]",
37+
"@scroll none",
38+
"---",
39+
"grass<type=1>: [0, 0]*[0, 1] # Comment 1",
40+
"stone<cracked=false>: [-1, 1]*[0, 2] # Comment 2",
41+
"end",
42+
"Some Comment Here"
43+
};
44+
Level2D l5 = static_cast<Level2D>(LevelZ::parseLines(l5v));
45+
r |= assert(l5.scroll() == Scroll::NONE);
46+
r |= assert(l5.spawn == Coordinate2D(-10, 4));
47+
r |= assert(l5.blocks().size() == 4);
48+
3449
return r;
3550
}

0 commit comments

Comments
 (0)