aboutsummaryrefslogtreecommitdiff
path: root/libbutl/path.ixx
blob: 578db81ebddf2f84536b8fedc2a2f205e6ddfd04 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
// file      : libbutl/path.ixx -*- C++ -*-
// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason.
{
#ifdef _WIN32
  template <>
  inline char path_traits<char>::
  tolower (char c)
  {
    return lcase (c);
  }

  template <>
  inline char path_traits<char>::
  toupper (char c)
  {
    return ucase (c);
  }
#endif

  template <class C, class K1, class K2>
  inline basic_path<C, K1>
  path_cast_impl (const basic_path<C, K2>& p, basic_path<C, K1>*)
  {
    typename basic_path<C, K1>::data_type d (
      typename basic_path<C, K1>::string_type (p.path_), p.tsep_);
    K1::cast (d);
    return basic_path<C, K1> (std::move (d));
  }

  template <class C, class K1, class K2>
  inline basic_path<C, K1>
  path_cast_impl (basic_path<C, K2>&& p, basic_path<C, K1>*)
  {
    typename basic_path<C, K1>::data_type d (std::move (p.path_), p.tsep_);
    K1::cast (d);
    return basic_path<C, K1> (std::move (d));
  }

  template <class P, class C, class K>
  inline P
  path_cast (const basic_path<C, K>& p)
  {
    return path_cast_impl (p, static_cast<P*> (nullptr));
  }

  template <class P, class C, class K>
  inline P
  path_cast (basic_path<C, K>&& p)
  {
    return path_cast_impl (std::move (p), static_cast<P*> (nullptr));
  }

  template <typename C, typename K>
  inline bool basic_path<C, K>::
  simple () const
  {
    return empty () ||
      traits::rfind_separator (this->path_, _size () - 1) == string_type::npos;
  }

  template <typename C, typename K>
  inline bool basic_path<C, K>::
  absolute () const
  {
    return traits::absolute (this->path_);
  }

  template <typename C, typename K>
  inline bool basic_path<C, K>::
  current () const
  {
    return traits::current (this->path_);
  }

  template <typename C, typename K>
  inline bool basic_path<C, K>::
  parent () const
  {
    return traits::parent (this->path_);
  }

  template <typename C, typename K>
  inline bool basic_path<C, K>::
  normalized (bool sep) const
  {
    return (!sep || this->tsep_ <= 1) &&
      traits::normalized (this->path_, sep);
  }

  template <typename C, typename K>
  inline bool basic_path<C, K>::
  root () const
  {
    return traits::root (this->path_);
  }

  template <typename C, typename K>
  inline bool basic_path<C, K>::
  sub (const basic_path& p) const
  {
    // The thinking here is that we can use the full string representations
    // (including the trailing slash in "/").
    //
    const string_type& ps (p.path_);
    size_type pn (ps.size ());

    if (pn == 0)
      return true;

    const string_type& s (this->path_);
    size_type n (s.size ());

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

  template <typename C, typename K>
  inline bool basic_path<C, K>::
  sup (const basic_path& p) const
  {
    // The thinking here is that we can use the full string representations
    // (including the trailing slash in "/").
    //
    const string_type& ps (p.path_);
    size_type pn (ps.size ());

    if (pn == 0)
      return true;

    const string_type& s (this->path_);
    size_type n (s.size ());

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

  template <typename C, typename K>
  inline basic_path<C, K> basic_path<C, K>::
  leaf () const
  {
    // While it would have been simpler to implement this one in term of
    // make_leaf(), this implementation is potentially more efficient
    // (think of the small string optimization).
    //
    const string_type& s (this->path_);
    size_type n (_size ());

    size_type p (n != 0
                 ? traits::rfind_separator (s, n - 1)
                 : string_type::npos);

    return p != string_type::npos
      ? basic_path (data_type (string_type (s, p + 1), this->tsep_))
      : *this;
  }

  template <typename C, typename K>
  inline basic_path<C, K>& basic_path<C, K>::
  make_leaf ()
  {
    string_type& s (this->path_);
    size_type n (_size ());

    size_type p (n != 0
                 ? traits::rfind_separator (s, n - 1)
                 : string_type::npos);

    if (p != string_type::npos)
    {
      s.erase (0, p + 1);

      // Keep the original tsep unless the path became empty.
      //
      if (s.empty ())
        this->tsep_ = 0;
    }

    return *this;
  }

  template <typename C, typename K>
  inline typename basic_path<C, K>::dir_type basic_path<C, K>::
  directory () const
  {
    // While it would have been simpler to implement this one in term of
    // make_directory(), this implementation is potentially more efficient
    // (think of the small string optimization).
    //
    const string_type& s (this->path_);
    size_type n (_size ());

    size_type p (n != 0
                 ? traits::rfind_separator (s, n - 1)
                 : string_type::npos);

    return p != string_type::npos
      ? dir_type (data_type (string_type (s, 0, p + 1))) // Include slash.
      : dir_type ();
  }

  template <typename C, typename K>
  inline basic_path<C, K>& basic_path<C, K>::
  make_directory ()
  {
    string_type& s (this->path_);
    size_type n (_size ());

    size_type p (n != 0
                 ? traits::rfind_separator (s, n - 1)
                 : string_type::npos);

    s.resize (p != string_type::npos ? p + 1 : 0); // Include trailing slash.
    _init ();

    return *this;
  }

  template <typename C, typename K>
  inline auto basic_path<C, K>::
  begin () const -> iterator
  {
    const string_type& s (this->path_);

    size_type b (s.empty () ? string_type::npos : 0);
    size_type e (b == 0 ? traits::find_separator (s) : b);

    return iterator (this, b, e);
  }

  template <typename C, typename K>
  inline auto basic_path<C, K>::
  end () const -> iterator
  {
    return iterator (this, string_type::npos, string_type::npos);
  }

  template <typename C, typename K>
  inline basic_path<C, K>::
  basic_path (const iterator& b, const iterator& e)
      : base_type (
        b == e
        ? data_type ()
        // We need to include the trailing separator but it is implied if
        // e == end().
        //
        : (e.b_ != string_type::npos
           ? data_type (string_type (b.p_->path_, b.b_, e.b_ - b.b_))
           : data_type (string_type (b.p_->path_, b.b_), b.p_->tsep_)))
  {
    //assert (b.p_ == e.p_);
  }

  template <typename C, typename K>
  inline basic_path<C, K>& basic_path<C, K>::
  canonicalize ()
  {
    traits::canonicalize (this->path_);

    if (this->tsep_ > 1) // Non-canonical trailing separator.
      this->tsep_ = 1;

    return *this;
  }

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

    return *this;
  }

  template <typename C, typename K>
  inline basic_path<C, K>& basic_path<C, K>::
  realize ()
  {
#ifdef _WIN32
    // This is not exactly the semantics of realpath(3). In particular, we
    // don't fail if the path does not exist. But we could have seeing that
    // we actualize it.
    //
    complete ();
    normalize (true);
#else
    traits::realize (this->path_); // Note: we retain the trailing slash.
#endif
    return *this;
  }

  template <typename C, typename K>
  inline typename basic_path<C, K>::dir_type basic_path<C, K>::
  root_directory () const
  {
#ifdef _WIN32
    // Note: on Windows we may have "c:" but still need to return "c:\".
    //
    const string_type& s (this->path_);

    return absolute ()
      ? dir_type (
        s.size () > 2
        ? data_type (string_type (s, 0, 3))
        : data_type (string_type (s), this->tsep_ != 0 ? this->tsep_ : 1))
      : dir_type ();
#else
    return absolute ()
      ? dir_type (data_type ("/", -1))
      : dir_type ();
#endif

  }

  template <typename C, typename K>
  inline basic_path<C, K> basic_path<C, K>::
  base () const
  {
    // While it would have been simpler to implement this one in term of
    // make_base(), this implementation is potentially more efficient (think
    // of the small string optimization).
    //
    const string_type& s (this->path_);
    size_type p (traits::find_extension (s));

    return p != string_type::npos
      ? basic_path (data_type (string_type (s, 0, p), this->tsep_))
      : *this;
  }

  template <typename C, typename K>
  inline basic_path<C, K>& basic_path<C, K>::
  make_base ()
  {
    string_type& s (this->path_);
    size_type p (traits::find_extension (s));

    if (p != string_type::npos)
    {
      s.resize (p);

      // Keep the original tsep unless the path became empty.
      //
      if (s.empty ())
        this->tsep_ = 0;
    }

    return *this;
  }

  template <typename C, typename K>
  inline typename basic_path<C, K>::string_type basic_path<C, K>::
  extension () const
  {
    const string_type& s (this->path_);
    size_type p (traits::find_extension (s));
    return p != string_type::npos
      ? string_type (s.c_str () + p + 1)
      : string_type ();
  }

  template <typename C, typename K>
  inline const C* basic_path<C, K>::
  extension_cstring () const
  {
    const string_type& s (this->path_);
    size_type p (traits::find_extension (s));
    return p != string_type::npos ? s.c_str () + p + 1 : nullptr;
  }

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

  template <typename C, typename K>
  inline typename basic_path<C, K>::string_type basic_path<C, K>::
  posix_string () &&
  {
    return std::move (*this).string ();
  }

  template <typename C, typename K>
  inline typename basic_path<C, K>::string_type basic_path<C, K>::
  posix_representation () const&
  {
    return representation ();
  }

  template <typename C, typename K>
  inline typename basic_path<C, K>::string_type basic_path<C, K>::
  posix_representation () &&
  {
    return std::move (*this).representation ();
  }
#endif

  template <typename C, typename K>
  inline void basic_path<C, K>::
  combine (const C* r, size_type rn, difference_type rts)
  {
    //assert (rn != 0);

    string_type& l (this->path_);
    difference_type& ts (this->tsep_);

    // Handle the separator. LHS should be empty or already have one.
    //
    switch (ts)
    {
    case  0: if (!l.empty ()) l += path_traits<C>::directory_separator; break;
    case -1: break; // Already in the string.
    default: l += path_traits<C>::directory_separators[ts - 1];
    }

    l.append (r, rn);
    ts = rts; // New trailing separator from RHS.
  }

  template <typename C, typename K>
  inline void basic_path<C, K>::
  combine (const C* r, size_type rn)
  {
    // If we do (dir_path / path) then we will end up with path. What should
    // we end up if we do (dir_path / "foo") vs (dir_path / "foo/")? We cannot
    // choose at runtime what kind of path to return. One (elaborate) option
    // would be to handle the trailing slash but also call K::cast() so that
    // dir_path gets the canonical trailing slash if one wasn't there.
    //
    // For now we won't allow the slash and will always add the canonical one
    // for dir_path (via cast()).
    //
    if (traits::find_separator (r, rn) != nullptr)
      throw invalid_basic_path<C> (r);

    combine (r, rn, 0);
    K::cast (*this);
  }

  template <typename C, typename K>
  inline basic_path<C, K>& basic_path<C, K>::
  operator/= (basic_path<C, K> const& r)
  {
    if (r.absolute () && !empty ()) // Allow ('' / '/foo').
      throw invalid_basic_path<C> (r.path_);

    if (!r.empty ())
      combine (r.path_.c_str (), r.path_.size (), r.tsep_);

    return *this;
  }

  template <typename C, typename K>
  inline basic_path<C, K>& basic_path<C, K>::
  operator/= (string_type const& r)
  {
    if (size_type rn = r.size ())
      combine (r.c_str (), rn);

    return *this;
  }

  template <typename C, typename K>
  inline basic_path<C, K>& basic_path<C, K>::
  operator/= (const C* r)
  {
    if (size_type rn = string_type::traits_type::length (r))
      combine (r, rn);

    return *this;
  }

  template <typename C, typename K>
  inline void basic_path<C, K>::
  append (const C* r, size_type rn)
  {
    //assert (this->tsep_ != -1); // Append to root?
    this->path_.append (r, rn);
  }

  template <typename C, typename K>
  inline basic_path<C, K>& basic_path<C, K>::
  operator+= (string_type const& s)
  {
    append (s.c_str (), s.size ());
    return *this;
  }

  template <typename C, typename K>
  inline basic_path<C, K>& basic_path<C, K>::
  operator+= (const C* s)
  {
    append (s, string_type::traits_type::length (s));
    return *this;
  }

  template <typename C, typename K>
  inline basic_path<C, K>& basic_path<C, K>::
  operator+= (C c)
  {
    append (&c, 1);
    return *this;
  }

  template <typename C, typename K>
  inline auto basic_path<C, K>::
  representation () const& -> string_type
  {
    string_type r (this->path_);

    if (this->tsep_ > 0)
      r += path_traits<C>::directory_separators[this->tsep_ - 1];

    return r;
  }

  template <typename C, typename K>
  inline auto basic_path<C, K>::
  representation () && -> string_type
  {
    string_type r;
    r.swap (this->path_);

    if (this->tsep_ > 0)
      r += path_traits<C>::directory_separators[this->tsep_ - 1];

    return r;
  }

  template <typename C, typename K>
  inline C basic_path<C, K>::
  separator () const
  {
    return (this->tsep_ ==  0 ? 0 :
            this->tsep_ == -1 ? this->path_[0] :
            path_traits<C>::directory_separators[this->tsep_ - 1]);
  }

  template <typename C, typename K>
  inline auto basic_path<C, K>::
  separator_string () const -> string_type
  {
    C c (separator ());
    return c == 0 ? string_type () : string_type (1, c);
  }

  template <typename C>
  inline void dir_path_kind<C>::
  cast (data_type& d)
  {
    // Add trailing slash if one isn't already there.
    //
    if (!d.path_.empty () && d.tsep_ == 0)
      d.tsep_ = 1; // Canonical separator is always first.
  }
}