forked from csutils/csdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabstract-writer.cc
138 lines (110 loc) · 3.56 KB
/
abstract-writer.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
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
/*
* Copyright (C) 2011 Red Hat, Inc.
*
* This file is part of csdiff.
*
* csdiff is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* csdiff is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with csdiff. If not, see <http://www.gnu.org/licenses/>.
*/
#include "abstract-writer.hh"
#include "cswriter.hh"
#include "instream.hh"
#include "json-writer.hh"
#include <boost/regex.hpp>
// /////////////////////////////////////////////////////////////////////////////
// implementation of AbstractWriter
bool AbstractWriter::handleFile(Parser &parser, const std::string &fileName) {
this->notifyFile(fileName);
// detect the input format and create the parser
if (inputFormat_ == FF_INVALID)
inputFormat_ = parser.inputFormat();
// read scan properties if still empty
if (this->getScanProps().empty())
this->setScanProps(parser.getScanProps());
Defect def;
while (parser.getNext(&def))
this->handleDef(def);
return !parser.hasError();
}
bool AbstractWriter::handleFile(
std::istream &str,
const std::string &fileName,
const bool silent)
{
Parser parser(str, fileName, silent);
return this->handleFile(parser, fileName);
}
bool AbstractWriter::handleFile(const std::string &fileName, bool silent) {
try {
InStream str(fileName.c_str());
return this->handleFile(str.str(), fileName, silent);
}
catch (const InFileException &e) {
std::cerr << e.fileName << ": failed to open input file\n";
return false;
}
}
void AbstractWriter::setScanProps(const TScanProps &scanProps) {
if (scanProps.empty())
return;
std::cerr << "warning: scan properties not supported by output format\n";
}
AbstractWriter* createWriter(
const EFileFormat format,
const EColorMode cm,
const TScanProps &scanProps)
{
AbstractWriter *writer = 0;
switch (format) {
case FF_INVALID:
case FF_COVERITY:
writer = new CovWriter(std::cout, cm);
break;
case FF_GCC:
// TODO
case FF_AUTO:
// TODO
case FF_JSON:
writer = new JsonWriter(std::cout);
break;
}
if (!scanProps.empty())
writer->setScanProps(scanProps);
return writer;
}
// /////////////////////////////////////////////////////////////////////////////
// implementation of CtxEventDetector
struct CtxEventDetector::Private {
boost::regex reAnyCtxLine;
boost::regex reKeyCtxLine;
Private():
reAnyCtxLine("^ *[0-9]+\\|(?:->)? .*$"),
reKeyCtxLine("^ *[0-9]+\\|-> .*$")
{
}
};
CtxEventDetector::CtxEventDetector():
d(new Private)
{
}
CtxEventDetector::~CtxEventDetector() {
delete d;
}
bool CtxEventDetector::isAnyCtxLine(const DefEvent &evt) const {
return (evt.event == "#")
&& boost::regex_match(evt.msg, d->reAnyCtxLine);
}
bool CtxEventDetector::isKeyCtxLine(const DefEvent &evt) const {
return (evt.event == "#")
&& boost::regex_match(evt.msg, d->reKeyCtxLine);
}