libzypp  17.23.8
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 
20 namespace zypp
21 {
22 
23  class Url;
24 
26  namespace filesystem
27  {
28 
30  //
31  // CLASS NAME : Pathname
32  //
44  class Pathname
45  {
46  public:
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  static Pathname assertprefix( const Pathname & root_r, const Pathname & path_r );
150 
152  static Pathname stripprefix( const Pathname & root_r, const Pathname & path_r );
153 
162  Pathname cat( const Pathname & r ) const { return cat( *this, r ); }
163  static Pathname cat( const Pathname & l, const Pathname & r );
164 
170  Pathname extend( const std::string & r ) const { return extend( *this, r ); }
171  static Pathname extend( const Pathname & l, const std::string & r );
172 
173  private:
174  std::string _name;
175  void _assign( const std::string & name_r );
176  };
178 
180  inline bool operator==( const Pathname & l, const Pathname & r )
181  { return l.asString() == r.asString(); }
182 
184  inline bool operator!=( const Pathname & l, const Pathname & r )
185  { return l.asString() != r.asString(); }
186 
188  inline Pathname operator/( const Pathname & l, const Pathname & r )
189  { return Pathname::cat( l, r ); }
190 
194  inline Pathname operator+( const Pathname & l, const Pathname & r )
195  { return Pathname::cat( l, r ); }
196 
198  inline bool operator<( const Pathname & l, const Pathname & r )
199  { return l.asString() < r.asString(); }
200 
202 
204  inline std::ostream & operator<<( std::ostream & str, const Pathname & obj )
205  { return str << obj.asString(); }
206 
208  } // namespace filesystem
210 
212  using filesystem::Pathname;
213 
215 } // namespace zypp
217 #endif // ZYPP_PATHNAME_H
zypp::filesystem::Pathname::_assign
void _assign(const std::string &name_r)
Definition: Pathname.cc:32
zypp::filesystem::Pathname::Pathname
Pathname(const char *name_r)
Ctor from char*.
Definition: Pathname.h:56
zypp::filesystem::Pathname::relativename
Pathname relativename() const
Return this path, removing a leading '/' if absolute.
Definition: Pathname.h:144
zypp::filesystem::Pathname::operator/
Pathname operator/(const Pathname &l, const Pathname &r)
Definition: Pathname.h:188
zypp::ResolverNamespace::filesystem
filesystems
zypp::filesystem::Pathname::absolute
bool absolute() const
Test for an absolute path.
Definition: Pathname.h:116
zypp::filesystem::Pathname::operator=
Pathname & operator=(Pathname rhs)
Assign.
Definition: Pathname.h:77
zypp::filesystem::Pathname::emptyOrRoot
bool emptyOrRoot() const
Test for "" or "/".
Definition: Pathname.h:121
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:135
zypp::filesystem::Pathname::asFileUrl
Url asFileUrl() const
Definition: Pathname.cc:185
zypp::filesystem::Pathname::absolutename
static Pathname absolutename(const Pathname &name_r)
Definition: Pathname.h:140
zypp::filesystem::Pathname::operator<<
std::ostream & operator<<(std::ostream &str, const Pathname &obj)
Definition: Pathname.h:204
zypp::filesystem::Pathname::operator+
Pathname operator+(const Pathname &l, const Pathname &r)
Definition: Pathname.h:194
zypp::filesystem::Pathname::operator==
bool operator==(const Pathname &l, const Pathname &r)
Definition: Pathname.h:180
zypp::filesystem::Pathname::assertprefix
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:235
zypp::filesystem::Pathname::Pathname
Pathname(const std::string &name_r)
Ctor from string.
Definition: Pathname.h:52
zypp::filesystem::Pathname::showRoot
static std::string showRoot(const Pathname &root_r, const Pathname &path_r)
String representation as "(root)/path".
Definition: Pathname.cc:189
zypp::filesystem::Pathname::stripprefix
static Pathname stripprefix(const Pathname &root_r, const Pathname &path_r)
Return path_r with any root_r dir prefix striped.
Definition: Pathname.cc:244
zypp::filesystem::Pathname::c_str
const char * c_str() const
String representation.
Definition: Pathname.h:110
zypp::filesystem::Pathname::operator/=
Pathname & operator/=(const Pathname &path_tv)
Concatenate and assign.
Definition: Pathname.h:81
zypp::filesystem::Pathname::asDirUrl
Url asDirUrl() const
Definition: Pathname.cc:182
zypp::filesystem::Pathname::absolutename
Pathname absolutename() const
Return this path, adding a leading '/' if relative.
Definition: Pathname.h:139
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
zypp::filesystem::Pathname::Pathname
Pathname(Pathname &&tmp)
Move Ctor.
Definition: Pathname.h:72
zypp::filesystem::Pathname::Pathname
Pathname()
Default ctor: an empty path.
Definition: Pathname.h:48
zypp::filesystem::Pathname::cat
Pathname cat(const Pathname &r) const
Concatenation of pathnames.
Definition: Pathname.h:162
zypp::filesystem::Pathname::dirname
Pathname dirname() const
Return all but the last component od this path.
Definition: Pathname.h:124
std
Definition: Arch.h:347
zypp::filesystem::Pathname::extend
Pathname extend(const std::string &r) const
Append string r to the last component of the path.
Definition: Pathname.h:170
zypp::filesystem::Pathname::empty
bool empty() const
Test for an empty path.
Definition: Pathname.h:114
zypp::filesystem::Pathname
Pathname.
Definition: Pathname.h:44
zypp::filesystem::Pathname::operator<
bool operator<(const Pathname &l, const Pathname &r)
Definition: Pathname.h:198
zypp::filesystem::Pathname::Pathname
Pathname(const Pathname &rhs)
Copy Ctor.
Definition: Pathname.h:60
zypp::filesystem::Pathname::basename
std::string basename() const
Return the last component of this path.
Definition: Pathname.h:128
zypp::filesystem::Pathname::swap
friend void swap(Pathname &lhs, Pathname &rhs)
Swap.
Definition: Pathname.h:65
zypp::filesystem::Pathname::operator!=
bool operator!=(const Pathname &l, const Pathname &r)
Definition: Pathname.h:184
str
String related utilities and Regular expression matching.
zypp::filesystem::Pathname::asString
const std::string & asString() const
String representation.
Definition: Pathname.h:91
zypp::filesystem::Pathname::relativename
static Pathname relativename(const Pathname &name_r)
Definition: Pathname.h:145
zypp::filesystem::Pathname::operator+=
Pathname & operator+=(const Pathname &path_tv)
Concatenate and assign.
Definition: Pathname.h:87
zypp::Url
Url manipulation class.
Definition: Url.h:87
zypp::filesystem::Pathname::_name
std::string _name
Definition: Pathname.h:174
zypp::filesystem::Pathname::asUrl
Url asUrl() const
Definition: Pathname.cc:179
zypp::filesystem::Pathname::showRootIf
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:194
zypp::filesystem::Pathname::relative
bool relative() const
Test for a relative path.
Definition: Pathname.h:118