初版。
This commit is contained in:
117
read_config.cpp
Normal file
117
read_config.cpp
Normal file
@@ -0,0 +1,117 @@
|
||||
#include "read_config.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
ReadConfig::ReadConfig()
|
||||
{
|
||||
}
|
||||
|
||||
bool ReadConfig::Read(CmdInfo& info, ActionType type, const std::string& path)
|
||||
{
|
||||
try {
|
||||
// 1. 读取JSON文件
|
||||
std::ifstream file(path);
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "Error: Cannot open config file: " << path << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. 解析JSON
|
||||
json j;
|
||||
file >> j;
|
||||
|
||||
// 3. 检查必须的键
|
||||
if (!j.contains("vsbat") || !j["vsbat"].is_string()) {
|
||||
std::cerr << "Error: Missing or invalid 'vsbat' field" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 定义固定配置键
|
||||
const std::vector<std::string> requiredKeys = {"Debug", "Release"};
|
||||
|
||||
// 检查所有必须的键是否存在
|
||||
for (const auto& key : requiredKeys) {
|
||||
if (!j.contains(key) || !j[key].is_array()) {
|
||||
std::cerr << "Error: Missing or invalid '" << key << "' field" << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 清空之前的数据
|
||||
info.data.clear();
|
||||
|
||||
// 5. 读取vsbat路径
|
||||
info.vsbat = j["vsbat"].get<std::string>();
|
||||
|
||||
// 6. 读取Debug和Release配置
|
||||
for (const auto& key : requiredKeys) {
|
||||
std::vector<CMakeCmd> cmds;
|
||||
|
||||
for (const auto& cmd_item : j[key]) {
|
||||
// 验证命令对象结构
|
||||
if (!cmd_item.is_object() || !cmd_item.contains("type") || !cmd_item["type"].is_string()) {
|
||||
std::cerr << "Error: Invalid command object in '" << key << "'" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string cmd_type_str = cmd_item["type"].get<std::string>();
|
||||
if (cmd_type_str != "config" && cmd_type_str != "build" && cmd_type_str != "clear") {
|
||||
std::cerr << "Error: Invalid command type '" << cmd_type_str << "' in '" << key
|
||||
<< "'. Must be 'config' or 'build'." << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
CMakeCmd cmd;
|
||||
cmd.type = GetType(cmd_type_str);
|
||||
// 修改:从读取单个字符串改为读取字符串数组
|
||||
if (cmd_item.contains("args") && cmd_item["args"].is_array()) {
|
||||
// 新格式:使用 args 数组
|
||||
for (const auto& arg_item : cmd_item["args"]) {
|
||||
if (arg_item.is_string()) {
|
||||
cmd.args.push_back(arg_item.get<std::string>());
|
||||
} else {
|
||||
std::cerr << "Error: Invalid argument type in 'args' array" << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else if (cmd_item.contains("arg") && cmd_item["arg"].is_string()) {
|
||||
// 向后兼容:支持旧的单字符串格式
|
||||
cmd.args.push_back(cmd_item["arg"].get<std::string>());
|
||||
} else {
|
||||
std::cerr << "Error: Missing or invalid 'args' or 'arg' field in command" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (cmd.type == type) {
|
||||
cmds.push_back(cmd);
|
||||
}
|
||||
}
|
||||
|
||||
info.data[key] = cmds;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
} catch (const json::exception& e) {
|
||||
std::cerr << "JSON parsing error: " << e.what() << std::endl;
|
||||
return false;
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error reading config file: " << e.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
ActionType ReadConfig::GetType(const std::string& action)
|
||||
{
|
||||
if (action == "Config" || action == "config") {
|
||||
return ActionType::CT_CONFIG;
|
||||
} else if (action == "Build" || action == "build") {
|
||||
return ActionType::CT_BUILD;
|
||||
} else {
|
||||
return ActionType::CT_CLEAR;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user