12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- #include "compdb.h"
-
- namespace compdb {
-
- std::string escape(const std::string &str) {
- std::string out;
- out.reserve(str.size() * 1.5);
-
- // TODO: Handle unicode according to JSON spec
- for (char ch: str) {
- if (ch == '\\' || ch == '\"') {
- out += '\\';
- out += ch;
- } else if (ch == '\r') {
- out += '\\';
- out += 'r';
- } else if (ch == '\n') {
- out += '\\';
- out += 'n';
- } else {
- out += ch;
- }
- }
-
- return out;
- }
-
- void Writer::write(
- const std::string &dir, const std::string &file,
- const std::vector<std::string> &cmd) {
- if (first_) {
- stream_ << "[\n\t{\n";
- first_ = false;
- } else {
- stream_ << ", {\n";
- }
-
- stream_
- << "\t\t\"directory\": \"" << escape(dir) << "\",\n"
- << "\t\t\"file\": \"" << escape(file) << "\",\n"
- << "\t\t\"command\": \"" << escape(cmd[0]);
-
- for (size_t i = 1; i < cmd.size(); ++i) {
- stream_ << ' ' << escape('"' + escape(cmd[i]) + '"');
- }
-
- stream_ << "\"\n\t}";
- }
-
- Writer::~Writer() {
- if (first_) {
- stream_ << "[]\n";
- } else {
- stream_ << "\n]\n";
- }
- }
-
- }
|