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