Build tool
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

compdb.cc 997B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "compdb.h"
  2. namespace compdb {
  3. std::string escape(const std::string &str) {
  4. std::string out;
  5. // TODO: Handle unicode according to JSON spec
  6. for (char ch: str) {
  7. if (ch == '\\' || ch == '\"') {
  8. out += '\\';
  9. out += ch;
  10. } else if (ch == '\r') {
  11. out += '\\';
  12. out += 'r';
  13. } else if (ch == '\n') {
  14. out += '\\';
  15. out += 'n';
  16. } else {
  17. out += ch;
  18. }
  19. }
  20. return out;
  21. }
  22. void Writer::write(
  23. const std::string &dir, const std::string &file,
  24. const std::vector<std::string> &cmd) {
  25. if (first_) {
  26. stream_ << "[\n\t{\n";
  27. first_ = false;
  28. } else {
  29. stream_ << ", {\n";
  30. }
  31. stream_
  32. << "\t\t\"directory\": \"" << escape(dir) << "\",\n"
  33. << "\t\t\"file\": \"" << escape(file) << "\",\n"
  34. << "\t\t\"command\": \"" << escape(cmd[0]);
  35. for (size_t i = 1; i < cmd.size(); ++i) {
  36. stream_ << ' ' << escape('"' + escape(cmd[i]) + '"');
  37. }
  38. stream_ << "\"\n\t}";
  39. }
  40. Writer::~Writer() {
  41. if (first_) {
  42. stream_ << "[]\n";
  43. } else {
  44. stream_ << "\n]\n";
  45. }
  46. }
  47. }