Path.hpp
1 #pragma once
2 
3 #include <opals/fwd.hpp>
4 #include <opals/String.hpp>
5 
6 namespace opals {
7 
8  /**
9  \class Path
10 
11  \brief A file/directory path
12 
13  Internally, all '\\' are replaced by '/'
14 
15  \author wk
16  \date 02.02.2011
17 
18  \internal
19  Path encapsulates a boost::filesystem::path using the pimpl-idiom.
20  Conversion constructors and operators for boost::filesystem::path are provided.
21  boost::filesystems's iterators are not exposed, as that may result in unexpected behaviour
22  when used as if Path were a string - convert to a boost::filesystem::path for that purpose (e.g. by calling Path::impl()).
23  In addition to boost::filesystem::path's interface, operator+ and operator+= are defined.
24  */
25 
26  class OPALS_API Path
27  {
28  struct Impl;
29  Impl *pimpl_;
30 
31  public:
32  using size_type = String::size_type;
33 
34  Path();
35  Path(const char*);
36  Path(const String&);
37  Path(String&&);
38  Path(const Path&);
39  Path(Path&&);
40  ~Path();
41 
42  /// \name assignment operators
43  ///@{
44  Path& operator=(const char*);
45  Path& operator=(const String&);
46  Path& operator=(String&&);
47  Path& operator=(const Path&);
48  Path& operator=(Path&&);
49  ///@}
50 
51  /// \name append a file / directory name
52  /// introducing a '/' between the stored path and the directory/filename to append
53  ///@{
54  Path& operator/=(const char*);
55  Path& operator/=(const String&);
56  Path& operator/=(const Path&);
57  ///@}
58 
59  /// \name add a string
60  /// return a path where a string has been appended to the stored path (not introducing a '/' in between)
61  ///@{
62  Path operator+(const char*) const;
63  Path operator+(const String&) const;
64  Path operator+(const Path&) const;
65  ///@}
66 
67  /// \name append a string
68  /// append a string to the stored path (not introducing a '/' in between)
69  ///@{
70  Path& operator+=(const char*);
71  Path& operator+=(const String&);
72  Path& operator+=(const Path&);
73  ///@}
74 
75  /// postcondition: this->empty() is true.
76  void clear();
77  /// postcondition: For the returned path, is_absolute() is true.
78  Path& make_absolute();
79  /// Convert the contained pathname to the native format.
80  Path& make_preferred();
81  /// if has_parent_path() then remove the last filename from the stored path.
82  Path& remove_filename();
83  /// postcondition: extension() == new_extension (where a leading dot has been removed from replacement, if present)
84  Path& replace_extension(const String& new_extension = String());
85  /// swaps the contents of the two paths.
86  void swap(Path&);
87 
88  /// returns the stored path, formatted according to the operating system rules for regular file pathnames
89  String string() const;
90 
91  /// returns the root name (e.g. d:/path/filename.ext -> d:)
92  Path root_name() const;
93  /// returns the root directory (e.g. d:/path/filename.ext -> /)
94  Path root_directory() const;
95  /// returns root_name() + root_directory()
96  Path root_path() const;
97 
98  /// returns the stored path without root_path() (e.g. d:/path/file.ext -> path/file.ext)
99  Path relative_path() const;
100  /// returns the parent directory, if any (e.g. d:/path/filename.ext -> d:/path/)
101  Path parent_path() const;
102 
103  /// returns the filename (i.e. /path/filename.ext -> filename.ext)
104  Path filename() const;
105  /// returns the filename without extension (i.e. /path/filename.ext -> filename)
106  String stem() const;
107  /// returns the filename extension (i.e. /path/filename.ext -> .ext)
108  String extension() const;
109 
110  /// returns string().empty()
111  bool empty() const;
112  /// returns !root_name().empty()
113  bool has_root_name() const;
114  /// returns !root_directory().empty()
115  bool has_root_directory() const;
116  /// returns !root_path().empty()
117  bool has_root_path() const;
118  /// returns !relative_path().empty()
119  bool has_relative_path() const;
120  /// returns !parent_path().empty()
121  bool has_parent_path() const;
122  /// returns !filename().empty()
123  bool has_filename() const;
124  /// returns !stem().empty()
125  bool has_stem() const;
126  /// returns !extension().empty()
127  bool has_extension() const;
128  /// returns true if the elements of root_path() uniquely identify a directory, else false
129  bool is_absolute() const;
130  /// returns !is_absolute()
131  bool is_relative() const;
132 
133  /// Replace symlinks, replace '/' with '\', replace "foo/../bar" with "bar", etc.
134  Path& normalize();
135 
136  /// shorten path to length, e.g. d:/foo_long_directory_name/bar.txt -> d:/foo.../bar.txt
137  String compressed(size_type length) const;
138 
139  };
140 
141  /// \relatesalso Path
142  void OPALS_API swap(Path&, Path&);
143 
144  /// \relatesalso Path
145  Path OPALS_API operator/(const Path& lhs, const Path& rhs);
146  /// \relatesalso Path
147  Path OPALS_API operator/(const Path& lhs, const char* rhs);
148  /// \relatesalso Path
149  Path OPALS_API operator/(const char* lhs, const Path& rhs);
150  /// \relatesalso Path
151  Path OPALS_API operator/(const Path& lhs, const String& rhs);
152  /// \relatesalso Path
153  Path OPALS_API operator/(const String& lhs, const Path& rhs);
154 
155  /// \relatesalso Path
156  bool OPALS_API operator< (const Path&, const Path&);
157 
158  /// \relatesalso Path
159  bool OPALS_API operator<=(const Path&, const Path&);
160 
161  /// \relatesalso Path
162  bool OPALS_API operator> (const Path&, const Path&);
163 
164  /// \relatesalso Path
165  bool OPALS_API operator>=(const Path&, const Path&);
166 
167  /// \relatesalso Path
168  bool OPALS_API operator==(const Path&, const Path&);
169  /// \relatesalso Path
170  bool OPALS_API operator==(const Path&, const String&);
171  /// \relatesalso Path
172  bool OPALS_API operator==(const String&, const Path&);
173  /// \relatesalso Path
174  bool OPALS_API operator==(const Path&, const char*);
175  /// \relatesalso Path
176  bool OPALS_API operator==(const char*, const Path&);
177 
178  /// \relatesalso Path
179  bool OPALS_API operator!=(const Path&, const Path&);
180  /// \relatesalso Path
181  bool OPALS_API operator!=(const Path&, const String&);
182  /// \relatesalso Path
183  bool OPALS_API operator!=(const String&, const Path&);
184  /// \relatesalso Path
185  bool OPALS_API operator!=(const Path&, const char*);
186  /// \relatesalso Path
187  bool OPALS_API operator!=(const char*, const Path&);
188 
189  /// \relatesalso Path
190  std::size_t hash_value(const Path& p);
191 
192 }
193 
A file/directory path.
Definition: Path.hpp:26
Contains the public interface of OPALS.
Definition: AbsValueOrQuantile.hpp:8
A dynamic character string whose interface conforms to STL's std::string.
Definition: String.hpp:35