libzypp 17.38.5

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
23 namespace zypp
24{
25
26 class Url;
27
29 namespace filesystem
30 {
31
33 //
34 // CLASS NAME : Pathname
35 //
47 class ZYPP_API Pathname
48 {
49 public:
51 Pathname()
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
80 Pathname & operator=( Pathname rhs )
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
224 using filesystem::Pathname;
225
227} // namespace zypp
229#endif // ZYPP_PATHNAME_H
Globals.h
Provides API related macros.
zypp::Url
Url manipulation class.
Definition Url.h:93
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:48
zypp::filesystem::Pathname::relativename
Pathname relativename() const
Return this path, removing a leading '/' if absolute.
Definition Pathname.h:153
zypp::filesystem::Pathname::absoluteNotRoot
bool absoluteNotRoot() const
Test for a not empty absolute Path (!= "/").
Definition Pathname.h:130
zypp::filesystem::Pathname::extend
Pathname extend(const std::string &r) const
Append string r to the last component of the path.
Definition Pathname.h:182
zypp::filesystem::Pathname::extend
static Pathname extend(const Pathname &l, const std::string &r)
zypp::filesystem::Pathname::Pathname
Pathname(const Pathname &rhs)
Copy Ctor.
Definition Pathname.h:63
zypp::filesystem::Pathname::operator<<
std::ostream & operator<<(std::ostream &str, const Pathname &obj)
Stream output.
Definition Pathname.h:216
zypp::filesystem::Pathname::operator/=
Pathname & operator/=(const Pathname &path_tv)
Concatenate and assign.
Definition Pathname.h:84
zypp::filesystem::Pathname::swap
friend void swap(Pathname &lhs, Pathname &rhs) noexcept
Swap.
Definition Pathname.h:68
zypp::filesystem::Pathname::Pathname
Pathname(Pathname &&tmp) noexcept
Move Ctor.
Definition Pathname.h:75
zypp::filesystem::Pathname::relativeDotDot
bool relativeDotDot() const
Test for a relative path referring to ../.
Definition Pathname.h:124
zypp::filesystem::Pathname::absolutename
static Pathname absolutename(const Pathname &name_r)
Definition Pathname.h:149
zypp::filesystem::Pathname::operator+=
Pathname & operator+=(const Pathname &path_tv)
Concatenate and assign.
Definition Pathname.h:90
zypp::filesystem::Pathname::dirname
Pathname dirname() const
Return all but the last component od this path.
Definition Pathname.h:133
zypp::filesystem::Pathname::emptyOrRoot
bool emptyOrRoot() const
Test for "" or "/".
Definition Pathname.h:127
zypp::filesystem::Pathname::absolute
bool absolute() const
Test for an absolute path.
Definition Pathname.h:119
zypp::filesystem::Pathname::operator+
Pathname operator+(const Pathname &l, const Pathname &r)
Concatenate two Pathname.
Definition Pathname.h:206
zypp::filesystem::Pathname::operator!=
bool operator!=(const Pathname &l, const Pathname &r)
Definition Pathname.h:196
zypp::filesystem::Pathname::cat
Pathname cat(const Pathname &r) const
Concatenation of pathnames.
Definition Pathname.h:174
zypp::filesystem::Pathname::Pathname
Pathname()
Default ctor: an empty path.
Definition Pathname.h:51
zypp::filesystem::Pathname::c_str
const char * c_str() const
String representation.
Definition Pathname.h:113
zypp::filesystem::Pathname::Pathname
Pathname(const char *name_r)
Ctor from char*.
Definition Pathname.h:59
zypp::filesystem::Pathname::asString
const std::string & asString() const
String representation.
Definition Pathname.h:94
zypp::filesystem::Pathname::basename
std::string basename() const
Return the last component of this path.
Definition Pathname.h:137
zypp::filesystem::Pathname::empty
bool empty() const
Test for an empty path.
Definition Pathname.h:117
zypp::filesystem::Pathname::operator==
bool operator==(const Pathname &l, const Pathname &r)
Definition Pathname.h:192
zypp::filesystem::Pathname::_assign
void _assign(const std::string &name_r)
Definition Pathname.cc:33
zypp::filesystem::Pathname::operator=
Pathname & operator=(Pathname rhs)
Assign.
Definition Pathname.h:80
zypp::filesystem::Pathname::absolutename
Pathname absolutename() const
Return this path, adding a leading '/' if relative.
Definition Pathname.h:148
zypp::filesystem::Pathname::Pathname
Pathname(const std::string &name_r)
Ctor from string.
Definition Pathname.h:55
zypp::filesystem::Pathname::operator<
bool operator<(const Pathname &l, const Pathname &r)
Definition Pathname.h:210
zypp::filesystem::Pathname::operator/
Pathname operator/(const Pathname &l, const Pathname &r)
Concatenate two Pathname.
Definition Pathname.h:200
zypp::filesystem::Pathname::relativename
static Pathname relativename(const Pathname &name_r)
Definition Pathname.h:154
zypp::filesystem::Pathname::extension
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
zypp::filesystem::Pathname::_name
std::string _name
Definition Pathname.h:186
zypp::filesystem::Pathname::relative
bool relative() const
Test for a relative path.
Definition Pathname.h:121
str
String related utilities and Regular expression matching.
zypp::filesystem
Types and functions for filesystem operations.
Definition Glob.cc:24
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::ZYPP_API
const Arch Arch_armv7hnl Arch_armv7nhl ZYPP_API
Definition Arch.h:247