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 /** String representation. */
71 const std::string & asString() const
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] != '/'; }
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 );
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 );
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; }
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; }
99 /** Concatenation of pathnames.
101 * "foo" + "baa" ==> "foo/baa"
102 * "foo/" + "baa" ==> "foo/baa"
103 * "foo" + "/baa" ==> "foo/baa"
104 * "foo/" + "/baa" ==> "foo/baa"
107 Pathname cat( const Pathname & r ) const { return cat( *this, r ); }
108 static Pathname cat( const Pathname & l, const Pathname & r );
110 /** Append string \a r to the last component of the path.
112 * "foo/baa".extend( ".h" ) ==> "foo/baa.h"
115 Pathname extend( const std::string & r ) const { return extend( *this, r ); }
116 static Pathname extend( const Pathname & l, const std::string & r );
119 std::string::size_type prfx_i;
122 void _assign( const std::string & name_tv );
124 ///////////////////////////////////////////////////////////////////
126 /** \relates Pathname */
127 inline bool operator==( const Pathname & l, const Pathname & r )
128 { return l.asString() == r.asString(); }
130 /** \relates Pathname */
131 inline bool operator!=( const Pathname & l, const Pathname & r )
132 { return l.asString() != r.asString(); }
134 /** \relates Pathname Concatenate two Pathname. */
135 inline Pathname operator+( const Pathname & l, const Pathname & r )
136 { return Pathname::cat( l, r ); }
138 ///////////////////////////////////////////////////////////////////
140 /** \relates Pathname Stream output */
141 inline std::ostream & operator<<( std::ostream & str, const Pathname & obj )
142 { return str << obj.asString(); }
144 /////////////////////////////////////////////////////////////////
145 } // namespace filesystem
146 ///////////////////////////////////////////////////////////////////
148 /** Dragged into namespace zypp. */
149 using filesystem::Pathname;
151 /////////////////////////////////////////////////////////////////
153 ///////////////////////////////////////////////////////////////////
154 #endif // ZYPP_PATHNAME_H