Main: refactor

This commit is contained in:
zvon 2020-01-15 17:12:54 +01:00
parent 2536881452
commit bae6f47bc5

148
main.cpp
View File

@ -7,18 +7,6 @@
#include <io.h> #include <io.h>
#include <windows.h> #include <windows.h>
#else
#include <getopt.h>
#endif
#include "filesystem.hpp"
#include "functions.hpp"
#include "tv_rename.hpp"
#ifdef _WIN32
using char_t = wchar_t; using char_t = wchar_t;
using string = std::wstring; using string = std::wstring;
@ -28,6 +16,8 @@ using string = std::wstring;
#else #else
#include <getopt.h>
using char_t = char; using char_t = char;
using string = std::string; using string = std::string;
@ -39,17 +29,24 @@ using string = std::string;
#endif #endif
constexpr size_t DB_ADD = 0x0001; #include "filesystem.hpp"
constexpr size_t DB_REFRESH = 0x0002; #include "functions.hpp"
constexpr size_t DB_UPDATE = 0x0004; #include "tv_rename.hpp"
constexpr size_t DB_CLEAN = 0x0008;
constexpr size_t DB_REMOVE = 0x0010;
constexpr size_t DB_PATTERN = 0x0020;
// DB flags
#define DB_ADD 0x0001
#define DB_REFRESH 0x0002
#define DB_UPDATE 0x0004
#define DB_CLEAN 0x0008
#define DB_REMOVE 0x0010
#define DB_PATTERN 0x0020
// return 0 - all went as expected, 1 - request of help or print languages,
// -1 - error
int handleArgument( char_t c, string &show, std::set< int > &seasons_num, int handleArgument( char_t c, string &show, std::set< int > &seasons_num,
bool &change_dir, string &path, bool &trust, bool &linux, string &path, string &language, string &pattern,
string &language, string &pattern, bool &dvd, size_t &db_flags, size_t &tv_flags, size_t &db_flags, string &db_pattern,
string &db_pattern, char_t *optional, int &i ) { char_t *optional, int &i ) {
switch ( c ) { switch ( c ) {
case 's': case 's':
show = optional; show = optional;
@ -60,38 +57,39 @@ int handleArgument( char_t c, string &show, std::set< int > &seasons_num,
i++; i++;
break; break;
case 'c': case 'c':
change_dir = false; tv_flags |= TV_CHDIR;
break; break;
case 'p': case 'p':
path = optional; path = optional;
i++; i++;
// if path provided, assume it's correct // if path provided, assume it's correct
change_dir = false; tv_flags &= ~TV_CHDIR;
break; break;
case 't': case 't':
trust = true; tv_flags |= TV_TRUST;
break; break;
case 'x': case 'x':
linux = true; tv_flags |= TV_LINUX;
break;
case 'd':
tv_flags |= TV_DVD;
break; break;
case 'l': case 'l':
if ( findLanguage( optional ) ) { if ( findLanguage( optional ) ) {
language = optional; language = optional;
} else { } else {
cerr << "Invalid language choice" << std::endl; cerr << "Invalid language choice" << std::endl;
authenticate( "XXXXX" );
printLangs(); printLangs();
return -1; return -1;
} }
i++; i++;
break; break;
case '0':
authenticate( "XXXXX" );
printLangs();
return 1;
case 'h': case 'h':
printHelp(); printHelp();
return 1; return 1;
case '0':
printLangs();
return 1;
case '1': case '1':
pattern = optional; pattern = optional;
i++; i++;
@ -105,11 +103,6 @@ int handleArgument( char_t c, string &show, std::set< int > &seasons_num,
case 'u': case 'u':
db_flags |= DB_UPDATE; db_flags |= DB_UPDATE;
break; break;
case 'd':
db_pattern = optional;
db_flags |= DB_PATTERN;
i++;
break;
case '2': case '2':
db_flags |= DB_CLEAN; db_flags |= DB_CLEAN;
break; break;
@ -117,7 +110,9 @@ int handleArgument( char_t c, string &show, std::set< int > &seasons_num,
db_flags |= DB_REMOVE; db_flags |= DB_REMOVE;
break; break;
case '4': case '4':
dvd = true; db_pattern = optional;
db_flags |= DB_PATTERN;
i++;
break; break;
default: default:
return -1; return -1;
@ -142,34 +137,36 @@ string getOptions( const char_t *option ) {
return L"t"; return L"t";
else if ( !wcscmp( option, L"--linux" ) ) else if ( !wcscmp( option, L"--linux" ) )
return L"x"; return L"x";
else if ( !wcscmp( option, L"--dvd" ) )
return L"d";
else if ( !wcscmp( option, L"--lang" ) ) else if ( !wcscmp( option, L"--lang" ) )
return L"l"; return L"l";
else if ( !wcscmp( option, L"--help" ) )
return L"h";
else if ( !wcscmp( option, L"--print-langs" ) ) else if ( !wcscmp( option, L"--print-langs" ) )
return L"0"; return L"0";
else if ( !wcscmp( option, L"--name-pattern" ) ) else if ( !wcscmp( option, L"--name-pattern" ) )
return L"1"; return L"1";
else if ( !wcscmp( option, L"--help" ) )
return L"h";
else if ( !wcscmp( option, L"--db-add" ) ) else if ( !wcscmp( option, L"--db-add" ) )
return L"a"; return L"a";
else if ( !wcscmp( option, L"--db-refresh" ) ) else if ( !wcscmp( option, L"--db-refresh" ) )
return L"r"; return L"r";
else if ( !wcscmp( option, L"--db-update" ) ) else if ( !wcscmp( option, L"--db-update" ) )
return L"u"; return L"u";
else if ( !wcscmp( option, L"--db-name-pattern" ) )
return L"d";
else if ( !wcscmp( option, L"--db-clean" ) ) else if ( !wcscmp( option, L"--db-clean" ) )
return L"2"; return L"2";
else if ( !wcscmp( option, L"--db-remove" ) ) else if ( !wcscmp( option, L"--db-remove" ) )
return L"3"; return L"3";
else if ( !wcscmp( option, L"--db-name-pattern" ) )
return L"4";
return L""; return L"";
} }
// there's no getopt for windows, so just use wcscmp // there's no getopt for windows, so just use wcscmp
int parseCommandLine( string &show, std::set< int > &seasons_num, string &path, int parseCommandLine( string &show, std::set< int > &seasons_num, string &path,
bool &change_dir, string &language, string &pattern, string &language, string &pattern, size_t &tv_flags,
bool &linux, bool &trust, size_t &db_flags, size_t &db_flags, string &db_pattern, const int argc,
string &db_pattern, const int argc, char_t **argv ) { char_t **argv ) {
string options{}; string options{};
char_t *optional; char_t *optional;
for ( auto i = 1; i < argc; i++ ) { for ( auto i = 1; i < argc; i++ ) {
@ -181,9 +178,9 @@ int parseCommandLine( string &show, std::set< int > &seasons_num, string &path,
optional = ( i < argc - 1 ) ? argv[i + 1] : nullptr; optional = ( i < argc - 1 ) ? argv[i + 1] : nullptr;
} }
for ( const auto &x : options ) { for ( const auto &x : options ) {
auto res = handleArgument( x, show, seasons_num, change_dir, path, auto res =
trust, linux, language, pattern, dvd, handleArgument( x, show, seasons_num, path, language, pattern,
db_flags, db_pattern, optional, i ); tv_flags, db_flags, db_pattern, optional, i );
if ( res != 0 ) if ( res != 0 )
return res; return res;
} }
@ -195,26 +192,26 @@ int parseCommandLine( string &show, std::set< int > &seasons_num, string &path,
// parse command line arguments using getopt // parse command line arguments using getopt
int parseCommandLine( string &show, std::set< int > &seasons_num, string &path, int parseCommandLine( string &show, std::set< int > &seasons_num, string &path,
bool &change_dir, string &language, string &pattern, string &language, string &pattern, size_t &tv_flags,
bool &linux, bool &trust, bool &dvd, size_t &db_flags, size_t &db_flags, string &db_pattern, int argc,
string &db_pattern, int argc, char **argv ) { char **argv ) {
static struct option long_options[] = { static struct option long_options[] = {
{ "show", required_argument, 0, 's' }, { "show", required_argument, 0, 's' },
{ "season", required_argument, 0, 'n' }, { "season", required_argument, 0, 'n' },
{ "correct-path", no_argument, 0, 'c' }, { "correct-path", no_argument, 0, 'c' },
{ "trust", no_argument, 0, 't' }, { "trust", no_argument, 0, 't' },
{ "linux", no_argument, 0, 'x' }, { "linux", no_argument, 0, 'x' },
{ "dvd", no_argument, 0, 'd' },
{ "lang", required_argument, 0, 'l' }, { "lang", required_argument, 0, 'l' },
{ "help", no_argument, 0, 'h' },
{ "print-langs", no_argument, 0, '0' }, { "print-langs", no_argument, 0, '0' },
{ "name-pattern", required_argument, 0, '1' }, { "name-pattern", required_argument, 0, '1' },
{ "db-add", no_argument, 0, 'a' }, { "db-add", no_argument, 0, 'a' },
{ "db-refresh", no_argument, 0, 'r' }, { "db-refresh", no_argument, 0, 'r' },
{ "db-update", no_argument, 0, 'u' }, { "db-update", no_argument, 0, 'u' },
{ "db-name-pattern", required_argument, 0, 'd' },
{ "db-clean", no_argument, 0, '2' }, { "db-clean", no_argument, 0, '2' },
{ "db-remove", no_argument, 0, '3' }, { "db-remove", no_argument, 0, '3' },
{ "dvd", no_argument, 0, '4' }, { "db-name-pattern", required_argument, 0, '4' },
{ "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 } { 0, 0, 0, 0 }
}; };
@ -222,13 +219,13 @@ int parseCommandLine( string &show, std::set< int > &seasons_num, string &path,
while ( 1 ) { while ( 1 ) {
int option_index{ 0 }; int option_index{ 0 };
auto c = getopt_long( argc, argv, "s:n:cp:txl:01:arud234h", long_options, auto c = getopt_long( argc, argv, "s:n:cp:txl:arudh", long_options,
&option_index ); &option_index );
if ( c == -1 ) if ( c == -1 )
break; break;
auto res = handleArgument( c, show, seasons_num, change_dir, path, auto res =
trust, linux, language, pattern, dvd, db_flags, handleArgument( c, show, seasons_num, path, language, pattern,
db_pattern, optarg, i ); tv_flags, db_flags, db_pattern, optarg, i );
if ( res != 0 ) if ( res != 0 )
return res; return res;
} }
@ -236,7 +233,7 @@ int parseCommandLine( string &show, std::set< int > &seasons_num, string &path,
if ( optind < argc ) { if ( optind < argc ) {
path = string( argv[optind] ); path = string( argv[optind] );
// if path provided, assume it's correct // if path provided, assume it's correct
change_dir = false; tv_flags &= ~TV_CHDIR;
} }
return 0; return 0;
@ -257,28 +254,25 @@ int main
#endif #endif
string show{}; string show{};
std::set< int > seasons_num{}; std::set< int > seasons_num{};
bool change_dir{ true }; size_t tv_flags{};
bool linux{ false };
bool trust{ false };
bool dvd{ false };
size_t db_flags{}; size_t db_flags{};
string path{ TEXT( "." ) }; string path{ TEXT( "." ) };
string language{ TEXT( "en" ) }; string language{ TEXT( "en" ) };
string pattern{ TEXT( "%filename - %epname" ) }; string pattern{ TEXT( "%filename - %epname" ) };
string db_pattern{}; string db_pattern{};
authenticate( "42B66F5E-C6BF-423F-ADF9-CC97163472F6" );
{ {
auto tmp = parseCommandLine( show, seasons_num, path, change_dir, auto tmp =
language, pattern, linux, trust, dvd, db_flags, parseCommandLine( show, seasons_num, path, language, pattern,
db_pattern, argc, argv ); tv_flags, db_flags, db_pattern, argc, argv );
if ( tmp == -1 ) if ( tmp == -1 )
return 1; return 1;
else if ( tmp == 1 ) else if ( tmp == 1 )
return 0; return 0;
} }
authenticate( "XXXXX" );
if ( !FSLib::isDirectory( path ) && FSLib::exists( path ) ) { if ( !FSLib::isDirectory( path ) && FSLib::exists( path ) ) {
// specified file, not directory // specified file, not directory
auto *file_set = new std::set< string >; auto *file_set = new std::set< string >;
@ -291,11 +285,11 @@ int main
} else { } else {
auto season = std::stoi( path.c_str() + season_pos ); auto season = std::stoi( path.c_str() + season_pos );
singleSeason( path, show, season, TEXT( "" ), language, pattern, singleSeason( path, show, season, TEXT( "" ), language, pattern,
linux, trust, file_set, true, dvd ); tv_flags, file_set, true );
return 0; return 0;
} }
} else if ( !FSLib::isDirectory( path ) ) { } else if ( !FSLib::isDirectory( path ) ) {
change_dir = true; tv_flags |= TV_CHDIR;
} }
if ( !db_pattern.empty() ) { if ( !db_pattern.empty() ) {
@ -310,17 +304,16 @@ int main
if ( db_flags & DB_REMOVE ) { if ( db_flags & DB_REMOVE ) {
removeFromDB( FSLib::canonical( path ) ); removeFromDB( FSLib::canonical( path ) );
} }
if ( db_flags & DB_ADD ) { if ( db_flags & DB_ADD ) {
addToDB( show, path, language, linux ); addToDB( show, path, language, tv_flags & TV_LINUX );
cout << "Added to database" << std::endl; cout << "Added to database" << std::endl;
} }
if ( db_flags & DB_REFRESH ) { if ( db_flags & DB_REFRESH ) {
refreshDB( linux ); refreshDB( tv_flags & TV_LINUX );
cout << "Refreshed database" << std::endl; cout << "Refreshed database" << std::endl;
} }
if ( db_flags & DB_UPDATE ) { if ( db_flags & DB_UPDATE ) {
updateDB( linux ); updateDB( tv_flags & TV_LINUX );
cout << "Updated database" << std::endl; cout << "Updated database" << std::endl;
} }
if ( db_flags & DB_CLEAN ) { if ( db_flags & DB_CLEAN ) {
@ -332,7 +325,7 @@ int main
if ( db_flags ) if ( db_flags )
return 0; return 0;
while ( change_dir ) { while ( tv_flags & TV_CHDIR ) {
if ( !FSLib::isDirectory( path ) ) { if ( !FSLib::isDirectory( path ) ) {
cout << "This directory doesn't exist, please insert a correct " cout << "This directory doesn't exist, please insert a correct "
"path: " "path: "
@ -347,7 +340,7 @@ int main
cin.ignore( 1, '\n' ); cin.ignore( 1, '\n' );
cin.clear(); cin.clear();
if ( response[0] == 'y' || response[0] == 'Y' ) { if ( response[0] == 'y' || response[0] == 'Y' ) {
change_dir = false; tv_flags &= ~TV_CHDIR;
} else { } else {
cout << "Insert correct path:" << std::endl; cout << "Insert correct path:" << std::endl;
std::getline( cin, path ); std::getline( cin, path );
@ -372,11 +365,10 @@ int main
if ( seasons_num.size() == 1 ) { if ( seasons_num.size() == 1 ) {
singleSeason( path, show, *seasons_num.begin(), string(), language, singleSeason( path, show, *seasons_num.begin(), string(), language,
pattern, linux, trust, nullptr, true, dvd ); pattern, tv_flags, nullptr, true );
} else if ( seasons_num.size() != 0 ) { } else if ( seasons_num.size() != 0 ) {
multipleSeasons( path, show, seasons_num, language, pattern, linux, multipleSeasons( path, show, seasons_num, language, pattern, tv_flags );
trust, dvd );
} else { } else {
allSeasons( path, show, language, pattern, linux, trust, dvd ); allSeasons( path, show, language, pattern, tv_flags );
} }
} }