Refactor code, only allocate new std::set<std::string> in singleSeason when needed

This commit is contained in:
zvon 2019-01-29 17:50:19 +01:00
parent 3d4c355342
commit bcd79546f5
3 changed files with 31 additions and 44 deletions

View File

@ -185,7 +185,7 @@ void MainWindow::finishedSelection() {
for( auto &x : selected ) {
// get renamed files for given season
auto renamed_files = singleSeason( static_cast<Glib::ustring>((*iter)[m_columns_url.m_col_show]), x,
auto renamed_files = getRenamedFiles( static_cast<Glib::ustring>((*iter)[m_columns_url.m_col_show]), x,
"https://www.thetvdb.com" + url,
language_code,
(input_pattern.empty() ? default_pattern : input_pattern),

View File

@ -36,27 +36,7 @@ std::vector<std::string> parseEpisodeNames( const std::string &season_code, cons
return episodes;
}
#ifdef GUI
std::vector<std::pair<std::string, std::pair<std::string, std::string>>> defaultSingleSeasonReturn() {
return {};
}
#else
void defaultSingleSeasonReturn() {
return;
}
#endif
#ifdef GUI
std::vector<std::pair<std::string, std::pair<std::string, std::string>>> 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<std::string> &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<std::string> const *files_ptr) {
#endif
#ifndef GUI
if( url.empty() )
url = getDefUrl(show, language, c);
#endif
std::vector<std::pair<std::string, std::pair<std::string, std::string>>> 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<std::string> &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<std::pair<std::string, std::pair<std::string, std::string>>> renamed_files;
#ifndef GUI
std::set<std::string> found_files;
if( files_ptr == nullptr ) {
findSeason(found_files, season, path);
files_ptr = &found_files;
}
const std::set<std::string> &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<std::string, std::string>(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<std::string> const *files_ptr) {
if( url.empty() )
url = getDefUrl(show, language, c);
std::set<std::string> *found_files = nullptr;
if( files_ptr == nullptr ) {
found_files = new std::set<std::string>;
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<int, std::set<std::string>> &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

View File

@ -8,7 +8,7 @@
#include <vector>
std::vector<std::pair<std::string, std::pair<std::string, std::string>>> 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<std::string> &files);
std::vector<std::pair<std::string, std::pair<std::string, std::string>>> 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<std::string> &files );
#else
@ -18,4 +18,4 @@ void allSeasons( const std::string &path, std::string &show, const std::string &
#endif
#endif
#endif // TV_RENAME_HPP