Add helper function Util::readJsonFile

This commit is contained in:
Sude 2023-04-26 12:36:43 +03:00
parent a1a7fb4a28
commit b4fdc2929c
4 changed files with 41 additions and 52 deletions

View File

@ -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

View File

@ -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)

View File

@ -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())
{

View File

@ -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;
}