1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Pathname.cc
11 * \todo replace by Blocxx
17 #include "zypp/Pathname.h"
23 ///////////////////////////////////////////////////////////////////
25 // CLASS NAME : DirStack
37 Dir( const string & n = "" ) {
67 DirStack() { top = bot = 0; }
73 void Push( const string & n ) {
74 if ( n.empty() || n == "." ) { // '.' or '/' only for bot
77 } else if ( n == ".." && top ) {
78 if ( top->name == "" ) // "/.." ==> "/"
81 if ( top->name != "." && top->name != ".." ) { // "somedir/.." ==> ""
85 // "../.." "./.." stays
88 Dir * d = new Dir( n );
103 for ( Dir * d = bot; d; d = d->up ) {
114 ///////////////////////////////////////////////////////////////////
117 ///////////////////////////////////////////////////////////////////
120 // METHOD NAME : Pathname::_assign
121 // METHOD TYPE : void
125 void Pathname::_assign( const string & name_tv )
130 if ( name_t.empty() )
136 char * Buf_aci = new char[name_tv.length() + 1];
137 char * W_pci = Buf_aci;
138 const char * R_pci = name_tv.c_str();
141 if ( name_t.length() >= 2
143 && ( 'a' <= name_t[0] && name_t[0] <= 'z'
144 || 'A' <= name_t[0] && name_t[0] <= 'Z' ) ) {
145 Tprfx = name_t.substr( 0, 2 );
151 if ( *R_pci == '/' || *R_pci == '\\' ) {
155 Stack_Ci.Push( "." );
163 if ( W_pci != Buf_aci ) {
166 Stack_Ci.Push( Buf_aci );
177 name_t = Tprfx + Stack_Ci.str();
180 ///////////////////////////////////////////////////////////////////
183 // METHOD NAME : Pathname::dirname
184 // METHOD TYPE : Pathname
188 Pathname Pathname::dirname( const Pathname & name_tv )
190 if ( name_tv.empty() )
193 Pathname ret_t( name_tv );
194 string::size_type idx = ret_t.name_t.find_last_of( '/' );
196 if ( idx == string::npos ) {
197 ret_t.name_t.erase( ret_t.prfx_i );
199 } else if ( idx == ret_t.prfx_i ) {
200 ret_t.name_t.erase( ret_t.prfx_i );
203 ret_t.name_t.erase( idx );
209 ///////////////////////////////////////////////////////////////////
212 // METHOD NAME : Pathname::basename
213 // METHOD TYPE : string
217 string Pathname::basename( const Pathname & name_tv )
219 if ( name_tv.empty() )
222 string ret_t( name_tv.asString() );
223 ret_t.erase( 0, name_tv.prfx_i );
224 string::size_type idx = ret_t.find_last_of( '/' );
225 if ( idx != string::npos ) {
226 ret_t.erase( 0, idx+1 );
232 ///////////////////////////////////////////////////////////////////
235 // METHOD NAME : Pathname::cat
236 // METHOD TYPE : Pathname
240 Pathname Pathname::cat( const Pathname & name_tv, const Pathname & add_tv )
242 if ( add_tv.empty() )
244 if ( name_tv.empty() )
247 string ret_ti( add_tv.asString() );
248 ret_ti.replace( 0, add_tv.prfx_i, "/" );
250 return name_tv.asString() + ret_ti;
253 ///////////////////////////////////////////////////////////////////
256 // METHOD NAME : Pathname::Extend
257 // METHOD TYPE : Pathname
261 Pathname Pathname::extend( const Pathname & l, const string & r )
263 return l.asString() + r;
266 ///////////////////////////////////////////////////////////////////
269 // METHOD NAME : Pathname::equal
270 // METHOD TYPE : bool
274 bool Pathname::equal( const Pathname & l, const Pathname & r )
276 return l.asString() == r.asString();
279 /******************************************************************
282 ** FUNCTION NAME : operator<<
283 ** FUNCTION TYPE : inline std::ostream &
287 ostream & operator<<( ostream & str, const Pathname & obj )
289 return str << obj.asString();