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 /** String representation. */
81 const char * c_str() const
82 { return name_t.c_str(); }
84 /** Test for an empty path. */
85 bool empty() const { return name_t.empty(); }
86 /** Test for an absolute path. */
87 bool absolute() const { return !empty() && name_t[prfx_i] == '/'; }
88 /** Test for a relative path. */
89 bool relative() const { return !empty() && name_t[prfx_i] != '/'; }
91 /** Return all but the last component od this path. */
92 Pathname dirname() const { return dirname( *this ); }
93 static Pathname dirname( const Pathname & name_tv );
95 /** Return the last component of this path. */
96 std::string basename() const { return basename( *this ); }
97 static std::string basename( const Pathname & name_tv );
99 /** Return all of the characters in name after and including
100 * the last dot in the last element of name. If there is no dot
101 * in the last element of name then returns the empty string.
103 std::string extension() const { return extension( *this ); }
104 static std::string extension( const Pathname & name_tv );
106 /** Return this path, adding a leading '/' if relative. */
107 Pathname absolutename() const { return absolutename( *this ); }
108 static Pathname absolutename( const Pathname & name_tv )
109 { return name_tv.relative() ? cat( "/", name_tv ) : name_tv; }
111 /** Return this path, removing a leading '/' if absolute.*/
112 Pathname relativename() const { return relativename( *this ); }
113 static Pathname relativename( const Pathname & name_tv )
114 { return name_tv.absolute() ? cat( ".", name_tv ) : name_tv; }
116 /** Concatenation of pathnames.
118 * "foo" / "baa" ==> "foo/baa"
119 * "foo/" / "baa" ==> "foo/baa"
120 * "foo" / "/baa" ==> "foo/baa"
121 * "foo/" / "/baa" ==> "foo/baa"
124 Pathname cat( const Pathname & r ) const { return cat( *this, r ); }
125 static Pathname cat( const Pathname & l, const Pathname & r );
127 /** Append string \a r to the last component of the path.
129 * "foo/baa".extend( ".h" ) ==> "foo/baa.h"
132 Pathname extend( const std::string & r ) const { return extend( *this, r ); }
133 static Pathname extend( const Pathname & l, const std::string & r );
136 std::string::size_type prfx_i;
139 void _assign( const std::string & name_tv );
141 ///////////////////////////////////////////////////////////////////
143 /** \relates Pathname */
144 inline bool operator==( const Pathname & l, const Pathname & r )
145 { return l.asString() == r.asString(); }
147 /** \relates Pathname */
148 inline bool operator!=( const Pathname & l, const Pathname & r )
149 { return l.asString() != r.asString(); }
151 /** \relates Pathname Concatenate two Pathname. */
152 inline Pathname operator/( const Pathname & l, const Pathname & r )
153 { return Pathname::cat( l, r ); }
155 /** \relates Pathname Concatenate two Pathname.
158 inline Pathname operator+( const Pathname & l, const Pathname & r )
159 { return Pathname::cat( l, r ); }
161 ///////////////////////////////////////////////////////////////////
163 /** \relates Pathname Stream output */
164 inline std::ostream & operator<<( std::ostream & str, const Pathname & obj )
165 { return str << obj.asString(); }
167 /////////////////////////////////////////////////////////////////
168 } // namespace filesystem
169 ///////////////////////////////////////////////////////////////////
171 /** Dragged into namespace zypp. */
172 using filesystem::Pathname;
174 /////////////////////////////////////////////////////////////////
176 ///////////////////////////////////////////////////////////////////
177 #endif // ZYPP_PATHNAME_H