Commit 04722596 authored by Bas Lijnse's avatar Bas Lijnse

Added simple directory module (now only Posix/Mac implementation)


git-svn-id: https://svn.cs.ru.nl/repos/clean-platform/trunk@90 2afc29ad-3112-4e41-907a-9359c7e6e986
parent 9d6ed44f
......@@ -4,16 +4,27 @@ from _Pointer import :: Pointer
WNOHANG :== 0x00000001
WUNTRACED :== 0x00000002
MAXPATHLEN :== 1024
DIRENT_D_NAME_OFFSET :== 8
//Posix API calls
errno :: !*World -> (!Int,!*World)
strerr :: !Int -> Pointer
stat :: !{#Char} !{#Char} !*World -> (!Int,!*World)
unlink :: !{#Char} !*World -> (!Int,!*World)
fork :: !*World -> (!Int,!*World)
execvp :: !{#Char} !{#Pointer} !*World -> (!Int,!*World)
waitpid :: !Int !{#Int} !Int !*World -> (!Int,!*World)
exit :: !Int !*World -> (!.a,!*World)
errno :: !*World -> (!Int,!*World)
strerr :: !Int -> Pointer
stat :: !{#Char} !{#Char} !*World -> (!Int,!*World)
unlink :: !{#Char} !*World -> (!Int,!*World)
fork :: !*World -> (!Int,!*World)
execvp :: !{#Char} !{#Pointer} !*World -> (!Int,!*World)
waitpid :: !Int !{#Int} !Int !*World -> (!Int,!*World)
exit :: !Int !*World -> (!.a,!*World)
getcwd :: !{#Char} !Int !*World -> (!Pointer,!*World)
chdir :: !{#Char} !*World -> (!Int,!*World)
mkdir :: !{#Char} !Int !*World -> (!Int,!*World)
rmdir :: !{#Char} !*World -> (!Int,!*World)
rename :: !{#Char} !{#Char} !*World -> (!Int,!*World)
opendir :: !{#Char} !*World -> (!Pointer,!*World)
closedir :: !Pointer !*World -> (!Int,!*World)
readdir :: !Pointer !*World -> (!Pointer,!*World)
//Memory (impure)
malloc :: !Int -> Pointer
......
......@@ -43,6 +43,38 @@ exit :: !Int !*World -> (!.a,!*World)
exit num world = code {
ccall exit "I:p:A"
}
getcwd :: !{#Char} !Int !*World -> (!Pointer,!*World)
getcwd buf size_t world = code {
ccall getcwd "sI:p:A"
}
chdir :: !{#Char} !*World -> (!Int,!*World)
chdir name world = code {
ccall chdir "s:I:A"
}
mkdir :: !{#Char} !Int !*World -> (!Int,!*World)
mkdir name mode world = code {
ccall mkdir "sI:I:A"
}
rmdir :: !{#Char} !*World -> (!Int,!*World)
rmdir name world = code {
ccall rmdir "s:I:A"
}
rename :: !{#Char} !{#Char} !*World -> (!Int,!*World)
rename old new world = code {
ccall rename "ss:I:A"
}
opendir :: !{#Char} !*World -> (!Pointer,!*World)
opendir path world = code {
ccall opendir "s:p:A"
}
closedir :: !Pointer !*World -> (!Int,!*World)
closedir dir world = code {
ccall closedir "p:I:A"
}
readdir :: !Pointer !*World -> (!Pointer,!*World)
readdir dir world = code {
ccall readdir "p:p:A"
}
malloc :: !Int -> Pointer
malloc num = code {
......
definition module Directory
from Void import :: Void
from FilePath import :: FilePath
from Error import :: MaybeError
from OSError import :: MaybeOSError, :: OSError, :: OSErrorMessage, :: OSErrorCode
createDirectory :: !FilePath !*World -> (!MaybeOSError Void, !*World)
removeDirectory :: !FilePath !*World -> (!MaybeOSError Void, !*World)
renameDirectory :: !FilePath !FilePath !*World -> (!MaybeOSError Void, !*World)
readDirectory :: !FilePath !*World -> (!MaybeOSError [FilePath], !*World)
getCurrentDirectory :: !*World -> (!MaybeOSError FilePath, !*World)
setCurrentDirectory :: !FilePath !*World -> (!MaybeOSError Void, !*World)
implementation module Directory
import StdArray, StdBool, StdClass, StdInt, StdChar, StdString
import Void
import FilePath
import OSError
import _Posix
import _Pointer
createDirectory :: !FilePath !*World -> (!MaybeOSError Void, !*World)
createDirectory path world
# (ret,world) = mkdir (packString path) 493 world // 493 = 0755 in octal
| ret == 0
= (Ok Void, world)
| otherwise
= getLastOSError world
removeDirectory :: !FilePath !*World -> (!MaybeOSError Void, !*World)
removeDirectory path world
# (ret,world) = rmdir (packString path) world
| ret == 0
= (Ok Void, world)
| otherwise
= getLastOSError world
renameDirectory :: !FilePath !FilePath !*World -> (!MaybeOSError Void, !*World)
renameDirectory oldpath newpath world
# (ret,world) = rename (packString oldpath) (packString newpath) world
| ret == 0
= (Ok Void, world)
| otherwise
= getLastOSError world
readDirectory :: !FilePath !*World -> (!MaybeOSError [FilePath], !*World)
readDirectory path world
# (dirptr,world) = opendir path world
| dirptr == 0
= getLastOSError world
# (entries,world) = readEntries dirptr world
# (ret,world) = closedir dirptr world
| ret == 0
= (Ok entries, world)
| otherwise
= getLastOSError world
where
readEntries :: !Pointer !*World -> (![String],!*World)
readEntries dirptr world
# (entryptr,world) = readdir dirptr world
| entryptr == 0
= ([],world)
# (entry,world) = readEntry entryptr world
# (entries,world) = readEntries dirptr world
= ([entry:entries],world)
readEntry :: !Pointer !*World -> (!String,!*World)
readEntry entryptr world
= (derefString (entryptr + DIRENT_D_NAME_OFFSET), world)
getCurrentDirectory :: !*World -> (!MaybeOSError FilePath, !*World)
getCurrentDirectory world
# buf = createArray MAXPATHLEN '\0'
# (ptr,world) = getcwd buf MAXPATHLEN world
| ptr == 0
= getLastOSError world
| otherwise
= (Ok {c \\ c <-: buf | c <> '\0'},world)
setCurrentDirectory :: !FilePath !*World -> (!MaybeOSError Void, !*World)
setCurrentDirectory path world
# (ret,world) = chdir (packString path) world
| ret == 0
= (Ok Void, world)
| otherwise
= getLastOSError world
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment