summaryrefslogtreecommitdiff
path: root/libcutl/cutl/fs/path.txx
blob: e95c890817ff669c33d30212ecb86af3685b44df (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// file      : cutl/fs/path.txx
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#include <vector>

namespace cutl
{
  namespace fs
  {
    template <typename C>
    basic_path<C> basic_path<C>::
    leaf () const
    {
      size_type p (traits::rfind_separator (path_));

      return p != string_type::npos
        ? basic_path (path_.c_str () + p + 1, path_.size () - p - 1)
        : *this;
    }

    template <typename C>
    basic_path<C> basic_path<C>::
    directory () const
    {
      if (root ())
        return basic_path ();

      size_type p (traits::rfind_separator (path_));

      // Include the trailing slash so that we get correct behavior
      // if directory is root.
      //
      return p != string_type::npos
        ? basic_path (path_.c_str (), p + 1)
        : basic_path ();
    }

    template <typename C>
    basic_path<C> basic_path<C>::
    base () const
    {
      size_type i (path_.size ());

      for (; i > 0; --i)
      {
        if (path_[i - 1] == '.')
          break;

        if (traits::is_separator (path_[i - 1]))
        {
          i = 0;
          break;
        }
      }

      // Weed out paths like ".txt" and "/.txt"
      //
      if (i > 1 && !traits::is_separator (path_[i - 2]))
      {
        return basic_path (path_.c_str (), i - 1);
      }
      else
        return *this;
    }

#ifdef _WIN32
    template <typename C>
    typename basic_path<C>::string_type basic_path<C>::
    posix_string () const
    {
      if (absolute ())
        throw invalid_basic_path<C> (path_);

      string_type r (path_);

      // Translate Windows-style separators to the POSIX ones.
      //
      for (size_type i (0), n (r.size ()); i != n; ++i)
        if (r[i] == '\\')
          r[i] = '/';

      return r;
    }
#endif

    template <typename C>
    basic_path<C>& basic_path<C>::
    operator/= (basic_path<C> const& r)
    {
      if (r.absolute ())
        throw invalid_basic_path<C> (r.path_);

      if (path_.empty () || r.path_.empty ())
      {
        path_ += r.path_;
        return *this;
      }

      if (!traits::is_separator (path_[path_.size () - 1]))
        path_ += traits::directory_separator;

      path_ += r.path_;

      return *this;
    }

    template <typename C>
    basic_path<C>& basic_path<C>::
    normalize ()
    {
      if (empty ())
        return *this;

      bool abs (absolute ());

      typedef std::vector<string_type> paths;
      paths ps;

      for (size_type b (0), e (traits::find_separator (path_)),
             n (path_.size ());;
           e = traits::find_separator (path_, b))
      {
        string_type s (path_, b, e == string_type::npos ? e : e - b);
        ps.push_back (s);

        if (e == string_type::npos)
          break;

        ++e;

        while (e < n && traits::is_separator (path_[e]))
          ++e;

        if (e == n)
          break;

        b = e;
      }

      // First collapse '.' and '..'.
      //
      paths r;

      for (typename paths::const_iterator i (ps.begin ()), e (ps.end ());
           i != e; ++i)
      {
        string_type const& s (*i);
        size_type n (s.size ());

        if (n == 1 && s[0] == '.')
          continue;

        if (n == 2 && s[0] == '.' && s[1] == '.')
        {
          // Pop the last directory from r unless it is '..'.
          //
          if (!r.empty ())
          {
            string_type const& s1 (r.back ());

            if (!(s1.size () == 2 && s1[0] == '.' && s1[1] == '.'))
            {
              // Cannot go past the root directory.
              //
              if (abs && r.size () == 1)
                throw invalid_basic_path<C> (path_);

              r.pop_back ();
              continue;
            }
          }
        }

        r.push_back (s);
      }

      // Reassemble the path.
      //
      string_type p;

      for (typename paths::const_iterator i (r.begin ()), e (r.end ());
           i != e;)
      {
#ifdef _WIN32
        for (size_type j (0), n (i->size ()); j < n; ++j)
          p += tolower ((*i)[j]);
#else
        p += *i;
#endif
        ++i;

        if (i != e)
          p += traits::directory_separator;
      }

      if (p.empty () && !r.empty ())
        p += traits::directory_separator; // Root directory.

      path_.swap (p);
      return *this;
    }

    template <typename C>
    void basic_path<C>::
    init ()
    {
      // Strip trailing slashes except for the case where the single
      // slash represents the root directory.
      //
      size_type n (path_.size ());
      for (; n > 1 && traits::is_separator (path_[n - 1]); --n) ;
      path_.resize (n);
    }
  }
}