diff options
author | Brad King <brad.king@kitware.com> | 2005-02-17 16:28:13 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2005-02-17 16:28:13 (GMT) |
commit | e2ec4a18453f3bdc1ab826a618a2202c5e9e9dec (patch) | |
tree | e3da86475a22bb5fadac55c4fe258a244603bb82 /Source/kwsys/SystemTools.cxx | |
parent | 508cff55f6a26fce3c2bc26b3d00de9c6f96c6e6 (diff) | |
download | CMake-e2ec4a18453f3bdc1ab826a618a2202c5e9e9dec.zip CMake-e2ec4a18453f3bdc1ab826a618a2202c5e9e9dec.tar.gz CMake-e2ec4a18453f3bdc1ab826a618a2202c5e9e9dec.tar.bz2 |
ENH: Adding kwsys::SystemTools::FileTimeCompare method to compare file modification times with the highest resolution possible on the file system.
Diffstat (limited to 'Source/kwsys/SystemTools.cxx')
-rw-r--r-- | Source/kwsys/SystemTools.cxx | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/Source/kwsys/SystemTools.cxx b/Source/kwsys/SystemTools.cxx index 5968bd6..9481d02 100644 --- a/Source/kwsys/SystemTools.cxx +++ b/Source/kwsys/SystemTools.cxx @@ -693,6 +693,90 @@ bool SystemTools::FileExists(const char* filename) } +bool SystemTools::FileTimeCompare(const char* f1, const char* f2, + int* result) +{ + // Default to same time. + *result = 0; +#if !defined(_WIN32) || defined(__CYGWIN__) + // POSIX version. Use stat function to get file modification time. + struct stat s1; + if(stat(f1, &s1) != 0) + { + return false; + } + struct stat s2; + if(stat(f2, &s2) != 0) + { + return false; + } +# if KWSYS_STAT_HAS_ST_MTIM + // Compare using nanosecond resolution. + if(s1.st_mtim.tv_sec < s2.st_mtim.tv_sec) + { + *result = -1; + } + else if(s1.st_mtim.tv_sec > s2.st_mtim.tv_sec) + { + *result = 1; + } + else if(s1.st_mtim.tv_nsec < s2.st_mtim.tv_nsec) + { + *result = -1; + } + else if(s1.st_mtim.tv_nsec > s2.st_mtim.tv_nsec) + { + *result = 1; + } +# else + // Compare using 1 second resolution. + if(s1.st_mtime < s2.st_mtime) + { + *result = -1; + } + else if(s1.st_mtime > s2.st_mtime) + { + *result = 1; + } +# endif +#else + // Windows version. Create file handles and get the modification times. + HANDLE hf1 = CreateFile(f1, GENERIC_READ, FILE_SHARE_READ, + NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, + NULL); + if(hf1 == INVALID_HANDLE_VALUE) + { + return false; + } + FILETIME tf1; + if(!GetFileTime(hf1, 0, 0, &tf1)) + { + CloseHandle(hf1); + return false; + } + CloseHandle(hf1); + HANDLE hf2 = CreateFile(f2, GENERIC_READ, FILE_SHARE_READ, + NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, + NULL); + if(hf2 == INVALID_HANDLE_VALUE) + { + return false; + } + FILETIME tf2; + if(!GetFileTime(hf2, 0, 0, &tf2)) + { + CloseHandle(hf2); + return false; + } + CloseHandle(hf2); + + // Compare the file times using resolution provided by system call. + *result = (int)CompareFileTime(&tf1, &tf2); +#endif + return true; +} + + // Return a capitalized string (i.e the first letter is uppercased, all other // are lowercased) kwsys_stl::string SystemTools::Capitalized(const kwsys_stl::string& s) |