blob: 391e6eebe9985aeec9199ad08b4906f482660100 (
plain)
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
|
#include "pgnp.hpp"
#include <iostream>
#define IS_BLANK(c) (c==' ' || c=='\n' || c=='\t')
#define IS_EOF(loc) (loc>=pgn_content.size())
#define EOF_CHECK(loc) {if(IS_EOF(loc)) throw UnexpectedEOF();}
namespace pgnp {
void PGN::FromFile(std::string filepath){
std::ifstream file(filepath);
std::string content((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
FromString(content);
}
void PGN::FromString(std::string pgn_content){
this->pgn_content=pgn_content;
int loc=0;
while(!IS_EOF(loc)) {
char c=pgn_content[loc];
if(!IS_BLANK(c)){
switch (c) {
case '[':
loc=ParseNextTag(loc);
break;
}
}
loc++;
}
/*for (auto const& [key, val] : tags){
std::cout << key <<"="<<val<<std::endl;
}*/
}
int PGN::ParseNextTag(int start_loc){
// Parse key
std::string key;
int keyloc=start_loc+1;
EOF_CHECK(keyloc);
char c=pgn_content[keyloc];
while(!IS_BLANK(c)){
key+=c;
keyloc++;
EOF_CHECK(keyloc);
c=pgn_content[keyloc];
}
// Parse value
std::string value;
int valueloc=NextNonBlank(keyloc)+1;
EOF_CHECK(keyloc);
c=pgn_content[valueloc];
while(c!='"' or IS_EOF(valueloc)){
value+=c;
valueloc++;
EOF_CHECK(keyloc);
c=pgn_content[valueloc];
}
// Add tag
tags[key]=value;
return(valueloc+1); // +1 For the last char of the tag which is ']'
}
int PGN::NextNonBlank(int loc){
char c=pgn_content[loc];
while(IS_BLANK(c)){
loc++;
c=pgn_content[loc];
}
return(loc);
}
}
|