diff --git a/include/util.h b/include/util.h index 1c45ed5..5ef5b5c 100644 --- a/include/util.h +++ b/include/util.h @@ -101,6 +101,7 @@ namespace Util std::snprintf(buf.get(), sz, format.c_str(), args ...); return std::string(buf.get(), buf.get() + sz - 1); // -1 because we don't want the null terminator } + Json::Value readJsonFile(const std::string& path); } #endif // UTIL_H diff --git a/main.cpp b/main.cpp index a7a7651..6ec5515 100644 --- a/main.cpp +++ b/main.cpp @@ -435,21 +435,7 @@ int main(int argc, char *argv[]) if (boost::filesystem::exists(Globals::globalConfig.sTransformConfigFilePath)) { - std::ifstream ifs(Globals::globalConfig.sTransformConfigFilePath, std::ifstream::binary); - Json::Value json; - try { - ifs >> json; - if (!json.empty()) - { - Globals::globalConfig.transformationsJSON = json; - } - } catch (const Json::Exception& exc) { - std::cerr << "Failed to parse " << Globals::globalConfig.sTransformConfigFilePath<< std::endl; - std::cerr << exc.what() << std::endl; - } - - if (ifs) - ifs.close(); + Globals::globalConfig.transformationsJSON = Util::readJsonFile(Globals::globalConfig.sTransformConfigFilePath); } if (!bUseDLCList) diff --git a/src/downloader.cpp b/src/downloader.cpp index 92a1f82..8696058 100644 --- a/src/downloader.cpp +++ b/src/downloader.cpp @@ -141,25 +141,14 @@ Downloader::Downloader() if (boost::filesystem::exists(Globals::galaxyConf.getFilepath())) { - std::ifstream ifs(Globals::galaxyConf.getFilepath(), std::ifstream::binary); - Json::Value json; - try { - ifs >> json; - if (!json.isMember("expires_at")) - { - std::time_t last_modified = boost::filesystem::last_write_time(Globals::galaxyConf.getFilepath()); - Json::Value::LargestInt expires_in = json["expires_in"].asLargestInt(); - json["expires_at"] = expires_in + last_modified; - } - - Globals::galaxyConf.setJSON(json); - } catch (const Json::Exception& exc) { - std::cerr << "Failed to parse " << Globals::galaxyConf.getFilepath() << std::endl; - std::cerr << exc.what() << std::endl; - } - - if (ifs) - ifs.close(); + Json::Value json = Util::readJsonFile(Globals::galaxyConf.getFilepath()); + if (!json.isMember("expires_at")) + { + std::time_t last_modified = boost::filesystem::last_write_time(Globals::galaxyConf.getFilepath()); + Json::Value::LargestInt expires_in = json["expires_in"].asLargestInt(); + json["expires_at"] = expires_in + last_modified; + } + Globals::galaxyConf.setJSON(json); } gogGalaxy = new galaxyAPI(Globals::globalConfig.curlConf); @@ -2004,13 +1993,9 @@ int Downloader::loadGameDetailsCache() bptime::ptime now = bptime::second_clock::local_time(); bptime::ptime cachedate; - std::ifstream json(cachepath, std::ifstream::binary); - Json::Value root; - try { - json >> root; - } catch (const Json::Exception& exc) { - std::cout << "Failed to parse cache" << std::endl; - std::cout << exc.what() << std::endl; + Json::Value root = Util::readJsonFile(cachepath); + if (root.empty()) + { return 2; } @@ -3829,18 +3814,9 @@ void Downloader::galaxyInstallGameById(const std::string& product_id, int build_ int old_build_index = -1; if (boost::filesystem::exists(info_path)) { - std::ifstream info_file_stream(info_path, std::ifstream::binary); - Json::Value info_json; - try { - info_file_stream >> info_json; + Json::Value info_json = Util::readJsonFile(info_path); + if (!info_json.empty()) old_build_id = info_json["buildId"].asString(); - } - catch (const Json::Exception& exc) - { - std::cout << "Failed to parse " << info_path << std::endl; - std::cout << exc.what() << std::endl; - return; - } if (!old_build_id.empty()) { diff --git a/src/util.cpp b/src/util.cpp index 551402e..80fe4ae 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -937,3 +937,29 @@ std::string Util::makeSizeString(const unsigned long long& iSizeInBytes) } return formattedString("%0.2f %s", iSize, size_unit.c_str()); } + +Json::Value Util::readJsonFile(const std::string& path) +{ + Json::Value json; + std::ifstream ifs(path, std::ifstream::binary); + + if (ifs) + { + try + { + ifs >> json; + } + catch (const Json::Exception& exc) + { + std::cerr << "Failed to parse " << path << std::endl; + std::cerr << exc.what() << std::endl; + } + ifs.close(); + } + else + { + std::cerr << "Failed to open " << path << std::endl; + } + + return json; +}