S_New4/parameterManager.cpp
2019-07-08 20:17:11 +02:00

385 lines
17 KiB
C++

#include "parameterManager.h"
Settings manageParameter(int argc, char **argv)
{
Settings settings;
std::string argv0 = argv[0];
argv0.erase(argv0.find_last_of("/\\") + 1 , argv0.length() - ( argv0.find_last_of("/\\") + 1) );
settings.cookieFilePath = argv0 + "S_New4_cookies";
settings.accountFilePath = argv0 + "Accounts";
settings.accountNumberPath = argv0 + "Account_Number";
if(argc < 2) {
std::cout << " => Keine Unteroption angegeben." << std::endl;
std::cout << "Aufruf: " << getProgramName(argv[0]) << " [Unteroption] [PARAMETER]" << std::endl;
std::cout << "" << getProgramName(argv[0]) << " --help“ liefert weitere Informationen." << std::endl;
settings.modus = Modus::EXIT;
return settings;
}
if(compare("--help\ndefault\nurl\n--version", argv[1]) != 1) {
std::cout << " => Unbekannte Unteroption: '" << argv[1] << "': Mehrere oder keine Option gefunden." << std::endl;
std::cout << "Aufruf: " << getProgramName(argv[0]) << " [Unteroption] [PARAMETER]" << std::endl;
std::cout << "" << getProgramName(argv[0]) << " --help“ liefert weitere Informationen." << std::endl;
settings.modus = Modus::EXIT;
return settings;
}
if(strncmp(argv[1], "--help", strlen(argv[1])) == 0) {
argv[1][0] = '\0';
unterOption_help(&settings, argv[0]);
return settings;
} else if (strncmp(argv[1], "default", strlen(argv[1])) == 0) {
argv[1][0] = '\0';
unterOption_default(&settings, argc, argv);
return settings;
} else if (strncmp(argv[1], "url", strlen(argv[1])) == 0) {
argv[1][0] = '\0';
unterOption_url(&settings, argc, argv);
return settings;
} else if (strncmp(argv[1], "--version", strlen(argv[1])) == 0) {
std::cout << "Version: " << settings.version << std::endl;
settings.modus = Modus::EXIT;
return settings;
} else {
std::cout << "Error: Invalid option " << argv[1] << ", but not detected in compare-Function" << std::endl;
settings.modus = Modus::EXIT;
return settings;
}
}
void unterOption_help(Settings *settings, char * argv0)
{
std::cout << "Aufruf: " << getProgramName(argv0) << " [Unteroption] [PARAMETER]" << std::endl << std::endl;
std::cout << "Unteroptionen:" << std::endl
<< "\t„--help“\tListe aller Unteroptionen" << std::endl
<< "\t„--version“\tVersion des Programmes" << std::endl
<< "\t„url“\t\tModus um eigene Redirect-Links umzuwandeln." << std::endl
<< "\t„default“\tModus um Links von Serien zu bekommen." << std::endl;
settings->modus = Modus::EXIT;
}
void unterOption_default(Settings *settings, int argc, char ** argv)
{
settings->modus = Modus::DEFAULT_MODUS;
if(settings->modus)
std::cout << "Modus: DEFAULT_MODUS" << std::endl;
int c = 0;
const option long_opts[] = {
{"name", required_argument, nullptr, 'n'},
{"ip-addresse", required_argument, nullptr, 'i'},
{"port", required_argument, nullptr, 'p'},
{"genauer-hoster", required_argument, nullptr, 'g'},
{"languages", required_argument, nullptr, 'l'},
{"output-file", required_argument, nullptr, 'o'},
{"start-episode", required_argument, nullptr, 'e'},
{"stop-episode", required_argument, nullptr, 'E'},
{"start-season", required_argument, nullptr, 's'},
{"stop-season", required_argument, nullptr, 'S'},
{"help", no_argument, nullptr, 'h'},
{"colorless", no_argument, nullptr, 'c'},
{"debug-mode", no_argument, nullptr, 'd'},
{nullptr, no_argument, nullptr, 0}
};
while( ( c = getopt_long (argc, argv, "n:i:p:g:l:o:e:E:s:S:hcd", long_opts, nullptr) ) != -1 ) {
switch(c) {
case 'n':
if(optarg)
settings->name = optarg;
if(settings->modus)
std::cout << "Name: " << settings->name << std::endl;
break;
case 'i':
if(optarg)
settings->proxy_ip = optarg;
if(settings->modus)
std::cout << "Proxy Ip Addresse: " << settings->proxy_ip << std::endl;
break;
case 'p':
if(optarg)
settings->proxy_port = std::to_string( atoi( optarg ) );
if(settings->proxy_port != optarg) {
std::cout << "Invalid Port: " << optarg << std::endl;
settings->modus = Modus::EXIT;
return;
}
if(settings->modus)
std::cout << "Proxy Port: " << settings->proxy_port << std::endl;
break;
case 'g':
if(optarg)
settings->genaueHoster =+ optarg + std::string(",");
if(settings->modus)
std::cout << "Hosterreihenfolge: " << settings->genaueHoster << std::endl;
break;
case 'l':
if(optarg)
settings->languages =+ optarg + std::string(",");
if(settings->modus)
std::cout << "Sprachenreihenfolge: " << settings->languages << std::endl;
break;
case 'o':
if(optarg)
settings->outputFilePath = optarg;
if(settings->modus)
std::cout << "Pfad zu Output-Datei: " << settings->outputFilePath << std::endl;
break;
case 'e':
if(!optarg)
break;
settings->startEpisode = atoi(optarg);
if (std::to_string(settings->startEpisode) != optarg) {
std::cout << "Error: -e [Folge]: '" << optarg << "' ist keine Zahl." << std::endl;
settings->modus = Modus::EXIT;
return;
} else if (settings->startEpisode == 0) {
std::cout << "Error: -e [Folge]: StartEpisode ist 0." << std::endl;
settings->modus = Modus::EXIT;
return;
} else if (settings->startEpisode < 0) {
std::cout << "Error: -e [Folge]: StartEpisode " << settings->startEpisode << " ist kleiner 0." << std::endl;
settings->modus = Modus::EXIT;
return;
}
if(settings->debugMode)
std::cout << "StartEpisode: " << settings->startEpisode << std::endl;
break;
case 'E':
if(!optarg)
break;
settings->stopEpisode = atoi(optarg);
if (std::to_string(settings->stopEpisode) != optarg) {
std::cout << "Error: -E [Folge]: '" << optarg << "' ist keine Zahl." << std::endl;
settings->modus = Modus::EXIT;
return;
} else if (settings->debugMode)
std::cout << "StopEpisode: " << settings->stopEpisode << std::endl;
break;
case 's':
if(!optarg)
break;
settings->startSeason = atoi(optarg);
if (std::to_string(settings->startSeason) != optarg) {
std::cout << "Error: -s [Staffel]: '" << optarg << "' ist keine Zahl." << std::endl;
settings->modus = Modus::EXIT;
return;
} else if (settings->startSeason == 0) {
std::cout << "Error: -s [Staffel]: StartStaffel ist 0." << std::endl;
settings->modus = Modus::EXIT;
return;
} else if (settings->startSeason < 0) {
std::cout << "Error: -s [Staffel]: StartStaffel " << settings->startSeason << " ist kleiner 0." << std::endl;
settings->modus = Modus::EXIT;
return;
}
if(settings->debugMode)
std::cout << "StartStaffel: " << settings->startSeason << std::endl;
break;
case 'S':
if(!optarg)
break;
settings->stopSeason = atoi(optarg);
if (std::to_string(settings->stopSeason) != optarg) {
std::cout << "Error: -S [Staffel]: '" << optarg << "' ist keine Zahl." << std::endl;
settings->modus = Modus::EXIT;
return;
} else if(settings->debugMode)
std::cout << "StopSeason: " << settings->stopSeason << std::endl;
break;
case 'c':
settings->colorless = true;
if(settings->modus)
std::cout << "Farblos: true" << std::endl;
break;
case 'd':
settings->debugMode = true;
if(settings->modus)
std::cout << "Debug Modus: true" << std::endl;
break;
case 'h':
unterOption_default_help(settings, argv[0]);
return;
default:
std::cout << "Aufruf: " << getProgramName(argv[0]) << " default [PARAMETER]" << std::endl;
std::cout << "" << getProgramName(argv[0]) << " default --help“ liefert weitere Informationen." << std::endl;
settings->modus = Modus::EXIT;
return;
}
}
}
void unterOption_default_help(Settings *settings, char * argv0)
{
std::cout << "Usage: " << getProgramName(argv0) << " default [ Parameter & {-n [Name]} ]..." << std::endl
<< "Parameter:" << std::endl << std::endl
<< " > Muss-Parameter:" << std::endl
<< "\t-n [Name], --name [Name]" << std::endl
<< "\t -> Namen der Serie, deren Links du willst." << std::endl
<< std::endl
<< " > Auswahloptionen:" << std::endl
<< "\t-g [Hoster1,Hoster2,...], --genauer-hoster [Hoster1,Hoster2,...]" << std::endl
<< "\t -> Die Namen der Hoster, deren Links du willst. Der wichtigste zuerst." << std::endl
<< "\t-l [GerSub/GerDub/Eng,...], --languages [GerSub/GerDub/Eng,...]" << std::endl
<< "\t -> Die Sprache(n) die du willst. Die wichtigsten zuerst. Default: GerDub,GerSub,Eng" << std::endl
<< std::endl
<< " > Proxy-Optionen:" << std::endl
<< "\t-i [ProxyIPAddresse], --ip-addresse [ProxyIPAddresse]" << std::endl
<< "\t -> Ip Addresse eines Socks5-Proxys angeben. Default: 127.0.0.1" << std::endl
<< "\t-p [ProxyPort], --port [ProxyPort]" << std::endl
<< "\t -> Port eines Socks5-Proxy angeben. Default: 9050" << std::endl
<< std::endl
<< " > Outputoptionen:" << std::endl
<< "\t-o [Pfad], --output-file [Pfad]" << std::endl
<< "\t -> Schreibe Links nach Redirect in diese Datei." << std::endl
<< "\t-c, --colorless" << std::endl
<< "\t -> Keine Farben beim Output verwenden. Default: false" << std::endl
<< "\t-d, --debug-mode" << std::endl
<< "\t -> Debug Nachrichten an. Default: false" << std::endl
<< std::endl
<< " > Durchlaufoptionen:" << std::endl
<< "\t-e [Folge], --start-episode [Folge]" << std::endl
<< "\t -> Das Programm startet mit dieser Folge." << std::endl
<< "\t Default: 1" << std::endl
<< "\t-E [Folge], --stop-episode [Folge]" << std::endl
<< "\t -> - Wenn -S nicht verwendet wird, stoppt das Programm sobald die Folge erreicht wurde." << std::endl
<< "\t - Wenn -S verwendet wird, stoppt es 1. wenn die Folge UND die Staffel erreicht wurden," << std::endl
<< "\t 2. wenn die die Staffel von -S fertig ist. Zum deaktivieren Folge <= 0 verwenden." << std::endl
<< "\t Default: 0" << std::endl
<< "\t-s [Staffel], --start-season [Staffel]" << std::endl
<< "\t -> Das Programm startet mit dieser Staffel." << std::endl
<< "\t Default: 1" << std::endl
<< "\t-S [Staffel], --stop-season [Staffel]" << std::endl
<< "\t -> Das Programm stopt mit dieser Staffel. Beachte Verwendung mit -E." << std::endl
<< "\t Zum deaktivieren Staffel <= 0 verwenden. Default: 0" << std::endl
<< std::endl
<< " > Help-Optionen" << std::endl
<< "\t-h, --help" << std::endl;
settings->modus = Modus::EXIT;
}
void unterOption_url(Settings *settings, int argc, char **argv)
{
settings->modus = Modus::DIRECT_LINK_MODUS;
if(settings->modus)
std::cout << "Modus: DIRECT_LINK_MODUS" << std::endl;
int c = 0;
const option long_opts[] = {
{"url", required_argument, nullptr, 'u'},
{"ip-addresse", required_argument, nullptr, 'i'},
{"port", required_argument, nullptr, 'p'},
{"output-file", required_argument, nullptr, 'o'},
{"help", no_argument, nullptr, 'h'},
{"colorless", no_argument, nullptr, 'c'},
{"debug-mode", no_argument, nullptr, 'd'},
{nullptr, no_argument, nullptr, 0}
};
while( ( c = getopt_long (argc, argv, "u:i:p:o:hcd", long_opts, nullptr) ) != -1 ) {
switch(c) {
case 'u':
if(optarg)
settings->name = optarg;
if(settings->modus)
std::cout << "Urls: " << settings->name << std::endl;
break;
case 'i':
if(optarg)
settings->proxy_ip = optarg;
if(settings->modus)
std::cout << "Proxy Ip Addresse: " << settings->proxy_ip << std::endl;
break;
case 'p':
if(optarg)
settings->proxy_port = std::to_string( atoi( optarg ) );
if(settings->proxy_port != optarg) {
std::cout << "Invalid Port: " << optarg << std::endl;
settings->modus = Modus::EXIT;
return;
}
if(settings->modus)
std::cout << "Proxy Port: " << settings->proxy_port << std::endl;
break;
case 'o':
if(optarg)
settings->outputFilePath = optarg;
if(settings->modus)
std::cout << "Pfad zu Output-Datei: " << settings->outputFilePath << std::endl;
break;
case 'c':
settings->colorless = true;
if(settings->modus)
std::cout << "Farblos: true" << std::endl;
break;
case 'd':
settings->debugMode = true;
if(settings->modus)
std::cout << "Debug Modus: true" << std::endl;
break;
case 'h':
unterOption_url_help(settings, argv[0]);
return;
default:
std::cout << "Aufruf: " << getProgramName(argv[0]) << " url [PARAMETER]" << std::endl;
std::cout << "" << getProgramName(argv[0]) << " url --help“ liefert weitere Informationen." << std::endl;
settings->modus = Modus::EXIT;
return;
}
}
}
void unterOption_url_help(Settings *settings, char * argv0)
{
std::cout << "Usage: " << getProgramName(argv0) << " url [ Parameter & {-u [Url]} ]..." << std::endl
<< "Parameter" << std::endl
<< "\t-u [Url1,Url2,...], \t--url [Url1,Url2,...]" << std::endl
<< "\t-i [ProxyIPAddresse], \t--ip-addresse [ProxyIPAddresse] Default: 127.0.0.1" << std::endl
<< "\t-p [ProxyPort], \t--port [ProxyPort]\t\t Default: 9050" << std::endl
<< "\t-o [Pfad], \t\t--output-file [Pfad]" << std::endl
<< "\t-c, \t\t\t--colorless\t\t\t Default: false" << std::endl
<< "\t-d, \t\t\t--debug-mode\t\t\t Default: false" << std::endl
<< "\t -> Debug Nachrichten an." << std::endl
<< "\t-h, \t\t\t--help" << std::endl;
settings->modus = Modus::EXIT;
}
std::string getProgramName(char *argv0)
{
return std::string(argv0).erase(0, ( (std::string(argv0).find_last_of("/\\") != std::string::npos ) ? std::string(argv0).find_last_of("/\\") +1 : 0 ) );
}
int compare(std::string All_Options_with_komma_between, std::string input)
{
std::istringstream iStrStream( All_Options_with_komma_between + "\n");
std::string line;
int allFounds = 0;
while (getline(iStrStream, line).good())
if(strncmp(line.c_str(), input.c_str(), input.length()) == 0) {
allFounds++;
//std::cout << "Unteroption '" << input << "' stimmt mit '" << line << "' überein." << std::endl;
}
return allFounds;
}