1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Pathname.h
12 #ifndef ZYPP_PATHNAME_H
13 #define ZYPP_PATHNAME_H
18 ///////////////////////////////////////////////////////////////////
20 { /////////////////////////////////////////////////////////////////
24 ///////////////////////////////////////////////////////////////////
26 { /////////////////////////////////////////////////////////////////
28 ///////////////////////////////////////////////////////////////////
30 // CLASS NAME : Pathname
34 * \note For convenience Pathname is available as zypp::Pathname too.
36 * Always stores normalized paths (no inner '.' or '..' components
37 * and no consecutive '/'es). Concatenation automatically adds
38 * the path separator '/'.
40 * \todo Add support for handling extensions incl. stripping
41 * extensions from basename (basename("/path/foo.baa", ".baa") ==> "foo")
46 /** Default ctor: an empty path. */
50 /** Ctor from string. */
51 Pathname( const std::string & name_r )
52 { _assign( name_r ); }
54 /** Ctor from char*. */
55 Pathname( const char * name_r )
56 { _assign( name_r ? name_r : "" ); }
59 Pathname( const Pathname & rhs )
64 friend void swap( Pathname & lhs, Pathname & rhs )
67 swap( lhs._name, rhs._name );
69 #ifndef SWIG // Swig treats it as syntax error
71 Pathname( Pathname && tmp )
72 : _name( std::move( tmp._name ) )
76 Pathname & operator=( Pathname rhs )
77 { swap( *this, rhs ); return *this; }
79 /** Concatenate and assing. \see cat */
80 Pathname & operator/=( const Pathname & path_tv )
81 { return( *this = cat( *this, path_tv ) ); }
83 /** Concatenate and assing. \see cat
86 Pathname & operator+=( const Pathname & path_tv )
87 { return( *this = cat( *this, path_tv ) ); }
89 /** String representation. */
90 const std::string & asString() const
93 /** String representation as "(root)/path" */
94 static std::string showRoot( const Pathname & root_r, const Pathname & path_r );
96 /** String representation as "(root)/path", unless \a root is \c "/" or empty. */
97 static std::string showRootIf( const Pathname & root_r, const Pathname & path_r );
99 /** Url representation using \c scheme_r schema . */
100 Url asUrl( const std::string & scheme_r ) const;
101 /** \overload using \c dir schema. */
103 /** \overload using \c dir schema. */
104 Url asDirUrl() const;
105 /** \overload using \c file schema. */
106 Url asFileUrl() const;
108 /** String representation. */
109 const char * c_str() const
110 { return _name.c_str(); }
112 /** Test for an empty path. */
113 bool empty() const { return _name.empty(); }
114 /** Test for an absolute path. */
115 bool absolute() const { return *_name.c_str() == '/'; }
116 /** Test for a relative path. */
117 bool relative() const { return !( absolute() || empty() ); }
119 /** Return all but the last component od this path. */
120 Pathname dirname() const { return dirname( *this ); }
121 static Pathname dirname( const Pathname & name_r );
123 /** Return the last component of this path. */
124 std::string basename() const { return basename( *this ); }
125 static std::string basename( const Pathname & name_r );
127 /** Return all of the characters in name after and including
128 * the last dot in the last element of name. If there is no dot
129 * in the last element of name then returns the empty string.
131 std::string extension() const { return extension( *this ); }
132 static std::string extension( const Pathname & name_r );
134 /** Return this path, adding a leading '/' if relative. */
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; }
139 /** Return this path, removing a leading '/' if absolute.*/
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; }
144 /** Return \c path_r prefixed with \c root_r, unless it is already prefixed. */
145 static Pathname assertprefix( const Pathname & root_r, const Pathname & path_r );
147 /** Concatenation of pathnames.
149 * "foo" / "baa" ==> "foo/baa"
150 * "foo/" / "baa" ==> "foo/baa"
151 * "foo" / "/baa" ==> "foo/baa"
152 * "foo/" / "/baa" ==> "foo/baa"
155 Pathname cat( const Pathname & r ) const { return cat( *this, r ); }
156 static Pathname cat( const Pathname & l, const Pathname & r );
158 /** Append string \a r to the last component of the path.
160 * "foo/baa".extend( ".h" ) ==> "foo/baa.h"
163 Pathname extend( const std::string & r ) const { return extend( *this, r ); }
164 static Pathname extend( const Pathname & l, const std::string & r );
168 void _assign( const std::string & name_r );
170 ///////////////////////////////////////////////////////////////////
172 /** \relates Pathname */
173 inline bool operator==( const Pathname & l, const Pathname & r )
174 { return l.asString() == r.asString(); }
176 /** \relates Pathname */
177 inline bool operator!=( const Pathname & l, const Pathname & r )
178 { return l.asString() != r.asString(); }
180 /** \relates Pathname Concatenate two Pathname. */
181 inline Pathname operator/( const Pathname & l, const Pathname & r )
182 { return Pathname::cat( l, r ); }
184 /** \relates Pathname Concatenate two Pathname.
187 inline Pathname operator+( const Pathname & l, const Pathname & r )
188 { return Pathname::cat( l, r ); }
190 /** \relates Pathname */
191 inline bool operator<( const Pathname & l, const Pathname & r )
192 { return l.asString() < r.asString(); }
194 ///////////////////////////////////////////////////////////////////
196 /** \relates Pathname Stream output */
197 inline std::ostream & operator<<( std::ostream & str, const Pathname & obj )
198 { return str << obj.asString(); }
200 /////////////////////////////////////////////////////////////////
201 } // namespace filesystem
202 ///////////////////////////////////////////////////////////////////
204 /** Dragged into namespace zypp. */
205 using filesystem::Pathname;
207 /////////////////////////////////////////////////////////////////
209 ///////////////////////////////////////////////////////////////////
210 #endif // ZYPP_PATHNAME_H