libstd/stdio

Console and file-descriptor I/O: the variadic print, buffered line input, and the raw read/write syscall wrappers. Including it also brings in libstd/string and libstd/list.

#include <libstd/stdio>

Includes libstd/macro, libstd/string, libstd/list. Same functions and constant names on Linux and Windows: the system-specific part is in wrapper/linux/stdio and wrapper/windows/stdio (see Linux and Windows).

Example

#include <libstd/stdio>

name = "world"
print("Hello, ", name, "! ", 42, " ", 2.5, "\n")

ib = input_buffer_new()
line = input(ib)              // one line from standard input, without the newline
fprint(2, "done\n")          // standard error
Hello, world! 42 2.500000

Constants

NameLinuxWindowsMeaning
INPUT_BUFFER_SIZE40964096

Classes

class
InputBuffer

A 4096-byte read buffer over a file descriptor; create with input_buffer_new. eof is set when the last read hit the end of the stream.

    buf str
    len int
    pos int
    eof int

Functions

def input_buffer_new() -> InputBuffer

Creates an empty InputBuffer. Several independent buffers can exist at once.

def input_buffered(ib: InputBuffer, fd: int) -> char

Returns the next character from fd through the buffer, issuing a real read only every 4096 characters. Returns '\0' at end of stream or on error and sets ib.eof.

def at_eof(ib: InputBuffer) -> int

1 if the last read through ib hit the end of the stream (or an error). Needed because input/readline return "" both for an empty line and at end of file.

def input(ib: InputBuffer) -> str

Reads one line from standard input (up to '\n' or end of file) and returns it without the newline. Check at_eof(ib) to tell an empty line from the end of input.

def readline(ib: InputBuffer, fd: int) -> str

Like input but from any file descriptor.

def fprint(fd: int, stringa: str) -> long

Writes a NUL-terminated string to a file descriptor (1 = stdout, 2 = stderr, or any open file/socket). Returns the number of bytes written.

def freadc(fd: int) -> int

Reads one byte from a file descriptor and returns it (0–255), or -1 at end of file or on error.

def print(*args) -> None

Prints any number of arguments of mixed type to standard output with no separator and no trailing newline: str as is, int/long with itoa, float with ftoa, a char as the character (c as int for its code).

def sys_read(fd: int, start_offs: str, length: long) -> int

Raw read(2): reads up to length bytes into start_offs. Returns the number of bytes read, 0 at end of file, negative on error. No bounds check: the buffer must be at least length bytes. On Windows it reads a socket with recv, so a receive timeout applies.