diff --git a/libs/base/System/File/ReadWrite.idr b/libs/base/System/File/ReadWrite.idr index e310ede62..8dea10ebc 100644 --- a/libs/base/System/File/ReadWrite.idr +++ b/libs/base/System/File/ReadWrite.idr @@ -3,6 +3,7 @@ module System.File.ReadWrite import public Data.Fuel import Data.List +import Data.SnocList import System.File.Handle import public System.File.Error @@ -113,6 +114,21 @@ fEOF (FHandle f) = do res <- primIO (prim__eof f) pure (res /= 0) +||| Read all the remaining contents of a file handle +export +covering +fRead : HasIO io => (h : File) -> io (Either FileError String) +fRead h = fRead' h [<] + where + fRead' : HasIO io' => (h : File) -> (acc : SnocList String) -> io' (Either FileError String) + fRead' h acc = do + if !(fEOF h) + then pure $ Right $ concat acc + else do + Right line <- fGetLine h + | Left err => pure $ Left err + fRead' h $ acc :< line + ||| Delete the file at the given name. ||| ||| @ fname the file to delete diff --git a/tests/base/system_file_fRead/ReadFile.idr b/tests/base/system_file_fRead/ReadFile.idr new file mode 100644 index 000000000..51c3cd6a8 --- /dev/null +++ b/tests/base/system_file_fRead/ReadFile.idr @@ -0,0 +1,9 @@ +import System.File + +main : IO () +main = do + Right f <- openFile "sampleFile.txt" Read + | Left err => printLn err + Right contents <- fRead f + | Left err => printLn err + printLn contents diff --git a/tests/base/system_file_fRead/expected b/tests/base/system_file_fRead/expected new file mode 100644 index 000000000..747a1e5e5 --- /dev/null +++ b/tests/base/system_file_fRead/expected @@ -0,0 +1,3 @@ +1/1: Building ReadFile (ReadFile.idr) +Main> "Hello, world\nLorem ipsum\n" +Main> Bye for now! diff --git a/tests/base/system_file_fRead/input b/tests/base/system_file_fRead/input new file mode 100644 index 000000000..fc5992c29 --- /dev/null +++ b/tests/base/system_file_fRead/input @@ -0,0 +1,2 @@ +:exec main +:q diff --git a/tests/base/system_file_fRead/run b/tests/base/system_file_fRead/run new file mode 100644 index 000000000..0f4e78239 --- /dev/null +++ b/tests/base/system_file_fRead/run @@ -0,0 +1,3 @@ +rm -rf build + +$1 --no-color --console-width 0 --no-banner ReadFile.idr < input diff --git a/tests/base/system_file_fRead/sampleFile.txt b/tests/base/system_file_fRead/sampleFile.txt new file mode 100644 index 000000000..05d109acb --- /dev/null +++ b/tests/base/system_file_fRead/sampleFile.txt @@ -0,0 +1,2 @@ +Hello, world +Lorem ipsum