File size: 1,751 Bytes
2edf277
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// FCS Glass - src/core/settings.cpp
//
// Implements: J8 rules 1 and 3 (the pure half). See src/core/settings.h.
#include "core/settings.h"

namespace fcs {
namespace core {
namespace {

// Index just past the last separator, or npos when the path has none. A path
// with no separator is all filename, so the "directory" is the relative case -
// which still compares equal to another bare filename, and that is correct.
std::size_t LastSeparator(const std::string& p) {
  const std::size_t slash = p.find_last_of("/\\");
  return slash == std::string::npos ? std::string::npos : slash + 1;
}

}  // namespace

// REQ J8 - rule 1.
std::string TempPathFor(const std::string& target) { return target + ".tmp"; }

// REQ J8 - rule 1, as a checkable predicate.
bool SameDirectory(const std::string& a, const std::string& b) {
  const std::size_t ia = LastSeparator(a);
  const std::size_t ib = LastSeparator(b);
  // npos means "no separator", i.e. the whole string is a filename. Testing
  // `== 0` here instead - which is what this used to do - never matches, so two
  // bare filenames fell through and compared their ENTIRE strings as if those
  // were directories. T-20 caught it with "ui.json.tmp" vs "other.tmp".
  const bool a_bare = (ia == std::string::npos);
  const bool b_bare = (ib == std::string::npos);
  // Both bare: both relative to the current directory, so the same directory.
  if (a_bare && b_bare) return true;
  // One bare and one absolute is not comparable, and claiming they match would
  // be the exact false "same volume" answer rule 1 exists to prevent.
  if (a_bare != b_bare) return false;
  return a.compare(0, ia, b, 0, ib) == 0;
}

}  // namespace core
}  // namespace fcs