nomlib
Loading...
Searching...
No Matches
filesystem.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice, this
14 list of conditions and the following disclaimer in the documentation and/or
15 other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_FILESYSTEM_HEADERS
30#define NOMLIB_FILESYSTEM_HEADERS
31
32#include <cstddef>
33#include <cstdint>
34#include <iterator>
35#include <memory>
36#include <string>
37#include <system_error>
38#include <vector>
39
40#include "nomlib/config.hpp"
41#include "export.hpp"
42
43namespace nom {
44namespace filesystem {
45
53class NOM_EXPORT path
54{
55 public:
56 using value_type = char;
57 using string_type = std::string;
58
59 #if defined( NOM_PLATFORM_WINDOWS )
60 static const value_type preferred_separator;
61 #else
62 static const value_type preferred_separator;
63 #endif
64
65 path();
66 path( const path& other );
67 path( path&& other ) noexcept;
68 path( const string_type& source );
69 path( const char* source );
70 ~path();
71
72 path& operator=( const path& other );
73 path& operator=( path&& other ) noexcept;
74 path& operator=( const string_type& source );
75 path& operator=( const char* source );
76
77 path& assign( const string_type& source );
78
79 path& operator/=( const path& other );
80 path& operator/=( const string_type& source );
81 path& operator/=( const char* source );
82
83 path& operator+=( const path& other );
84 path& operator+=( const string_type& source );
85 path& operator+=( const char* source );
86
87 void clear() noexcept;
88
92
97 path lexically_normal() const;
98
101 class iterator
102 {
103 public:
104 typedef path value_type;
105 typedef const path* pointer;
106 typedef const path& reference;
107 typedef std::ptrdiff_t difference_type;
108 typedef std::input_iterator_tag iterator_category;
109
110 iterator();
111 iterator( const path& p, bool end_it );
112
113 reference operator*() const;
114 pointer operator->() const;
115
116 iterator& operator++();
117 iterator operator++( int );
118
119 bool operator==( const iterator& other ) const;
120 bool operator!=( const iterator& other ) const;
121
122 private:
123 std::vector<path> elements_;
124 std::size_t pos_;
125 };
126
127 iterator begin() const;
128 iterator end() const;
130
132 const string_type& native() const;
133
135 const value_type* c_str() const;
136
138 string_type string() const;
139
141 string_type generic_string() const;
142
145 using u8string_type = std::string;
146 using u16string_type = std::u16string;
147
148 path( const u16string_type& source );
149 path( const std::wstring& source );
150
151 path& operator=( const u16string_type& source );
152 path& operator=( const std::wstring& source );
153
154 path& assign( const u16string_type& source );
155 path& assign( const std::wstring& source );
156
158 u8string_type u8string() const;
159
161 u16string_type u16string() const;
162
164 std::wstring wstring() const;
165
167 u8string_type generic_u8string() const;
168
170 u16string_type generic_u16string() const;
171
173 std::wstring generic_wstring() const;
175
178 path root_name() const;
179 path root_directory() const;
180 path root_path() const;
181 path relative_path() const;
182 path parent_path() const;
183 path filename() const;
184 path stem() const;
185 path extension() const;
187
190 bool has_root_name() const;
191 bool has_root_directory() const;
192 bool has_root_path() const;
193 bool has_relative_path() const;
194 bool has_parent_path() const;
195 bool has_filename() const;
196 bool has_stem() const;
197 bool has_extension() const;
198
199 bool is_absolute() const;
200 bool is_relative() const;
201
202 bool empty() const;
204
205 private:
206 void parse( const string_type& source );
207 void normalize();
208 void invalidate_cache() const;
209 void rebuild_cache() const;
210
213 std::vector<path> path_elements() const;
214
215 mutable string_type native_pathname_;
216 mutable bool dirty_;
217
218 string_type root_name_;
219 bool has_root_directory_;
220 bool absolute_;
221 std::vector<string_type> components_;
222 bool has_trailing_separator_;
223};
224
226class NOM_EXPORT directory_entry
227{
228 public:
229 directory_entry();
230 explicit directory_entry( class path p );
231
233 const class path& path() const;
234
235 private:
236 class path path_;
237};
238
243class NOM_EXPORT directory_iterator
244{
245 public:
246 typedef directory_entry value_type;
247 typedef const directory_entry* pointer;
248 typedef const directory_entry& reference;
249 typedef std::ptrdiff_t difference_type;
250 typedef std::input_iterator_tag iterator_category;
251
254
256 explicit directory_iterator( const class path& p );
257
258 directory_iterator( const directory_iterator& other ) = default;
259 directory_iterator& operator=( const directory_iterator& other ) = default;
260
262
263 const directory_entry& operator*() const;
264 const directory_entry* operator->() const;
265
266 directory_iterator& operator++();
267
268 bool operator==( const directory_iterator& other ) const;
269 bool operator!=( const directory_iterator& other ) const;
270
271 private:
272 struct state;
273 std::shared_ptr<state> state_;
274};
275
277NOM_EXPORT directory_iterator begin( directory_iterator it );
278NOM_EXPORT directory_iterator end( const directory_iterator& );
279
281NOM_EXPORT path operator/( const path& lhs, const path& rhs );
282NOM_EXPORT path operator/( const path& lhs, const std::string& rhs );
283NOM_EXPORT path operator/( const path& lhs, const char* rhs );
284NOM_EXPORT path operator/( const std::string& lhs, const path& rhs );
285
287NOM_EXPORT path operator+( const path& lhs, const path& rhs );
288NOM_EXPORT path operator+( const path& lhs, const std::string& rhs );
289NOM_EXPORT path operator+( const path& lhs, const char* rhs );
290
293
295NOM_EXPORT bool exists( const path& p );
296
298NOM_EXPORT bool is_directory( const path& p );
299
301NOM_EXPORT bool is_regular_file( const path& p );
302
307NOM_EXPORT bool is_symlink( const path& p );
308
314NOM_EXPORT bool exists_symlink( const path& p );
315
320NOM_EXPORT bool is_symlink_directory( const path& p );
321
326NOM_EXPORT bool is_symlink_regular_file( const path& p );
327
332NOM_EXPORT std::uintmax_t file_size_symlink( const path& p );
333
338NOM_EXPORT bool create_directory( const path& p );
339
344NOM_EXPORT bool create_directories( const path& p );
345
349NOM_EXPORT bool remove( const path& p );
350
352NOM_EXPORT std::uintmax_t file_size( const path& p );
353
355NOM_EXPORT path current_path();
356
358NOM_EXPORT void current_path( const path& p );
359
365NOM_EXPORT path absolute( const path& p, const path& base = current_path() );
366
371NOM_EXPORT path canonical( const path& p, const path& base = current_path() );
372
378NOM_EXPORT path weakly_canonical( const path& p, const path& base = current_path() );
379
384NOM_EXPORT bool valid_utf8( const std::string& s );
385
387
394class NOM_EXPORT filesystem_error: public std::system_error
395{
396 public:
397 filesystem_error( const std::string& what_arg, const path& p1,
398 std::error_code ec );
399
400 filesystem_error( const std::string& what_arg, const path& p1,
401 const path& p2, std::error_code ec );
402
404 const path& path1() const noexcept;
405
407 const path& path2() const noexcept;
408
410 const char* what() const noexcept override;
411
412 private:
413 path path1_;
414 path path2_;
415 mutable std::string what_;
416};
417
418} // namespace filesystem
419} // namespace nom
420
421#endif // include guard
422
Directory entry used by nom::filesystem::directory_iterator.
const class path & path() const
The file's full pathname (directory path + filename).
Minimal input iterator over the entries of a directory.
directory_iterator(const class path &p)
Open the directory at p and position at the first entry.
directory_iterator()
Construct an end iterator.
const path & path1() const noexcept
Return the first path associated with the error.
const char * what() const noexcept override
Return a message describing the error and the involved paths.
const path & path2() const noexcept
Return the second path associated with the error, if any.
A C++14 reimplementation of the path abstraction described in ISO/IEC TS 18822:2015 (N4100) / Boost....
u8string_type u8string() const
Return the path encoded as UTF-8.
path & make_preferred()
Convert any directory separators to the platform's preferred separator in-place and return this path.
u8string_type generic_u8string() const
Generic-string alias for u8string().
u16string_type u16string() const
Return the path encoded as UTF-16.
const value_type * c_str() const
Return a NUL-terminated C-string in native format.
std::wstring generic_wstring() const
Generic-string alias for wstring().
path lexically_normal() const
Return a normalized copy of this path.
const string_type & native() const
Return the path in the platform's native format.
u16string_type generic_u16string() const
Generic-string alias for u16string().
std::wstring wstring() const
Return the path as a wide-character string.
std::vector< path > path_elements() const
Break the path into its iteration elements (root-name, root-directory, and each path component).
string_type generic_string() const
For nomlib, generic and native strings are interchangeable.
string_type string() const
Convenience alias for native().