implementation module File import StdEnv, StdMaybe, Directory import FastString import Platform print :: !String !*World -> *World print msg world # (console,world) = stdio world # console = fwrites msg console # (_,world) = fclose console world = world // TODO! Stringappender? readFile :: !String !*World -> (!String,!*World) readFile filename world # (ok,file,world) = fopen filename FReadData world | ok # (content,file)= rec file "" # (ok,world) = fclose file world = (content,world) | otherwise = ("", world) //abort ("Failed to read file: " +++ filename) where rec :: *File String -> (String, *File) rec file acc # (string, file) = freads file 1000 | string == "" = (acc, file) | otherwise = rec file (acc +++ string) readLine :: !String !*World -> (Bool, Maybe String, !*World) readLine filename world # (ok,file,world) = fopen filename FReadData world | not ok = (False, Nothing, world) # (line,file) = freadline file # (ok,world) = fclose file world = (True, Just line, world) readFileArray :: !String !*World -> (Bool, [String],!*World) readFileArray filename world # (ok,file,world) = fopen filename FReadData world | ok # (content,file)= rec file [] # (ok,world) = fclose file world = (True,content,world) | otherwise = (False,[],world) where rec :: *File [String] -> ([String], *File) rec file acc # (string, file) = freadline file | string == "" = (acc, file) | otherwise = rec file [string:acc] writeFile :: !String !String !*World -> *World writeFile filename content world # (ok,file,world) = fopen filename FWriteData world | not ok = abort ("Failed to write file: " +++ filename) # file = fwrites content file # (ok,world) = fclose file world = world copyFile :: !String !String !*World -> *World copyFile inf outf world # (ok,inh,world) = fopen inf FReadData world | not ok = world # (ok,outh,world) = fopen outf FWriteData world | not ok = world # (inh,outh) = copy inh outh # (_,world) = fclose inh world # (_,world) = fclose outh world = world where copy inh outh # (string, inh) = freads inh 1000 | string == "" = (inh, outh) | otherwise # outh = fwrites string outh = copy inh outh isDirectory :: !String !*World -> (Bool, *World) isDirectory dir world # ((ok, path), world) = pd_StringToPath dir world = case getFileInfo path world of ((NoDirError, {pi_fileInfo = {isDirectory = True}}), world) = (True, world) (_ , world) = (False, world) removeFiles :: !String !*World -> *World removeFiles dir world # ((ok, path), world) = pd_StringToPath dir world | ok # ((_, entries), world) = getDirectoryContents path world = foldl remove_one world entries where remove_one world entry = if entry.fileInfo.pi_fileInfo.isDirectory (if (entry.fileName == "." || entry.fileName == "..") (world) (remove_file (full_path entry.fileName) (removeFiles (full_path entry.fileName) world))) (remove_file (full_path entry.fileName) world) remove_file file world # ((ok, path), world) = pd_StringToPath file world # (_, world) = fremove path world = world full_path fileName = dir +++ {path_separator} +++ fileName fileList :: !String !*World -> (Bool, [String], *World) fileList dir world # ((ok, path), world) = pd_StringToPath dir world | ok # ((_, entries), world) = getDirectoryContents path world = (True, map to_file_name (filter files_only entries), world) = (False, [], world) where files_only entry = not entry.fileInfo.pi_fileInfo.isDirectory to_file_name entry = entry.fileName extractPathAndFile :: !String -> (!String,!String) extractPathAndFile path_and_file #! (dir_delimiter_found,i) = charIndexBackwards path_and_file (size path_and_file - 1) path_separator | dir_delimiter_found # file_name_with_extension = path_and_file % (i+1,size path_and_file - 1) = (if (i == 0) (toString path_separator) (path_and_file % (0,i-1)),file_name_with_extension) = ("",path_and_file) extractFileName :: !String -> String extractFileName path_name_ext #! (_,name_ext) = extractPathAndFile path_name_ext #! (name,ext) = extractPathFileAndExtension name_ext = name extractPathFileAndExtension :: !String -> (!String,!String) extractPathFileAndExtension path_and_file | dot_found && not path_separator_after_dot_found #! extension = path_and_file % (inc dot_index,size path_and_file-1) #! pathfile = path_and_file % (0, dot_index-1) = (pathfile,extension) = (path_and_file,"") where (dot_found,dot_index) = charIndexBackwards path_and_file (size path_and_file - 1) '.' (path_separator_after_dot_found,path_sep_index) = charIndex path_and_file (inc dot_index) path_separator