File.readAll
func File.readAll(f: File.Handle, ctx: Context | null = null): Result[string, Error]File.readAll reads the rest of f as text. Bytes that are not valid UTF-8 become U+FFFD.
Details
File.readAllreads from where the handle is to the end. What earlier reads of the handle took, such as a header line fromFile.readLine, is not in the result.- At the end of the file it is
Okwith"", nevernull, so a secondFile.readAllgives"". - For binary data, read the handle with
File.readChunk, or the path withFile.readBytes. - On a connection, a pipe, or a terminal,
File.readAllwaits for the end of the input; standard input is often one of these. Actxends the wait, and what was read by then stays for the next read of the handle. - An
Errfrom a read of a handle does not name the file. Add the path withResult.wrapErr.
Basic examples
Read a whole file through its handle:
let f = try File.open("notes.txt")
defer { f.close() }
print(try File.readAll(f))buy milk
call AnnRead everything on standard input:
let input = try File.readAll(Os.stdin())
println(input.trim().toUpper())HELLO FROM STDINScope
After File.readLine, File.readAll returns the rest. The reads of a handle share one buffer, so nothing is skipped or read twice:
let f = try File.open("people.csv")
defer { f.close() }
let header = try f.readLine()
let rest = try f.readAll()
println("header: ${header}")
print(rest)header: name,age
Ann,31
Bob,27An empty file, and a handle already at its end, give Ok with "":
let f = try File.open("empty.txt")
defer { f.close() }
println(f.readAll())Ok{ value: "" }Bytes that are not valid UTF-8 become U+FFFD, the replacement character. Here 0xe9 is é in Latin-1:
try File.writeBytes("latin1.txt", Bytes.fromArray([0x63, 0x61, 0x66, 0xe9]))
let f = try File.open("latin1.txt")
defer { f.close() }
println(try f.readAll())caf�A pipe from Os.openPipe is read to its end once every copy of its write end is closed. Here a child process writes into it:
let pipe = try Os.openPipe()
defer { pipe.read.close() }
let child = try Cmd.start(Cmd.new("echo", ["built", "ok"]), stdout: pipe.write)
try pipe.write.close()
print(try pipe.read.readAll())
println(try child.wait())built ok
0Named arguments
ctx
A context ends a wait on a pipe, a connection, or a terminal with Err and the context's reason. The text that arrived before stays in the handle, and the next read returns it:
let pipe = try Os.openPipe()
defer { pipe.read.close() }
try pipe.write.writeText("first part, ")
let ctx = Context.new(50ms)
println(pipe.read.readAll(ctx: ctx))
try pipe.write.writeText("second part")
try pipe.write.close()
println(pipe.read.readAll())Err{ error: Error{ message: "timeout" } }
Ok{ value: "first part, second part" }A read of a regular file never waits, so there the context is looked at only before the read begins:
let ctx = Context.new()
Context.cancel(ctx)
let f = try File.open("notes.txt")
defer { f.close() }
println(f.readAll(ctx: ctx))Err{ error: Error{ message: "cancelled" } }Applications
Count the words a pipeline sends to a script, as wc -w does:
let text = try Os.stdin().readAll()
println(text.fields().length())9Read a message whose first line is a subject and whose body is everything after it. The reads of the handle name no file, so loadMessage adds the path:
type Message = { subject: string, body: string }
func readMessage(f: File.Handle): Result[Message, Error] {
let first = try f.readLine() ?? ""
let body = try f.readAll()
Result.ok(Message{ subject: first.trimPrefix("Subject: "), body: body.trim() })
}
func loadMessage(path: string): Result[Message, Error] {
let f = try File.open(path)
defer { f.close() }
readMessage(f) | Result.wrapErr("read ${path}")
}
let message = try loadMessage("message.txt")
println(message.subject)
println(message.body)Release notes
Version 1.2 is out.
It fixes two bugs.Properties and relations
Reading a fresh handle to the end gives what File.read gives for the path:
let f = try File.open("notes.txt")
defer { f.close() }
assert(try f.readAll() == try File.read("notes.txt"))
assert(try f.readAll() == "")A header line read with File.readLine, its line break, and the rest make up the whole file:
let f = try File.open("people.csv")
defer { f.close() }
let header = try f.readLine() ?? ""
let rest = try f.readAll()
assert("${header}\n${rest}" == try File.read("people.csv"))Possible issues
A second File.readAll does not start over. It returns "", since the first one read to the end:
let f = try File.open("notes.txt")
defer { f.close() }
println(f.readAll())
println(f.readAll())Ok{ value: "buy milk\ncall Ann\n" }
Ok{ value: "" }A pipe is at its end only when every copy of its write end is closed, the program's own included. Without the close, File.readAll waits forever; here a context stops it:
let pipe = try Os.openPipe()
defer { pipe.read.close() }
defer { pipe.write.close() }
try pipe.write.writeText("done")
// The write end is still open, so the end never comes.
println(pipe.read.readAll(ctx: Context.new(50ms)))Err{ error: Error{ message: "timeout" } }A handle from File.create or File.append is for writing, and a closed handle reads nothing. Both are Err, and the message does not name the file:
let out = try File.create("log.txt")
println(out.readAll())
try out.close()
let f = try File.open("log.txt")
try f.close()
println(f.readAll())Err{ error: Error{ message: "the file is not open for reading" } }
Err{ error: Error{ message: "the file is closed" } }See also
File.read · File.open · File.readLine · File.readChunk · File.readExactly · File.pipe · File.close · Os.stdin · Os.openPipe
