[svn:parrot] r46200 - in trunk: runtime/parrot/library runtime/parrot/library/Archive runtime/parrot/library/TAP t/library

fperrad at svn.parrot.org fperrad at svn.parrot.org
Fri Apr 30 20:14:53 UTC 2010


Author: fperrad
Date: Fri Apr 30 20:14:52 2010
New Revision: 46200
URL: https://trac.parrot.org/parrot/changeset/46200

Log:
[osutils] add catfile() & splitpath()

Modified:
   trunk/runtime/parrot/library/Archive/TAR.pir
   trunk/runtime/parrot/library/TAP/Harness.pir
   trunk/runtime/parrot/library/osutils.pir
   trunk/t/library/osutils.t

Modified: trunk/runtime/parrot/library/Archive/TAR.pir
==============================================================================
--- trunk/runtime/parrot/library/Archive/TAR.pir	Fri Apr 30 17:32:38 2010	(r46199)
+++ trunk/runtime/parrot/library/Archive/TAR.pir	Fri Apr 30 20:14:52 2010	(r46200)
@@ -24,7 +24,7 @@
 .namespace ['Archive';'TAR';'File']
 
 .sub '' :init :load :anon
-    load_bytecode 'osutils.pbc' # basename, dirname
+    load_bytecode 'osutils.pbc' # splitpath
     $P0 = newclass ['Archive';'TAR';'File']
     $P0.'add_attribute'('name')
     $P0.'add_attribute'('mode')
@@ -179,12 +179,9 @@
 
 .sub '_prefix_and_file' :anon
     .param string path
-    $S0 = dirname(path)
-    unless $S0 == '.' goto L1
-    $S0 = ''
-  L1:
-    $S1 = basename(path)
-    .return ($S0, $S1)
+    .local string volume, directories, file
+    (volume, directories, file) = splitpath(path)
+    .return (directories, file)
 .end
 
 =item full_path
@@ -200,8 +197,7 @@
     unless prefix == '' goto L1
     .return (name)
   L1:
-    $S0 = prefix . '/'
-    $S0 .= name
+    $S0 = catfile(prefix, name)
     .return ($S0)
 .end
 

Modified: trunk/runtime/parrot/library/TAP/Harness.pir
==============================================================================
--- trunk/runtime/parrot/library/TAP/Harness.pir	Fri Apr 30 17:32:38 2010	(r46199)
+++ trunk/runtime/parrot/library/TAP/Harness.pir	Fri Apr 30 20:14:52 2010	(r46200)
@@ -212,9 +212,8 @@
     $I0 = exists $P0['PARROT_TEST_HARNESS_DUMP_TAP']
     unless $I0 goto L1
     .local string spool
-    spool = $P0['PARROT_TEST_HARNESS_DUMP_TAP']
-    spool .= '/'
-    spool .= test
+    $S0 = $P0['PARROT_TEST_HARNESS_DUMP_TAP']
+    spool = catfile($S0, test)
     $S0 = dirname(spool)
     mkpath($S0)
     $P0 = new 'FileHandle'

Modified: trunk/runtime/parrot/library/osutils.pir
==============================================================================
--- trunk/runtime/parrot/library/osutils.pir	Fri Apr 30 17:32:38 2010	(r46199)
+++ trunk/runtime/parrot/library/osutils.pir	Fri Apr 30 20:14:52 2010	(r46200)
@@ -795,6 +795,55 @@
     rethrow e
 .end
 
+=item catfile
+
+=cut
+
+.sub 'catfile'
+    .param pmc args             :slurpy
+    .param int native           :named('native') :optional
+    .param int has_native       :opt_flag
+    .local string slash
+    slash = '/'
+    unless has_native goto L1
+    unless native goto L1
+    $P0 = getinterp
+    $P0 = $P0[.IGLOBALS_CONFIG_HASH]
+    slash = $P0['slash']
+  L1:
+    $S0 = join slash, args
+    .return ($S0)
+.end
+
+=item splitpath
+
+=cut
+
+.sub 'splitpath'
+    .param string path
+    .local string volume, directories, file
+    volume = ''
+    $I0 = index path, ':'
+    unless $I0 == 1 goto L1
+    volume = substr path, 0, 2
+    path = substr path, 2
+  L1:
+    $I0 = 0
+  L2:
+    $I1 = index path, '/', $I0
+    if $I1 < 0 goto L3
+    $I0 = $I1 + 1
+    goto L2
+  L3:
+    file = substr path, $I0
+    directories = ''
+    dec $I0
+    unless $I0 > 0 goto L4
+    directories = substr path, 0, $I0
+  L4:
+    .return (volume, directories, file)
+.end
+
 =back
 
 =head1 AUTHOR

Modified: trunk/t/library/osutils.t
==============================================================================
--- trunk/t/library/osutils.t	Fri Apr 30 17:32:38 2010	(r46199)
+++ trunk/t/library/osutils.t	Fri Apr 30 20:14:52 2010	(r46200)
@@ -21,9 +21,11 @@
 
     load_bytecode 'osutils.pir'
 
-    plan(4)
+    plan(11)
     test_basename()
     test_dirname()
+    test_catfile()
+    test_splitpath()
 .end
 
 .sub 'test_basename'
@@ -42,6 +44,24 @@
     is( $S0, '.', "dirname" )
 .end
 
+.sub 'test_catfile'
+    $S0 = catfile('abc', 'def', 'ghi')
+    is($S0, 'abc/def/ghi', "catfile")
+.end
+
+.sub 'test_splitpath'
+    .local string volume, directories, file
+    (volume, directories, file) = splitpath('abc/def/ghi.txt')
+    is(volume, '', "splitpath vol")
+    is(directories, 'abc/def', "splitpath dirs")
+    is(file, 'ghi.txt', "splitpath file")
+
+    (volume, directories, file) = splitpath('c:/abc/def/ghi.txt')
+    is(volume, 'c:', "splitpath vol")
+    is(directories, '/abc/def', "splitpath dirs")
+    is(file, 'ghi.txt', "splitpath file")
+.end
+
 # Local Variables:
 #   mode: pir
 #   fill-column: 100


More information about the parrot-commits mailing list