File: path.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (635 lines) | stat: -rw-r--r-- 12,837 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/*
   FALCON - The Falcon Programming Language.
   FILE: path.cpp

   RFC 3986 compliant path-parth - implementation
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: Wed, 27 Feb 2008 22:05:33 +0100

   -------------------------------------------------------------------
   (C) Copyright 2004: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/

#include <falcon/uri.h>
#include <falcon/path.h>

namespace Falcon
{

Path::Path():
   m_bValid( true ),
   m_owner(0)
{
}

Path::Path( URI *owner ):
   m_bValid( true ),
   m_owner( owner )
{
}

void Path::copy( const Path &other )
{
   if ( m_owner != 0 ) m_owner->m_encoded.size(0);

   m_path = other.m_path;
   m_bValid = other.m_bValid;

   m_device = other.m_device;
   m_location = other.m_location;
   m_file = other.m_file;
   m_extension = other.m_extension;
}

bool Path::set( const String &path )
{
   uint32 len = path.length();

   m_device.size(0);
   m_location.size(0);
   m_file.size(0);
   m_extension.size(0);

   if ( len == 0 )
   {
      m_bValid = true;
      m_path = "";
      if ( m_owner ) m_owner->m_encoded.size(0);
      return true;
   }

   // a single element should be considered as the file.
   bool bColon = false;
   uint32 p = 0;
   while( p < len )
   {
      uint32 chr = path.getCharAt( p );
      if ( chr == '\\' )
      {
         chr = '/';
      }

      switch( chr )
      {
         case ':':
            if ( bColon )
            {
               m_bValid = false;
               if ( m_owner ) m_owner->m_encoded.size(0);
               return false;
            }

            bColon = true;
            m_location.size(0); // prevent storing the intial "/"
            m_device = m_file;
            m_file.size(0);
            break;

         case '/':
            if ( m_file.size() == 0 )
            {
               if( m_location.size() == 0 )
                  m_location = "/";

               // otherwise ignore; treat // as just /
            }
            else {
               if ( m_location.size() != 0 && m_location.getCharAt( m_location.length() - 1 ) != '/' )
                  m_location.append( '/' );
               m_location += m_file;
               m_file.size(0);
            }
            break;

         default:
            m_file.append( chr );
      }

      p++;
   }

   // detect if we have an extension
   uint32 pos = m_file.rfind( "." );
   if ( m_file.size() > 0 && pos > 0 && pos < m_file.length() - 1 )
   {
      m_extension = m_file.subString( pos + 1 );
      m_file.remove( pos, m_file.length() );
   }

   m_bValid = true;
   compose();

   return true;
}


void Path::compose()
{
   m_path.size(0);

   if ( m_device.size() > 0 )
      m_path = "/" + m_device + ":";

   if ( m_location.size() > 0 )
   {
      m_path += m_location;
      if ( m_location.getCharAt( m_location.length() - 1 ) != '/' && m_file.size() > 0 )
         m_path += "/";
   }

   if ( m_file.size() != 0 )
   {
      m_path += m_file;

      if ( m_extension.size() != 0 )
         m_path += "." + m_extension;
   }

   if ( m_owner ) m_owner->m_encoded.size(0);
}


void Path::getWinFormat( String &str ) const
{
   if ( m_path.size() == 0 )
   {
      str.size(0);
      return;
   }

   str.reserve( m_path.size() );

   // if we have a resource specifier, we know we have a leading /
   uint32 startPos = m_path.getCharAt(0) == '/' && m_device.size() != 0 ? 1: 0;
   uint32 endPos = m_path.length();
   while( startPos < endPos )
   {
      uint32 chr = m_path.getCharAt( startPos );
      if( chr != '/' )
         str.append( chr );
      else
         str.append( '\\' );
      ++startPos;
   }
}


bool Path::getResource( String &str ) const
{
   str = m_device;
   return str.size() != 0;
}


bool Path::getLocation( String &str ) const
{
   str = m_location;
   return str.size() != 0;
}


bool Path::getFullLocation( String &str ) const
{
   if( m_device.size() != 0 )
      str = "/" + m_device + ":" + m_location;
   else
      str = m_location;

   return str.size() != 0;
}


bool Path::setFullLocation( const String &str )
{
   // full location is disk + path + filename;
   // we don't know if we have the disk, but we 
   // know we should have at least the path (unless it's "").

   // It's simpler to add the filename and reparse everything.

   if( str == "" )
   {
      m_location = "";
      m_device = "";
      return true;
   }
   else 
   {
      String loc;

      // only the resource?
      if( str.getCharAt( str.length() - 1 ) == ':' )
      {
         // then, don't add a slash which would translate in an absolute location.
         loc = str + getFilename();
      }
      else
      {
         // ok, add the slash after the path 
         // (notice that "//" is correctly parsed into "/")
         loc = str + "/" + getFilename();
      }

      return set( loc );
   }
}


bool Path::getWinLocation( String &str ) const
{
   if ( ! getLocation( str ) )
      return false;

   uint32 len = str.length();
   for( uint32 i = 0; i < len; i ++ )
   {
      if ( str.getCharAt( i ) == '/' )
         str.setCharAt( i, '\\' );
   }

   return true;
}


bool Path::getFullWinLocation( String &str ) const
{
   if ( m_device != "" )
   {
      String loc;
      if ( getWinLocation( loc ) )
      {
         str = m_device + ":" + loc;
         return true;
      }
   }
   else if ( getWinLocation( str ) )
   {
      return true;
   }
   
   // str has been cleaned by getWinLocation()
   return false;
}


bool Path::getFile( String &str ) const
{
   str = m_file;
   return str.size() != 0;
}


bool Path::getFilename( String &str ) const
{
   if ( m_file.size() != 0 )
   {
      if ( m_extension.size() != 0 )
         str = m_file + "." + m_extension;
      else
         str = m_file;
      return true;
   }

   return false;
}


bool Path::getExtension( String &str ) const
{
   str = m_extension;
   return str.size() != 0;
}


void Path::setResource( const String &res )
{
   if ( res != m_device )
   {
      m_device = res;
      if ( m_device.find( ":" ) != String::npos || m_device.find( "/" ) != String::npos )
         m_bValid = false;

      compose();
   }
}


void Path::extendLocation( const String &npath )
{
   if ( m_location.size() == 0 )
      m_location = npath;
   else
   {
      if( npath.size() != 0 )
      {
         if( npath.getCharAt(0) != '/' )
            m_location += "/";
         m_location += npath;
      }
   }

   // remove trailing "/".
   if ( npath.size() > 0 && m_location.getCharAt( m_location.length() - 1 ) == '/' )
      m_location.remove( m_location.length() - 1, 1 );

   compose();
}


void Path::setLocation( const String &loc )
{
   uint32 pos = loc.find( ":" );
   if ( pos != String::npos )
   {
      if( loc.find( ":", pos + 1 ) != String::npos )
      {
         m_bValid = false;
      }
      else
      {
         setResource( loc.subString( 0, pos ) );
         setLocation( loc.subString( pos + 1 ) );
      }
   }
   else
   {
      if( m_location != loc )
      {
         m_location = loc;

         uint32 pos1 = m_location.find( "\\" );
         while( pos1 != String::npos )
         {
            m_location.setCharAt( pos1, '/' );
            pos1 = m_location.find( "\\", pos1 );
         }

         // remove trailing "/"
         if ( m_location.size() > 1 && m_location.getCharAt( m_location.length() - 1 ) == '/' )
            m_location.remove( m_location.length() - 1, 1 );

         compose();
      }

   }
}


void Path::setFile( const String &file )
{
   if ( file.find( "/" ) != String::npos || file.find( "\\" ) != String::npos || file.find( ":" ) != String::npos )
   {
      m_bValid = false;
   }
   else
   {
      if ( m_file != file )
      {
         m_file = file;
         compose();
      }
   }
}


void Path::setExtension( const String &ext )
{
   if ( ext.find( "/" ) != String::npos || ext.find( "\\" ) != String::npos
       || ext.find( ":" ) != String::npos || ext.find( "." ) != String::npos )
   {
      m_bValid = false;
   }
   else
   {
      if ( m_extension != ext )
      {
         m_extension = ext;
         if ( m_owner ) m_owner->m_encoded.size(0);
         compose();
      }
   }
}


void Path::setFilename( const String &fname )
{
   if ( fname.find( "/" ) != String::npos || fname.find( "\\" ) != String::npos || fname.find( ":" ) != String::npos )
   {
      m_bValid = false;
   }
   else
   {
      uint32 posdot = fname.rfind( "." );
      if ( posdot != String::npos && posdot != 0 && posdot != fname.length() - 1 )
      {
         m_file = fname.subString( 0, posdot );
         m_extension = fname.subString( posdot + 1 );
      }
      else {
         m_file = fname;
         m_extension = "";
      }

      if ( m_owner ) m_owner->m_encoded.size(0);
      compose();
   }
}


bool Path::isAbsolute() const
{
   return m_location.size() > 0 && m_location.getCharAt(0) == '/';
}


bool Path::isLocation() const
{
   return (m_file.size() == m_extension.size()) == 0;
}


void Path::split( String &loc, String &name, String &ext )
{
   if ( m_device.size() > 0 )
   {
      loc = m_device + ":" + m_location;
   }
   else
      loc = m_location;

   name = m_file;
   ext = m_extension;
}


void Path::split( String &res, String &loc, String &name, String &ext )
{
   res = m_device;
   loc = m_location;
   name = m_file;
   ext = m_extension;
}


void Path::splitWinFormat( String &res, String &loc, String &name, String &ext )
{
   split( res, loc, name, ext );
   uint32 len = loc.length();
   for( uint32 i = 0; i < len; i ++ )
   {
      if ( loc.getCharAt( i ) == '/' )
         loc.setCharAt( i, '\\' );
   }
}


void Path::join( const String &loc, const String &name, const String &ext )
{
   if ( m_owner ) m_owner->m_encoded.size(0);

   m_path = loc;
   if( loc.length() !=  0 )
   {
      if( loc.getCharAt( loc.length() - 1 )  != '/' )
         m_path += '/';
   }

   if ( name.length() != 0 )
   {
      m_path += name;

      if( ext.length() != 0 )
      {
         if( name.getCharAt( name.length() - 1 ) != '.' )
            m_path.append( '.' );
         m_path += ext;
      }
   }

   set( m_path );
}


void Path::join( const String &res, const String &loc, const String &name, const String &ext )
{
   if ( m_owner ) m_owner->m_encoded.size(0);

   m_path = res;

   if ( res.length() != 0 && res.getCharAt( res.length() - 1 ) != ':' )
      m_path.append( ':' );

   m_path += loc;

   if( loc.length() !=  0 )
   {
      if( loc.getCharAt( loc.length() - 1 )  != '/' )
         m_path += '/';
   }

   if ( name.length() != 0 )
   {
      m_path += name;

      if( ext.length() != 0 )
      {
         if( name.getCharAt( name.length() - 1 ) != '.' )
            m_path.append( '.' );
         m_path += ext;
      }
   }

   set(m_path);
}


void Path::winToUri( String &ret )
{
   int size = ret.length();
   ret.bufferize();
   bool prefix = false;

   for( int i = 0; i < size; i ++ ) {
      int chr = ret.getCharAt( i );
      if( chr == '\\' )
         ret.setCharAt( i, '/' );
      else if ( chr == ':' )
         prefix = true;
   }

   if( prefix && ret.getCharAt(0) != '/' )
      ret.prepend('/');
}


void Path::uriToWin( String &result )
{
   result.bufferize();
   bool bRem = false;

   unsigned int i = 0;
   while( i < result.length() )
   {
      int chr = result.getCharAt( i );
      switch ( chr )
      {
      case '/':
         // "/C:" disk specificator?
         if ( i == 0 )
            bRem = true;
         else
            bRem = false;

         result.setCharAt( i, '\\' );
         break;
      
      case ':':
         // was the first "/" to be removed?
         if ( bRem )
         {
            result.remove(0,1);
            // get I back.
            i--;
         }
         break;
      
      case '+':
         result.setCharAt( i, ' ' );
         break;
      
      // hex escape?
      case '%':
         {
            // have we got enough space for 2 chars?
            if( result.length() < i + 2 )
            {
               return;
            }

            char n1 = result.getCharAt( i+1 );
            char n2 = result.getCharAt( i+2 );
            uint32 r = 0;
            if( n1 >= 'a' && n1 <= 'f' ) r += (n1-'a')*0x10;
            else if( n1 >= 'A' && n1 <= 'F' ) r += (n1-'A')*0x10;
            else if( n1 >= '0' && n1 <= '9' ) r += (n1-'0')*0x10;

            if( n2 >= 'a' && n2 <= 'f' ) r += (n2-'a');
            else if( n1 >= 'A' && n2 <= 'F' ) r += (n2-'A');
            else if( n2 >= '0' && n2 <= '9' ) r += (n2-'0');

            // remove extra chars
            result.remove( i, i+2 );
            // change the transformed one.
            result.setCharAt( i, r );
         }
         break;
      }

      i ++;
   }
}

}