From bcc582f91200e78a56834e20a6eca59b80112b66 Mon Sep 17 00:00:00 2001 From: twanvl Date: Sun, 3 Oct 2010 16:47:38 +0000 Subject: [PATCH] added 'file_modified_time' function, which should be faster than using wxFileName git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1519 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/util/file_utils.cpp | 17 +++++++++++++++++ src/util/file_utils.hpp | 9 +++++++-- src/util/io/package.cpp | 7 +++---- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/util/file_utils.cpp b/src/util/file_utils.cpp index db8bed05..08d7a5a9 100644 --- a/src/util/file_utils.cpp +++ b/src/util/file_utils.cpp @@ -10,6 +10,8 @@ #include #include #include +#include +#include DECLARE_TYPEOF_COLLECTION(String); @@ -88,6 +90,21 @@ bool resolve_filename_conflicts(wxFileName& fn, FilenameConflicts conflicts, set } } +// ----------------------------------------------------------------------------- : File info + +time_t file_modified_time(const String& path) { + // Note: wxFileName also provides a function for this, but that is very slow. + struct stat statbuf; + if (stat(path.mb_str(), &statbuf) != 0) { + if (errno == ENOENT) { + return 0; + } else { + throw InternalError(_("could not stat ") + path); + } + } + return statbuf.st_mtime; +} + // ----------------------------------------------------------------------------- : Directories bool create_parent_dirs(const String& file) { diff --git a/src/util/file_utils.hpp b/src/util/file_utils.hpp index 23687b27..2bec4b98 100644 --- a/src/util/file_utils.hpp +++ b/src/util/file_utils.hpp @@ -4,8 +4,8 @@ //| License: GNU General Public License 2 or later (see file COPYING) | //+----------------------------------------------------------------------------+ -#ifndef HEADER_UTIL_FILE_UTILS -#define HEADER_UTIL_FILE_UTILS +#ifndef HEADER_UTIL_FILE_UTILS +#define HEADER_UTIL_FILE_UTILS // ----------------------------------------------------------------------------- : Includes @@ -32,6 +32,11 @@ String clean_filename(const String& name); /** Returns true if the filename should be used, false if failed. */ bool resolve_filename_conflicts(wxFileName& fn, FilenameConflicts conflicts, set& used); +// ----------------------------------------------------------------------------- : File info + +/// Get the last modified time of a file +time_t file_modified_time(const String& name); + // ----------------------------------------------------------------------------- : Removing and renaming /// Ensure that the parent directories of the given filename exist diff --git a/src/util/io/package.cpp b/src/util/io/package.cpp index 5ab9dde2..682bbdeb 100644 --- a/src/util/io/package.cpp +++ b/src/util/io/package.cpp @@ -349,12 +349,11 @@ void Package::openSubdir(const String& name) { String f; // filename for(bool ok = d.GetFirst(&f, wxEmptyString, wxDIR_FILES | wxDIR_HIDDEN) ; ok ; ok = d.GetNext(&f)) { if (ignore_file(f)) continue; - // add file + // add file to list of known files addFile(name + f); // get modified time - wxFileName fn(filename + _("/") + name + f); - wxDateTime mod; - if (fn.GetTimes(0, &mod, 0) && mod > modified) modified = mod; + wxDateTime file_time = file_modified_time(filename + _("/") + name + f); + modified = max(modified,file_time); } // find subdirs for(bool ok = d.GetFirst(&f, wxEmptyString, wxDIR_DIRS | wxDIR_HIDDEN) ; ok ; ok = d.GetNext(&f)) {