diff --git a/functions.cpp b/functions.cpp index 9208232..e1c32b0 100644 --- a/functions.cpp +++ b/functions.cpp @@ -7,28 +7,6 @@ #include #include -#ifdef _WIN32 - -#include -#include - -#else // UNIX - -#include -#include -#include -#include - -#ifndef GUI - -#include -#include -#include - -#endif // GUI - -#endif // UNIX - #include "filesystem.hpp" #include "functions.hpp" #include "progress.hpp" @@ -37,6 +15,9 @@ #ifdef _WIN32 +#include +#include + #define cout std::wcout #define cerr std::wcerr #define cin std::wcin @@ -45,6 +26,11 @@ constexpr const char_t *dir_divider = L"\\"; #else // UNIX +#include +#include +#include +#include + #define TEXT( a ) a #define cout std::cout @@ -95,50 +81,10 @@ string encodeUrl( const string &url ) { return encoded.str(); } -// return true if filename has specified season -// set ep_pos to position where episode number starts -bool searchSpecificSeason( const char_t *const path, size_t &ep_pos, - const string &number ) { - size_t cur_pos{}; -#ifdef _WIN32 - auto ncompare = wcsncmp; -#else - auto ncompare = strncmp; -#endif - // search for S[0-9]+E[0-9]+ - while ( path[cur_pos] != '\0' ) { - if ( ( path[cur_pos] == 's' || path[cur_pos] == 'S' ) && - iswdigit( path[cur_pos + 1] ) ) { - cur_pos++; - while ( path[cur_pos] == '0' ) - cur_pos++; - // if season number is 0, move back because previous loop skipped it - if ( number == TEXT( "0" ) ) - cur_pos--; - // make sure season's number is the same as provided in argument - // `number` - if ( !ncompare( path + cur_pos, number.c_str(), number.size() ) ) { - cur_pos += number.size(); - if ( ( path[cur_pos] == 'e' || path[cur_pos] == 'E' ) && - iswdigit( path[cur_pos + 1] ) ) { - ep_pos = cur_pos + 1; - return true; - } - } - } - cur_pos++; - } - return false; -} - -bool searchSpecificSeason( const char_t *const p, const string &number ) { - size_t tmp; - return searchSpecificSeason( p, tmp, number ); -} - -// return true if file contains S[0-9]+E[0-9]+ nad set +// return true if file contains S[0-9]+E[0-9]+ and set // season_pos to start of season number -bool searchSeason( const char_t *const path, size_t &season_pos ) { +bool searchSeason( const char_t *const path, size_t &season_pos, + size_t &ep_pos ) { size_t cur_pos{}; while ( path[cur_pos] != '\0' ) { if ( ( path[cur_pos] == 's' || path[cur_pos] == 'S' ) && @@ -150,6 +96,7 @@ bool searchSeason( const char_t *const path, size_t &season_pos ) { cur_pos++; if ( ( path[cur_pos] == 'e' || path[cur_pos] == 'E' ) && iswdigit( path[cur_pos + 1] ) ) { + ep_pos = cur_pos + 1; return true; } } @@ -158,15 +105,16 @@ bool searchSeason( const char_t *const path, size_t &season_pos ) { return false; } -bool searchSeason( const char_t *const path ) { - size_t tmp{}; - return searchSeason( path, tmp ); +bool searchSeason( const char_t *const path, size_t &season_pos ) { + size_t tmp; + return searchSeason( path, season_pos, tmp ); } -void iterateFS( std::map< int, std::set< string > > &seasons, +void iterateFS( std::map< int, std::map< int, string > > &seasons, const string &path ) { // season_pos - position of first digit of the season size_t season_pos{ string::npos }; + size_t ep_pos{ string::npos }; for ( const auto p : FSLib::Directory( path ) ) { // if p is directory, iterate through it if ( FSLib::isDirectory( path + dir_divider + p ) ) { @@ -176,50 +124,9 @@ void iterateFS( std::map< int, std::set< string > > &seasons, // if file is a correct format, add it to file list // for its season - if ( searchSeason( p, season_pos ) ) - seasons[std::stoi( p + season_pos )].insert( path + dir_divider + - p ); - } -} - -// find all files for provided season in `path` and store it in `files` -void findSeason( std::set< string > &files, int season, const string &path ) { -#ifdef _WIN32 - auto number = std::to_wstring( season ); -#else - auto number = std::to_string( season ); -#endif - - for ( const auto p : FSLib::Directory( path ) ) { - // if p is directory, iterate through it - if ( FSLib::isDirectory( path + dir_divider + p ) ) { - findSeason( files, season, path + dir_divider + p ); - continue; - } - - if ( searchSpecificSeason( p, number ) ) - files.insert( path + dir_divider + p ); - } -} - -// find all files that comply with the S[0-9]+E[0-9]+ pattern -// and their season is in season_numbers and store tem in `seasons` -void findSeasons( std::map< int, std::set< string > > &seasons, - const string &path, const std::set< int > &season_numbers ) { - // season_pos - position of first digit of the season - size_t season_pos{ string::npos }; - for ( const auto p : FSLib::Directory( path ) ) { - // if p is directory, iterate through it - if ( FSLib::isDirectory( path + dir_divider + p ) ) { - findSeasons( seasons, path + dir_divider + p, season_numbers ); - continue; - } - - if ( searchSeason( p, season_pos ) ) { - auto num = std::stoi( p + season_pos ); - if ( season_numbers.find( num ) != season_numbers.end() ) - seasons[num].insert( path + dir_divider + p ); - } + if ( searchSeason( p, season_pos, ep_pos ) ) + seasons[std::stoi( p + season_pos )][std::stoi( p + ep_pos )] = + path + dir_divider + p; } } @@ -246,7 +153,9 @@ void printHelp() { << " or 'all'" << std::endl; cout << " for all seasons in selected directory" << std::endl; - cout << " --dvd use dvd ordering instead of aired ordering" << std::endl; + cout << " -d, --dvd use dvd ordering instead of aired " + "ordering" + << std::endl; cout << " --name-pattern Pattern to which change the file name." << std::endl; cout << " Possible sequences are:" << std::endl; @@ -330,7 +239,22 @@ void parseSeasonNumbers( std::set< int > &seasons_num, #endif // ndef GUI -#ifndef _WIN32 +#ifdef _WIN32 + +// get user's %APPDATA% folder location +string userHome() { + wchar_t *dir = static_cast< wchar_t * >( CoTaskMemAlloc( MAX_PATH ) ); + auto res = SHGetKnownFolderPath( FOLDERID_RoamingAppData, 0, NULL, &dir ); + if ( res == S_OK ) { + string dir_s = dir; + CoTaskMemFree( dir ); + return dir_s; + } + CoTaskMemFree( dir ); + return L""; +} + +#else // UNIX // get user's home directory string userHome() { @@ -349,20 +273,6 @@ string userHome() { return user_passwd->pw_dir; } -#else // UNIX - -// get user's %APPDATA% folder location -string userHome() { - wchar_t *dir = static_cast< wchar_t * >( CoTaskMemAlloc( MAX_PATH ) ); - auto res = SHGetKnownFolderPath( FOLDERID_RoamingAppData, 0, NULL, &dir ); - if ( res == S_OK ) { - string dir_s = dir; - CoTaskMemFree( dir ); - return dir_s; - } - return L""; -} - #endif // UNIX // create file name based on given pattern @@ -502,7 +412,8 @@ void prepareDB( const std::string &_pattern ) { db.exec( "CREATE TABLE IF NOT EXISTS SHOWS (ID INTEGER NOT NULL " "PRIMARY KEY AUTOINCREMENT, TVID TEXT NOT NULL, SHOW TEXT NOT NULL," - "PATH TEXT NOT NULL UNIQUE, LANGUAGE TEXT NOT NULL);" ); + "PATH TEXT NOT NULL UNIQUE, LANGUAGE TEXT NOT NULL, DVD INTEGER NOT " + "NULL);" ); db.exec( "CREATE TABLE IF NOT EXISTS EPISODES (SHOWID INTEGER NOT NULL," "PATH TEXT NOT NULL UNIQUE, FOREIGN KEY(SHOWID) " "REFERENCES SHOWS(ID));" ); @@ -515,9 +426,9 @@ void prepareDB( const std::string &_pattern ) { } else { pattern = &_pattern; } - db.exec( TEXT( "INSERT INTO SHOWS ( TVID, SHOW, PATH, LANGUAGE ) " + db.exec( TEXT( "INSERT INTO SHOWS ( TVID, SHOW, PATH, LANGUAGE, DVD ) " "VALUES ( 'pattern', 'pattern', '" ) + - sanitize( *pattern ) + TEXT( "', 'pattern' );" ) ); + sanitize( *pattern ) + TEXT( "', 'pattern', 0 );" ) ); if ( pattern != &_pattern ) { delete pattern; } @@ -525,12 +436,12 @@ void prepareDB( const std::string &_pattern ) { #ifndef GUI void addToDB( string &show, const string &path, const string &language, - bool linux ) { + bool linux, bool dvd ) { if ( !FSLib::exists( getDBName() ) ) prepareDB(); #else void addToDB( string &show, const string &path, const string &language, - const string &id, const string &pattern, bool linux ) { + const string &id, const string &pattern, bool linux, bool dvd ) { if ( !FSLib::exists( getDBName() ) ) prepareDB( pattern ); #endif @@ -543,23 +454,24 @@ void addToDB( string &show, const string &path, const string &language, throw e; } #ifndef GUI + // gui gives id and correct show name auto id = getShowId( show, language ); show = showNameFromId( id, language ); #endif - string show_id{}; - db.exec( TEXT( "INSERT OR IGNORE INTO SHOWS ( TVID, SHOW, PATH, LANGUAGE ) " - "VALUES ( '" ) + - sanitize( id ) + TEXT( "', '" ) + sanitize( show ) + - TEXT( "', '" ) + sanitize( absolute ) + TEXT( "', '" ) + - sanitize( language ) + TEXT( "' );" ) ); - show_id = std::to_string( db.lastRowID() ); + db.exec( + TEXT( "INSERT OR IGNORE INTO SHOWS ( TVID, SHOW, PATH, LANGUAGE, DVD ) " + "VALUES ( '" ) + + sanitize( id ) + TEXT( "', '" ) + sanitize( show ) + TEXT( "', '" ) + + sanitize( absolute ) + TEXT( "', '" ) + sanitize( language ) + "', " + + ( dvd ? TEXT( "1" ) : TEXT( "0" ) ) + TEXT( " );" ) ); + string show_id = std::to_string( db.lastRowID() ); #ifndef GUI string pattern{}; db.exec( TEXT( "SELECT PATH FROM SHOWS WHERE TVID == 'pattern';" ), pattern ); #endif - std::map< int, std::set< string > > seasons; + std::map< int, std::map< int, string > > seasons; // get all seasons and episodes iterateFS( seasons, absolute ); auto size = seasons.size(); @@ -571,9 +483,9 @@ void addToDB( string &show, const string &path, const string &language, #ifndef GUI ProgressBar::print( 0 ); #endif - for ( const auto &x : seasons ) { + for ( auto &x : seasons ) { singleSeason( absolute, show, x.first, id, language, pattern, linux, - true, &x.second, false ); + true, &x.second, true, dvd ); i++; #ifndef GUI ProgressBar::print( ( i * 100 ) / size ); @@ -582,8 +494,6 @@ void addToDB( string &show, const string &path, const string &language, #ifndef GUI cout << std::endl; #endif - seasons.clear(); - iterateFS( seasons, absolute ); size = seasons.size(); i = 0; @@ -598,7 +508,7 @@ void addToDB( string &show, const string &path, const string &language, for ( auto &episode : season.second ) { db.exec( TEXT( "INSERT OR IGNORE INTO EPISODES ( SHOWID, PATH ) " "VALUES ( " ) + - show_id + TEXT( ", '" ) + sanitize( episode ) + + show_id + TEXT( ", '" ) + sanitize( episode.second ) + TEXT( "' );" ) ); } i++; @@ -611,8 +521,8 @@ void addToDB( string &show, const string &path, const string &language, #endif } -void cleanUpLine() { #ifdef WIN32 +void cleanUpLine() { CONSOLE_SCREEN_BUFFER_INFO csbi; int width; @@ -626,21 +536,21 @@ void cleanUpLine() { GetConsoleScreenBufferInfo( h, &info ); COORD c = { 0, info.dwCursorPosition.Y - 3 }; SetConsoleCursorPosition( h, c ); + cout << string( width, ' ' ) << std::endl << std::endl; + SetConsoleCursorPosition( h, c ); +} #else +void cleanUpLine() { struct winsize w; ioctl( 0, TIOCGWINSZ, &w ); auto width = w.ws_col; cout << "\x1b[2A"; -#endif cout << string( width, ' ' ) << std::endl << std::endl; -#ifdef WIN32 - SetConsoleCursorPosition( h, c ); -#else cout << "\x1b[2A"; -#endif } +#endif void refreshDB( bool linux ) { std::vector< std::unordered_map< string, string > > shows; @@ -653,8 +563,9 @@ void refreshDB( bool linux ) { } db.exec( "DELETE FROM EPISODES;" ); db.exec( - TEXT( "SELECT ID, TVID, SHOW, PATH, LANGUAGE FROM SHOWS WHERE TVID " - "!= 'pattern';" ), + TEXT( + "SELECT ID, TVID, SHOW, PATH, LANGUAGE, DVD FROM SHOWS WHERE TVID " + "!= 'pattern';" ), shows ); string pattern{}; @@ -675,18 +586,19 @@ void refreshDB( bool linux ) { #ifndef GUI ProgressBar::print( 0 ); #endif - std::map< int, std::set< string > > seasons; + std::map< int, std::map< int, string > > seasons; // get all season number from this directory and subdirectories iterateFS( seasons, show[TEXT( "PATH" )] ); #ifndef GUI auto size = seasons.size(); #endif size_t i{}; - for ( const auto &x : seasons ) { + for ( auto &x : seasons ) { singleSeason( show[TEXT( "PATH" )], show[TEXT( "SHOW" )], x.first, show[TEXT( "TVID" )], show[TEXT( "LANGUAGE" )], pattern, linux, true, - &x.second, false ); + &x.second, false, + show[TEXT( "DVD" )] == TEXT( "1" ) ); i++; #ifndef GUI ProgressBar::print( ( i * 100 ) / size ); @@ -696,14 +608,12 @@ void refreshDB( bool linux ) { ProgressBar::print( 100 ); cout << std::endl; #endif - seasons.clear(); - iterateFS( seasons, show[TEXT( "PATH" )] ); for ( auto &season : seasons ) { for ( auto &episode : season.second ) { db.exec( TEXT( "INSERT INTO EPISODES ( SHOWID, PATH ) " "VALUES ( " ) + show[TEXT( "ID" )] + TEXT( ", '" ) + - sanitize( episode ) + TEXT( "' );" ) ); + sanitize( episode.second ) + TEXT( "' );" ) ); } } } @@ -719,9 +629,10 @@ void updateDB( bool linux ) { cerr << "Can't open database, make sure it exists" << std::endl; throw e; } - db.exec( TEXT( "SELECT ID, TVID, SHOW, PATH, LANGUAGE FROM SHOWS WHERE TVID" - " != 'pattern';" ), - shows ); + db.exec( + TEXT( "SELECT ID, TVID, SHOW, PATH, LANGUAGE, DVD FROM SHOWS WHERE TVID" + " != 'pattern';" ), + shows ); string pattern{}; db.exec( TEXT( "SELECT PATH FROM SHOWS WHERE TVID == 'pattern';" ), @@ -744,13 +655,13 @@ void updateDB( bool linux ) { db.exec( TEXT( "SELECT PATH FROM EPISODES WHERE SHOWID == " ) + show[TEXT( "ID" )] + TEXT( ";" ), episodes ); - std::map< int, std::set< string > > seasons; - std::map< int, std::set< string > > new_eps; - // get all season number from this directory and subdirectories + std::map< int, std::map< int, string > > seasons; + std::map< int, std::map< int, string > > new_eps; + // get all season numbers from this directory and subdirectories iterateFS( seasons, show[TEXT( "PATH" )] ); for ( const auto &x : seasons ) { for ( const auto &episode : x.second ) { - if ( episodes.find( episode ) == episodes.end() ) { + if ( episodes.find( episode.second ) == episodes.end() ) { new_eps[x.first].insert( episode ); } } @@ -763,11 +674,12 @@ void updateDB( bool linux ) { auto size = new_eps.size(); #endif size_t i{}; - for ( const auto &x : new_eps ) { + for ( auto &x : new_eps ) { singleSeason( show[TEXT( "PATH" )], show[TEXT( "SHOW" )], x.first, show[TEXT( "TVID" )], show[TEXT( "LANGUAGE" )], pattern, linux, true, - &x.second, false ); + &x.second, false, + show[TEXT( "DVD" )] == TEXT( "1" ) ); i++; #ifndef GUI ProgressBar::print( ( i * 100 ) / size ); @@ -780,7 +692,7 @@ void updateDB( bool linux ) { db.exec( TEXT( "INSERT OR IGNORE INTO EPISODES ( SHOWID, " "PATH ) VALUES ( " ) + show[TEXT( "ID" )] + TEXT( ", '" ) + - sanitize( episode ) + TEXT( "' );" ) ); + sanitize( episode.second ) + TEXT( "' );" ) ); } } } @@ -812,24 +724,18 @@ void cleanDB() { cerr << "Can't open database, make sure it exists" << std::endl; throw e; } - db.exec( TEXT( "SELECT ID, TVID, SHOW, PATH, LANGUAGE FROM SHOWS WHERE TVID" + db.exec( TEXT( "SELECT ID, PATH FROM SHOWS WHERE TVID" " != 'pattern';" ), shows ); - string pattern{}; - db.exec( TEXT( "SELECT PATH FROM SHOWS WHERE TVID == 'pattern';" ), - pattern ); - for ( auto &show : shows ) { - if ( FSLib::exists( show[TEXT( "PATH" )] ) ) { - continue; - } + bool dir_exists = FSLib::exists( show[TEXT( "PATH" )] ); std::unordered_set< string > episodes; db.exec( TEXT( "SELECT PATH FROM EPISODES WHERE SHOWID == " ) + show[TEXT( "ID" )] + TEXT( ";" ), episodes ); for ( const auto &episode : episodes ) { - if ( !FSLib::exists( episode ) ) { + if ( !dir_exists || !FSLib::exists( episode ) ) { db.exec( TEXT( "DELETE FROM EPISODES WHERE PATH == '" ) + sanitize( episode ) + TEXT( "';" ) ); } @@ -876,7 +782,7 @@ std::vector< std::unordered_map< std::string, std::string > > dbGetShows() { } void changeDB( size_t index, const string &path, const string &language, - const string &id ) { + const string &id, bool dvd ) { SQLite::Database db{}; auto absolute = FSLib::canonical( path ); try { @@ -890,14 +796,13 @@ void changeDB( size_t index, const string &path, const string &language, #else string show_id = std::to_wstring( index ); #endif -#ifdef _WIN32 - string source_code = utf8_to_wstring( - c.execute( url ); -#else auto real_show = showNameFromId( id, language ); -#endif - db.exec( TEXT( "UPDATE SHOWS SET TVID = '" ) + sanitize( id ) + TEXT("', SHOW = '") + sanitize(real_show) + TEXT("', PATH = '") + sanitize(absolute) + TEXT("', LANGUAGE = '") + - sanitize( language ) + TEXT( "' WHERE ID == " + show_id + ";" ) ); + db.exec( TEXT( "UPDATE SHOWS SET TVID = '" ) + sanitize( id ) + + TEXT( "', SHOW = '" ) + sanitize( real_show ) + + TEXT( "', PATH = '" ) + sanitize( absolute ) + + TEXT( "', LANGUAGE = '" ) + sanitize( language ) + + TEXT( "', DVD = " + ( dvd ? TEXT( "1" ) : TEXT( "0" ) ) + + " WHERE ID == " + show_id + ";" ) ); } void refreshSingleDB( const size_t &index, bool linux ) { @@ -911,9 +816,10 @@ void refreshSingleDB( const size_t &index, bool linux ) { throw e; } db.exec( "DELETE FROM EPISODES WHERE SHOWID == " + show_id + ";" ); - db.exec( TEXT( "SELECT TVID, SHOW, PATH, LANGUAGE FROM SHOWS WHERE ID == " + - show_id + ";" ), - shows ); + db.exec( + TEXT( "SELECT TVID, SHOW, PATH, LANGUAGE, DVD FROM SHOWS WHERE ID == " + + show_id + ";" ), + shows ); std::unordered_map< string, string > &show = shows[0]; @@ -934,17 +840,18 @@ void refreshSingleDB( const size_t &index, bool linux ) { #ifndef GUI ProgressBar::print( 0 ); #endif - std::map< int, std::set< string > > seasons; + std::map< int, std::map< int, string > > seasons; // get all season number from this directory and subdirectories iterateFS( seasons, show[TEXT( "PATH" )] ); #ifndef GUI auto size = seasons.size(); #endif size_t i{}; - for ( const auto &x : seasons ) { + for ( auto &x : seasons ) { singleSeason( show[TEXT( "PATH" )], show[TEXT( "SHOW" )], x.first, show[TEXT( "TVID" )], show[TEXT( "LANGUAGE" )], - pattern, linux, true, &x.second, false ); + pattern, linux, true, &x.second, false, + show[TEXT( "DVD" )] == TEXT( "1" ) ); i++; #ifndef GUI ProgressBar::print( ( i * 100 ) / size ); @@ -960,7 +867,7 @@ void refreshSingleDB( const size_t &index, bool linux ) { for ( auto &episode : season.second ) { db.exec( TEXT( "INSERT INTO EPISODES ( SHOWID, PATH ) " "VALUES ( " ) + - show_id + TEXT( ", '" ) + sanitize( episode ) + + show_id + TEXT( ", '" ) + sanitize( episode.second ) + TEXT( "' );" ) ); } } diff --git a/functions.hpp b/functions.hpp index f36c10c..0dcb315 100644 --- a/functions.hpp +++ b/functions.hpp @@ -1,18 +1,12 @@ #ifndef TV_FUNCTIONS_H #define TV_FUNCTIONS_H -#include #include #include #include #include - -#ifdef GUI - #include -#endif - #include "network.hpp" #ifdef _WIN32 @@ -38,9 +32,8 @@ void findSeasons( std::map< int, std::set< string > > &seasons, void printHelp(); string encodeUrl( const string &url ); -bool searchSpecificSeason( const char_t *const path, const string &number ); bool searchSeason( const char_t *const path, size_t &season_pos ); -bool searchSeason( const char_t *const path ); +bool searchSeason( const char_t *const path, size_t &season_pos, size_t &ep_pos ); void parseSeasonNumbers( std::set< int > &seasons_num, const char_t *argument ); @@ -71,15 +64,12 @@ void refreshDB( bool linux ); void updateDB( bool linux ); void cleanDB(); void changeDB( size_t index, const string &path, const string &language, - const string &url ); + const string &id, bool dvd ); void refreshSingleDB( const size_t &index, bool linux ); -void iterateFS( std::map< int, std::set< string > > &seasons, +void iterateFS( std::map< int, std::map< int, string > > &seasons, const string &path ); -bool searchSpecificSeason( const char_t *const path, size_t &ep_pos, - const string &number ); - string compilePattern( const string &pattern, int season, int episode, const string &filename, const string &episodeName, const string &showName );