vera:io
Console I/O and file operations.
import "vera:io"
Overview
Provides console I/O and file operations. All functions are built on compiler primitives (__vera_sys_*) which the compiler maps to platform-specific syscalls.
Dependencies: None (uses primitives directly)
Constants
File Open Flags
Combine with bitwise_or when calling open_file:
| Constant | Value | Description |
|---|---|---|
FILE_READ | 1 | Open for reading |
FILE_WRITE | 2 | Open for writing |
FILE_CREATE | 4 | Create if it does not exist |
FILE_TRUNCATE | 8 | Truncate to zero length if it exists |
FILE_APPEND | 16 | Writes append to end of file |
Seek Origins
| Constant | Value | Description |
|---|---|---|
SEEK_START | 0 | Seek from beginning of file |
SEEK_CURRENT | 1 | Seek from current position |
SEEK_END | 2 | Seek from end of file |
Functions
Console I/O
write_stdout
function write_stdout(data: ptr, length: i64) -> i64
Write bytes to standard output (fd 1).
Parameters:
data(ptr) — pointer to the byte buffer to writelength(i64) — number of bytes to write
Returns: number of bytes actually written
write_stderr
function write_stderr(data: ptr, length: i64) -> i64
Write bytes to standard error (fd 2).
Parameters:
data(ptr) — pointer to the byte buffer to writelength(i64) — number of bytes to write
Returns: number of bytes actually written
read_stdin
function read_stdin(buffer: ptr, max_length: i64) -> i64
Read bytes from standard input (fd 0).
Parameters:
buffer(ptr) — pointer to the buffer to read intomax_length(i64) — maximum number of bytes to read
Returns: number of bytes actually read
File Operations
open_file
function open_file(path: ptr, flags: i32) -> i32
Open a file with the specified flags. The path must be a null-terminated string. Uses default file permissions 0644 (owner read/write, group/other read).
Parameters:
path(ptr) — null-terminated file pathflags(i32) — combination of file open flags (see Constants above)
Returns: file descriptor on success, or a negative value on error
create_file
function create_file(path: ptr) -> i32
Create a new file (or truncate an existing one) for writing. Equivalent to opening with FILE_WRITE | FILE_CREATE | FILE_TRUNCATE (flags = 14). The path must be null-terminated.
Parameters:
path(ptr) — null-terminated file path
Returns: file descriptor on success, or a negative value on error
close_file
function close_file(fd: i32) -> i32
Close an open file descriptor.
Parameters:
fd(i32) — file descriptor to close
Returns: 0 on success, or a negative value on error
write_file
function write_file(fd: i32, data: ptr, length: i64) -> i64
Write bytes to an open file.
Parameters:
fd(i32) — file descriptordata(ptr) — pointer to the byte buffer to writelength(i64) — number of bytes to write
Returns: number of bytes actually written
read_file
function read_file(fd: i32, buffer: ptr, max_length: i64) -> i64
Read bytes from an open file into a buffer.
Parameters:
fd(i32) — file descriptorbuffer(ptr) — pointer to the buffer to read intomax_length(i64) — maximum number of bytes to read
Returns: number of bytes actually read
delete_file
function delete_file(path: ptr) -> i32
Delete a file from the filesystem. The path must be null-terminated.
Parameters:
path(ptr) — null-terminated file path
Returns: 0 on success, or a negative value on error
rename_file
function rename_file(old_path: ptr, new_path: ptr) -> i32
Rename or move a file. Both paths must be null-terminated.
Parameters:
old_path(ptr) — null-terminated current file pathnew_path(ptr) — null-terminated new file path
Returns: 0 on success, or a negative value on error
file_size
function file_size(path: ptr) -> i64
Get the size of a file in bytes. The path must be null-terminated.
Parameters:
path(ptr) — null-terminated file path
Returns: file size in bytes, or a negative value on error
seek_file
function seek_file(fd: i32, offset: i64, origin: i32) -> i64
Reposition the read/write offset of an open file.
Parameters:
fd(i32) — file descriptoroffset(i64) — byte offset relative tooriginorigin(i32) — seek origin: 0 = from start, 1 = from current position, 2 = from end
Returns: the new absolute position in the file, or a negative value on error