libzypp 17.38.1
Pathname.h
Go to the documentation of this file.
1
2/*---------------------------------------------------------------------\
3| ____ _ __ __ ___ |
4| |__ / \ / / . \ . \ |
5| / / \ V /| _/ _/ |
6| / /__ | | | | | | |
7| /_____||_| |_| |_| |
8| |
9\---------------------------------------------------------------------*/
13#ifndef ZYPP_PATHNAME_H
14#define ZYPP_PATHNAME_H
15
16#include <cstring>
17#include <iosfwd>
18#include <string>
19
20#include <zypp-core/Globals.h>
21
23namespace zypp
24{
25
26 class Url;
27
29 namespace filesystem
30 {
31
33 //
34 // CLASS NAME : Pathname
35 //
48 {
49 public:
52 {}
53
55 Pathname( const std::string & name_r )
56 { _assign( name_r ); }
57
59 Pathname( const char * name_r )
60 { _assign( name_r ? name_r : "" ); }
61
63 Pathname( const Pathname & rhs )
64 : _name( rhs._name )
65 {}
66
68 friend void swap( Pathname & lhs, Pathname & rhs ) noexcept
69 {
70 using std::swap;
71 swap( lhs._name, rhs._name );
72 }
73
75 Pathname( Pathname && tmp ) noexcept
76 : _name( std::move( tmp._name ) )
77 {}
78
81 { swap( *this, rhs ); return *this; }
82
84 Pathname & operator/=( const Pathname & path_tv )
85 { return( *this = cat( *this, path_tv ) ); }
86
90 Pathname & operator+=( const Pathname & path_tv )
91 { return( *this = cat( *this, path_tv ) ); }
92
94 const std::string & asString() const
95 { return _name; }
96
98 static std::string showRoot( const Pathname & root_r, const Pathname & path_r );
99
101 static std::string showRootIf( const Pathname & root_r, const Pathname & path_r );
102
104 Url asUrl( const std::string & scheme_r ) const;
106 Url asUrl() const;
108 Url asDirUrl() const;
110 Url asFileUrl() const;
111
113 const char * c_str() const
114 { return _name.c_str(); }
115
117 bool empty() const { return _name.empty(); }
119 bool absolute() const { return *_name.c_str() == '/'; }
121 bool relative() const { return !( absolute() || empty() ); }
122
124 bool relativeDotDot() const { return strncmp( _name.c_str() , "./..", 4 ) == 0 && ( _name.size() == 4 || _name.c_str()[4] == '/' ); }
125
127 bool emptyOrRoot() const { return( _name.empty() || _name == "/" ); }
128
130 bool absoluteNotRoot() const { return( absolute() && _name != "/" ); }
131
133 Pathname dirname() const { return dirname( *this ); }
134 static Pathname dirname( const Pathname & name_r );
135
137 std::string basename() const { return basename( *this ); }
138 static std::string basename( const Pathname & name_r );
139
144 std::string extension() const { return extension( *this ); }
145 static std::string extension( const Pathname & name_r );
146
148 Pathname absolutename() const { return absolutename( *this ); }
149 static Pathname absolutename( const Pathname & name_r )
150 { return name_r.relative() ? cat( "/", name_r ) : name_r; }
151
153 Pathname relativename() const { return relativename( *this ); }
154 static Pathname relativename( const Pathname & name_r )
155 { return name_r.absolute() ? cat( ".", name_r ) : name_r; }
156
158 Pathname realpath() const;
159
161 static Pathname assertprefix( const Pathname & root_r, const Pathname & path_r );
162
164 static Pathname stripprefix( const Pathname & root_r, const Pathname & path_r );
165
174 Pathname cat( const Pathname & r ) const { return cat( *this, r ); }
175 static Pathname cat( const Pathname & l, const Pathname & r );
176
182 Pathname extend( const std::string & r ) const { return extend( *this, r ); }
183 static Pathname extend( const Pathname & l, const std::string & r );
184
185 private:
186 std::string _name;
187 void _assign( const std::string & name_r );
188 };
189
190
192 inline bool operator==( const Pathname & l, const Pathname & r )
193 { return l.asString() == r.asString(); }
194
196 inline bool operator!=( const Pathname & l, const Pathname & r )
197 { return l.asString() != r.asString(); }
198
200 inline Pathname operator/( const Pathname & l, const Pathname & r )
201 { return Pathname::cat( l, r ); }
202
206 inline Pathname operator+( const Pathname & l, const Pathname & r )
207 { return Pathname::cat( l, r ); }
208
210 inline bool operator<( const Pathname & l, const Pathname & r )
211 { return l.asString() < r.asString(); }
212
214
216 inline std::ostream & operator<<( std::ostream & str, const Pathname & obj )
217 { return str << obj.asString(); }
218
220 } // namespace filesystem
222
225
227} // namespace zypp
229#endif // ZYPP_PATHNAME_H
Provides API related macros.
#define ZYPP_API
Definition Globals.h:69
Pathname cat(const Pathname &r) const
Concatenation of pathnames.
Definition Pathname.h:174
Url manipulation class.
Definition Url.h:93
Pathname relativename() const
Return this path, removing a leading '/' if absolute.
Definition Pathname.h:153
bool absoluteNotRoot() const
Test for a not empty absolute Path (!= "/").
Definition Pathname.h:130
Pathname extend(const std::string &r) const
Append string r to the last component of the path.
Definition Pathname.h:182
static Pathname extend(const Pathname &l, const std::string &r)
Pathname(const Pathname &rhs)
Copy Ctor.
Definition Pathname.h:63
std::ostream & operator<<(std::ostream &str, const Pathname &obj)
Stream output.
Definition Pathname.h:216
Pathname & operator/=(const Pathname &path_tv)
Concatenate and assign.
Definition Pathname.h:84
friend void swap(Pathname &lhs, Pathname &rhs) noexcept
Swap.
Definition Pathname.h:68
Pathname(Pathname &&tmp) noexcept
Move Ctor.
Definition Pathname.h:75
bool relativeDotDot() const
Test for a relative path referring to ../.
Definition Pathname.h:124
static Pathname absolutename(const Pathname &name_r)
Definition Pathname.h:149
Pathname & operator+=(const Pathname &path_tv)
Concatenate and assign.
Definition Pathname.h:90
Pathname dirname() const
Return all but the last component od this path.
Definition Pathname.h:133
bool emptyOrRoot() const
Test for "" or "/".
Definition Pathname.h:127
bool absolute() const
Test for an absolute path.
Definition Pathname.h:119
Pathname operator+(const Pathname &l, const Pathname &r)
Concatenate two Pathname.
Definition Pathname.h:206
bool operator!=(const Pathname &l, const Pathname &r)
Definition Pathname.h:196
Pathname cat(const Pathname &r) const
Concatenation of pathnames.
Definition Pathname.h:174
Pathname()
Default ctor: an empty path.
Definition Pathname.h:51
const char * c_str() const
String representation.
Definition Pathname.h:113
Pathname(const char *name_r)
Ctor from char*.
Definition Pathname.h:59
const std::string & asString() const
String representation.
Definition Pathname.h:94
std::string basename() const
Return the last component of this path.
Definition Pathname.h:137
bool empty() const
Test for an empty path.
Definition Pathname.h:117
bool operator==(const Pathname &l, const Pathname &r)
Definition Pathname.h:192
void _assign(const std::string &name_r)
Definition Pathname.cc:33
Pathname & operator=(Pathname rhs)
Assign.
Definition Pathname.h:80
Pathname absolutename() const
Return this path, adding a leading '/' if relative.
Definition Pathname.h:148
Pathname(const std::string &name_r)
Ctor from string.
Definition Pathname.h:55
bool operator<(const Pathname &l, const Pathname &r)
Definition Pathname.h:210
Pathname operator/(const Pathname &l, const Pathname &r)
Concatenate two Pathname.
Definition Pathname.h:200
static Pathname relativename(const Pathname &name_r)
Definition Pathname.h:154
std::string extension() const
Return all of the characters in name after and including the last dot in the last element of name.
Definition Pathname.h:144
bool relative() const
Test for a relative path.
Definition Pathname.h:121
String related utilities and Regular expression matching.
Types and functions for filesystem operations.
Definition PathInfo.cc:41