From 22821377a401696e4793571a9a27e7b5e99b2c3a Mon Sep 17 00:00:00 2001 From: TwinFan Date: Tue, 13 Feb 2024 22:50:09 +0100 Subject: [PATCH] Fix/OpenSky Master File: Cleanup old database files (Lin/Max only) --- Include/DataRefs.h | 6 +++--- Include/LTOpenSky.h | 1 + Include/LiveTraffic.h | 4 ++++ Src/LTMain.cpp | 19 +++++++++++++++++++ Src/LTOpenSky.cpp | 40 ++++++++++++++++++++++++++++++++++++---- docs/readme.html | 5 ++++- 6 files changed, 67 insertions(+), 8 deletions(-) diff --git a/Include/DataRefs.h b/Include/DataRefs.h index 33a0039..09ef0ef 100644 --- a/Include/DataRefs.h +++ b/Include/DataRefs.h @@ -1036,9 +1036,9 @@ class DataRefs int DecNumAc(); // Get XP System Path - inline std::string GetXPSystemPath() const { return XPSystemPath; } - inline std::string GetLTPluginPath() const { return LTPluginPath; } - inline std::string GetDirSeparator() const { return DirSeparator; } + const std::string& GetXPSystemPath() const { return XPSystemPath; } + const std::string& GetLTPluginPath() const { return LTPluginPath; } + const std::string& GetDirSeparator() const { return DirSeparator; } // Load/save config file (basically a subset of LT dataRefs) bool LoadConfigFile(); diff --git a/Include/LTOpenSky.h b/Include/LTOpenSky.h index 392c7c2..43a122b 100644 --- a/Include/LTOpenSky.h +++ b/Include/LTOpenSky.h @@ -103,6 +103,7 @@ constexpr size_t OPSKY_MD_TEXT_VEHICLE_LEN = 20; ///< length after which cate #define OPSKY_MD_DB_NAME "OpenSky Masterdata File" #define OPSKY_MD_DB_URL "https://opensky-network.org/datasets/metadata/" +#define OPSKY_MD_DB_FILE_BEGIN "aircraft-database-complete-" #define OPSKY_MD_DB_FILE "aircraft-database-complete-%04d-%02d.csv" #define OPSKY_ROUTE_URL "https://opensky-network.org/api/routes?callsign=" diff --git a/Include/LiveTraffic.h b/Include/LiveTraffic.h index 3bf367d..4142085 100644 --- a/Include/LiveTraffic.h +++ b/Include/LiveTraffic.h @@ -220,6 +220,10 @@ bool RemoteFileDownload (const std::string& url, const std::string& path); std::string& str_toupper(std::string& s); /// return a std::string copy converted to uppercase std::string str_toupper_c(const std::string& s); +/// Case-insensitive equal +bool striequal (const std::string& a, const std::string& b); +/// Case-insensitive begins with +bool stribeginwith (const std::string& s, const std::string& begin); // are all chars alphanumeric? bool str_isalnum(const std::string& s); // limits text to m characters, replacing the last ones with ... if too long diff --git a/Src/LTMain.cpp b/Src/LTMain.cpp index e26bf77..add3afb 100644 --- a/Src/LTMain.cpp +++ b/Src/LTMain.cpp @@ -387,6 +387,25 @@ std::string str_toupper_c(const std::string& s) return c; } +// Case-insensitive equal +/// @see https://stackoverflow.com/a/4119881 +bool striequal (const std::string& a, const std::string& b) +{ + return std::equal(a.begin(), a.end(), b.begin(), b.end(), + [](unsigned char x, unsigned char y) + { return std::tolower(x) == std::tolower(y); }); +} + +// Case-insensitive begins with +bool stribeginwith (const std::string& s, const std::string& begin) +{ + if (begin.size() > s.size()) return false; + return std::equal(begin.begin(), begin.end(), s.begin(), + [](unsigned char x, unsigned char y) + { return std::tolower(x) == std::tolower(y); }); +} + + bool str_isalnum(const std::string& s) { return std::all_of(s.cbegin(), s.cend(), [](unsigned char c){return isalnum(c);}); diff --git a/Src/LTOpenSky.cpp b/Src/LTOpenSky.cpp index 70c3343..3244a32 100644 --- a/Src/LTOpenSky.cpp +++ b/Src/LTOpenSky.cpp @@ -25,6 +25,11 @@ // All includes are collected in one header #include "LiveTraffic.h" +#if IBM +#else +#include +#endif + // //MARK: OpenSky // @@ -851,10 +856,8 @@ bool OpenSkyAcMasterFile::TryOpenDbFile (int year, int month) try { // Is the file available already? - std::string filePath = dataRefs.GetLTPluginPath(); - filePath += PATH_RESOURCES; - filePath += '/'; - filePath += fileName; + const std::string fileDir = dataRefs.GetLTPluginPath() + PATH_RESOURCES + '/'; + const std::string filePath = fileDir + fileName; // Just try to open and see what happens fAcDb.open(filePath); @@ -917,6 +920,35 @@ bool OpenSkyAcMasterFile::TryOpenDbFile (int year, int month) // looks good! fAcDb.clear(); + + // Lastly, we remove all _other_ database files given that each takes up 50MB of disk space + // (Can't use XPLMGetDirectoryContents in non-main thread, + // using std::filesystem crashed CURL... + // so we go back to basic POSIX C and native Windows) + { + std::vector vToBeDeleted; +#if IBM +#error Traversing directory to be implemented +#else + DIR *d = nullptr; + struct dirent *dir = nullptr; + d = opendir(fileDir.c_str()); + if (d) { + while ((dir = readdir(d)) != NULL) { + // If begins like a database file but is not the one we just processed + std::string f = dir->d_name; + if (stribeginwith(f, OPSKY_MD_DB_FILE_BEGIN) && + !striequal(f, fileName)) + vToBeDeleted.emplace_back(std::move(f)); + } + closedir(d); + } +#endif + // Now delete what we remembered + for (const std::string& p: vToBeDeleted) + std::remove((fileDir+p).c_str()); + } + return true; } catch (const std::exception& e) { diff --git a/docs/readme.html b/docs/readme.html index e4f40a8..92bf856 100755 --- a/docs/readme.html +++ b/docs/readme.html @@ -147,7 +147,7 @@

v3.5.1

Change log:

    -
  • Not designated a Beta version. No usage time limit.
  • +
  • Not designated a Beta version as v3.5.0 accidently was. No usage time limit.
  • RealTraffic with historic data:
      @@ -156,6 +156,9 @@

      v3.5.1

      historic timestamp when modifying timestamp again.
  • +
  • + OpenSky Master File: Cleanup old database files. +

v3.5.0