|
17 | 17 | * along with csdiff. If not, see <http://www.gnu.org/licenses/>.
|
18 | 18 | */
|
19 | 19 |
|
| 20 | +#include "abstract-tree.hh" |
20 | 21 | #include "csfilter.hh"
|
21 | 22 | #include "regex.hh"
|
22 | 23 |
|
23 |
| -#include <iostream> |
| 24 | +#include <boost/property_tree/json_parser.hpp> |
24 | 25 |
|
25 | 26 | // Setup verbosity for debugging string substitions while matching them.
|
26 | 27 | // Verbosity levels are from 0 to 3 (0 is off)
|
@@ -150,6 +151,56 @@ void MsgFilter::setIgnorePath(bool enable)
|
150 | 151 | d->ignorePath = enable;
|
151 | 152 | }
|
152 | 153 |
|
| 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 | + |
153 | 204 | void MsgFilter::setFileNameSubstitution(
|
154 | 205 | const std::string &oldFile,
|
155 | 206 | const std::string &newFile)
|
|
0 commit comments