-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeviceData.h
75 lines (57 loc) · 1.62 KB
/
DeviceData.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
#ifndef DEVICE_DATA_H
#define DEVICE_DATA_H
#include "Trie.h"
#include <boost/spirit/include/classic.hpp>
#include <boost/algorithm/string.hpp>
struct Device
{
Device() : modelId(0), brandId(0), mobileDevice(false) {/*_*/}
int modelId;
int brandId;
bool mobileDevice;
};
typedef Trie<Device> DeviceTrie;
struct CreateDevice
{
CreateDevice(DeviceTrie& trie) : trie_(trie) {/*_*/};
void operator()(char const* first, char const* last) const
{
std::string row(first, last);
std::vector<std::string> strVector;
boost::split(strVector, row, boost::is_any_of("\t"));
assert(strVector.size() == 4);
Device device;
device.modelId = atoi(strVector[0].c_str());
device.brandId = atoi(strVector[1].c_str());
device.mobileDevice = atoi(strVector[2].c_str());
trie_.insert(strVector[3], device);
}
DeviceTrie& trie_;
};
struct DeviceDataParser
: public boost::spirit::classic::grammar<DeviceDataParser>
{
DeviceDataParser(DeviceTrie& trie) : trie_(trie) {}
template <typename ScannerT> struct definition
{
typedef boost::spirit::classic::rule<ScannerT> RuleType;
RuleType dataString, model, brand, mobileDevice, prefix;
RuleType const& start() const { return dataString; }
definition(DeviceDataParser const& self)
{
using namespace boost::spirit::classic;
model = int_p;
brand = int_p;
mobileDevice = int_p;
prefix = *(~ch_p('\n'));
dataString = (
model >> ch_p('\t') >>
brand >> ch_p('\t') >>
mobileDevice >> ch_p('\t') >>
prefix
)[CreateDevice(self.trie_)];
}
};
DeviceTrie& trie_;
};
#endif