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

View File

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