Skip to content

Commit 38aec91

Browse files
committed
csfilter: add custom message filters from JSON files
Closes #11
1 parent b452c92 commit 38aec91

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/csfilter.cc

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
* along with csdiff. If not, see <http://www.gnu.org/licenses/>.
1818
*/
1919

20+
#include "abstract-tree.hh"
2021
#include "csfilter.hh"
2122
#include "regex.hh"
2223

23-
#include <iostream>
24+
#include <boost/property_tree/json_parser.hpp>
2425

2526
// Setup verbosity for debugging string substitions while matching them.
2627
// Verbosity levels are from 0 to 3 (0 is off)
@@ -150,6 +151,56 @@ void MsgFilter::setIgnorePath(bool enable)
150151
d->ignorePath = enable;
151152
}
152153

154+
bool MsgFilter::setFilterFiles(
155+
const TStringList &fileNames,
156+
bool silent)
157+
{
158+
try {
159+
for (const std::string &file : fileNames) {
160+
InStream filter(file, silent);
161+
if (!setJSONFilter(filter))
162+
return false;
163+
}
164+
return true;
165+
}
166+
catch (const InFileException &e) {
167+
std::cerr << e.fileName << ": failed to open filter file\n";
168+
return false;
169+
}
170+
}
171+
172+
bool MsgFilter::setJSONFilter(InStream &input)
173+
{
174+
using namespace boost::property_tree;
175+
176+
try {
177+
// parse JSON
178+
ptree root;
179+
read_json(input.str(), root);
180+
181+
// read filtering rules
182+
for (const auto &filter_rule : root.get_child("msg-filter")) {
183+
const auto &filter = filter_rule.second;
184+
d->addMsgFilter(getStringValue(filter.get_child("checker")),
185+
getStringValue(filter.get_child("regexp")),
186+
valueOf(filter, "replace", std::string{}));
187+
}
188+
return true;
189+
}
190+
catch (boost::regex_error &e) {
191+
input.handleError(e.what());
192+
return false;
193+
}
194+
catch (file_parser_error &e) {
195+
input.handleError(e.message(), e.line());
196+
return false;
197+
}
198+
catch (ptree_error &e) {
199+
input.handleError(e.what());
200+
return false;
201+
}
202+
}
203+
153204
void MsgFilter::setFileNameSubstitution(
154205
const std::string &oldFile,
155206
const std::string &newFile)

src/csfilter.hh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@
2020
#ifndef H_GUARD_CSFILTER_H
2121
#define H_GUARD_CSFILTER_H
2222

23+
#include "instream.hh"
24+
2325
#include <map>
2426
#include <string>
27+
#include <vector>
2528

29+
typedef std::vector<std::string> TStringList;
2630
typedef std::map<std::string, std::string> TSubstMap;
2731

2832
class MsgFilter {
@@ -35,6 +39,8 @@ class MsgFilter {
3539
}
3640

3741
void setIgnorePath(bool);
42+
bool setFilterFiles(const TStringList &fileNames,
43+
bool silent);
3844
void setFileNameSubstitution(
3945
const std::string &oldFile,
4046
const std::string &newFile);
@@ -48,6 +54,8 @@ class MsgFilter {
4854
MsgFilter();
4955
~MsgFilter();
5056

57+
bool setJSONFilter(InStream &filter);
58+
5159
static MsgFilter *self_;
5260
struct Private;
5361
Private *d;

0 commit comments

Comments
 (0)