libzypp 17.31.23
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 <iosfwd>
17#include <string>
18
20namespace zypp
21{
22
23 class Url;
24
26 namespace filesystem
27 {
28
30 //
31 // CLASS NAME : Pathname
32 //
44 class Pathname
45 {
46 public:
48 Pathname()
49 {}
50
52 Pathname( const std::string & name_r )
53 { _assign( name_r ); }
54
56 Pathname( const char * name_r )
57 { _assign( name_r ? name_r : "" ); }
58
60 Pathname( const Pathname & rhs )
61 : _name( rhs._name )
62 {}
63
65 friend void swap( Pathname & lhs, Pathname & rhs )
66 {
67 using std::swap;
68 swap( lhs._name, rhs._name );
69 }
70
72 Pathname( Pathname && tmp )
73 : _name( std::move( tmp._name ) )
74 {}
75
78 { swap( *this, rhs ); return *this; }
79
81 Pathname & operator/=( const Pathname & path_tv )
82 { return( *this = cat( *this, path_tv ) ); }
83
87 Pathname & operator+=( const Pathname & path_tv )
88 { return( *this = cat( *this, path_tv ) ); }
89
91 const std::string & asString() const
92 { return _name; }
93
95 static std::string showRoot( const Pathname & root_r, const Pathname & path_r );
96
98 static std::string showRootIf( const Pathname & root_r, const Pathname & path_r );
99
101 Url asUrl( const std::string & scheme_r ) const;
103 Url asUrl() const;
105 Url asDirUrl() const;
107 Url asFileUrl() const;
108
110 const char * c_str() const
111 { return _name.c_str(); }
112
114 bool empty() const { return _name.empty(); }
116 bool absolute() const { return *_name.c_str() == '/'; }
118 bool relative() const { return !( absolute() || empty() ); }
119
121 bool emptyOrRoot() const { return( _name.empty() || _name == "/" ); }
122
124 Pathname dirname() const { return dirname( *this ); }
125 static Pathname dirname( const Pathname & name_r );
126
128 std::string basename() const { return basename( *this ); }
129 static std::string basename( const Pathname & name_r );
130
135 std::string extension() const { return extension( *this ); }
136 static std::string extension( const Pathname & name_r );
137
139 Pathname absolutename() const { return absolutename( *this ); }
140 static Pathname absolutename( const Pathname & name_r )
141 { return name_r.relative() ? cat( "/", name_r ) : name_r; }
142
144 Pathname relativename() const { return relativename( *this ); }
145 static Pathname relativename( const Pathname & name_r )
146 { return name_r.absolute() ? cat( ".", name_r ) : name_r; }
147
149 Pathname realpath() const;
150
152 static Pathname assertprefix( const Pathname & root_r, const Pathname & path_r );
153
155 static Pathname stripprefix( const Pathname & root_r, const Pathname & path_r );
156
165 Pathname cat( const Pathname & r ) const { return cat( *this, r ); }
166 static Pathname cat( const Pathname & l, const Pathname & r );
167
173 Pathname extend( const std::string & r ) const { return extend( *this, r ); }
174 static Pathname extend( const Pathname & l, const std::string & r );
175
176 private:
177 std::string _name;
178 void _assign( const std::string & name_r );
179 };
181
183 inline bool operator==( const Pathname & l, const Pathname & r )
184 { return l.asString() == r.asString(); }
185
187 inline bool operator!=( const Pathname & l, const Pathname & r )
188 { return l.asString() != r.asString(); }
189
191 inline Pathname operator/( const Pathname & l, const Pathname & r )
192 { return Pathname::cat( l, r ); }
193
197 inline Pathname operator+( const Pathname & l, const Pathname & r )
198 { return Pathname::cat( l, r ); }
199
201 inline bool operator<( const Pathname & l, const Pathname & r )
202 { return l.asString() < r.asString(); }
203
205
207 inline std::ostream & operator<<( std::ostream & str, const Pathname & obj )
208 { return str << obj.asString(); }
209
211 } // namespace filesystem
213
215 using filesystem::Pathname;
216
218} // namespace zypp
220#endif // ZYPP_PATHNAME_H
Pathname relativename() const
Return this path, removing a leading '/' if absolute.
Definition: Pathname.h:144
static std::string showRoot(const Pathname &root_r, const Pathname &path_r)
String representation as "(root)/path".
Definition: Pathname.cc:190
Pathname extend(const std::string &r) const
Append string r to the last component of the path.
Definition: Pathname.h:173
Pathname & operator/=(const Pathname &path_tv)
Concatenate and assign.
Definition: Pathname.h:81
friend void swap(Pathname &lhs, Pathname &rhs)
Swap.
Definition: Pathname.h:65
static Pathname stripprefix(const Pathname &root_r, const Pathname &path_r)
Return path_r with any root_r dir prefix striped.
Definition: Pathname.cc:281
Pathname & operator+=(const Pathname &path_tv)
Concatenate and assign.
Definition: Pathname.h:87
Pathname dirname() const
Return all but the last component od this path.
Definition: Pathname.h:124
bool emptyOrRoot() const
Test for "" or "/".
Definition: Pathname.h:121
bool absolute() const
Test for an absolute path.
Definition: Pathname.h:116
Pathname cat(const Pathname &r) const
Concatenation of pathnames.
Definition: Pathname.h:165
Pathname()
Default ctor: an empty path.
Definition: Pathname.h:48
const char * c_str() const
String representation.
Definition: Pathname.h:110
const std::string & asString() const
String representation.
Definition: Pathname.h:91
std::string basename() const
Return the last component of this path.
Definition: Pathname.h:128
bool empty() const
Test for an empty path.
Definition: Pathname.h:114
static Pathname assertprefix(const Pathname &root_r, const Pathname &path_r)
Return path_r prefixed with root_r, unless it is already prefixed.
Definition: Pathname.cc:272
void _assign(const std::string &name_r)
Definition: Pathname.cc:33
Pathname & operator=(Pathname rhs)
Assign.
Definition: Pathname.h:77
Pathname realpath() const
Returns this path as the absolute canonical pathname.
Definition: Pathname.cc:231
Pathname absolutename() const
Return this path, adding a leading '/' if relative.
Definition: Pathname.h:139
static std::string showRootIf(const Pathname &root_r, const Pathname &path_r)
String representation as "(root)/path", unless root is "/" or empty.
Definition: Pathname.cc:195
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:135
bool relative() const
Test for a relative path.
Definition: Pathname.h:118
Definition: Arch.h:361
String related utilities and Regular expression matching.
std::ostream & operator<<(std::ostream &str, const Glob &obj)
Definition: Glob.cc:53
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
bool operator<(const StrMatcher &lhs, const StrMatcher &rhs)
Definition: StrMatcher.cc:335
bool operator==(const SetRelation::Enum &lhs, const SetCompare &rhs)
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool operator!=(const SetRelation::Enum &lhs, const SetCompare &rhs)
This is an overloaded member function, provided for convenience. It differs from the above function o...
constexpr std::string_view Url("url")