cmFileTimeCache.cxx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmFileTimeCache.h"
  4. #include <string>
  5. #include <unordered_map>
  6. #include <utility>
  7. cmFileTimeCache::cmFileTimeCache() = default;
  8. cmFileTimeCache::~cmFileTimeCache() = default;
  9. bool cmFileTimeCache::Load(std::string const& fileName, cmFileTime& fileTime)
  10. {
  11. // Use the stored time if available.
  12. {
  13. auto fit = this->Cache.find(fileName);
  14. if (fit != this->Cache.end()) {
  15. fileTime = fit->second;
  16. return true;
  17. }
  18. }
  19. // Read file time from OS
  20. if (!fileTime.Load(fileName)) {
  21. return false;
  22. }
  23. // Store file time in cache
  24. this->Cache[fileName] = fileTime;
  25. return true;
  26. }
  27. bool cmFileTimeCache::Remove(std::string const& fileName)
  28. {
  29. return (this->Cache.erase(fileName) != 0);
  30. }
  31. bool cmFileTimeCache::Compare(std::string const& f1, std::string const& f2,
  32. int* result)
  33. {
  34. // Get the modification time for each file.
  35. cmFileTime ft1;
  36. cmFileTime ft2;
  37. if (this->Load(f1, ft1) && this->Load(f2, ft2)) {
  38. // Compare the two modification times.
  39. *result = ft1.Compare(ft2);
  40. return true;
  41. }
  42. // No comparison available. Default to the same time.
  43. *result = 0;
  44. return false;
  45. }
  46. bool cmFileTimeCache::DifferS(std::string const& f1, std::string const& f2)
  47. {
  48. // Get the modification time for each file.
  49. cmFileTime ft1;
  50. cmFileTime ft2;
  51. if (this->Load(f1, ft1) && this->Load(f2, ft2)) {
  52. // Compare the two modification times.
  53. return ft1.DifferS(ft2);
  54. }
  55. // No comparison available. Default to different times.
  56. return true;
  57. }