aboutsummaryrefslogtreecommitdiff
path: root/cutl/fs/path.hxx
blob: 9fed07ad359cfd045cdbf64a6b8781c5232dab86 (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
// file      : cutl/fs/path.hxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2010 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>

#include <cutl/details/export.hxx>

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

    template <typename C>
    struct path_traits
    {
      typedef std::basic_string<C> string_type;
      typedef typename string_type::size_type size_type;

      // Canonical directory and path seperators.
      //
#ifdef _WIN32
      static char const directory_separator = '\\';
      static char const path_separator = ';';
#else
      static char const directory_separator = '/';
      static char const path_separator = ':';
#endif

      // Directory separator tests. On some platforms there
      // could be multiple seperators. For example, on Windows
      // we check for both '/' and '\'.
      //

      static bool
      is_separator (C c)
      {
#ifdef _WIN32
        return c == '\\' || c == '/';
#else
        return c == '/';
#endif
      }

      static size_type
      find_separator (string_type const& s)
      {
        for (size_type i (0), n (s.size ()); i < n; ++i)
        {
          if (is_separator (s[i]))
            return i;
        }

        return string_type::npos;
      }

      static size_type
      rfind_separator (string_type const& s)
      {
        for (size_type i (s.size ()) ; i > 0; --i)
        {
          if (is_separator (s[i - 1]))
            return i - 1;
        }

        return string_type::npos;
      }
    };


    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;

    //
    //
    class LIBCUTL_EXPORT invalid_path_base: exception
    {
    public:
      virtual char const*
      what () const throw ();
    };

    template <typename C>
    class invalid_basic_path: public invalid_path_base
    {
    public:
      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;

      typedef path_traits<C> traits;

      // Construct special empty path.
      //
      basic_path ()
      {
      }

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

      basic_path (C const* s, size_type n)
          : path_ (s, n)
      {
        init ();
      }

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

    public:
      // Return the path without the directory part.
      //
      basic_path
      leaf () const;

      // Return the directory part of the path or empty path if
      // there is no directory.
      //
      basic_path
      directory () const;

      // Return the path without the extension, if any.
      //
      basic_path
      base () const;

    public:
      basic_path
      operator/ (basic_path const& x)
      {
        basic_path r (*this);
        r /= x;
        return r;
      }

      basic_path&
      operator/= (basic_path const&);

      basic_path
      operator+ (string_type const& s)
      {
        return basic_path (path_ + s);
      }

      basic_path&
      operator+= (string_type const& s)
      {
        path_ += s;
        return *this;
      }

      bool
      operator== (basic_path const& x) const
      {
        return path_ == x.path_;
      }

      bool
      operator!= (basic_path const& x) const
      {
        return !(*this == x);
      }

    public:
      bool
      empty () const
      {
        return path_.empty ();
      }

      string_type
      string () const
      {
        return path_;
      }

    private:
      void
      init ();

      bool
      root () const
      {
        return path_.size () == 1 && traits::is_separator (path_[0]);
      }

    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