#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 &cmd) { if (first_) { buf_.put("[\n\t{\n"); first_ = false; } else { buf_.put(", {\n"); } buf_.put("\t\t\"directory\": \""); buf_.put(escape(dir)); buf_.put("\",\n"); buf_.put("\t\t\"file\": \""); buf_.put(escape(file)); buf_.put("\",\n"); buf_.put("\t\t\"command\": \""); buf_.put(escape(cmd[0])); for (size_t i = 1; i < cmd.size(); ++i) { buf_.put(' '); buf_.put(escape('"' + escape(cmd[i]) + '"')); } buf_.put("\"\n\t}"); } Writer::~Writer() { if (first_) { buf_.put("[]\n"); } else { buf_.put("\n]\n"); } } }