diff --git a/src/fileio/fileio.hpp b/src/fileio/fileio.hpp index 1ea9f448..921ed693 100644 --- a/src/fileio/fileio.hpp +++ b/src/fileio/fileio.hpp @@ -9,8 +9,10 @@ #ifndef BOE_FILEIO_HPP #define BOE_FILEIO_HPP +#include #include #include +#include #include #include #include @@ -27,6 +29,10 @@ bool load_scenario(fs::path file_to_load, cScenario& scenario, bool only_header fs::path nav_get_or_decode_party(); fs::path nav_put_or_temp_party(fs::path def = ""); +const std::set save_extensions = {".exg", ".boe", ".SAV", ".mac"}; +// Return a directory's files sorted by last modified time +std::vector> sorted_file_mtimes(fs::path dir, std::set valid_extensions = save_extensions); + bool load_party(fs::path file_to_load, cUniverse& univ); bool save_party(cUniverse& univ, bool save_as = false); diff --git a/src/fileio/fileio_party.cpp b/src/fileio/fileio_party.cpp index 3cf8b160..d4b4d9e9 100644 --- a/src/fileio/fileio_party.cpp +++ b/src/fileio/fileio_party.cpp @@ -552,3 +552,20 @@ bool save_party(cUniverse& univ, bool save_as) { if(univ.file.empty()) return false; return save_party_const(univ, save_as); } + +static bool compare_mtime(std::pair a, std::pair b) { + return std::difftime(a.second, b.second) > 0; +} + +std::vector> sorted_file_mtimes(fs::path dir, std::set valid_extensions){ + std::vector> files; + for(fs::directory_iterator it{dir}; it != fs::directory_iterator{}; ++it) { + fs::path file = *it; + if(valid_extensions.count(file.extension())){ + files.push_back(std::make_pair(file, last_write_time(file))); + } + } + + std::sort(files.begin(), files.end(), compare_mtime); + return files; +} \ No newline at end of file