From 309b07d35309cdfc2efadf34dace3f46f1b47d7b Mon Sep 17 00:00:00 2001 From: zvon Date: Thu, 17 Jan 2019 01:24:04 +0100 Subject: [PATCH] Removed global variable --- tv_rename.cpp | 66 +++++++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/tv_rename.cpp b/tv_rename.cpp index a53aaf7..d93b1b4 100644 --- a/tv_rename.cpp +++ b/tv_rename.cpp @@ -10,26 +10,22 @@ #include #include "filesystem.hpp" -void singleSeason( const std::string &path, const std::string &show, int season, std::string url ); -void multipleSeasons( const std::string &path, const std::string &show, const std::set seasons ); -void allSeasons( const std::string &path, const std::string &show ); +void singleSeason( const std::string &path, const std::string &show, int season, std::string url, const std::string &language, const bool &linux, const bool &trust, Curl &c); +void multipleSeasons( const std::string &path, const std::string &show, const std::set seasons, const std::string &language, const bool &linux, const bool &trust, Curl &c); +void allSeasons( const std::string &path, const std::string &show, const std::string &language, const bool &linux, const bool &trust, Curl &c); +bool findLanguage( const char *language ); -std::string language{"en"}; -bool trust{false}; -bool linux{false}; -Curl c; // global so the connection doesn't close at every function - -std::map languages{ - {"en", "English"}, {"sv", "Svenska"}, {"no", "Norsk"}, {"da", "Dansk"}, {"fi", "Suomeksi"}, - {"nl", "Nederlands"}, {"de", "Deutsch"}, {"it", "Italiano"}, {"es", "Español"}, {"fr", "Français"}, - {"pl", "Polski"}, {"hu", "Magyar"}, {"el", "Greek"}, {"tr", "Turkish"}, {"ru", "Russian"}, - {"he", "Hebrew"}, {"ja", "Japanese"}, {"pt", "Portuguese"}, {"zh", "Chinese"}, {"cs", "Czech"}, - {"sl", "Slovenian"}, {"hr", "Croatian"}, {"ko","Korea"} +constexpr std::array languages{ + "en", "English", "sv", "Svenska", "no", "Norsk", "da", "Dansk", "fi", "Suomeksi", + "nl", "Nederlands", "de", "Deutsch", "it", "Italiano", "es", "Español", "fr", "Français", + "pl", "Polski", "hu", "Magyar", "el", "Greek", "tr", "Turkish", "ru", "Russian", + "he", "Hebrew", "ja", "Japanese", "pt", "Portuguese", "zh", "Chinese", "cs", "Czech", + "sl", "Slovenian", "hr", "Croatian", "ko","Korea" }; void printLangs() { - for( const auto &x : languages ) { - std::cout << x.first << " - " << x.second << std::endl; + for( size_t i = 0; i < languages.size(); i += 2 ) { + std::cout << languages[i] << " - " << languages[i+1] << std::endl; } } @@ -51,7 +47,7 @@ void parseSeasonNumbers(std::set &seasons_num, const char *argument) { } } -int parseCommandLine(std::string &show, std::set &seasons_num, std::string &path, bool &change_dir, int argc, char **argv) { +int parseCommandLine(std::string &show, std::set &seasons_num, std::string &path, bool &change_dir, std::string &language, bool &linux, bool &trust, int argc, char **argv) { static struct option long_options[] = { {"show", required_argument, 0, 's' }, {"season", required_argument, 0, 'n' }, @@ -91,7 +87,7 @@ int parseCommandLine(std::string &show, std::set &seasons_num, std::string linux = true; break; case 'l': - if( languages.find(optarg) != languages.end() ) { + if( findLanguage(optarg) ) { language = optarg; } else { std::cerr << "Invalid language choice" << std::endl; @@ -118,9 +114,13 @@ int main(int argc, char** argv) { std::set seasons_num; std::string path{"."}; bool change_dir{true}; + std::string language{"en"}; + bool linux{false}; + bool trust{false}; + Curl c; { - auto tmp = parseCommandLine(show, seasons_num, path, change_dir, argc, argv); + auto tmp = parseCommandLine(show, seasons_num, path, change_dir, language, linux, trust, argc, argv); if( tmp == -1 ) return 1; else if ( tmp == 1 ) @@ -165,15 +165,15 @@ int main(int argc, char** argv) { } if( seasons_num.size() == 1 ) { - singleSeason(path, show, *seasons_num.begin(), ""); + singleSeason(path, show, *seasons_num.begin(), "", language, linux, trust, c); } else if ( seasons_num.size() != 0 ) { - multipleSeasons(path, show, seasons_num); + multipleSeasons(path, show, seasons_num, language, linux, trust, c); } else { - allSeasons(path, show); + allSeasons(path, show, language, linux, trust, c); } } -void parseEpisodeNames( const std::string &season_code, std::vector &episodes) { +void parseEpisodeNames( const std::string &season_code, std::vector &episodes, const std::string &language) { size_t pos = 0; while( true ) { pos = season_code.find("ge=\"" + language + "\" ", pos); @@ -193,7 +193,7 @@ void parseEpisodeNames( const std::string &season_code, std::vector } } -void singleSeason( const std::string &path, const std::string &show, int season, std::string url) { +void singleSeason( const std::string &path, const std::string &show, int season, std::string url, const std::string &language, const bool &linux, const bool &trust, Curl &c) { if( url.empty() ) url = getDefUrl(show, language, c); url += "/seasons/" + std::to_string(season); @@ -218,7 +218,7 @@ void singleSeason( const std::string &path, const std::string &show, int season, std::vector episodes; //get episode names in all languages - parseEpisodeNames(season_code, episodes); + parseEpisodeNames(season_code, episodes, language); if( episodes.empty() ) return; @@ -300,17 +300,25 @@ void singleSeason( const std::string &path, const std::string &show, int season, } } -void multipleSeasons( const std::string &path, const std::string &show, const std::set seasons) { +void multipleSeasons( const std::string &path, const std::string &show, const std::set seasons, const std::string &language, const bool &linux, const bool &trust, Curl &c) { auto url = getDefUrl(show, language, c); for( const auto &x : seasons ) { - singleSeason( path, show, x, url); + singleSeason( path, show, x, url, language, linux, trust, c); } } -void allSeasons( const std::string &path, const std::string &show) { +void allSeasons( const std::string &path, const std::string &show, const std::string &language, const bool &linux, const bool &trust, Curl &c) { std::set seasons; //get all season number from this directory and subdirectories iterateFS(seasons, path); - multipleSeasons( path, show, seasons); + multipleSeasons( path, show, seasons, language, linux, trust, c); +} + +bool findLanguage( const char *language ) { + for( size_t i = 0; i < languages.size(); i += 2 ) { + if( !strcmp(language, languages[i]) ) + return true; + } + return false; }