From bcd79546f540df7e6ca4c623920b1c303dad3614 Mon Sep 17 00:00:00 2001 From: zvon Date: Tue, 29 Jan 2019 17:50:19 +0100 Subject: [PATCH] Refactor code, only allocate new std::set in singleSeason when needed --- mainwindow.cpp | 2 +- tv_rename.cpp | 69 ++++++++++++++++++++------------------------------ tv_rename.hpp | 4 +-- 3 files changed, 31 insertions(+), 44 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index 3601faf..6453f55 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -185,7 +185,7 @@ void MainWindow::finishedSelection() { for( auto &x : selected ) { // get renamed files for given season - auto renamed_files = singleSeason( static_cast((*iter)[m_columns_url.m_col_show]), x, + auto renamed_files = getRenamedFiles( static_cast((*iter)[m_columns_url.m_col_show]), x, "https://www.thetvdb.com" + url, language_code, (input_pattern.empty() ? default_pattern : input_pattern), diff --git a/tv_rename.cpp b/tv_rename.cpp index 99941b2..e035ad5 100644 --- a/tv_rename.cpp +++ b/tv_rename.cpp @@ -36,27 +36,7 @@ std::vector parseEpisodeNames( const std::string &season_code, cons return episodes; } -#ifdef GUI -std::vector>> defaultSingleSeasonReturn() { - return {}; -} -#else -void defaultSingleSeasonReturn() { - return; -} -#endif - -#ifdef GUI -std::vector>> singleSeason( const std::string &show, int season, std::string url, const std::string &language, const std::string &pattern, const bool &linux, Curl &c, const std::set &files) { -#else -void singleSeason( const std::string &path, std::string &show, int season, std::string url, const std::string &language, const std::string &pattern, const bool &linux, const bool &trust, Curl &c, std::set const *files_ptr) { -#endif - -#ifndef GUI - if( url.empty() ) - url = getDefUrl(show, language, c); -#endif - +std::vector>> getRenamedFiles( const std::string &show, int season, std::string url, const std::string &language, const std::string &pattern, const bool &linux, Curl &c, const std::set &files ) { url += "/seasons/" + std::to_string(season); //get source code of season's page auto season_code = c.execute(url); @@ -72,31 +52,20 @@ void singleSeason( const std::string &path, std::string &show, int season, std:: if( pos != std::string::npos ) season_code = season_code.substr(0,pos); else - return defaultSingleSeasonReturn(); + return {}; } else { - return defaultSingleSeasonReturn(); + return {}; } auto episodes = parseEpisodeNames(season_code, language); if( episodes.empty() ) - return defaultSingleSeasonReturn(); + return {}; std::vector>> renamed_files; -#ifndef GUI - std::set found_files; - - if( files_ptr == nullptr ) { - findSeason(found_files, season, path); - files_ptr = &found_files; - } - - const std::set &files = *files_ptr; -#endif - if( files.empty() ) - return defaultSingleSeasonReturn(); + return {}; for( const auto &x : files ) { auto last = x.find_last_of("/"); @@ -144,10 +113,25 @@ void singleSeason( const std::string &path, std::string &show, int season, std:: renamed_files.emplace_back(dir, std::pair(og_name, name)); } } - -#ifdef GUI return renamed_files; -#else +} + +#ifndef GUI + +void singleSeason( const std::string &path, std::string &show, int season, std::string url, const std::string &language, const std::string &pattern, const bool &linux, const bool &trust, Curl &c, std::set const *files_ptr) { + if( url.empty() ) + url = getDefUrl(show, language, c); + + std::set *found_files = nullptr; + + if( files_ptr == nullptr ) { + found_files = new std::set; + findSeason(*found_files, season, path); + files_ptr = found_files; + } + + auto renamed_files = getRenamedFiles(show, season, std::move(url), language, pattern, linux, c, *files_ptr); + for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) { std::cout << renamed->second.first << " --> " << renamed->second.second << std::endl; } @@ -165,10 +149,12 @@ void singleSeason( const std::string &path, std::string &show, int season, std:: for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) { FSLib::rename(renamed->first + "/" + renamed->second.first, renamed->first + "/" + renamed->second.second); } -#endif + + if( found_files != nullptr ) { + delete found_files; + } } -#ifndef GUI void multipleSeasons( const std::string &path, std::string &show, const std::map> &seasons, const std::string &language, const std::string &pattern, const bool &linux, const bool &trust, Curl &c) { auto url = getDefUrl(show, language, c); for( const auto &x : seasons ) { @@ -188,5 +174,6 @@ void allSeasons( const std::string &path, std::string &show, const std::string & iterateFS(seasons, path); multipleSeasons( path, show, seasons, language, pattern, linux, trust, c); } + #endif diff --git a/tv_rename.hpp b/tv_rename.hpp index 914eea9..9e25e27 100644 --- a/tv_rename.hpp +++ b/tv_rename.hpp @@ -8,7 +8,7 @@ #include -std::vector>> singleSeason( const std::string &show, int season, std::string url, const std::string &language, const std::string &pattern, const bool &linux, Curl &c, const std::set &files); +std::vector>> getRenamedFiles( const std::string &show, int season, std::string url, const std::string &language, const std::string &pattern, const bool &linux, Curl &c, const std::set &files ); #else @@ -18,4 +18,4 @@ void allSeasons( const std::string &path, std::string &show, const std::string & #endif -#endif +#endif // TV_RENAME_HPP