libzypp  17.3.0
Pathname.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_PATHNAME_H
13 #define ZYPP_PATHNAME_H
14 
15 #include <iosfwd>
16 #include <string>
17 
19 namespace zypp
20 {
21 
22  class Url;
23 
25  namespace filesystem
26  {
27 
29  //
30  // CLASS NAME : Pathname
31  //
43  class Pathname
44  {
45  public:
48  {}
49 
51  Pathname( const std::string & name_r )
52  { _assign( name_r ); }
53 
55  Pathname( const char * name_r )
56  { _assign( name_r ? name_r : "" ); }
57 
59  Pathname( const Pathname & rhs )
60  : _name( rhs._name )
61  {}
62 
64  friend void swap( Pathname & lhs, Pathname & rhs )
65  {
66  using std::swap;
67  swap( lhs._name, rhs._name );
68  }
69 #ifndef SWIG // Swig treats it as syntax error
70 
71  Pathname( Pathname && tmp )
72  : _name( std::move( tmp._name ) )
73  {}
74 #endif
75 
77  { swap( *this, rhs ); return *this; }
78 
80  Pathname & operator/=( const Pathname & path_tv )
81  { return( *this = cat( *this, path_tv ) ); }
82 
86  Pathname & operator+=( const Pathname & path_tv )
87  { return( *this = cat( *this, path_tv ) ); }
88 
90  const std::string & asString() const
91  { return _name; }
92 
94  static std::string showRoot( const Pathname & root_r, const Pathname & path_r );
95 
97  static std::string showRootIf( const Pathname & root_r, const Pathname & path_r );
98 
100  Url asUrl( const std::string & scheme_r ) const;
102  Url asUrl() const;
104  Url asDirUrl() const;
106  Url asFileUrl() const;
107 
109  const char * c_str() const
110  { return _name.c_str(); }
111 
113  bool empty() const { return _name.empty(); }
115  bool absolute() const { return *_name.c_str() == '/'; }
117  bool relative() const { return !( absolute() || empty() ); }
118 
120  bool emptyOrRoot() const { return( _name.empty() || _name == "/" ); }
121 
123  Pathname dirname() const { return dirname( *this ); }
124  static Pathname dirname( const Pathname & name_r );
125 
127  std::string basename() const { return basename( *this ); }
128  static std::string basename( const Pathname & name_r );
129 
134  std::string extension() const { return extension( *this ); }
135  static std::string extension( const Pathname & name_r );
136 
138  Pathname absolutename() const { return absolutename( *this ); }
139  static Pathname absolutename( const Pathname & name_r )
140  { return name_r.relative() ? cat( "/", name_r ) : name_r; }
141 
143  Pathname relativename() const { return relativename( *this ); }
144  static Pathname relativename( const Pathname & name_r )
145  { return name_r.absolute() ? cat( ".", name_r ) : name_r; }
146 
148  static Pathname assertprefix( const Pathname & root_r, const Pathname & path_r );
149 
151  static Pathname stripprefix( const Pathname & root_r, const Pathname & path_r );
152 
161  Pathname cat( const Pathname & r ) const { return cat( *this, r ); }
162  static Pathname cat( const Pathname & l, const Pathname & r );
163 
169  Pathname extend( const std::string & r ) const { return extend( *this, r ); }
170  static Pathname extend( const Pathname & l, const std::string & r );
171 
172  private:
173  std::string _name;
174  void _assign( const std::string & name_r );
175  };
177 
179  inline bool operator==( const Pathname & l, const Pathname & r )
180  { return l.asString() == r.asString(); }
181 
183  inline bool operator!=( const Pathname & l, const Pathname & r )
184  { return l.asString() != r.asString(); }
185 
187  inline Pathname operator/( const Pathname & l, const Pathname & r )
188  { return Pathname::cat( l, r ); }
189 
193  inline Pathname operator+( const Pathname & l, const Pathname & r )
194  { return Pathname::cat( l, r ); }
195 
197  inline bool operator<( const Pathname & l, const Pathname & r )
198  { return l.asString() < r.asString(); }
199 
201 
203  inline std::ostream & operator<<( std::ostream & str, const Pathname & obj )
204  { return str << obj.asString(); }
205 
207  } // namespace filesystem
209 
211  using filesystem::Pathname;
212 
214 } // namespace zypp
216 #endif // ZYPP_PATHNAME_H
Pathname(const std::string &name_r)
Ctor from string.
Definition: Pathname.h:51
Pathname extend(const std::string &r) const
Append string r to the last component of the path.
Definition: Pathname.h:169
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
const char * c_str() const
String representation.
Definition: Pathname.h:109
String related utilities and Regular expression matching.
Definition: Arch.h:344
std::string basename() const
Return the last component of this path.
Definition: Pathname.h:127
friend void swap(Pathname &lhs, Pathname &rhs)
Swap.
Definition: Pathname.h:64
Pathname & operator+=(const Pathname &path_tv)
Concatenate and assign.
Definition: Pathname.h:86
Pathname operator/(const Pathname &l, const Pathname &r)
Definition: Pathname.h:187
bool emptyOrRoot() const
Test for "" or "/".
Definition: Pathname.h:120
bool empty() const
Test for an empty path.
Definition: Pathname.h:113
Pathname & operator=(Pathname rhs)
Assign.
Definition: Pathname.h:76
Pathname(Pathname &&tmp)
Move Ctor.
Definition: Pathname.h:71
bool operator<(const Pathname &l, const Pathname &r)
Definition: Pathname.h:197
const std::string & asString() const
String representation.
Definition: Pathname.h:90
Pathname(const char *name_r)
Ctor from char*.
Definition: Pathname.h:55
Pathname dirname() const
Return all but the last component od this path.
Definition: Pathname.h:123
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
Pathname relativename() const
Return this path, removing a leading &#39;/&#39; if absolute.
Definition: Pathname.h:143
Pathname()
Default ctor: an empty path.
Definition: Pathname.h:47
bool absolute() const
Test for an absolute path.
Definition: Pathname.h:115
bool operator!=(const Pathname &l, const Pathname &r)
Definition: Pathname.h:183
static std::string showRoot(const Pathname &root_r, const Pathname &path_r)
String representation as "(root)/path".
Definition: Pathname.cc:189
Pathname(const Pathname &rhs)
Copy Ctor.
Definition: Pathname.h:59
std::ostream & operator<<(std::ostream &str, const Pathname &obj)
Definition: Pathname.h:203
Pathname operator+(const Pathname &l, const Pathname &r)
Definition: Pathname.h:193
bool operator==(const Pathname &l, const Pathname &r)
Definition: Pathname.h:179
static Pathname relativename(const Pathname &name_r)
Definition: Pathname.h:144
Pathname absolutename() const
Return this path, adding a leading &#39;/&#39; if relative.
Definition: Pathname.h:138
Pathname & operator/=(const Pathname &path_tv)
Concatenate and assign.
Definition: Pathname.h:80
void _assign(const std::string &name_r)
Definition: Pathname.cc:32
static Pathname absolutename(const Pathname &name_r)
Definition: Pathname.h:139
bool relative() const
Test for a relative path.
Definition: Pathname.h:117
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
Pathname cat(const Pathname &r) const
Concatenation of pathnames.
Definition: Pathname.h:161
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
Url manipulation class.
Definition: Url.h:87
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:134