- Pathname,PathInfo
[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   namespace filesystem
23   { /////////////////////////////////////////////////////////////////
24
25     ///////////////////////////////////////////////////////////////////
26     //
27     //  CLASS NAME : Pathname
28     //
29     /** Pathname.
30      *
31      * \note For convenience Pathname is available as zypp::Pathname too.
32      *
33      * Always stores normalized paths (no inner '.' or '..' components
34      * and no consecutive '/'es). Concatenation automatically adds
35      * the path separator '/'.
36      *
37      * \todo Add support for handling extensions incl. stripping
38      * extensions from basename (basename("/path/foo.baa", ".baa") ==> "foo")
39      * \todo Review. Maybe use COW pimpl, ckeck storage.
40      * \todo \b EXPLICIT ctors.
41     */
42     class Pathname
43     {
44     public:
45       /** Default ctor: an empty path. */
46       Pathname()
47       : prfx_i( 0 )
48       {}
49
50       /** Ctor from string. */
51       Pathname( const std::string & name_tv )
52       { _assign( name_tv ); }
53
54       /** Ctor from char*. */
55       Pathname( const char * name_tv )
56       { _assign( name_tv ? name_tv : "" ); }
57
58       /** Assign */
59       Pathname & operator=( const Pathname & path_tv )
60       {
61         prfx_i = path_tv.prfx_i;
62         name_t = path_tv.name_t;
63         return *this;
64       }
65
66       /** Concatenate and assing. \see cat */
67       Pathname & operator+=( const Pathname & path_tv )
68       { return( *this = cat( *this, path_tv ) ); }
69
70       /** String representation. */
71       const std::string & asString() const
72       { return name_t; }
73
74       /** Test for an empty path. */
75       bool empty()    const { return name_t.empty(); }
76       /** Test for an absolute path. */
77       bool absolute() const { return !empty() && name_t[prfx_i] == '/'; }
78       /** Test for a relative path. */
79       bool relative() const { return !empty() && name_t[prfx_i] != '/'; }
80
81       /** Return all but the last component od this path. */
82       Pathname dirname() const { return dirname( *this ); }
83       static Pathname dirname( const Pathname & name_tv );
84
85       /** Return the last component of this path. */
86       std::string basename() const { return basename( *this ); }
87       static std::string basename( const Pathname & name_tv );
88
89       /** Return this path, adding a leading '/' if relative. */
90       Pathname absolutename() const { return absolutename( *this ); }
91       static Pathname absolutename( const Pathname & name_tv )
92       { return name_tv.relative() ? cat( "/", name_tv ) : name_tv; }
93
94       /** Return this path, removing a leading '/' if absolute.*/
95       Pathname relativename() const { return relativename( *this ); }
96       static Pathname relativename( const Pathname & name_tv )
97       { return name_tv.absolute() ? cat( ".", name_tv ) : name_tv; }
98
99       /** Concatenation of pathnames.
100        * \code
101        *   "foo"  + "baa"  ==> "foo/baa"
102        *   "foo/" + "baa"  ==> "foo/baa"
103        *   "foo"  + "/baa" ==> "foo/baa"
104        *   "foo/" + "/baa" ==> "foo/baa"
105        * \endcode
106       */
107       Pathname cat( const Pathname & r ) const { return cat( *this, r ); }
108       static Pathname cat( const Pathname & l, const Pathname & r );
109
110       /** Append string \a r to the last component of the path.
111        * \code
112        *   "foo/baa".extend( ".h" ) ==> "foo/baa.h"
113        * \endcode
114       */
115       Pathname extend( const std::string & r ) const { return extend( *this, r ); }
116       static Pathname extend( const Pathname & l, const std::string & r );
117
118     private:
119       std::string::size_type prfx_i;
120       std::string            name_t;
121
122       void _assign( const std::string & name_tv );
123     };
124     ///////////////////////////////////////////////////////////////////
125
126     /** \relates Pathname */
127     inline bool operator==( const Pathname & l, const Pathname & r )
128     { return l.asString() == r.asString(); }
129
130     /** \relates Pathname */
131     inline bool operator!=( const Pathname & l, const Pathname & r )
132     { return l.asString() != r.asString(); }
133
134     /** \relates Pathname Concatenate two Pathname. */
135     inline Pathname operator+( const Pathname & l, const Pathname & r )
136     { return Pathname::cat( l, r ); }
137
138     ///////////////////////////////////////////////////////////////////
139
140     /** \relates Pathname Stream output */
141     inline std::ostream & operator<<( std::ostream & str, const Pathname & obj )
142     { return str << obj.asString(); }
143
144     /////////////////////////////////////////////////////////////////
145   } // namespace filesystem
146   ///////////////////////////////////////////////////////////////////
147
148   /** Dragged into namespace zypp. */
149   using filesystem::Pathname;
150
151   /////////////////////////////////////////////////////////////////
152 } // namespace zypp
153 ///////////////////////////////////////////////////////////////////
154 #endif // ZYPP_PATHNAME_H