a0375f72333000f76d867f93020c3eea4d013a3f
[platform/upstream/libzypp.git] / zypp / Pathname.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/Pathname.h
10  *
11 */
12 #ifndef ZYPP_PATHNAME_H
13 #define ZYPP_PATHNAME_H
14
15 #include <iosfwd>
16 #include <string>
17
18 ///////////////////////////////////////////////////////////////////
19 namespace zypp
20 { /////////////////////////////////////////////////////////////////
21
22   class Url;
23
24   ///////////////////////////////////////////////////////////////////
25   namespace filesystem
26   { /////////////////////////////////////////////////////////////////
27
28     ///////////////////////////////////////////////////////////////////
29     //
30     //  CLASS NAME : Pathname
31     //
32     /** Pathname.
33      *
34      * \note For convenience Pathname is available as zypp::Pathname too.
35      *
36      * Always stores normalized paths (no inner '.' or '..' components
37      * and no consecutive '/'es). Concatenation automatically adds
38      * the path separator '/'.
39      *
40      * \todo Add support for handling extensions incl. stripping
41      * extensions from basename (basename("/path/foo.baa", ".baa") ==> "foo")
42     */
43     class Pathname
44     {
45     public:
46       /** Default ctor: an empty path. */
47       Pathname()
48       {}
49
50       /** Ctor from string. */
51       Pathname( const std::string & name_r )
52       { _assign( name_r ); }
53
54       /** Ctor from char*. */
55       Pathname( const char * name_r )
56       { _assign( name_r ? name_r : "" ); }
57
58       /** Copy Ctor */
59       Pathname( const Pathname & rhs )
60       : _name( rhs._name )
61       {}
62
63       /** Swap */
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       /** Move Ctor */
71       Pathname( Pathname && tmp )
72       : _name( std::move( tmp._name ) )
73       {}
74 #endif
75       /** Assign */
76       Pathname & operator=( Pathname rhs )
77       { swap( *this, rhs ); return *this; }
78
79       /** Concatenate and assing. \see cat */
80       Pathname & operator/=( const Pathname & path_tv )
81       { return( *this = cat( *this, path_tv ) ); }
82
83       /** Concatenate and assing. \see cat
84        * \deprecated: use /=
85       */
86       Pathname & operator+=( const Pathname & path_tv )
87       { return( *this = cat( *this, path_tv ) ); }
88
89       /** String representation. */
90       const std::string & asString() const
91       { return _name; }
92
93       /** String representation as "(root)/path" */
94       static std::string showRoot( const Pathname & root_r, const Pathname & path_r );
95
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 );
98
99       /** Url representation using \c scheme_r schema . */
100       Url asUrl( const std::string & scheme_r ) const;
101       /** \overload using \c dir schema. */
102       Url asUrl() const;
103       /** \overload using \c dir schema. */
104       Url asDirUrl() const;
105       /** \overload using \c file schema. */
106       Url asFileUrl() const;
107
108       /** String representation. */
109       const char * c_str() const
110       { return _name.c_str(); }
111
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() ); }
118
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 );
122
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 );
126
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.
130       */
131       std::string extension() const { return extension( *this ); }
132       static std::string extension( const Pathname & name_r );
133
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; }
138
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; }
143
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 );
146
147       /** Concatenation of pathnames.
148        * \code
149        *   "foo"  / "baa"  ==> "foo/baa"
150        *   "foo/" / "baa"  ==> "foo/baa"
151        *   "foo"  / "/baa" ==> "foo/baa"
152        *   "foo/" / "/baa" ==> "foo/baa"
153        * \endcode
154       */
155       Pathname cat( const Pathname & r ) const { return cat( *this, r ); }
156       static Pathname cat( const Pathname & l, const Pathname & r );
157
158       /** Append string \a r to the last component of the path.
159        * \code
160        *   "foo/baa".extend( ".h" ) ==> "foo/baa.h"
161        * \endcode
162       */
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     };
170     ///////////////////////////////////////////////////////////////////
171
172     /** \relates Pathname */
173     inline bool operator==( const Pathname & l, const Pathname & r )
174     { return l.asString() == r.asString(); }
175
176     /** \relates Pathname */
177     inline bool operator!=( const Pathname & l, const Pathname & r )
178     { return l.asString() != r.asString(); }
179
180     /** \relates Pathname Concatenate two Pathname. */
181     inline Pathname operator/( const Pathname & l, const Pathname & r )
182     { return Pathname::cat( l, r ); }
183
184     /** \relates Pathname Concatenate two Pathname.
185      * \deprecated: use /
186     */
187     inline Pathname operator+( const Pathname & l, const Pathname & r )
188     { return Pathname::cat( l, r ); }
189
190     /** \relates Pathname */
191     inline bool operator<( const Pathname & l, const Pathname & r )
192     { return l.asString() < r.asString(); }
193
194     ///////////////////////////////////////////////////////////////////
195
196     /** \relates Pathname Stream output */
197     inline std::ostream & operator<<( std::ostream & str, const Pathname & obj )
198     { return str << obj.asString(); }
199
200     /////////////////////////////////////////////////////////////////
201   } // namespace filesystem
202   ///////////////////////////////////////////////////////////////////
203
204   /** Dragged into namespace zypp. */
205   using filesystem::Pathname;
206
207   /////////////////////////////////////////////////////////////////
208 } // namespace zypp
209 ///////////////////////////////////////////////////////////////////
210 #endif // ZYPP_PATHNAME_H