Functions: slight refactor

This commit is contained in:
zvon 2020-01-18 22:18:58 +01:00
parent d546d8442f
commit 90f45464ac
2 changed files with 29 additions and 33 deletions

View File

@ -1,6 +1,3 @@
#include <algorithm>
#include <array>
#include <cctype>
#include <iomanip>
#include <map>
#include <sstream>
@ -111,7 +108,8 @@ bool searchSeason( const char_t *const path, size_t &season_pos ) {
void iterateFS( std::map< int, std::map< int, string > > &seasons,
const string &path ) {
// season_pos - position of first digit of the season
// season_pos - position of first digit of the season number
// ep_pos - position of first digit of the episode number
size_t season_pos{ string::npos };
size_t ep_pos{ string::npos };
for ( const auto p : FSLib::Directory( path ) ) {
@ -250,7 +248,7 @@ string userHome() {
return dir_s;
}
CoTaskMemFree( dir );
return L"";
throw std::runtime_error( "Couldn't find user's %%APPDATA%%!" );
}
#else // UNIX
@ -434,17 +432,15 @@ void prepareDB( const string &_pattern ) {
}
#ifndef GUI
void addToDB( const string &show, const string &path, const string &language,
void addToDB( string &show, const string &path, const string &language,
bool linux, bool dvd ) {
if ( !FSLib::exists( getDBName() ) )
prepareDB();
#else
void addToDB( const string &show, const string &path, const string &language,
const string &id, const string &pattern, bool linux, bool dvd,
void *progress_ptr ) {
if ( !FSLib::exists( getDBName() ) )
prepareDB( pattern );
#endif
if ( !FSLib::exists( getDBName() ) )
prepareDB();
SQLite::Database db{};
auto absolute = FSLib::canonical( path );
try {
@ -465,9 +461,9 @@ void addToDB( const string &show, const string &path, const string &language,
sanitize( absolute ) + TEXT( "', '" ) + sanitize( language ) +
TEXT( "', " ) + ( dvd ? TEXT( "1" ) : TEXT( "0" ) ) + TEXT( " );" ) );
#ifdef _WIN32
string show_id = std::to_wstring( db.lastRowID() );
string db_id = std::to_wstring( db.lastRowID() );
#else
string show_id = std::to_string( db.lastRowID() );
string db_id = std::to_string( db.lastRowID() );
#endif
#ifndef GUI
string pattern{};
@ -478,8 +474,6 @@ void addToDB( const string &show, const string &path, const string &language,
std::map< int, std::map< int, string > > seasons;
// get all seasons and episodes
iterateFS( seasons, absolute );
auto size = seasons.size();
size_t i = 0;
#ifndef GUI
ProgressBar p;
@ -489,29 +483,32 @@ void addToDB( const string &show, const string &path, const string &language,
p.print( TEXT( "Renaming" ) );
p.print( 0 );
size_t i = 0;
size_t seasons_size = seasons.size();
for ( auto &x : seasons ) {
singleSeason( absolute, show, x.first, id, language, pattern, linux,
true, &x.second, false, dvd );
i++;
p.print( ( i * 100 ) / size );
p.print( ( i * 100 ) / seasons_size );
}
#ifndef GUI
cout << std::endl;
#endif
size = seasons.size();
i = 0;
p.print( TEXT( "Adding to database" ) );
p.print( 0 );
i = 0;
for ( auto &season : seasons ) {
for ( auto &episode : season.second ) {
db.exec( TEXT( "INSERT OR IGNORE INTO EPISODES ( SHOWID, PATH ) "
"VALUES ( " ) +
show_id + TEXT( ", '" ) + sanitize( episode.second ) +
db_id + TEXT( ", '" ) + sanitize( episode.second ) +
TEXT( "' );" ) );
}
i++;
p.print( ( i * 100 ) / size );
p.print( ( i * 100 ) / seasons_size );
}
#ifndef GUI
cout << std::endl;
@ -526,13 +523,12 @@ void cleanUpLine() {
GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &csbi );
width = csbi.srWindow.Right - csbi.srWindow.Left + 1;
static HANDLE h = NULL;
if ( !h )
h = GetStdHandle( STD_OUTPUT_HANDLE );
static HANDLE h = GetStdHandle( STD_OUTPUT_HANDLE );
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo( h, &info );
COORD c = { 0, info.dwCursorPosition.Y - 3 };
SetConsoleCursorPosition( h, c );
cout << string( width, ' ' ) << std::endl << std::endl;
SetConsoleCursorPosition( h, c );
}
@ -564,6 +560,7 @@ void refreshDB( bool linux, void *progress_ptr ) {
cerr << "Can't open database, make sure it exists" << std::endl;
throw e;
}
db.exec( "DELETE FROM EPISODES;" );
db.exec(
TEXT(
@ -589,9 +586,9 @@ void refreshDB( bool linux, void *progress_ptr ) {
p.print( TEXT( "Refreshing " ) + show[TEXT( "SHOW" )] );
p.print( 0 );
std::map< int, std::map< int, string > > seasons;
// get all season number from this directory and subdirectories
iterateFS( seasons, show[TEXT( "PATH" )] );
auto size = seasons.size();
auto seasons_size = seasons.size();
size_t i{};
for ( auto &x : seasons ) {
singleSeason( show[TEXT( "PATH" )], show[TEXT( "SHOW" )],
@ -600,7 +597,7 @@ void refreshDB( bool linux, void *progress_ptr ) {
&x.second, false,
show[TEXT( "DVD" )] == TEXT( "1" ) );
i++;
p.print( ( i * 100 ) / size );
p.print( ( i * 100 ) / seasons_size );
}
p.print( 100 );
#ifndef GUI
@ -612,7 +609,7 @@ void refreshDB( bool linux, void *progress_ptr ) {
p.print( 0 );
i = 0;
size_t j{};
size_t addition = 100 / seasons.size();
size_t addition = 100 / seasons_size;
for ( auto &season : seasons ) {
j = 0;
size_t smalladdition = addition / season.second.size();
@ -703,9 +700,7 @@ void updateDB( bool linux, void *progress_ptr ) {
i++;
p.print( ( i * 100 ) / size );
}
seasons.clear();
iterateFS( seasons, show[TEXT( "PATH" )] );
for ( auto &season : seasons ) {
for ( auto &season : new_eps ) {
for ( auto &episode : season.second ) {
db.exec( TEXT( "INSERT OR IGNORE INTO EPISODES ( SHOWID, "
"PATH ) VALUES ( " ) +
@ -792,6 +787,7 @@ void removeFromDB( const string &path ) {
db.exec( TEXT( "DELETE FROM SHOWS WHERE ID == " ) + show_id + TEXT( ";" ) );
}
#ifdef GUI
std::vector< std::unordered_map< string, string > > dbGetShows() {
if ( !FSLib::exists( getDBName() ) )
return {};
@ -833,7 +829,6 @@ void changeDB( size_t index, const string &path, const string &language,
TEXT( " WHERE ID == " ) + show_id + TEXT( ";" ) );
}
#ifdef GUI
void refreshSelectDB( std::unordered_set< size_t > indexes, bool linux,
void *progress_ptr ) {
if ( !FSLib::exists( getDBName() ) )

View File

@ -43,13 +43,12 @@ string userHome();
void prepareDB( const string &_pattern = TEXT( "" ) );
#ifndef GUI
void addToDB( const string &show, const string &path, const string &language,
void addToDB( string &show, const string &path, const string &language,
bool linux, bool dvd );
#else
void addToDB( const string &show, const string &path, const string &language,
const string &id, const string &pattern, bool linux, bool dvd,
void *progress_ptr );
std::vector< std::unordered_map< std::string, std::string > > dbGetShows();
#endif
void removeFromDB( const string &path );
void changeDBPattern( const string &pattern );
@ -64,9 +63,11 @@ void updateDB( bool linux );
void updateDB( bool linux, void *progress_ptr );
#endif
void cleanDB();
#ifdef GUI
std::vector< std::unordered_map< std::string, std::string > > dbGetShows();
void changeDB( size_t index, const string &path, const string &language,
const string &id, bool dvd );
#ifdef GUI
void refreshSelectDB( std::unordered_set< size_t > indexes, bool linux,
void *progress_ptr );
#endif