factor/library/io/files.factor

49 lines
1.2 KiB
Factor
Raw Normal View History

2006-05-15 00:03:55 -04:00
! Copyright (C) 2004, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
IN: io
2006-05-15 01:01:47 -04:00
USING: hashtables kernel math memory namespaces sequences
2006-03-27 22:20:42 -05:00
strings styles ;
! Words for accessing filesystem meta-data.
2004-11-25 21:51:47 -05:00
: path+ ( path path -- path )
over "/" tail? [ append ] [ "/" swap append3 ] if ;
2005-09-01 16:37:32 -04:00
: exists? ( file -- ? ) stat >boolean ;
2005-09-01 16:37:32 -04:00
2006-05-15 00:03:55 -04:00
: directory? ( file -- ? ) stat first ;
2005-09-01 16:37:32 -04:00
: directory ( dir -- list )
2006-01-09 01:34:23 -05:00
(directory)
[ { "." ".." } member? not ] subset natural-sort ;
2005-09-01 16:37:32 -04:00
: file-length ( file -- length ) stat third ;
2005-09-01 16:37:32 -04:00
2006-03-27 22:20:42 -05:00
: parent-dir ( path -- path )
CHAR: / over last-index CHAR: \\ pick last-index max
dup -1 = [ 2drop "." ] [ swap head ] if ;
2005-09-14 00:37:50 -04:00
: resource-path ( path -- path )
2006-03-27 22:20:42 -05:00
image parent-dir swap path+ ;
: <resource-reader> ( path -- stream )
2005-09-14 00:37:50 -04:00
resource-path <file-reader> ;
2005-09-27 14:12:17 -04:00
TUPLE: pathname string ;
: (file.) ( name path -- )
<pathname> write-object ;
2005-09-27 14:12:17 -04:00
DEFER: directory.
: (directory.) ( name path -- )
>r "/" append r> dup <pathname> swap [ directory. ] curry
write-outliner terpri ;
2005-09-27 14:12:17 -04:00
: file. ( dir name -- )
tuck path+
dup directory? [ (directory.) ] [ (file.) terpri ] if ;
2005-09-27 14:12:17 -04:00
: directory. ( dir -- )
dup directory [ file. ] each-with ;