Adding temporary files in io.files.tmp

db4
Alex Chapman 2008-02-15 15:41:15 +11:00
parent 622a35ec9f
commit 72ee9ff67c
2 changed files with 27 additions and 0 deletions

View File

@ -0,0 +1,5 @@
USING: io.files io.files.tmp kernel strings tools.test ;
IN: temporary
[ t ] [ tmpdir string? ] unit-test
[ t f ] [ ".tmp" [ dup exists? swap ] with-tmpfile exists? ] unit-test

View File

@ -0,0 +1,22 @@
USING: continuations io io.files kernel sequences strings.lib ;
IN: io.files.tmp
: tmpdir ( -- dirname )
#! ensure that a tmp dir exists and return its name
#! I'm using a sub-directory of factor for crossplatconformity (windows doesn't have /tmp)
"tmp" resource-path dup directory? [ dup make-directory ] unless ;
: touch ( filename -- )
<file-writer> stream-close ;
: tmpfile ( extension -- filename )
16 random-alphanumeric-string over append
tmpdir swap path+ dup exists? [
drop tmpfile
] [
nip dup touch
] if ;
: with-tmpfile ( extension quot -- )
#! quot should have stack effect ( filename -- )
swap tmpfile tuck swap curry swap [ delete-file ] curry [ ] cleanup ;