aboutsummaryrefslogtreecommitdiff
path: root/build/path.ixx
blob: 95608e95e02584112a761f44fdf145c5fde5d00e (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// file      : build/path.ixx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#ifdef _WIN32
#  include <cctype>  // std::tolower
#  include <cwctype> // std::towlower
#endif

namespace build
{
#ifdef _WIN32
  template <>
  inline char path_traits<char>::
  tolower (char c)
  {
    return std::tolower (c);
  }

  template <>
  inline wchar_t path_traits<wchar_t>::
  tolower (wchar_t c)
  {
    return std::towlower (c);
  }
#endif

  template <typename C>
  inline bool basic_path<C>::
  absolute () const
  {
#ifdef _WIN32
    return path_.size () > 1 && path_[1] == ':';
#else
    return !path_.empty () && traits::is_separator (path_[0]);
#endif
  }

  template <typename C>
  inline bool basic_path<C>::
  root () const
  {
#ifdef _WIN32
    return path_.size () == 2 && path_[1] == ':';
#else
    return path_.size () == 1 && traits::is_separator (path_[0]);
#endif
  }

  template <typename C>
  inline bool basic_path<C>::
  sub (const basic_path& p) const
  {
    size_type n (p.path_.size ());

    if (n == 0)
      return true;

    size_type m (path_.size ());

    // The second condition guards against the /foo-bar vs /foo case.
    //
    return m >= n && path_.compare (0, n, p.path_) == 0 &&
      (traits::is_separator (p.path_.back ()) || // p ends with a separator
       m == n                                 || // *this == p
       traits::is_separator (path_[n]));         // next char is a separator
  }

  template <typename C>
  inline bool basic_path<C>::
  sup (const basic_path& p) const
  {
    size_type n (p.path_.size ());

    if (n == 0)
      return true;

    size_type m (path_.size ());

    // The second condition guards against the /foo-bar vs bar case.
    //
    return m >= n && path_.compare (m - n, n, p.path_) == 0 &&
      (m == n                                   || // *this == p
       traits::is_separator (path_[m - n - 1]));   // prev char is a separator
  }

  template <typename C>
  inline basic_path<C>& basic_path<C>::
  complete ()
  {
    if (relative ())
      *this = current () / *this;

    return *this;
  }

  template <typename C>
  inline basic_path<C> basic_path<C>::
  root_directory () const
  {
    return absolute ()
#ifdef _WIN32
      ? path (path_, 2)
#else
      ? path ("/")
#endif
      : path ();
  }

  template <typename C>
  inline basic_path<C> basic_path<C>::
  base () const
  {
    size_type p (traits::find_extension (path_));
    return p != string_type::npos ? basic_path (path_.c_str (), p) : *this;
  }

  template <typename C>
  inline const C* basic_path<C>::
  extension () const
  {
    size_type p (traits::find_extension (path_));
    return p != string_type::npos ? path_.c_str () + p + 1 : nullptr;
  }

#ifndef _WIN32
  template <typename C>
  inline typename basic_path<C>::string_type basic_path<C>::
  posix_string () const
  {
    return string ();
  }
#endif
}