summaryrefslogtreecommitdiffstats
path: root/Source/cmFileTimeComparison.cxx
blob: 5022cab7a099db2319d3cb0ead97724de8422473 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
#include "cmFileTimeComparison.h"

#include <string>
#include <unordered_map>
#include <utility>

class cmFileTimeComparisonInternal
{
public:
  inline bool Load(std::string const& fname, cmFileTime& ftm);
  inline bool FileTimeCompare(std::string const& f1, std::string const& f2,
                              int* result);

  bool FileTimesDiffer(std::string const& f1, std::string const& f2);

private:
  typedef std::unordered_map<std::string, cmFileTime> FileStatsMap;
  FileStatsMap Files;
};

bool cmFileTimeComparisonInternal::Load(std::string const& fname,
                                        cmFileTime& ftm)
{
  // Use the stored time if available.
  {
    auto fit = this->Files.find(fname);
    if (fit != this->Files.end()) {
      ftm = fit->second;
      return true;
    }
  }
  // Read file time from OS
  if (!ftm.Load(fname)) {
    return false;
  }
  // Store file time in cache
  this->Files[fname] = ftm;
  return true;
}

cmFileTimeComparison::cmFileTimeComparison()
{
  this->Internals = new cmFileTimeComparisonInternal;
}

cmFileTimeComparison::~cmFileTimeComparison()
{
  delete this->Internals;
}

bool cmFileTimeComparison::Load(std::string const& fileName,
                                cmFileTime& fileTime)
{
  return this->Internals->Load(fileName, fileTime);
}

bool cmFileTimeComparison::FileTimeCompare(std::string const& f1,
                                           std::string const& f2, int* result)
{
  return this->Internals->FileTimeCompare(f1, f2, result);
}

bool cmFileTimeComparison::FileTimesDiffer(std::string const& f1,
                                           std::string const& f2)
{
  return this->Internals->FileTimesDiffer(f1, f2);
}

bool cmFileTimeComparisonInternal::FileTimeCompare(std::string const& f1,
                                                   std::string const& f2,
                                                   int* result)
{
  // Get the modification time for each file.
  cmFileTime ft1, ft2;
  if (this->Load(f1, ft1) && this->Load(f2, ft2)) {
    // Compare the two modification times.
    *result = ft1.Compare(ft2);
    return true;
  }
  // No comparison available.  Default to the same time.
  *result = 0;
  return false;
}

bool cmFileTimeComparisonInternal::FileTimesDiffer(std::string const& f1,
                                                   std::string const& f2)
{
  // Get the modification time for each file.
  cmFileTime ft1, ft2;
  if (this->Load(f1, ft1) && this->Load(f2, ft2)) {
    // Compare the two modification times.
    return ft1.DifferS(ft2);
  }
  // No comparison available.  Default to different times.
  return true;
}