Simplified file reading, fixed an oversight

This commit is contained in:
MaggotHATE 2023-12-06 18:41:25 +05:00
parent 96b6806e1c
commit 872d004b64
2 changed files with 11 additions and 19 deletions

View file

@ -115,25 +115,17 @@ void process_escapes(std::string& input) {
input.resize(output_idx); input.resize(output_idx);
} }
nlohmann::json get_json(std::string file_name) { nlohmann::json get_json(std::string& file_name) noexcept {
nlohmann::json config; try {
std::fstream jstream(file_name); printf("Opening a json file %s\n", file_name.c_str());
if (jstream.is_open()) { std::ifstream jstream(file_name);
try { return nlohmann::json::parse(jstream);
config = nlohmann::json::parse(jstream); }
jstream.close(); catch (const std::exception& ex) {
printf("Opened a json file %s\n", file_name.c_str()); fprintf(stderr, "%s\n", ex.what());
}
catch (nlohmann::json::parse_error& ex) {
jstream.close();
fprintf(stderr, "%s\n", ex.what());
return config;
}
} else {
printf("%s not found!\n", file_name.c_str());
} }
return config; return {};
} }
bool gpt_params_parse(int argc, char ** argv, gpt_params & params) { bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
@ -145,7 +137,7 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
if (argc > 1) { if (argc > 1) {
// console arguments should override json values, so json processing goes first // console arguments should override json values, so json processing goes first
std::string json_name = argv[1]; std::string json_name = argv[1];
nlohmann::json file_config = get_json(argv[1]); nlohmann::json file_config = get_json(json_name);
pos = 2; // avoid putting file name into arguments pos = 2; // avoid putting file name into arguments
if (!file_config.empty()) { if (!file_config.empty()) {

View file

@ -133,7 +133,7 @@ struct gpt_params {
std::string image = ""; // path to an image file std::string image = ""; // path to an image file
}; };
nlohmann::json get_json(std::string file_name); nlohmann::json get_json(std::string& file_name) noexcept;
bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params); bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params);