1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Pathname.h
12 #ifndef ZYPP_PATHNAME_H
13 #define ZYPP_PATHNAME_H
18 ///////////////////////////////////////////////////////////////////
20 { /////////////////////////////////////////////////////////////////
21 ///////////////////////////////////////////////////////////////////
23 { /////////////////////////////////////////////////////////////////
25 ///////////////////////////////////////////////////////////////////
27 // CLASS NAME : Pathname
31 * \note For convenience Pathname is available as zypp::Pathname too.
33 * Always stores normalized paths (no inner '.' or '..' components
34 * and no consecutive '/'es). Concatenation automatically adds
35 * the path separator '/'.
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.
45 /** Default ctor: an empty path. */
50 /** Ctor from string. */
51 Pathname( const std::string & name_tv )
52 { _assign( name_tv ); }
54 /** Ctor from char*. */
55 Pathname( const char * name_tv )
56 { _assign( name_tv ? name_tv : "" ); }
59 Pathname & operator=( const Pathname & path_tv )
61 prfx_i = path_tv.prfx_i;
62 name_t = path_tv.name_t;
66 /** Concatenate and assing. \see cat */
67 Pathname & operator/=( const Pathname & path_tv )
68 { return( *this = cat( *this, path_tv ) ); }
70 /** Concatenate and assing. \see cat
73 Pathname & operator+=( const Pathname & path_tv )
74 { return( *this = cat( *this, path_tv ) ); }
76 /** String representation. */
77 const std::string & asString() const
80 /** Test for an empty path. */
81 bool empty() const { return name_t.empty(); }
82 /** Test for an absolute path. */
83 bool absolute() const { return !empty() && name_t[prfx_i] == '/'; }
84 /** Test for a relative path. */
85 bool relative() const { return !empty() && name_t[prfx_i] != '/'; }
87 /** Return all but the last component od this path. */
88 Pathname dirname() const { return dirname( *this ); }
89 static Pathname dirname( const Pathname & name_tv );
91 /** Return the last component of this path. */
92 std::string basename() const { return basename( *this ); }
93 static std::string basename( const Pathname & name_tv );
95 /** Return this path, adding a leading '/' if relative. */
96 Pathname absolutename() const { return absolutename( *this ); }
97 static Pathname absolutename( const Pathname & name_tv )
98 { return name_tv.relative() ? cat( "/", name_tv ) : name_tv; }
100 /** Return this path, removing a leading '/' if absolute.*/
101 Pathname relativename() const { return relativename( *this ); }
102 static Pathname relativename( const Pathname & name_tv )
103 { return name_tv.absolute() ? cat( ".", name_tv ) : name_tv; }
105 /** Concatenation of pathnames.
107 * "foo" / "baa" ==> "foo/baa"
108 * "foo/" / "baa" ==> "foo/baa"
109 * "foo" / "/baa" ==> "foo/baa"
110 * "foo/" / "/baa" ==> "foo/baa"
113 Pathname cat( const Pathname & r ) const { return cat( *this, r ); }
114 static Pathname cat( const Pathname & l, const Pathname & r );
116 /** Append string \a r to the last component of the path.
118 * "foo/baa".extend( ".h" ) ==> "foo/baa.h"
121 Pathname extend( const std::string & r ) const { return extend( *this, r ); }
122 static Pathname extend( const Pathname & l, const std::string & r );
125 std::string::size_type prfx_i;
128 void _assign( const std::string & name_tv );
130 ///////////////////////////////////////////////////////////////////
132 /** \relates Pathname */
133 inline bool operator==( const Pathname & l, const Pathname & r )
134 { return l.asString() == r.asString(); }
136 /** \relates Pathname */
137 inline bool operator!=( const Pathname & l, const Pathname & r )
138 { return l.asString() != r.asString(); }
140 /** \relates Pathname Concatenate two Pathname. */
141 inline Pathname operator/( const Pathname & l, const Pathname & r )
142 { return Pathname::cat( l, r ); }
144 /** \relates Pathname Concatenate two Pathname.
147 inline Pathname operator+( const Pathname & l, const Pathname & r )
148 { return Pathname::cat( l, r ); }
150 ///////////////////////////////////////////////////////////////////
152 /** \relates Pathname Stream output */
153 inline std::ostream & operator<<( std::ostream & str, const Pathname & obj )
154 { return str << obj.asString(); }
156 /////////////////////////////////////////////////////////////////
157 } // namespace filesystem
158 ///////////////////////////////////////////////////////////////////
160 /** Dragged into namespace zypp. */
161 using filesystem::Pathname;
163 /////////////////////////////////////////////////////////////////
165 ///////////////////////////////////////////////////////////////////
166 #endif // ZYPP_PATHNAME_H