-
Notifications
You must be signed in to change notification settings - Fork 0
/
BT.h
308 lines (247 loc) · 6.55 KB
/
BT.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#pragma once
#include <string>
#include <list>
#include <map>
#include <memory>
#include <vector>
class BTData {
public:
virtual std::string bencoding() const {
return std::string("error");
}
virtual ~BTData() {
}
};
#include "simpleregex.h"
class BTString : public BTData {
public:
BTString(const std::string& str)
:data(str){
}
std::string bencoding() const {
char head[33];
std::string str{_itoa(data.size(),head,10)};
str.push_back(':');
return str+data;
}
bool compare_and_replace(const std::pair<std::string, std::string>& pair) {
std::pair<const char*, const char*> pos;
unsigned count = 0;
do{
auto pos = match(pair.first.c_str(), data.c_str());
if (!pos.second)
break;
++count;
data.replace(
data.begin()+(pos.first-data.data()),
data.begin() + (pos.second - data.data()),
pair.second);
} while (true);
return count != 0;
}
std::string raw_string() const {
return data;
}
private:
std::string data;
};
class BTInt : public BTData {
public:
BTInt(__int64 _int)
:data(_int) {
}
std::string bencoding() const {
char head[34] = "i";
_i64toa_s(data, head + 1, 33, 10);
std::string str{head};
str.push_back('e');
return str;
}
private:
__int64 data;
};
class BTList :public BTData {
public:
BTList(){}
BTList(std::unique_ptr<BTData>&& ptr) {
attach(std::move(ptr));
}
void attach(std::unique_ptr<BTData>&& ptr)
{
data.emplace_back(std::move(ptr));
}
void replace(const std::vector<std::pair<std::string, std::string>>& pairs);
std::string bencoding() const {
std::string str;
str.push_back('l');
for (auto& ptr : data)
str += ptr->bencoding();
str.push_back('e');
return str;
}
private:
std::list<std::unique_ptr<BTData>> data;
};
class BTDict:public BTData {
public:
BTDict() {
}
void attach(std::string&& key,std::unique_ptr<BTData>&& ptr)
{
data.emplace(std::pair<const std::string, std::unique_ptr<BTData>>(std::move(key),std::move(ptr)));
}
void replace(const std::vector<std::pair<std::string, std::string>>& pairs) {
for (auto & pair : data) {
//if store type,will be faster
if (auto dict_ptr = dynamic_cast<BTDict*>(pair.second.get())) {
dict_ptr->replace(pairs);
}
else if (auto dict_ptr = dynamic_cast<BTList*>(pair.second.get())) {
dict_ptr->replace(pairs);
}
else if (auto str_ptr = dynamic_cast<BTString*>(pair.second.get())){
if (std::find(std::begin(key_table), std::end(key_table), pair.first) != std::end(key_table))
for (auto & vp : pairs)
str_ptr->compare_and_replace(vp);
}
}
}
std::string get_name() {
for (auto & pair : data) {
//if store type,will be faster
if (auto dict_ptr = dynamic_cast<BTDict*>(pair.second.get())) {
auto name = dict_ptr->get_name();
if (name.size())
return name;
}
else if (auto str_ptr = dynamic_cast<BTString*>(pair.second.get())) {
if (std::find(std::begin(key_table), std::begin(key_table) + 2, pair.first) != std::begin(key_table) + 2)
return str_ptr->raw_string();
}
}
return std::string();
}
std::string bencoding() const {
std::string str;
str.push_back('d');
for (auto& pair : data){
str +=( BTString(pair.first).bencoding()+pair.second->bencoding());
}
str.push_back('e');
return str;
}
private:
std::map<std::string, std::unique_ptr<BTData>> data;
static std::string const key_table[10];
};
#include <fstream>
#include <functional>
#include <streambuf>
inline std::unique_ptr<BTDict> Parse(const char* bt_filename) {
std::ifstream fin(bt_filename, std::ios::binary);
if(!fin.good())
return nullptr;
/*fin.seekg(0, std::ios::end);
auto len =(std::size_t)fin.tellg();
std::string str (len,char());
fin.seekg(0, std::ios::beg);
fin.read(&str[0], len);*/
std::string str{ std::istreambuf_iterator<char>(fin),std::istreambuf_iterator<char>() };
auto iter = str.begin();
auto get_current_char = [&iter, &str]() {
if (iter == str.end())
throw std::runtime_error("arrive at file end");
return *iter;
};
auto get_current_char_move = [&iter, &str]() {
if (iter == str.end())
throw std::runtime_error("arrive at file end");
return *(iter++);
};
std::function<std::unique_ptr<BTDict>()> parse_dict;
std::function<std::unique_ptr<BTList>()> parse_list;
auto parse_str = [&get_current_char, &get_current_char_move]() {
auto current_char = get_current_char_move();
if (!isdigit(current_char))
throw std::runtime_error("string need a number in the begin");
std::string num_str;
do{
num_str.push_back(current_char);
current_char = get_current_char_move();
} while (isdigit(current_char));
if (current_char != ':')
throw std::runtime_error("string's mid must be a ':' char");
auto len = std::stoi(num_str);
std::string str (len,char());
for (unsigned i = 0; i != len;++i)
str[i] = get_current_char_move();
return str;
};
auto parse_int = [&]() {
if (get_current_char_move() != 'i')
throw std::runtime_error("Integer's begin must be a 'i' char");
std::string num_str;
char current_char ;
while ( (current_char = get_current_char_move()) != 'e') {
num_str.push_back(current_char);
}
return std::stoll(num_str);
};
parse_list = [&]() {
if (get_current_char_move() != 'l')
throw std::runtime_error("list's begin must be a 'l' char");
auto list_ptr = std::make_unique<BTList>();
while (get_current_char() != 'e'){
switch (get_current_char())
{
case 'i':
list_ptr->attach(std::make_unique<BTInt>(parse_int()));
break;
case 'd':
list_ptr->attach(parse_dict());
break;
case 'l':
list_ptr->attach(parse_list());
break;
default:
list_ptr->attach(std::make_unique<BTString>(parse_str()));
break;
}
};
get_current_char_move();
return list_ptr;
};
parse_dict = [&]() {
if (get_current_char_move() != 'd')
throw std::runtime_error("dict's begin must be a 'd' char");
auto dict_ptr = std::make_unique<BTDict>();
while (get_current_char() != 'e') {
std::string key;
try{
key = parse_str();
}
catch (std::exception& e) {
std::printf(e.what());
throw std::runtime_error("dict's value's begin must be string");
}
switch (get_current_char())
{
case 'i':
dict_ptr->attach(std::move(key), std::make_unique<BTInt>(parse_int()));
break;
case 'd':
dict_ptr->attach(std::move(key),parse_dict());
break;
case 'l':
dict_ptr->attach(std::move(key), parse_list());
break;
default:
dict_ptr->attach(std::move(key),std::make_unique<BTString>(parse_str()));
break;
}
} ;
get_current_char_move();
return dict_ptr;
};
return parse_dict();
}