Skip to content

Commit 98fc448

Browse files
committed
csv-parser: new class AbstractCsvParser
... that contains reusable code for CVS parsing taken from CweMapper No changes in behavior intended by this commit.
1 parent 005afb0 commit 98fc448

File tree

6 files changed

+146
-59
lines changed

6 files changed

+146
-59
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ add_library(cs STATIC
2626
csdiff-core.cc
2727
csfilter.cc
2828
csparser.cc
29+
csv-parser.cc
2930
cswriter.cc
3031
cwe-mapper.cc
3132
deflookup.cc

src/cslinker.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ int main(int argc, char *argv[])
234234
try {
235235
// load CWE mapping from the given file
236236
InStream strCwe(fnCwe);
237-
if (!cweDec->cweMap().loadCweMap(strCwe.str(), fnCwe))
237+
if (!cweDec->cweMap().parse(strCwe))
238238
hasError = true;
239239
}
240240
catch (const InFileException &e) {

src/csv-parser.cc

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (C) 2020 Red Hat, Inc.
3+
*
4+
* This file is part of csdiff.
5+
*
6+
* csdiff is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* any later version.
10+
*
11+
* csdiff is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with csdiff. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#include "csv-parser.hh"
21+
22+
#include <boost/tokenizer.hpp>
23+
24+
#include <cassert>
25+
26+
struct AbstractCsvParser::Private {
27+
const std::string *pFileName = nullptr;
28+
int lineno = 0;
29+
bool silent = false;
30+
bool hasError = false;
31+
};
32+
33+
AbstractCsvParser::AbstractCsvParser():
34+
d(new Private)
35+
{
36+
}
37+
38+
AbstractCsvParser::~AbstractCsvParser()
39+
{
40+
delete d;
41+
}
42+
43+
bool AbstractCsvParser::parse(InStream &ins)
44+
{
45+
assert(!d->pFileName);
46+
d->pFileName = &ins.fileName();
47+
d->hasError = false;
48+
49+
std::istream &str = ins.str();
50+
std::string line;
51+
for (d->lineno = 0; std::getline(str, line); d->lineno++) {
52+
// initialize tokenizer
53+
typedef boost::escaped_list_separator<char> TSeparator;
54+
typedef boost::tokenizer<TSeparator> TTokenizer;
55+
TTokenizer tok(line);
56+
57+
// break the current line into fields
58+
const TStringList fields(tok.begin(), tok.end());
59+
60+
// call the template method
61+
if (!/* continue */this->handleLine(fields))
62+
break;
63+
}
64+
65+
d->pFileName = nullptr;
66+
return !d->hasError;
67+
}
68+
69+
void AbstractCsvParser::parseError(const std::string &msg)
70+
{
71+
assert(d->pFileName);
72+
d->hasError = true;
73+
if (d->silent)
74+
return;
75+
76+
std::cerr
77+
<< *(d->pFileName) << ":"
78+
<< d->lineno << ": error: "
79+
<< msg << "\n";
80+
}

src/csv-parser.hh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (C) 2020 Red Hat, Inc.
3+
*
4+
* This file is part of csdiff.
5+
*
6+
* csdiff is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* any later version.
10+
*
11+
* csdiff is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with csdiff. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef H_GUARD_CSV_PARSER_H
21+
#define H_GUARD_CSV_PARSER_H
22+
23+
#include "instream.hh"
24+
25+
#include <vector>
26+
#include <string>
27+
28+
class AbstractCsvParser {
29+
public:
30+
AbstractCsvParser();
31+
virtual ~AbstractCsvParser();
32+
bool parse(InStream &str);
33+
34+
void setSilent(const bool silent) {
35+
this->silent = silent;
36+
}
37+
38+
protected:
39+
typedef std::vector<std::string> TStringList;
40+
41+
virtual bool /* continue */ handleLine(const TStringList &) = 0;
42+
void parseError(const std::string &msg);
43+
44+
bool silent;
45+
46+
private:
47+
struct Private;
48+
Private *d;
49+
};
50+
51+
#endif /* H_GUARD_CSV_PARSER_H */

src/cwe-mapper.cc

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,18 @@
2121

2222
#include <cstdio>
2323

24-
#include <boost/tokenizer.hpp>
25-
2624
// /////////////////////////////////////////////////////////////////////////////
2725
// implementation of CweMap
2826
struct CweMap::Private {
2927
typedef std::map<std::string, int> TNumByEvent;
3028
typedef std::map<std::string, TNumByEvent> TMapByChk;
3129

32-
std::string fileName;
33-
std::string line;
34-
int lineno;
35-
bool silent;
36-
bool hasError;
3730
TMapByChk mapByChk;
38-
39-
void parseError(const std::string &msg);
40-
void parseLine();
4131
};
4232

4333
CweMap::CweMap():
4434
d(new Private)
4535
{
46-
d->silent = false;
47-
d->hasError = false;
4836
}
4937

5038
CweMap::~CweMap()
@@ -57,35 +45,11 @@ bool CweMap::empty() const
5745
return d->mapByChk.empty();
5846
}
5947

60-
void CweMap::setSilent(bool silent)
61-
{
62-
d->silent = silent;
63-
}
64-
65-
void CweMap::Private::parseError(const std::string &msg)
66-
{
67-
this->hasError = true;
68-
if (this->silent)
69-
return;
70-
std::cerr
71-
<< this->fileName << ":"
72-
<< this->lineno << ": error: "
73-
<< msg << "\n";
74-
}
75-
76-
void CweMap::Private::parseLine()
48+
bool CweMap::handleLine(const TStringList &fields)
7749
{
78-
// initialize tokenizer
79-
typedef boost::escaped_list_separator<char> TSeparator;
80-
typedef boost::tokenizer<TSeparator> TTokenizer;
81-
TTokenizer tok(this->line);
82-
83-
// break the current line into fields
84-
typedef std::vector<std::string> TStringList;
85-
TStringList fields(tok.begin(), tok.end());
8650
if (3U != fields.size()) {
8751
this->parseError("invalid count of fields");
88-
return;
52+
return /* continue */ true;
8953
}
9054

9155
// parse CWE number
@@ -94,12 +58,12 @@ void CweMap::Private::parseLine()
9458
if (1 != sscanf(fields[/* CWE */ 2].c_str(), "CWE-%u%c", &cwe, &c) || !cwe)
9559
{
9660
this->parseError("invalid CWE ID");
97-
return;
61+
return /* continue */ true;
9862
}
9963

10064
// lookup by checker
10165
const std::string &chk = fields[/* chk */ 0];
102-
TNumByEvent &row = this->mapByChk[chk];
66+
Private::TNumByEvent &row = d->mapByChk[chk];
10367

10468
// lookup by event
10569
const std::string &evt = fields[/* evt */ 1];
@@ -108,19 +72,8 @@ void CweMap::Private::parseLine()
10872

10973
// store the mapping
11074
row[evt] = cwe;
111-
}
112-
113-
bool CweMap::loadCweMap(std::istream &str, const std::string &fileName)
114-
{
115-
d->fileName = fileName;
116-
d->lineno = 0;
117-
118-
while (std::getline(str, d->line)) {
119-
d->lineno++;
120-
d->parseLine();
121-
}
12275

123-
return !d->hasError;
76+
return /* continue */ true;
12477
}
12578

12679
bool CweMap::assignCwe(Defect &def) const
@@ -134,7 +87,7 @@ bool CweMap::assignCwe(Defect &def) const
13487
// CWE already assigned, stay silent
13588
return true;
13689

137-
if (!d->silent)
90+
if (!this->silent)
13891
std::cerr << "warning: CWE not found: checker = "
13992
<< def.checker <<"\n";
14093
return false;
@@ -149,7 +102,7 @@ bool CweMap::assignCwe(Defect &def) const
149102
// CWE already assigned, stay silent
150103
return true;
151104

152-
if (!d->silent)
105+
if (!this->silent)
153106
std::cerr << "warning: CWE not found: checker = " << def.checker
154107
<< ", event = " << evt.event << "\n";
155108

@@ -167,7 +120,7 @@ bool CweMap::assignCwe(Defect &def) const
167120
// already assigned to the requested value
168121
return true;
169122

170-
if (cweDst && !d->silent)
123+
if (cweDst && !this->silent)
171124
// we are rewriting the CWE
172125
std::cerr << "warning: CWE overriden: "
173126
<< cweSrc << " -> "

src/cwe-mapper.hh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@
2121
#define H_GUARD_CWE_MAPPER_H
2222

2323
#include "abstract-filter.hh"
24+
#include "csv-parser.hh"
2425

2526
#include <iostream>
2627

27-
class CweMap {
28+
class CweMap: public AbstractCsvParser {
2829
public:
2930
CweMap();
3031
~CweMap();
3132

32-
bool loadCweMap(std::istream &, const std::string &fileName);
3333
bool assignCwe(Defect &def) const;
3434
bool empty() const;
35-
void setSilent(bool);
35+
36+
protected:
37+
virtual bool /* continue */ handleLine(const TStringList &);
3638

3739
private:
3840
struct Private;

0 commit comments

Comments
 (0)