libzypp  13.10.6
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  Pathname dirname() const { return dirname( *this ); }
121  static Pathname dirname( const Pathname & name_r );
122 
124  std::string basename() const { return basename( *this ); }
125  static std::string basename( const Pathname & name_r );
126 
131  std::string extension() const { return extension( *this ); }
132  static std::string extension( const Pathname & name_r );
133 
135  Pathname absolutename() const { return absolutename( *this ); }
136  static Pathname absolutename( const Pathname & name_r )
137  { return name_r.relative() ? cat( "/", name_r ) : name_r; }
138 
140  Pathname relativename() const { return relativename( *this ); }
141  static Pathname relativename( const Pathname & name_r )
142  { return name_r.absolute() ? cat( ".", name_r ) : name_r; }
143 
145  static Pathname assertprefix( const Pathname & root_r, const Pathname & path_r );
146 
155  Pathname cat( const Pathname & r ) const { return cat( *this, r ); }
156  static Pathname cat( const Pathname & l, const Pathname & r );
157 
163  Pathname extend( const std::string & r ) const { return extend( *this, r ); }
164  static Pathname extend( const Pathname & l, const std::string & r );
165 
166  private:
167  std::string _name;
168  void _assign( const std::string & name_r );
169  };
171 
173  inline bool operator==( const Pathname & l, const Pathname & r )
174  { return l.asString() == r.asString(); }
175 
177  inline bool operator!=( const Pathname & l, const Pathname & r )
178  { return l.asString() != r.asString(); }
179 
181  inline Pathname operator/( const Pathname & l, const Pathname & r )
182  { return Pathname::cat( l, r ); }
183 
187  inline Pathname operator+( const Pathname & l, const Pathname & r )
188  { return Pathname::cat( l, r ); }
189 
191  inline bool operator<( const Pathname & l, const Pathname & r )
192  { return l.asString() < r.asString(); }
193 
195 
197  inline std::ostream & operator<<( std::ostream & str, const Pathname & obj )
198  { return str << obj.asString(); }
199 
201  } // namespace filesystem
203 
205  using filesystem::Pathname;
206 
208 } // namespace zypp
210 #endif // ZYPP_PATHNAME_H
Pathname absolutename() const
Return this path, adding a leading &#39;/&#39; if relative.
Definition: Pathname.h:135
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:124
Pathname relativename() const
Return this path, removing a leading &#39;/&#39; if absolute.
Definition: Pathname.h:140
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 &quot;(root)/path&quot;, unless root is &quot;/&quot; or empty.
Definition: Pathname.cc:194
Pathname dirname() const
Return all but the last component od this path.
Definition: Pathname.h:120
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 assing.
Definition: Pathname.h:86
Pathname operator/(const Pathname &l, const Pathname &r)
Definition: Pathname.h:181
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:191
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:177
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:131
static std::string showRoot(const Pathname &root_r, const Pathname &path_r)
String representation as &quot;(root)/path&quot;.
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:197
Pathname operator+(const Pathname &l, const Pathname &r)
Definition: Pathname.h:187
bool operator==(const Pathname &l, const Pathname &r)
Definition: Pathname.h:173
Pathname cat(const Pathname &r) const
Concatenation of pathnames.
Definition: Pathname.h:155
static Pathname relativename(const Pathname &name_r)
Definition: Pathname.h:141
Pathname & operator/=(const Pathname &path_tv)
Concatenate and assing.
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:136
bool absolute() const
Test for an absolute path.
Definition: Pathname.h:115
Url manipulation class.
Definition: Url.h:87
Pathname extend(const std::string &r) const
Append string r to the last component of the path.
Definition: Pathname.h:163