|
| 1 | +#include "config.h" |
| 2 | +#include "picojson.h" |
| 3 | + |
| 4 | +#include <fstream> |
| 5 | +#include <vector> |
| 6 | +#include <cstring> |
| 7 | +#include <cerrno> |
| 8 | + |
| 9 | +std::string Config::load(const std::filesystem::path &path) |
| 10 | +{ |
| 11 | + // Read config file |
| 12 | + std::ifstream ifs(path); |
| 13 | + if (ifs.fail()) |
| 14 | + return std::strerror(errno); |
| 15 | + |
| 16 | + std::streampos length; |
| 17 | + ifs.seekg(0, std::ios::end); |
| 18 | + length = ifs.tellg(); |
| 19 | + ifs.seekg(0, std::ios::beg); |
| 20 | + |
| 21 | + std::vector<char> buffer(length); |
| 22 | + ifs.read(&buffer[0], length); |
| 23 | + |
| 24 | + if (ifs.fail()) |
| 25 | + return std::strerror(errno); |
| 26 | + |
| 27 | + std::string text = buffer.data(); |
| 28 | + |
| 29 | + // Parse JSON |
| 30 | + picojson::value data; |
| 31 | + std::string err = picojson::parse(data, text); |
| 32 | + if (!err.empty()) { |
| 33 | + return err; |
| 34 | + } |
| 35 | + |
| 36 | + if (!data.is<picojson::object>()) |
| 37 | + return "Invalid config format"; |
| 38 | + |
| 39 | + const picojson::object &obj = data.get<picojson::object>(); |
| 40 | + |
| 41 | + // Read settings |
| 42 | + for (auto [key, value] : obj) { |
| 43 | + |
| 44 | + if (key == "project_file") { |
| 45 | + if (!value.is<std::string>()) { |
| 46 | + return "Invalid value type for '" + key + "'"; |
| 47 | + } |
| 48 | + m_projectFilePath = value.get<std::string>(); |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + if (key == "cppcheck") { |
| 53 | + if (!value.is<std::string>()) { |
| 54 | + return "Invalid value type for '" + key + "'"; |
| 55 | + } |
| 56 | + m_cppcheck = value.get<std::string>(); |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + if (key == "args") { |
| 61 | + if (!value.is<picojson::array>()) |
| 62 | + return "Invalid value type for '" + key + "'"; |
| 63 | + |
| 64 | + for (auto arg : value.get<picojson::array>()) { |
| 65 | + if (!arg.is<std::string>()) |
| 66 | + return "Invalid value type for array element in '" + key + "'"; |
| 67 | + m_args.push_back(arg.get<std::string>()); |
| 68 | + } |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + return "Invalid config key '" + key + "'"; |
| 73 | + } |
| 74 | + |
| 75 | + return ""; |
| 76 | +} |
| 77 | + |
| 78 | +std::string Config::command() const |
| 79 | +{ |
| 80 | + std::string cmd; |
| 81 | + |
| 82 | + cmd += m_cppcheck; |
| 83 | + |
| 84 | + for (auto arg : m_args) |
| 85 | + cmd += " " + arg; |
| 86 | + |
| 87 | + if (!m_projectFilePath.empty()) { |
| 88 | + |
| 89 | + std::string filter = m_filename; |
| 90 | + if (std::strchr(filter.c_str(), ' ')) |
| 91 | + filter = "\"" + filter + "\""; |
| 92 | + |
| 93 | + cmd += " --project=" + m_projectFilePath.string() + " --file-filter=" + filter; |
| 94 | + |
| 95 | + } else { |
| 96 | + cmd += " " + m_filename; |
| 97 | + } |
| 98 | + |
| 99 | + cmd += " 2>&1"; |
| 100 | + |
| 101 | + return cmd; |
| 102 | +} |
| 103 | + |
| 104 | +static const char *startsWith(const char *arg, const char *start) { |
| 105 | + if (std::strncmp(arg, start, std::strlen(start)) != 0) |
| 106 | + return NULL; |
| 107 | + return arg + std::strlen(start); |
| 108 | +} |
| 109 | + |
| 110 | +std::string Config::parseArgs(int argc, char **argv) |
| 111 | +{ |
| 112 | + (void) argc; |
| 113 | + |
| 114 | + ++argv; |
| 115 | + |
| 116 | + for (; *argv; ++argv) { |
| 117 | + const char *arg = *argv; |
| 118 | + const char *value; |
| 119 | + |
| 120 | + if ((value = startsWith(arg, "--config="))) { |
| 121 | + std::string err = load(value); |
| 122 | + if (!err.empty()) |
| 123 | + return "Failed to load config file '" + std::string(value) + "': " + err; |
| 124 | + continue; |
| 125 | + } |
| 126 | + |
| 127 | + if (arg[0] == '-') |
| 128 | + return "Invalid option '" + std::string(arg) + "'"; |
| 129 | + |
| 130 | + if (!m_filename.empty()) |
| 131 | + return "Multiple filenames provided"; |
| 132 | + |
| 133 | + m_filename = arg; |
| 134 | + } |
| 135 | + |
| 136 | + if (m_filename.empty()) |
| 137 | + return "Missing filename"; |
| 138 | + |
| 139 | + return ""; |
| 140 | +} |
0 commit comments