-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoomParser.cpp
86 lines (68 loc) · 3.37 KB
/
RoomParser.cpp
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
#include "RoomParser.hpp"
#include "data_path.hpp"
#include <iostream>
#include <fstream>
Room RoomParser::parse_room(std::string path) {
Room room;
std::ifstream room_file(data_path("rooms/" + path).c_str());
if(!room_file.is_open()) {
std::cerr << path << " did not open!\n";
}
std::string text;
std::getline(room_file, text, '\0');
size_t first_choice_index = text.find_first_of('[');
if(first_choice_index != std::string::npos) {
room.main_text = text.substr(0, first_choice_index);
text = text.substr(first_choice_index);
assert(text.length() > 1);
}
while(text.find_first_of('[') != std::string::npos) {
first_choice_index = text.find_first_of('[');
text = text.substr(first_choice_index);
//separated to check for malformed text files
size_t first_colon = text.find_first_of(':');
if (first_colon == std::string::npos) std::cerr << "Missing colon!\n";
size_t first_closed_bracket = text.find_first_of(']');
if (first_closed_bracket == std::string::npos) std::cerr << "Missing bracket!\n";
size_t first_open_paren = text.find_first_of('(');
if (first_open_paren == std::string::npos) std::cerr << "Missing open paren!\n";
size_t first_closed_paren = text.find_first_of(')');
if (first_closed_paren == std::string::npos) std::cerr << "Missing closed paren!\n";
std::string number = text.substr(1, first_colon - 1);
std::string choice_text = text.substr(1, first_closed_bracket - 1);
std::string new_path = text.substr(first_open_paren + 1, first_closed_paren - first_open_paren - 1);
room.choices.push_back(choice_text);
room.inputs.insert(std::pair< std::string, std::string >(number, new_path));
if(text.length() == first_closed_paren + 1) return room;
text = text.substr(first_closed_paren + 1);
}
size_t first_word_index = text.find_first_of('{');
if(first_word_index != std::string::npos) {
if(room.main_text.empty()) {
room.main_text = text.substr(0, first_word_index);
}
text = text.substr(first_word_index);
assert(text.length() > 1);
}
while(text.find_first_of('{') != std::string::npos) {
first_word_index = text.find_first_of('{');
text = text.substr(first_word_index);
//separated to check for malformed text files
size_t first_closed_brace = text.find_first_of('}');
if (first_closed_brace == std::string::npos) std::cerr << "Missing brace!\n";
size_t first_open_paren = text.find_first_of('(');
if (first_open_paren == std::string::npos) std::cerr << "Missing open paren!\n";
size_t first_closed_paren = text.find_first_of(')');
if (first_closed_paren == std::string::npos) std::cerr << "Missing closed paren!\n";
std::string word = text.substr(1, first_closed_brace - 1);
std::string choice_text = text.substr(1, first_closed_brace - 1);
std::string new_path = text.substr(first_open_paren + 1, first_closed_paren - first_open_paren - 1);
room.inputs.insert(std::pair< std::string, std::string >(word, new_path));
if(text.length() == first_closed_paren + 1) return room;
text = text.substr(first_closed_paren + 1);
}
if(room.main_text.empty()) {
room.main_text = text;
}
return room;
}