2005-02-08 22:02:44 -05:00
|
|
|
! Copyright (C) 2004, 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2004-08-28 16:43:43 -04:00
|
|
|
IN: files
|
2005-02-08 22:02:44 -05:00
|
|
|
USING: kernel hashtables lists namespaces presentation stdio
|
|
|
|
strings unparser ;
|
2004-08-28 16:43:43 -04:00
|
|
|
|
2004-11-25 21:51:47 -05:00
|
|
|
: exists? ( file -- ? )
|
|
|
|
stat >boolean ;
|
|
|
|
|
|
|
|
: directory? ( file -- ? )
|
|
|
|
stat dup [ car ] when ;
|
|
|
|
|
|
|
|
: directory ( dir -- list )
|
|
|
|
#! List a directory.
|
2005-03-05 16:33:40 -05:00
|
|
|
(directory) [ string> ] sort ;
|
2004-11-25 21:51:47 -05:00
|
|
|
|
|
|
|
: file-length ( file -- length )
|
|
|
|
stat dup [ cdr cdr car ] when ;
|
|
|
|
|
2004-10-30 23:18:55 -04:00
|
|
|
: file-actions ( -- list )
|
|
|
|
[
|
2005-01-13 19:49:47 -05:00
|
|
|
[[ "Push" "" ]]
|
|
|
|
[[ "Run file" "run-file" ]]
|
|
|
|
[[ "List directory" "directory." ]]
|
|
|
|
[[ "Change directory" "cd" ]]
|
2004-10-30 23:18:55 -04:00
|
|
|
] ;
|
|
|
|
|
2004-08-28 16:43:43 -04:00
|
|
|
: set-mime-types ( assoc -- )
|
2004-11-20 16:57:01 -05:00
|
|
|
"mime-types" global set-hash ;
|
2004-08-28 16:43:43 -04:00
|
|
|
|
|
|
|
: mime-types ( -- assoc )
|
2004-11-20 16:57:01 -05:00
|
|
|
"mime-types" global hash ;
|
2004-08-28 16:43:43 -04:00
|
|
|
|
|
|
|
: file-extension ( filename -- extension )
|
|
|
|
"." split cdr dup [ last ] when ;
|
|
|
|
|
|
|
|
: mime-type ( filename -- mime-type )
|
|
|
|
file-extension mime-types assoc [ "text/plain" ] unless* ;
|
|
|
|
|
2004-08-30 20:24:19 -04:00
|
|
|
: dir-icon
|
|
|
|
"/library/icons/Folder.png" ;
|
|
|
|
|
|
|
|
: file-icon
|
|
|
|
"/library/icons/File.png" ;
|
|
|
|
|
|
|
|
: file-icon. ( path -- )
|
|
|
|
directory? dir-icon file-icon ? write-icon ;
|
|
|
|
|
|
|
|
: file-link. ( dir name -- )
|
2004-10-30 23:18:55 -04:00
|
|
|
tuck "/" swap cat3 dup "file-link" swons swap
|
2004-12-20 15:29:55 -05:00
|
|
|
unparse file-actions <actions> "actions" swons
|
2005-01-01 17:20:48 -05:00
|
|
|
2list write-attr ;
|
2004-08-30 20:24:19 -04:00
|
|
|
|
|
|
|
: file. ( dir name -- )
|
|
|
|
#! If "doc-root" set, create links relative to it.
|
|
|
|
2dup "/" swap cat3 file-icon. " " write file-link. terpri ;
|
|
|
|
|
|
|
|
: directory. ( dir -- )
|
|
|
|
#! If "doc-root" set, create links relative to it.
|
|
|
|
dup directory [
|
|
|
|
dup [ "." ".." ] contains? [
|
2005-01-01 17:20:48 -05:00
|
|
|
2drop
|
2004-08-30 20:24:19 -04:00
|
|
|
] [
|
2005-01-01 17:20:48 -05:00
|
|
|
file.
|
2004-08-30 20:24:19 -04:00
|
|
|
] ifte
|
2005-01-01 17:20:48 -05:00
|
|
|
] each-with ;
|
2004-08-30 20:24:19 -04:00
|
|
|
|
2004-09-04 03:06:53 -04:00
|
|
|
: pwd cwd print ;
|
|
|
|
: dir. cwd directory. ;
|
|
|
|
|
2004-08-28 16:43:43 -04:00
|
|
|
[
|
2005-01-13 19:49:47 -05:00
|
|
|
[[ "html" "text/html" ]]
|
|
|
|
[[ "txt" "text/plain" ]]
|
2004-08-28 22:25:59 -04:00
|
|
|
|
2005-01-13 19:49:47 -05:00
|
|
|
[[ "gif" "image/gif" ]]
|
|
|
|
[[ "png" "image/png" ]]
|
|
|
|
[[ "jpg" "image/jpeg" ]]
|
|
|
|
[[ "jpeg" "image/jpeg" ]]
|
2004-08-28 22:25:59 -04:00
|
|
|
|
2005-01-13 19:49:47 -05:00
|
|
|
[[ "jar" "application/octet-stream" ]]
|
|
|
|
[[ "zip" "application/octet-stream" ]]
|
|
|
|
[[ "tgz" "application/octet-stream" ]]
|
|
|
|
[[ "tar.gz" "application/octet-stream" ]]
|
|
|
|
[[ "gz" "application/octet-stream" ]]
|
2004-08-28 22:25:59 -04:00
|
|
|
|
2005-01-13 19:49:47 -05:00
|
|
|
[[ "factor" "application/x-factor" ]]
|
|
|
|
[[ "factsp" "application/x-factor-server-page" ]]
|
2004-08-28 16:43:43 -04:00
|
|
|
] set-mime-types
|