~/bend-docscommunity

src/io/file.bend checks

raw source on the hub · import 0x74799d3f846634a3d7461d4336d7c9d4/src/io/file.bend as MFile

io/file: a whole file read or written in one call. Base gives an open handle and chunked reads; this is the loop over them.

1 import
import Base

Definitions

def slurp.end source · line 6 · raw

@file:File -> @acc:String -> IO(String)

the handle closed, and whatever was gathered

def slurp.cut source · line 12 · raw

@chunk:String -> @file:File -> @acc:String -> @rest:(@_:File -> @_:String -> IO(String)) -> IO(String)

an empty chunk means the end of the file

def slurp.more source · line 20 · raw

@res:Result<&1, &1, Pair(U32, String), String> -> @file:File -> @acc:String -> @rest:(@_:File -> @_:String -> IO(String)) -> IO(String)

a failed read ends the file as surely as an empty one

def slurp.next source · line 33 · raw

@got:Pair(File, Result<&1, &1, Pair(U32, String), String>) -> @acc:String -> @rest:(@_:File -> @_:String -> IO(String)) -> IO(String)

the handle comes back paired with what it read

def slurp source · line 42 · raw

@fuel:Nat -> @file:File -> @acc:String -> IO(String)

the whole file, read in chunks until one is empty, or the fuel is

def unsized source · line 54 · raw

Nat

the reads a file with no size to go by may take: a pipe, a device or a /proc file reports 0 bytes and a file past 4 GiB fails to report, so these are read until they end, up to 100000 reads of 64 KiB, past any file ez reads

def reads.given source · line 58 · raw

@none:Bool -> @bytes:U32 -> Nat

a size of 0 is no size to go by; any other is read to

def reads source · line 67 · raw

@+bytes:U32 -> Nat

the reads a file of this many bytes takes: one for every 64 KiB, one for the part past the last whole 64 KiB, and one to spare for a short read

def reads.of source · line 71 · raw

@res:Result<&1, &1, Pair(U32, String), U32> -> Nat

the reads the size the host reported allows

def read.sized source · line 79 · raw

@got:Pair(File, Result<&1, &1, Pair(U32, String), U32>) -> IO(String)

a file read whole, with fuel enough for the size it reported

def read.opened source · line 84 · raw

@res:Result<&1, &1, Pair(U32, String), File> -> IO(Maybe<&2, String>)

what an opened file holds, or nothing when it would not open

def read source · line 95 · raw

@path:String -> IO(Maybe<&2, String>)

a file's text, or None when it cannot be opened

def write.ok source · line 101 · raw

@res:Result<&1, &1, Pair(U32, String), Unit> -> Bool

whether a write reported success

def write.done source · line 109 · raw

@written:Pair(File, Result<&1, &1, Pair(U32, String), Unit>) -> IO(Bool)

a write's outcome, once the handle is shut

def write.opened source · line 116 · raw

@res:Result<&1, &1, Pair(U32, String), File> -> @text:String -> IO(Bool)

the text written to an opened file

def write source · line 126 · raw

@path:String -> @text:String -> IO(Bool)

a file replaced with this text; False when it could not be written

def text_of source · line 132 · raw

@got:Maybe<&2, String> -> String

a file's text, or "" when there is none