Function to get files in folder sorted by mtime
This commit is contained in:
@@ -9,8 +9,10 @@
|
||||
#ifndef BOE_FILEIO_HPP
|
||||
#define BOE_FILEIO_HPP
|
||||
|
||||
#include <ctime>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <SFML/System/InputStream.hpp>
|
||||
#include <boost/filesystem/path.hpp>
|
||||
@@ -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<fs::path> save_extensions = {".exg", ".boe", ".SAV", ".mac"};
|
||||
// Return a directory's files sorted by last modified time
|
||||
std::vector<std::pair<fs::path, std::time_t>> sorted_file_mtimes(fs::path dir, std::set<fs::path> valid_extensions = save_extensions);
|
||||
|
||||
bool load_party(fs::path file_to_load, cUniverse& univ);
|
||||
bool save_party(cUniverse& univ, bool save_as = false);
|
||||
|
||||
|
@@ -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<fs::path, std::time_t> a, std::pair<fs::path, std::time_t> b) {
|
||||
return std::difftime(a.second, b.second) > 0;
|
||||
}
|
||||
|
||||
std::vector<std::pair<fs::path, std::time_t>> sorted_file_mtimes(fs::path dir, std::set<fs::path> valid_extensions){
|
||||
std::vector<std::pair<fs::path, std::time_t>> 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;
|
||||
}
|
Reference in New Issue
Block a user