Add Util::getOptionNameString to reduce some duplicate code

This commit is contained in:
Sude 2015-10-03 19:03:24 +03:00
parent ba27e4fbcc
commit d648d3a073
3 changed files with 15 additions and 19 deletions

View File

@ -47,6 +47,7 @@ namespace Util
std::string getCacheHome(); std::string getCacheHome();
std::vector<std::string> tokenize(const std::string& str, const std::string& separator = ","); std::vector<std::string> tokenize(const std::string& str, const std::string& separator = ",");
unsigned int getOptionValue(const std::string& str, const std::vector<GlobalConstants::optionsStruct>& options); unsigned int getOptionValue(const std::string& str, const std::vector<GlobalConstants::optionsStruct>& options);
std::string getOptionNameString(const unsigned int& value, const std::vector<GlobalConstants::optionsStruct>& options);
} }
#endif // UTIL_H #endif // UTIL_H

View File

@ -409,12 +409,7 @@ void Downloader::listGames()
continue; continue;
} }
std::string languages; std::string languages = Util::getOptionNameString(games[i].installers[j].language, GlobalConstants::LANGUAGES);
for (unsigned int k = 0; k < GlobalConstants::LANGUAGES.size(); k++) // Check which languages the installer supports
{
if (games[i].installers[j].language & GlobalConstants::LANGUAGES[k].id)
languages += (languages.empty() ? "" : ", ")+GlobalConstants::LANGUAGES[k].str;
}
std::cout << "\tid: " << games[i].installers[j].id << std::endl std::cout << "\tid: " << games[i].installers[j].id << std::endl
<< "\tname: " << games[i].installers[j].name << std::endl << "\tname: " << games[i].installers[j].name << std::endl
@ -461,12 +456,7 @@ void Downloader::listGames()
continue; continue;
} }
std::string languages; std::string languages = Util::getOptionNameString(games[i].patches[j].language, GlobalConstants::LANGUAGES);
for (unsigned int k = 0; k < GlobalConstants::LANGUAGES.size(); k++) // Check which languages the patch supports
{
if (games[i].patches[j].language & GlobalConstants::LANGUAGES[k].id)
languages += (languages.empty() ? "" : ", ")+GlobalConstants::LANGUAGES[k].str;
}
std::cout << "\tid: " << games[i].patches[j].id << std::endl std::cout << "\tid: " << games[i].patches[j].id << std::endl
<< "\tname: " << games[i].patches[j].name << std::endl << "\tname: " << games[i].patches[j].name << std::endl
@ -3268,13 +3258,7 @@ void Downloader::showWishlist()
if (config.bPlatformDetection && !(platform & config.iInstallerPlatform)) if (config.bPlatformDetection && !(platform & config.iInstallerPlatform))
continue; continue;
for (unsigned int j = 0; j < GlobalConstants::PLATFORMS.size(); ++j) platforms_text = Util::getOptionNameString(platform, GlobalConstants::PLATFORMS);
{
if (GlobalConstants::PLATFORMS[j].id & platform)
{
platforms_text += (platforms_text.empty() ? "" : ", ")+GlobalConstants::PLATFORMS[j].str;
}
}
} }
std::vector<std::string> tags; std::vector<std::string> tags;

View File

@ -460,3 +460,14 @@ unsigned int Util::getOptionValue(const std::string& str, const std::vector<Glob
} }
return value; return value;
} }
std::string Util::getOptionNameString(const unsigned int& value, const std::vector<GlobalConstants::optionsStruct>& options)
{
std::string str;
for (unsigned int i = 0; i < options.size(); ++i)
{
if (value & options[i].id)
str += (str.empty() ? "" : ", ")+options[i].str;
}
return str;
}