1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Pathname.cc
14 #include "zypp/Pathname.h"
19 ///////////////////////////////////////////////////////////////////
21 { /////////////////////////////////////////////////////////////////
22 ///////////////////////////////////////////////////////////////////
24 { /////////////////////////////////////////////////////////////////
26 ///////////////////////////////////////////////////////////////////
28 { /////////////////////////////////////////////////////////////////
30 ///////////////////////////////////////////////////////////////////
32 // CLASS NAME : DirStack
34 /** silly helper to build Pathnames.
44 Dir( const string & n = "" ) {
74 DirStack() { top = bot = 0; }
80 void Push( const string & n ) {
81 if ( n.empty() || n == "." ) { // '.' or '/' only for bot
84 } else if ( n == ".." && top ) {
85 if ( top->name == "" ) // "/.." ==> "/"
88 if ( top->name != "." && top->name != ".." ) { // "somedir/.." ==> ""
92 // "../.." "./.." stays
95 Dir * d = new Dir( n );
110 for ( Dir * d = bot; d; d = d->up ) {
121 /////////////////////////////////////////////////////////////////
123 ///////////////////////////////////////////////////////////////////
125 ///////////////////////////////////////////////////////////////////
127 // METHOD NAME : Pathname::_assign
128 // METHOD TYPE : void
130 void Pathname::_assign( const string & name_tv )
135 if ( name_t.empty() )
141 char * Buf_aci = new char[name_tv.length() + 1];
142 char * W_pci = Buf_aci;
143 const char * R_pci = name_tv.c_str();
146 if ( name_t.length() >= 2
148 && ( 'a' <= name_t[0] && name_t[0] <= 'z'
149 || 'A' <= name_t[0] && name_t[0] <= 'Z' ) ) {
150 Tprfx = name_t.substr( 0, 2 );
156 if ( *R_pci == '/' || *R_pci == '\\' ) {
160 Stack_Ci.Push( "." );
168 if ( W_pci != Buf_aci ) {
171 Stack_Ci.Push( Buf_aci );
182 name_t = Tprfx + Stack_Ci.str();
185 ///////////////////////////////////////////////////////////////////
187 // METHOD NAME : Pathname::dirname
188 // METHOD TYPE : Pathname
190 Pathname Pathname::dirname( const Pathname & name_tv )
192 if ( name_tv.empty() )
195 Pathname ret_t( name_tv );
196 string::size_type idx = ret_t.name_t.find_last_of( '/' );
198 if ( idx == string::npos ) {
199 ret_t.name_t.erase( ret_t.prfx_i );
201 } else if ( idx == ret_t.prfx_i ) {
202 ret_t.name_t.erase( ret_t.prfx_i );
205 ret_t.name_t.erase( idx );
211 ///////////////////////////////////////////////////////////////////
213 // METHOD NAME : Pathname::basename
214 // METHOD TYPE : string
216 string Pathname::basename( const Pathname & name_tv )
218 if ( name_tv.empty() )
221 string ret_t( name_tv.asString() );
222 ret_t.erase( 0, name_tv.prfx_i );
223 string::size_type idx = ret_t.find_last_of( '/' );
224 if ( idx != string::npos ) {
225 ret_t.erase( 0, idx+1 );
231 ///////////////////////////////////////////////////////////////////
233 // METHOD NAME : Pathname::asUrl
236 Url Pathname::asUrl() const
238 Url ret( "dir:///" );
239 ret.setPathName( asString() );
243 ///////////////////////////////////////////////////////////////////
245 // METHOD NAME : Pathname::extension
246 // METHOD TYPE : string
248 string Pathname::extension( const Pathname & name_tv )
250 if ( name_tv.empty() )
253 string base( basename( name_tv ) );
254 string::size_type pos = base.rfind( '.' );
255 if ( pos == string::npos )
257 return base.substr( pos );
260 ///////////////////////////////////////////////////////////////////
262 // METHOD NAME : Pathname::cat
263 // METHOD TYPE : Pathname
265 Pathname Pathname::cat( const Pathname & name_tv, const Pathname & add_tv )
267 if ( add_tv.empty() )
269 if ( name_tv.empty() )
272 string ret_ti( add_tv.asString() );
273 ret_ti.replace( 0, add_tv.prfx_i, "/" );
275 return name_tv.asString() + ret_ti;
278 ///////////////////////////////////////////////////////////////////
280 // METHOD NAME : Pathname::Extend
281 // METHOD TYPE : Pathname
283 Pathname Pathname::extend( const Pathname & l, const string & r )
285 return l.asString() + r;
288 /////////////////////////////////////////////////////////////////
289 } // namespace filesystem
290 ///////////////////////////////////////////////////////////////////
291 /////////////////////////////////////////////////////////////////
293 ///////////////////////////////////////////////////////////////////