Add option --json-output (-j)

If given -j, format the output of -l and of searches as JSON.
This commit is contained in:
Peter
2017-07-07 08:46:26 +02:00
parent 5f0f6e036f
commit 3105823e8b
10 changed files with 162 additions and 62 deletions

View File

@@ -27,6 +27,8 @@
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <sstream>
#include <iomanip>
#include "utils.hpp"
@@ -90,3 +92,27 @@ void for_each_file(const std::list<std::string>& dirs_list, const std::string& s
for (const std::string& item : dirs_list)
__for_each_file(item, suff, order_list, disable_list, f);
}
// based on https://stackoverflow.com/questions/7724448/simple-json-string-escape-for-c/33799784#33799784
std::string json_escape_string(const std::string &s) {
std::ostringstream o;
for (auto c = s.cbegin(); c != s.cend(); c++) {
switch (*c) {
case '"': o << "\\\""; break;
case '\\': o << "\\\\"; break;
case '\b': o << "\\b"; break;
case '\f': o << "\\f"; break;
case '\n': o << "\\n"; break;
case '\r': o << "\\r"; break;
case '\t': o << "\\t"; break;
default:
if ('\x00' <= *c && *c <= '\x1f') {
o << "\\u"
<< std::hex << std::setw(4) << std::setfill('0') << (int)*c;
} else {
o << *c;
}
}
}
return o.str();
}