aboutsummaryrefslogtreecommitdiff
path: root/cutl/fs/path.hxx
blob: 139a3cc0b017c19bf089d2cd33398f8254b4c8c4 (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
// file      : cutl/fs/path.hxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#ifndef CUTL_FS_PATH_HXX
#define CUTL_FS_PATH_HXX

#include <string>
#include <iosfwd>

#include <cutl/exception.hxx>

namespace cutl
{
  namespace fs
  {
    template <typename C>
    class basic_path;

    template <typename C>
    class invalid_basic_path;

    typedef basic_path<char> path;
    typedef invalid_basic_path<char> invalid_path;

    typedef basic_path<wchar_t> wpath;
    typedef invalid_basic_path<wchar_t> invalid_wpath;

    //
    //
    struct invalid_path_base: exception
    {
      virtual char const*
      what () const throw ();
    };

    template <typename C>
    struct invalid_basic_path: invalid_path_base
    {
      typedef std::basic_string<C> string_type;

      invalid_basic_path (C const* p): path_ (p) {}
      invalid_basic_path (string_type const& p): path_ (p) {}
      ~invalid_basic_path () throw () {}

      string_type const&
      path () const
      {
        return path_;
      }

    private:
      string_type path_;
    };

    template <typename C>
    class basic_path
    {
    public:
      typedef std::basic_string<C> string_type;
      typedef typename string_type::size_type size_type;

      explicit
      basic_path (C const* s)
          : path_ (s)
      {
        init (false);
      }

      explicit
      basic_path (string_type const& s)
          : path_ (s)
      {
        init (false);
      }

    public:
      basic_path
      leaf () const;

      basic_path
      directory () const;

      basic_path
      base () const;

    public:
      basic_path
      operator/ (basic_path const&);

    public:
      string_type
      string () const
      {
        return path_.empty () ? string_type (1, '/') : path_;
      }

    private:
      void
      init (bool internal);

      // Assume internal format.
      //
      basic_path (C const* s, size_type n)
          : path_ (s, n)
      {
        init (true);
      }

    private:
      string_type path_;
    };

    template <typename C>
    inline std::basic_ostream<C>&
    operator<< (std::basic_ostream<C>& os, basic_path<C> const& p)
    {
      return os << p.string ();
    }
  }
}

#include <cutl/fs/path.txx>

#endif // CUTL_FS_PATH_HXX