From 7ae7f4ed83616794cb8ac09d975c99a48215cd10 Mon Sep 17 00:00:00 2001 From: Markus Date: Thu, 17 Oct 2019 18:10:13 +0200 Subject: [PATCH] v3.3.1: better VersionCheck --- pageManager.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ pageManager.h | 3 +++ parameterManager.h | 3 ++- programManager.cpp | 27 ++++++++++++++++----------- 4 files changed, 62 insertions(+), 12 deletions(-) diff --git a/pageManager.cpp b/pageManager.cpp index b056c6d..f92187d 100644 --- a/pageManager.cpp +++ b/pageManager.cpp @@ -383,6 +383,47 @@ std::string PageManager::getExePath() return std::string(wstr.begin(), wstr.end()); #endif +} + +int PageManager::compareVersions(std::string Version1, std::string Version2) +{ + std::string StringList[2] = { ( Version1 + "." ), ( Version2 + "." ) }; + int Versions[2][3]; + size_t pos; + + //Für beide Versionen: + for (int n = 0; n < 2; ++n) { + //Für 3 Punkte: + for (int i = 0; i < 3; ++i) { + //Wenn kein Punkt gefunden werden konnte => Error: + if( (pos = StringList[n].find(".")) == std::string::npos) { + std::cout << " => Error: Ungültige ProgrammVersion: '" << ( (n == 0) ? Version1 : Version2 ) << "'" << std::endl; + return -1; + //wenn punkt gefunden werden konnte + } else { + //Wenn der Teilstring keine Zahl ist error: + if( !isNumber(StringList[n].substr(0, pos)) ) { + std::cout << " => Error: Ungültige ProgrammVersion: (Keine Zahl): '" << ( (n == 0) ? Version1 : Version2 ) << "'" << std::endl; + return -1; + } else { + //Sonst speicher Zahl in array und entferne den anfang des strings + Versions[n][i] = atoi(StringList[n].substr(0, pos).c_str()); + StringList[n].erase(0, pos + 1); + } + } + } + } + + for (int i = 0; i < 3; ++i) { + if(Versions[0][i] > Versions[1][i]) { + return 2; + } else if(Versions[0][i] < Versions[1][i]) { + return 1; + } + } + return 0; + + } int PageManager::writeToFile(std::string path, std::string text) diff --git a/pageManager.h b/pageManager.h index 866b02f..4ba549b 100644 --- a/pageManager.h +++ b/pageManager.h @@ -3,6 +3,7 @@ #include "accountManager.h" #include "curl/curl.h" +#include "parameterManager.h" // for isNumber #include #include // sleep @@ -45,6 +46,8 @@ public: size_t getDate(); std::string getExePath(); + int compareVersions(std::string Version1, std::string Version2); + int writeToFile(std::string path, std::string text); const std::string UrlPraefix = "https://s.to/serie/stream/"; diff --git a/parameterManager.h b/parameterManager.h index f10ff43..56d2bff 100644 --- a/parameterManager.h +++ b/parameterManager.h @@ -34,6 +34,7 @@ enum Modus { }; + struct Settings { Settings() {} Settings(std::string name) : name(name) {} @@ -60,7 +61,7 @@ struct Settings { proxy_ip = "127.0.0.1", languages = "GerDub,GerSub,Eng", genaueHoster = "", - version = "3.3.0", + version = "3.3.1", defaultFileVersion="1.6", outputFilePath = "", default_checkPath = "", diff --git a/programManager.cpp b/programManager.cpp index ea58b60..d0f9ea7 100644 --- a/programManager.cpp +++ b/programManager.cpp @@ -970,7 +970,7 @@ int ProgramManager::updateModus(Settings *settings) std::cout << " => INFO: Suche nach der neusten Version..." << std::endl; std::string html = pageManager.getServerRequest(settings->VersionFileUrl).html; - if( html == "-1" || html == "") { + if( html == "-1" || html == "" || html.find("\n") == std::string::npos) { // Wenn debug mode print error if(settings->debugMode) std::cerr << " => Error: Download newst Version failed." << std::endl; @@ -982,15 +982,10 @@ int ProgramManager::updateModus(Settings *settings) html = pageManager.replace(html, "\r", ""); } + // Get newst Version - std::stringstream strstream(html+ "\n"); - html.clear(); - std::string neusteVersion; - std::getline(strstream, neusteVersion); - if( !isNumber( (pageManager.replace(neusteVersion, ".", "")) ) ) { - std::cout << " => Error: Gedownloadete neuste Version ist ungültig: '" << neusteVersion << "'" << std::endl; - return 2; - } + std::string neusteVersion = html.substr(0, html.find("\n")); + html.erase(0, html.find("\n") + 1); //Gib Versionenunterschie aus std::cout << " => INFO: Jetzige Version: '" << settings->version << "' - Neueste Version: '" << neusteVersion << "'" << std::endl; @@ -998,16 +993,26 @@ int ProgramManager::updateModus(Settings *settings) //Print INFOs std::cout << " => Infos (zur Version): " << std::endl; std::string line; + std::stringstream strstream(html+ "\n"); while (std::getline(strstream, line).good()) { if(line != "") std::cout << " > " << line << std::endl; } - //Vergleiche Versionen: - if( atoi(pageManager.replace(neusteVersion, ".", "").c_str()) <= atoi(pageManager.replace(settings->version, ".", "").c_str()) ) { + switch (pageManager.compareVersions(settings->version, neusteVersion)) { + case 0: std::cout << "\n => Du benützt bereits die neuste Release-Version." << std::endl; return 0; + case 1: + //Diese Version ist klleienr als aktuelle Version => Updatete + break; + case 2: + std::cout << "\n => Du benützt bereits eine Aktuellere Version als die neuste Release-Version." << std::endl; + return 0; + default: + //Error + return 2; } //lege pfad zum speichern fest