config_lite.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // 配置访问器实现 —— 语义与 Python `portcv.config.Config` 对齐(见 config_lite.hpp)。
  2. #include "portcv/config_lite.hpp"
  3. #include <cstddef>
  4. #include <string>
  5. #include <utility>
  6. #include <vector>
  7. namespace portcv {
  8. namespace {
  9. /// 缺失值单例(`get()` 查不到时返回它,避免返回临时对象造成悬垂引用)。
  10. const json::Value& missing() {
  11. static const json::Value value;
  12. return value;
  13. }
  14. /// 按 `a.b.c` 逐段深入;任一段缺失 / 中间不是对象 → 返回 nullptr。
  15. const json::Value* find(const json::Value& root, const std::string& path) {
  16. const json::Value* node = &root;
  17. std::size_t start = 0;
  18. while (true) {
  19. const std::size_t dot = path.find('.', start);
  20. const std::string part =
  21. dot == std::string::npos ? path.substr(start) : path.substr(start, dot - start);
  22. if (part.empty()) { return nullptr; } // 路径写错("a..b" / 结尾多一个点)
  23. if (!node->is_object() || !node->has(part)) { return nullptr; }
  24. node = &node->member(part);
  25. if (dot == std::string::npos) { return node; }
  26. start = dot + 1;
  27. }
  28. }
  29. } // namespace
  30. // ---------------------------------------------------------------- 构造
  31. bool Config::from_text(const std::string& text, Config* out, std::string* error) {
  32. if (out == nullptr) {
  33. if (error != nullptr) { *error = "Config::from_text: 输出参数为空指针"; }
  34. return false;
  35. }
  36. std::string local_error;
  37. json::Value root = json::parse(text, &local_error);
  38. if (!local_error.empty()) {
  39. if (error != nullptr) { *error = local_error; }
  40. return false;
  41. }
  42. if (!root.is_object()) {
  43. if (error != nullptr) { *error = "配置文件的根必须是 JSON 对象 { ... }"; }
  44. return false;
  45. }
  46. out->root_ = static_cast<json::Value&&>(root);
  47. if (error != nullptr) { error->clear(); }
  48. return true;
  49. }
  50. bool Config::from_file(const std::string& path, Config* out, std::string* error) {
  51. std::string local_error;
  52. std::string text;
  53. if (!json::read_file(path, &text, &local_error)) {
  54. if (error != nullptr) { *error = local_error; }
  55. return false;
  56. }
  57. if (!Config::from_text(text, out, &local_error)) {
  58. if (error != nullptr) { *error = path + ":" + local_error; }
  59. return false;
  60. }
  61. if (error != nullptr) { error->clear(); }
  62. return true;
  63. }
  64. // ---------------------------------------------------------------- 访问
  65. bool Config::has(const std::string& path) const {
  66. const json::Value* value = find(root_, path);
  67. return value != nullptr && !value->is_null();
  68. }
  69. const json::Value& Config::get(const std::string& path) const {
  70. const json::Value* value = find(root_, path);
  71. return value != nullptr ? *value : missing();
  72. }
  73. double Config::number(const std::string& path, double fallback) const {
  74. const json::Value& value = get(path);
  75. return value.is_number() ? value.number : fallback;
  76. }
  77. int Config::integer(const std::string& path, int fallback) const {
  78. const json::Value& value = get(path);
  79. if (!value.is_number()) { return fallback; }
  80. return static_cast<int>(value.number < 0.0 ? value.number - 0.5 : value.number + 0.5);
  81. }
  82. bool Config::boolean(const std::string& path, bool fallback) const {
  83. const json::Value& value = get(path);
  84. if (value.is_bool()) { return value.boolean; }
  85. if (value.is_number()) { return value.number != 0.0; } // 与 Python bool(value) 同口径
  86. return fallback;
  87. }
  88. std::string Config::text(const std::string& path, const std::string& fallback) const {
  89. const json::Value& value = get(path);
  90. return value.is_string() ? value.text : fallback;
  91. }
  92. std::vector<double> Config::numbers(const std::string& path) const {
  93. std::vector<double> out;
  94. const json::Value& value = get(path);
  95. if (!value.is_array()) { return out; }
  96. out.reserve(value.items.size());
  97. for (const json::Value& item : value.items) {
  98. out.push_back(item.is_number() ? item.number : 0.0);
  99. }
  100. return out;
  101. }
  102. std::vector<std::string> Config::strings(const std::string& path) const {
  103. std::vector<std::string> out;
  104. const json::Value& value = get(path);
  105. if (!value.is_array()) { return out; }
  106. out.reserve(value.items.size());
  107. for (const json::Value& item : value.items) {
  108. if (item.is_string()) { out.push_back(item.text); }
  109. }
  110. return out;
  111. }
  112. std::vector<std::pair<double, double>> Config::points(const std::string& path) const {
  113. std::vector<std::pair<double, double>> out;
  114. const json::Value& value = get(path);
  115. if (!value.is_array()) { return out; }
  116. out.reserve(value.items.size());
  117. for (const json::Value& item : value.items) {
  118. if (item.is_array() && item.items.size() >= 2 && item.items[0].is_number() &&
  119. item.items[1].is_number()) {
  120. out.emplace_back(item.items[0].number, item.items[1].number);
  121. }
  122. }
  123. return out;
  124. }
  125. } // namespace portcv