libzypp  17.14.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