-
Notifications
You must be signed in to change notification settings - Fork 7
/
regex_scanner.cc
81 lines (68 loc) · 1.59 KB
/
regex_scanner.cc
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
/* Copyright (c) 2011 Akamai Technologies, Inc. */
#include "system.hh"
#include "cgen.h"
#include "sensors.h"
#include "log.h"
#include "encode.h"
#include "decode.h"
#include "scanner.h"
#include "regex_scanner.h"
#include "scannable.h"
RegexScanner::RegexScanner()
:
sensors_(NULL),
pat_all_(NULL)
{}
RegexScanner::~RegexScanner() {}
int RegexScanner::Init(Counter* counter,
list<struct sensor>* sensors,
const RE2 * pat_all)
{
CHK(Scanner::Init(counter) != 0,
"unable to init scanner", out_error)
CHK(sensors == NULL, "null sensors", out_error);
CHK(pat_all == NULL, "null pat_all", out_error);
sensors_ = sensors;
pat_all_ = pat_all;
return 0;
out_error:
return 1;
}
int RegexScanner::scan_buf(Scannable* sc, StringPiece buf, void*)
{
bool did_match = false;
for (sensors_iterator sens = sensors_->begin(), sens_end = sensors_->end();
sens != sens_end;
++sens)
{
sens->count = 0;
}
StringPiece capture;
string qpath;
/* For all pat_all matches, reconstruct which sensor fired
* by linear search. When you figure it out, write an F line. */
while (RE2::FindAndConsume(&buf, *pat_all_, &capture))
{
if (did_match == false)
{
CHK(sc->get_quoted_path(&qpath) != 0,
"unable to get current qpath", out_error);
did_match = true;
}
for (sensors_iterator sens = sensors_->begin(), sens_end = sensors_->end();
sens != sens_end;
++sens)
{
if (RE2::PartialMatch(capture, *sens->pat))
{
write_f_line(&cout, qpath, &(*sens), capture);
sens->count++;
}
}
}
if (did_match)
write_p_line(sensors_, &cout, qpath);
return 0;
out_error:
return 1;
}