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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>
use bufio;
use errors;
use fs;
use io;
// Opens a file and allocates read and/or write buffers for buffered I/O. To
// open an unbuffered [[io::file]] see [[open]].
//
// [[fs::flag::CREATE]] isn't very useful with this function, since the new
// file's mode is set to zero. For this use-case, use [[create]] instead.
export fn open_buffered(
path: str,
flags: fs::flag = fs::flag::RDONLY,
) (bufio::stream | fs::error | nomem) = {
const (rd, wr) = mkbuffers(flags)?;
match (open(path, flags)) {
case let file: io::file =>
return bufio::init(file, rd, wr, bufio::flag::MANAGED);
case let err: fs::error =>
free(rd);
free(wr);
return err;
};
};
// Creates a new file with the given mode if it doesn't already exist and opens
// it for writing, allocating read and/or write buffers for buffered I/O. To
// open an unbuffered [[io::file]] see [[create]].
//
// Only the permission bits of the mode are used. If other bits are set, they
// are discarded.
export fn create_buffered(
path: str,
mode: fs::mode,
flags: fs::flag = fs::flag::WRONLY | fs::flag::TRUNC,
) (bufio::stream | fs::error | nomem) = {
const (rd, wr) = mkbuffers(flags)?;
match (create(path, mode, flags)) {
case let file: io::file =>
return bufio::init(file, rd, wr, bufio::flag::MANAGED);
case let err: fs::error =>
free(rd);
free(wr);
return err;
};
};
fn mkbuffers(flags: fs::flag) (([]u8, []u8) | nomem) = {
const mode = flags & 0b11;
const need_read = mode != fs::flag::WRONLY;
const need_write = mode != fs::flag::RDONLY;
let ok = false;
let read: []u8 = [];
let write: []u8 = [];
defer if (!ok) {
free(read);
free(write);
};
if (need_read) {
read = alloc([0...], BUFSZ)?;
};
if (need_write) {
write = alloc([0...], BUFSZ)?;
};
ok = true;
return (read, write);
};
|