v2 / vlib / os / file_buffering.c.v
42 lines · 36 sloc · 2.75 KB · 09cc0c5133d075ee6dcff29286dce7eb7d4421da
Raw
1module os
2
3fn C.setvbuf(stream &C.FILE, buffer &char, mode i32, size usize) i32
4
5// FileBufferMode describes the available buffering modes for an os.File: unbuffered, line buffered and block/fully buffered.
6// Normally all files are block buffered. If a stream refers to a terminal (as stdout normally does), it is line buffered.
7// The standard error stream stderr is always unbuffered by default.
8pub enum FileBufferMode {
9 fully_buffered = C._IOFBF // many characters are saved up and written as a block
10 line_buffered = C._IOLBF // characters are saved up until a newline is output or input is read from any stream attached to a terminal device (typically stdin)
11 not_buffered = C._IONBF // information appears on the destination file or terminal as soon as it is written
12}
13
14// setvbuf sets the buffer and buffering mode for the given file `f`. See also os.FileBufferMode.
15// It returns 0 on success. It returns nonzero on failure (the mode is invalid, or the request cannot be honored).
16// Except for unbuffered files, the buffer argument should point to a buffer at least size bytes long; this buffer will be used instead of the current buffer.
17// If the argument buffer is nil, only the mode is affected; a new buffer will be allocated on the next read or write operation.
18// Note: make sure, that the space, that buffer points to (when != 0), still exists by the time the file stream is closed, which also happens at program termination.
19// Note: f.setvbuf() may be used only after opening a file stream, and before any other operations have been performed on it.
20@[unsafe]
21pub fn (mut f File) setvbuf(buffer &char, mode FileBufferMode, size usize) int {
22 return C.setvbuf(f.cfile, buffer, int(mode), size)
23}
24
25// set_buffer sets the buffer for the file, and the file buffering mode (see also os.FileBufferMode).
26// Unlike File.setvbuf, it allows you to pass an existing V []u8 array directly.
27// Note: f.set_buffer() may be used only after opening a file stream, and before any other operations have been performed on it.
28pub fn (mut f File) set_buffer(mut buffer []u8, mode FileBufferMode) int {
29 return unsafe { f.setvbuf(buffer.data, mode, usize(buffer.len)) }
30}
31
32// set_line_buffered sets the file buffering mode to FileBufferMode.line_buffered.
33// Note: f.set_line_buffered() may be used only after opening a file stream, and before any other operations have been performed on it.
34pub fn (mut f File) set_line_buffered() {
35 unsafe { f.setvbuf(&char(nil), .line_buffered, usize(0)) }
36}
37
38// set_unbuffered sets the file buffering mode to FileBufferMode.not_buffered.
39// Note: f.set_unbuffered() may be used only after opening a file stream, and before any other operations have been performed on it.
40pub fn (mut f File) set_unbuffered() {
41 unsafe { f.setvbuf(&char(nil), .not_buffered, usize(0)) }
42}
43