libzypp  16.22.5
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 
158  Pathname cat( const Pathname & r ) const { return cat( *this, r ); }
159  static Pathname cat( const Pathname & l, const Pathname & r );
160 
166  Pathname extend( const std::string & r ) const { return extend( *this, r ); }
167  static Pathname extend( const Pathname & l, const std::string & r );
168 
169  private:
170  std::string _name;
171  void _assign( const std::string & name_r );
172  };
174 
176  inline bool operator==( const Pathname & l, const Pathname & r )
177  { return l.asString() == r.asString(); }
178 
180  inline bool operator!=( const Pathname & l, const Pathname & r )
181  { return l.asString() != r.asString(); }
182 
184  inline Pathname operator/( const Pathname & l, const Pathname & r )
185  { return Pathname::cat( l, r ); }
186 
190  inline Pathname operator+( const Pathname & l, const Pathname & r )
191  { return Pathname::cat( l, r ); }
192 
194  inline bool operator<( const Pathname & l, const Pathname & r )
195  { return l.asString() < r.asString(); }
196 
198 
200  inline std::ostream & operator<<( std::ostream & str, const Pathname & obj )
201  { return str << obj.asString(); }
202 
204  } // namespace filesystem
206 
208  using filesystem::Pathname;
209 
211 } // namespace zypp
213 #endif // ZYPP_PATHNAME_H
Pathname absolutename() const
Return this path, adding a leading '/' if relative.
Definition: Pathname.h:138
Pathname(const std::string &name_r)
Ctor from string.
Definition: Pathname.h:51
std::string basename() const
Return the last component of this path.
Definition: Pathname.h:127
Pathname relativename() const
Return this path, removing a leading '/' if absolute.
Definition: Pathname.h:143
bool empty() const
Test for an empty path.
Definition: Pathname.h:113
const std::string & asString() const
String representation.
Definition: Pathname.h:90
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
Pathname dirname() const
Return all but the last component od this path.
Definition: Pathname.h:123
bool relative() const
Test for a relative path.
Definition: Pathname.h:117
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:184
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:194
Pathname(const char *name_r)
Ctor from char*.
Definition: Pathname.h:55
const char * c_str() const
String representation.
Definition: Pathname.h:109
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()
Default ctor: an empty path.
Definition: Pathname.h:47
bool operator!=(const Pathname &l, const Pathname &r)
Definition: Pathname.h:180
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
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:200
Pathname operator+(const Pathname &l, const Pathname &r)
Definition: Pathname.h:190
bool operator==(const Pathname &l, const Pathname &r)
Definition: Pathname.h:176
Pathname cat(const Pathname &r) const
Concatenation of pathnames.
Definition: Pathname.h:158
static Pathname relativename(const Pathname &name_r)
Definition: Pathname.h:144
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 absolute() const
Test for an absolute path.
Definition: Pathname.h:115
Url manipulation class.
Definition: Url.h:87
bool emptyOrRoot() const
Test for "" or "/".
Definition: Pathname.h:120
Pathname extend(const std::string &r) const
Append string r to the last component of the path.
Definition: Pathname.h:166