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  class Impl;
29  Impl *pimpl_;
30 
31  public:
32  typedef String::size_type size_type;
33 
34  Path();
35  Path( const char* );
36  Path( const String& );
37  Path( const Path& );
38  ~Path();
39 
40  /// \name assignment operators
41  ///@{
42  Path& operator= ( const char* );
43  Path& operator= ( const String& );
44  Path& operator= ( const Path& );
45  ///@}
46 
47  /// \name append a file / directory name
48  /// introducing a '/' between the stored path and the directory/filename to append
49  ///@{
50  Path& operator/= ( const char* );
51  Path& operator/= ( const String& );
52  Path& operator/= ( const Path& );
53  ///@}
54 
55  /// \name add a string
56  /// return a path where a string has been appended to the stored path (not introducing a '/' in between)
57  ///@{
58  Path operator+ ( const char* ) const;
59  Path operator+ ( const String& ) const;
60  ///@}
61 
62  /// \name append a string
63  /// append a string to the stored path (not introducing a '/' in between)
64  ///@{
65  Path& operator+= ( const char* );
66  Path& operator+= ( const String& );
67  ///@}
68 
69  /// postcondition: this->empty() is true.
70  void clear();
71  /// postcondition: For the returned path, is_absolute() is true.
72  Path& make_absolute();
73  /// Convert the contained pathname to the native format.
74  Path& make_preferred();
75  /// if has_parent_path() then remove the last filename from the stored path.
76  Path& remove_filename();
77  /// postcondition: extension() == new_extension (where a leading dot has been removed from replacement, if present)
78  Path& replace_extension( const String& new_extension = String() );
79  /// swaps the contents of the two paths.
80  void swap( Path& );
81 
82  /// returns the stored path, formatted according to the operating system rules for regular file pathnames
83  const String string() const;
84 
85  /// returns the root name (e.g. d:/path/filename.ext -> d:)
86  Path root_name() const;
87  /// returns the root directory (e.g. d:/path/filename.ext -> /)
88  Path root_directory() const;
89  /// returns root_name() + root_directory()
90  Path root_path() const;
91 
92  /// returns the stored path without root_path() (e.g. d:/path/file.ext -> path/file.ext)
93  Path relative_path() const;
94  /// returns the parent directory, if any (e.g. d:/path/filename.ext -> d:/path/)
95  Path parent_path() const;
96 
97  /// returns the filename (i.e. /path/filename.ext -> filename.ext)
98  Path filename() const;
99  /// returns the filename without extension (i.e. /path/filename.ext -> filename)
100  String stem() const;
101  /// returns the filename extension (i.e. /path/filename.ext -> .ext)
102  String extension() const;
103 
104  /// returns string().empty()
105  bool empty() const;
106  /// returns !root_name().empty()
107  bool has_root_name() const;
108  /// returns !root_directory().empty()
109  bool has_root_directory() const;
110  /// returns !root_path().empty()
111  bool has_root_path() const;
112  /// returns !relative_path().empty()
113  bool has_relative_path() const;
114  /// returns !parent_path().empty()
115  bool has_parent_path() const;
116  /// returns !filename().empty()
117  bool has_filename() const;
118  /// returns !stem().empty()
119  bool has_stem() const;
120  /// returns !extension().empty()
121  bool has_extension() const;
122  /// returns true if the elements of root_path() uniquely identify a directory, else false
123  bool is_absolute() const;
124  /// returns !is_absolute()
125  bool is_relative() const;
126 
127  /// Replace symlinks, replace '/' with '\', replace "foo/../bar" with "bar", etc.
128  Path &normalize();
129 
130  /// shorten path to length, e.g. d:/foo_long_directory_name/bar.txt -> d:/foo.../bar.txt
131  String compressed( size_type length ) const;
132 
133  };
134 
135  /// \relatesalso Path
136  void OPALS_API swap( Path&, Path& );
137  /// \relatesalso Path
138  Path OPALS_API operator/(const Path& lhs, const Path& rhs );
139  /// \relatesalso Path
140  Path OPALS_API operator/(const Path& lhs, const char* rhs );
141  /// \relatesalso Path
142  Path OPALS_API operator/(const char* lhs, const Path& rhs );
143  /// \relatesalso Path
144  Path OPALS_API operator/(const Path& lhs, const String& rhs );
145  /// \relatesalso Path
146  Path OPALS_API operator/(const String& lhs, const Path& rhs );
147 
148  /// \relatesalso Path
149  bool OPALS_API operator< ( const Path& , const Path& );
150 
151  /// \relatesalso Path
152  bool OPALS_API operator<=( const Path& , const Path& );
153 
154  /// \relatesalso Path
155  bool OPALS_API operator> ( const Path& , const Path& );
156 
157  /// \relatesalso Path
158  bool OPALS_API operator>=( const Path& , const Path& );
159 
160  /// \relatesalso Path
161  bool OPALS_API operator==( const Path& , const Path& );
162  /// \relatesalso Path
163  bool OPALS_API operator==( const Path& , const String& );
164  /// \relatesalso Path
165  bool OPALS_API operator==( const String& , const Path& );
166  /// \relatesalso Path
167  bool OPALS_API operator==( const Path& , const char * );
168  /// \relatesalso Path
169  bool OPALS_API operator==( const char * , const Path& );
170 
171  /// \relatesalso Path
172  bool OPALS_API operator!=( const Path& , const Path& );
173  /// \relatesalso Path
174  bool OPALS_API operator!=( const Path& , const String& );
175  /// \relatesalso Path
176  bool OPALS_API operator!=( const String& , const Path& );
177  /// \relatesalso Path
178  bool OPALS_API operator!=( const Path& , const char * );
179  /// \relatesalso Path
180  bool OPALS_API operator!=( const char * , const Path& );
181 
182 }
183 
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