-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzebra.cpp
More file actions
executable file
·76 lines (64 loc) · 2.38 KB
/
Copy pathzebra.cpp
File metadata and controls
executable file
·76 lines (64 loc) · 2.38 KB
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
/**
* @brief
* @author Yuki Koizumi
*/
#include "zebra.hpp"
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/lexical_cast.hpp>
#include <cstdint>
#include <string>
#include <vector>
#include <iterator>
#include <fstream>
#include <algorithm>
#include <iostream>
std::ostream& operator<<(std::ostream &os, const entry_type &v)
{
os << v.prefix
<< " (0x" << std::hex << v.prefix << std::dec << "), "
<< v.length << ", "
<< v.number;
return os;
}
void read_zebra_data(std::vector<entry_type> &container, const char *filename)
{
std::ifstream infile(filename);
if(infile.fail()) {
std::cerr << "cannot open file: " << filename << std::endl;
exit(1);
}
std::string line;
try {
while(!infile.eof()) {
std::getline(infile, line);
boost::algorithm::trim(line);
if(line.empty()) continue;
std::vector<std::string> str_element;
boost::algorithm::split(str_element, line, boost::algorithm::is_space(), boost::algorithm::token_compress_on);
std::vector<std::string> prefix_element;
boost::algorithm::split(prefix_element, str_element.at(0), boost::is_any_of("/"), boost::algorithm::token_compress_on);
std::vector<std::string> ip_element;
boost::algorithm::split(ip_element, prefix_element.at(0), boost::is_any_of("."), boost::algorithm::token_compress_on);
std::vector<uint32_t> ip_val_list;
std::transform(ip_element.begin(), ip_element.end(),
std::back_inserter(ip_val_list),
[](const std::string &x) { return boost::lexical_cast<uint32_t>(x); });
uint32_t number = boost::lexical_cast<uint32_t>(str_element.at(1));
uint32_t length = boost::lexical_cast<uint32_t>(prefix_element.at(1));
uint32_t prefix = (ip_val_list.at(0) << 24) | (ip_val_list.at(1) << 16) | (ip_val_list.at(2) << 8) | ip_val_list.at(3);
container.push_back(entry_type(prefix, length, number));
}
}
catch(boost::bad_lexical_cast &cast_error) {
infile.close();
std::cerr << cast_error.what() << std::endl;
exit(1);
}
catch(std::exception &e) {
infile.close();
std::cerr << e.what() << std::endl;
exit(1);
}
return;
}