CMake/Source/cmFileTimeComparison.cxx

187 lines
5.1 KiB
C++
Raw Normal View History

2005-10-12 21:36:07 +04:00
/*=========================================================================
Program: CMake - Cross-Platform Makefile Generator
Module: $RCSfile$
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
2005-10-12 21:36:07 +04:00
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "cmFileTimeComparison.h"
// Use a hash table to avoid duplicate file time checks from disk.
2005-10-12 21:36:07 +04:00
#if defined(CMAKE_BUILD_WITH_CMAKE)
# include <cmsys/hash_map.hxx>
#endif
// Use a platform-specific API to get file times efficiently.
2005-10-12 21:36:07 +04:00
#if !defined(_WIN32) || defined(__CYGWIN__)
# define cmFileTimeComparison_Type struct stat
2005-10-12 21:50:25 +04:00
# include <ctype.h>
# include <sys/stat.h>
2005-10-12 21:36:07 +04:00
#else
# define cmFileTimeComparison_Type FILETIME
2005-10-12 21:50:25 +04:00
# include <windows.h>
2005-10-12 21:36:07 +04:00
#endif
//----------------------------------------------------------------------------
2005-10-12 21:36:07 +04:00
class cmFileTimeComparisonInternal
{
public:
// Internal comparison method.
2005-10-12 21:36:07 +04:00
inline bool FileTimeCompare(const char* f1, const char* f2, int* result);
private:
#if defined(CMAKE_BUILD_WITH_CMAKE)
// Use a hash table to efficiently map from file name to modification time.
2005-10-12 21:36:07 +04:00
class HashString
{
public:
size_t operator()(const cmStdString& s) const
{
return h(s.c_str());
}
cmsys::hash<const char*> h;
};
typedef cmsys::hash_map<cmStdString,
cmFileTimeComparison_Type, HashString> FileStatsMap;
2005-10-12 21:36:07 +04:00
FileStatsMap Files;
#endif
// Internal methods to lookup and compare modification times.
2005-10-12 21:36:07 +04:00
inline bool Stat(const char* fname, cmFileTimeComparison_Type* st);
inline int Compare(cmFileTimeComparison_Type* st1,
cmFileTimeComparison_Type* st2);
2005-10-12 21:36:07 +04:00
};
//----------------------------------------------------------------------------
bool cmFileTimeComparisonInternal::Stat(const char* fname,
cmFileTimeComparison_Type* st)
2005-10-12 21:36:07 +04:00
{
#if defined(CMAKE_BUILD_WITH_CMAKE)
// Use the stored time if available.
cmFileTimeComparisonInternal::FileStatsMap::iterator fit =
this->Files.find(fname);
2005-10-12 21:36:07 +04:00
if ( fit != this->Files.end() )
{
*st = fit->second;
return true;
2005-10-12 21:36:07 +04:00
}
#endif
2005-10-12 21:36:07 +04:00
#if !defined(_WIN32) || defined(__CYGWIN__)
// POSIX version. Use the stat function.
2005-10-12 21:36:07 +04:00
int res = ::stat(fname, st);
if ( res != 0 )
{
return false;
}
#else
// Windows version. Get the modification time from extended file attributes.
WIN32_FILE_ATTRIBUTE_DATA fdata;
if(!GetFileAttributesEx(fname, GetFileExInfoStandard, &fdata))
2005-10-12 21:36:07 +04:00
{
return false;
}
// Copy the file time to the output location.
*st = fdata.ftLastWriteTime;
2005-10-12 21:36:07 +04:00
#endif
2005-10-12 21:36:07 +04:00
#if defined(CMAKE_BUILD_WITH_CMAKE)
// Store the time for future use.
2005-10-12 21:36:07 +04:00
this->Files[fname] = *st;
#endif
2005-10-12 21:36:07 +04:00
return true;
}
//----------------------------------------------------------------------------
2005-10-12 21:36:07 +04:00
cmFileTimeComparison::cmFileTimeComparison()
{
m_Internals = new cmFileTimeComparisonInternal;
}
//----------------------------------------------------------------------------
2005-10-12 21:36:07 +04:00
cmFileTimeComparison::~cmFileTimeComparison()
{
delete m_Internals;
}
//----------------------------------------------------------------------------
2005-10-12 21:36:07 +04:00
bool cmFileTimeComparison::FileTimeCompare(const char* f1, const char* f2, int* result)
{
return m_Internals->FileTimeCompare(f1, f2, result);
}
//----------------------------------------------------------------------------
2005-10-12 21:36:07 +04:00
int cmFileTimeComparisonInternal::Compare(cmFileTimeComparison_Type* s1, cmFileTimeComparison_Type* s2)
{
#if !defined(_WIN32) || defined(__CYGWIN__)
# if cmsys_STAT_HAS_ST_MTIM
2005-10-12 21:36:07 +04:00
// Compare using nanosecond resolution.
if(s1->st_mtim.tv_sec < s2->st_mtim.tv_sec)
{
return -1;
}
else if(s1->st_mtim.tv_sec > s2->st_mtim.tv_sec)
{
return 1;
}
else if(s1->st_mtim.tv_nsec < s2->st_mtim.tv_nsec)
{
return -1;
}
else if(s1->st_mtim.tv_nsec > s2->st_mtim.tv_nsec)
{
return 1;
}
# else
// Compare using 1 second resolution.
if(s1->st_mtime < s2->st_mtime)
{
return -1;
}
else if(s1->st_mtime > s2->st_mtime)
{
return 1;
}
# endif
// Files have the same time.
return 0;
2005-10-12 21:36:07 +04:00
#else
// Compare using system-provided function.
2005-10-12 21:36:07 +04:00
return (int)CompareFileTime(s1, s2);
#endif
}
//----------------------------------------------------------------------------
bool cmFileTimeComparisonInternal::FileTimeCompare(const char* f1,
const char* f2,
int* result)
2005-10-12 21:36:07 +04:00
{
// Get the modification time for each file.
2005-10-12 21:36:07 +04:00
cmFileTimeComparison_Type s1;
cmFileTimeComparison_Type s2;
if(this->Stat(f1, &s1) &&
this->Stat(f2, &s2))
2005-10-12 21:36:07 +04:00
{
// Compare the two modification times.
*result = this->Compare(&s1, &s2);
return true;
2005-10-12 21:36:07 +04:00
}
else
2005-10-12 21:36:07 +04:00
{
// No comparison available. Default to the same time.
*result = 0;
2005-10-12 21:36:07 +04:00
return false;
}
}