1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Pathname.cc
12 #include "zypp/Pathname.h"
16 ///////////////////////////////////////////////////////////////////
18 { /////////////////////////////////////////////////////////////////
19 ///////////////////////////////////////////////////////////////////
21 { /////////////////////////////////////////////////////////////////
23 ///////////////////////////////////////////////////////////////////
25 { /////////////////////////////////////////////////////////////////
27 ///////////////////////////////////////////////////////////////////
29 // CLASS NAME : DirStack
31 /** silly helper to build Pathnames.
41 Dir( const string & n = "" ) {
71 DirStack() { top = bot = 0; }
77 void Push( const string & n ) {
78 if ( n.empty() || n == "." ) { // '.' or '/' only for bot
81 } else if ( n == ".." && top ) {
82 if ( top->name == "" ) // "/.." ==> "/"
85 if ( top->name != "." && top->name != ".." ) { // "somedir/.." ==> ""
89 // "../.." "./.." stays
92 Dir * d = new Dir( n );
107 for ( Dir * d = bot; d; d = d->up ) {
118 /////////////////////////////////////////////////////////////////
120 ///////////////////////////////////////////////////////////////////
122 ///////////////////////////////////////////////////////////////////
124 // METHOD NAME : Pathname::_assign
125 // METHOD TYPE : void
127 void Pathname::_assign( const string & name_tv )
132 if ( name_t.empty() )
138 char * Buf_aci = new char[name_tv.length() + 1];
139 char * W_pci = Buf_aci;
140 const char * R_pci = name_tv.c_str();
143 if ( name_t.length() >= 2
145 && ( 'a' <= name_t[0] && name_t[0] <= 'z'
146 || 'A' <= name_t[0] && name_t[0] <= 'Z' ) ) {
147 Tprfx = name_t.substr( 0, 2 );
153 if ( *R_pci == '/' || *R_pci == '\\' ) {
157 Stack_Ci.Push( "." );
165 if ( W_pci != Buf_aci ) {
168 Stack_Ci.Push( Buf_aci );
179 name_t = Tprfx + Stack_Ci.str();
182 ///////////////////////////////////////////////////////////////////
184 // METHOD NAME : Pathname::dirname
185 // METHOD TYPE : Pathname
187 Pathname Pathname::dirname( const Pathname & name_tv )
189 if ( name_tv.empty() )
192 Pathname ret_t( name_tv );
193 string::size_type idx = ret_t.name_t.find_last_of( '/' );
195 if ( idx == string::npos ) {
196 ret_t.name_t.erase( ret_t.prfx_i );
198 } else if ( idx == ret_t.prfx_i ) {
199 ret_t.name_t.erase( ret_t.prfx_i );
202 ret_t.name_t.erase( idx );
208 ///////////////////////////////////////////////////////////////////
210 // METHOD NAME : Pathname::basename
211 // METHOD TYPE : string
213 string Pathname::basename( const Pathname & name_tv )
215 if ( name_tv.empty() )
218 string ret_t( name_tv.asString() );
219 ret_t.erase( 0, name_tv.prfx_i );
220 string::size_type idx = ret_t.find_last_of( '/' );
221 if ( idx != string::npos ) {
222 ret_t.erase( 0, idx+1 );
228 ///////////////////////////////////////////////////////////////////
230 // METHOD NAME : Pathname::extension
231 // METHOD TYPE : string
233 string Pathname::extension( const Pathname & name_tv )
235 if ( name_tv.empty() )
238 string base( basename( name_tv ) );
239 string::size_type pos = base.rfind( '.' );
240 if ( pos == string::npos )
242 return base.substr( pos );
245 ///////////////////////////////////////////////////////////////////
247 // METHOD NAME : Pathname::cat
248 // METHOD TYPE : Pathname
250 Pathname Pathname::cat( const Pathname & name_tv, const Pathname & add_tv )
252 if ( add_tv.empty() )
254 if ( name_tv.empty() )
257 string ret_ti( add_tv.asString() );
258 ret_ti.replace( 0, add_tv.prfx_i, "/" );
260 return name_tv.asString() + ret_ti;
263 ///////////////////////////////////////////////////////////////////
265 // METHOD NAME : Pathname::Extend
266 // METHOD TYPE : Pathname
268 Pathname Pathname::extend( const Pathname & l, const string & r )
270 return l.asString() + r;
273 /////////////////////////////////////////////////////////////////
274 } // namespace filesystem
275 ///////////////////////////////////////////////////////////////////
276 /////////////////////////////////////////////////////////////////
278 ///////////////////////////////////////////////////////////////////