0 | module FS.Posix.Internal
 1 |
 2 | import FS.Internal.Bytes
 3 | import IO.Async.Posix
 4 |
 5 | %default total
 6 |
 7 | parameters {auto fid : FileDesc a}
 8 |            (fd       : a)
 9 |            {auto has : Has Errno es}
10 |            {auto ph  : PollH e}
11 |
12 |   bytes : SnocList ByteString -> Async e es ()
13 |   bytes [<]   = pure ()
14 |   bytes [<bs] = fwritenb fd bs
15 |   bytes bss   = fwritenb fd (fastConcat $ bss <>> [])
16 |
17 |   ||| Writes a chunk of data
18 |   export
19 |   writeAll : Cast r ByteString => List r -> Async e es ()
20 |   writeAll = go [<]
21 |     where
22 |       go : SnocList ByteString -> List r -> Async e es ()
23 |       go sx []        = bytes sx
24 |       go sx (x :: xs) =
25 |         case cast {to = ByteString} x of
26 |           BS 0 _ => go sx xs
27 |           bs     => go (sx :< bs) xs
28 |
29 |   ||| Writes a chunk of data, inserting a newline character (`0x0a`)
30 |   ||| after every item.
31 |   export
32 |   writeLines : Cast r ByteString => List r -> Async e es ()
33 |   writeLines = go [<]
34 |     where
35 |       go : SnocList ByteString -> List r -> Async e es ()
36 |       go sx []        = bytes sx
37 |       go sx (x :: xs) =
38 |         case cast {to = ByteString} x of
39 |           BS 0 _ => go (sx :< nl) xs
40 |           bs     => go (sx :< bs :< nl) xs
41 |