-
Notifications
You must be signed in to change notification settings - Fork 0
/
bpp.h
211 lines (187 loc) · 3.82 KB
/
bpp.h
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <iostream>
#include <fstream>
#include <sstream>
#include <cctype>
#include <map>
#include <vector>
#include <memory>
class Metadata {
public:
Metadata() {}
virtual ~Metadata() {}
virtual void print(std::string tabs="") const {}
};
class MetaStr : public Metadata {
public:
MetaStr(std::string&& s) { str = std::move(s); }
bool operator<(const MetaStr& other) const { return str < other.str; }
std::string str;
void print(std::string tabs="") const { std::cout << tabs << str; }
};
class MetaDict : public Metadata {
public:
std::map<MetaStr, std::shared_ptr<Metadata>> dict;
void print(std::string tabs="") const
{
std::cout << tabs << "{\n";
std::string newtabs = tabs + '\t';
for (auto itr = dict.cbegin(); itr != dict.cend(); ++itr) {
itr->first.print(newtabs);
std::cout << ",\n";
itr->second->print(newtabs);
std::cout << "\n\n";
}
std::cout << tabs << '}';
}
};
class MetaList : public Metadata {
public:
std::vector<std::shared_ptr<Metadata>> list;
void print(std::string tabs="") const
{
std::cout << tabs << "[\n";
std::string newtabs = tabs + '\t';
for (const auto& x : list) {
x->print(newtabs);
std::cout << ",\n";
}
std::cout << tabs << ']';
}
};
class MetaInt : public Metadata {
public:
MetaInt(long long i) { integer = i; }
long long integer;
void print(std::string tabs="") const { std::cout << tabs << integer; }
};
class Parser {
public:
Parser(const std::string&);
std::shared_ptr<Metadata> parse() { return elem(); }
int count();
struct Exception {
std::string msg;
Exception(std::string&& m) { msg = std::move(m); }
};
private:
std::ifstream infile;
int charcount = 1;
int getchar();
std::string read(long long);
long long getint();
void expect(char);
std::shared_ptr<Metadata> elem();
MetaList list();
void list_elems(MetaList*);
MetaDict dict();
void dict_elems(MetaDict*);
MetaStr string();
MetaInt integer();
};
Parser::Parser(const std::string& fname)
{
infile.open(fname);
}
int Parser::count()
{
return charcount;
}
int Parser::getchar()
{
++charcount;
return infile.get();
}
void Parser::expect(char c)
{
if (this->getchar() != c)
throw Exception("expected '" + std::string(1, c) + "'");
}
std::shared_ptr<Metadata> Parser::elem()
{
int c = infile.peek();
if (c == 'd')
return std::make_unique<MetaDict>(dict());
else if (c == 'l')
return std::make_unique<MetaList>(list());
else if (c == 'i')
return std::make_unique<MetaInt>(integer());
else if (isdigit(c))
return std::make_unique<MetaStr>(string());
else
throw Exception("expected element");
}
MetaInt Parser::integer()
{
expect('i');
long long i = getint();
expect('e');
return MetaInt(i);
}
void Parser::list_elems(MetaList *ml)
{
ml->list.push_back(elem());
if (infile.peek() == 'e')
return;
list_elems(ml);
}
MetaList Parser::list()
{
MetaList ml;
expect('l');
list_elems(&ml);
expect('e');
return ml;
}
void Parser::dict_elems(MetaDict *md)
{
MetaStr s = string();
md->dict[std::move(s)] = elem();
if (infile.peek() == 'e')
return;
dict_elems(md);
}
MetaDict Parser::dict()
{
MetaDict md;
expect('d');
dict_elems(&md);
expect('e');
return md;
}
long long Parser::getint()
{
long long i;
if (!(infile >> i))
throw Exception("expected number");
charcount += infile.gcount();
return i;
}
std::string Parser::read(long long len)
{
char buf[len+1];
buf[len] = '\0';
if (!infile.read(buf, len))
throw Exception("expected string");
charcount += infile.gcount();
return std::string(buf);
}
MetaStr Parser::string()
{
long long numchars = getint();
expect(':');
return MetaStr(read(numchars));
}
/*
int main(int argc, char **argv)
{
Parser p("test.torrent");
try {
std::shared_ptr<Metadata> m = p.parse();
m->print();
} catch (Parser::Exception e) {
std::cout << "Error at " << p.count() << ": " << e.msg << "\n";
return 1;
}
return 0;
}
*/