-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileIO.idr
57 lines (50 loc) · 1.71 KB
/
FileIO.idr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
module FileIO
import Data.Vect
read_from_console : IO (List String)
read_from_console = do
cur_line <- getLine
if (cur_line == "") then pure Nil else do
tail <- read_from_console
pure (cur_line :: tail)
concat_list : List String -> String
concat_list [] = ""
concat_list (x :: xs) =
let tail_string = concat_list xs in
x ++ "\n" ++ tail_string
read_and_save : IO ()
read_and_save = do
putStrLn "Enter content, that should be saved"
user_input <- read_from_console
putStrLn "Enter file name"
file_name <- getLine
let content = concat_list user_input
Right _ <- writeFile file_name content |
Left error => do
putStrLn "An I/O error occured, see log for description"
putStrLn (show error)
putStrLn "Content has been saved"
read_file_content : (file : File) ->
IO (Either FileError (n : Nat ** Vect n String))
read_file_content file = do
is_eof <- fEOF file
if (is_eof) then
pure (Right (_ ** []))
else do
Right cur_line <- fGetLine file | Left error => pure (Left error)
Right (tail_lenght ** file_tail) <- read_file_content file |
Left error => pure (Left error)
pure (Right ((S tail_lenght) ** cur_line :: file_tail))
read_vector_from_file : (file_name : String) -> IO (n : Nat ** Vect n String)
read_vector_from_file file_name = do
Right file <- openFile file_name Read |
Left error => do
putStrLn "An I/O error occured while opening file, see log for description"
putStrLn (show error)
pure (_ ** [])
Right result <- read_file_content file |
Left error => do
putStrLn "An I/O error occured while reading file, see log for description"
putStrLn (show error)
pure (_ ** [])
_ <- closeFile file
pure result