Files
baidu_fanyi/bf.config.cpp
2026-03-10 14:32:11 +08:00

68 lines
1.9 KiB
C++

#include "bf.config.h"
#include <fstream>
#include <nlohmann/json.hpp>
#include "bf.util.h"
bool BF_Config::parseConfig(const std::string& file)
{
try {
std::ifstream ifs(file);
if (!ifs.is_open()) {
gLogger->error("Failed to open config file: {}", file);
return false;
}
nlohmann::json configJson;
ifs >> configJson;
if (!configJson.contains("baseUrl")) {
gLogger->error("Missing 'baseUrl' field in config file");
return false;
}
baseUrl = configJson["baseUrl"];
if (!configJson.contains("appId")) {
gLogger->error("Missing 'appId' field in config file");
return false;
}
appId = configJson["appId"];
if (!configJson.contains("appKey")) {
gLogger->error("Missing 'appKey' field in config file");
return false;
}
appKey = configJson["appKey"];
if (configJson.contains("interval")) {
interval = configJson["interval"];
} else {
gLogger->info("Using default interval: {}", interval);
}
if (!configJson.contains("appSecret")) {
gLogger->error("Missing 'appSecret' field in config file");
return false;
}
appSecret = configJson["appSecret"];
if (configJson.contains("type")) {
int typeValue = configJson["type"];
if (typeValue == BFT_AI) {
type = BFT_AI;
} else {
type = BFT_COMMON;
}
}
gLogger->info("Config loaded successfully from: {}", file);
return true;
} catch (const nlohmann::json::exception& e) {
gLogger->error("JSON parsing error: {}", e.what());
return false;
} catch (const std::exception& e) {
gLogger->error("Failed to parse config: {}", e.what());
return false;
}
}