Add directory reading functions

and support for Chez and Racket
This commit is contained in:
Edwin Brady 2020-02-23 12:17:36 +00:00
parent e126dcd28f
commit 66d67c84cc
3 changed files with 60 additions and 0 deletions

View File

@ -2,6 +2,9 @@ module System.Directory
import public System.File
public export
data DirPtr : Type where
toFileError : Int -> FileError
toFileError 1 = FileReadError
toFileError 2 = FileWriteError
@ -22,6 +25,19 @@ prim_changeDir : String -> PrimIO Int
%foreign "scheme:blodwen-create-directory"
prim_createDir : String -> PrimIO (Either Int ())
%foreign "scheme:blodwen-open-directory"
prim_openDir : String -> PrimIO (Either Int DirPtr)
%foreign "scheme:blodwen-close-directory"
prim_closeDir : DirPtr -> PrimIO ()
%foreign "scheme:blodwen-next-dir-entry"
prim_dirEntry : DirPtr -> PrimIO (Either Int String)
export
data Directory : Type where
MkDir : DirPtr -> Directory
export
createDir : String -> IO (Either FileError ())
createDir dir
@ -37,3 +53,19 @@ changeDir dir
export
currentDir : IO String
currentDir = primIO prim_currentDir
export
dirOpen : String -> IO (Either FileError Directory)
dirOpen d
= do res <- primIO (prim_openDir d)
fpure (map MkDir res)
export
dirClose : Directory -> IO ()
dirClose (MkDir d) = primIO (prim_closeDir d)
export
dirEntry : Directory -> IO (Either FileError String)
dirEntry (MkDir d)
= do res <- primIO (prim_dirEntry d)
fpure res

View File

@ -177,6 +177,20 @@
(define (blodwen-create-directory dir)
(blodwen-file-op (lambda () (mkdir dir) 0)))
; Scheme only gives a primitive for reading all the files in a directory,
; so this is faking the C interface!
(define (blodwen-open-directory dir)
(blodwen-file-op (lambda () (box (directory-list dir)))))
(define (blodwen-close-directory dir) '()) ; no-op, it's not really open
(define (blodwen-next-dir-entry dir)
(let [(dlist (unbox dir))]
(if (null? dlist)
(either-left 255)
(begin (set-box! dir (cdr dlist))
(either-right (car dlist))))))
;; Threads
(define blodwen-thread-data (make-thread-parameter #f))

View File

@ -182,6 +182,20 @@
(define (blodwen-create-directory dir)
(blodwen-file-op (lambda () (make-directory dir))))
; Scheme only gives a primitive for reading all the files in a directory,
; so this is faking the C interface!
(define (blodwen-open-directory dir)
(blodwen-file-op (lambda () (box (directory-list dir)))))
(define (blodwen-close-directory dir) '()) ; no-op, it's not really open
(define (blodwen-next-dir-entry dir)
(let [(dlist (unbox dir))]
(if (null? dlist)
(either-left 255)
(begin (set-box! dir (cdr dlist))
(either-right (path->string (car dlist)))))))
;; Threads
(define blodwen-thread-data (make-thread-cell #f))