Input
Reads a regular, seekable file into memory. The file functions use fseek and ftell, so a pipe or other non-seekable path fails.
Usage#
inp_get_binary reads a whole regular file in binary mode. Pass a size out-parameter. On success the caller owns *fsz bytes and frees them. An empty file still returns a non-NULL buffer with *fsz == 0.
size_t fsz = 0;
unsigned char *buf = inp_get_binary("data.bin", &fsz);
if (buf != NULL) {
/* fsz bytes at buf */
free(buf);
}
*_arena functions take an ArenaAllocator. A non-NULL arena allocates from that arena, and those pointers must not be passed to free. LfLines from an arena has heap == 0, and inp_lines_free then only zeros the struct. Those pointers stay valid only while that storage remains in place. arena_clear, arena_restore past the allocation, and arena_free drop it. A successful arena_resize_buf on an owning arena can move it. A NULL arena uses malloc.
Structs#
LfLines#
A table of NUL-terminated strings. Each string in a heap result is its own allocation. Arena results live in the arena.
typedef struct {
char *base;
char **lines;
size_t n;
int heap;
} LfLines;
lines is the table and n is its length. heap is nonzero when the table was allocated with malloc. base is freed with a heap table. This module sets base to NULL.
Functions#
inp_get_binary#
Reads path in binary mode ("rb"). On success *fsz is the byte length and the caller frees the heap buffer. The buffer is not NUL-terminated. fsz must not be NULL. An empty file returns a non-NULL allocation and sets *fsz to 0. Returns NULL if path or fsz is NULL, the file cannot be opened, fseek or ftell fails, allocation fails, or the read is short. Ignore *fsz when the return is NULL. The size may already have been stored if the failure came after ftell. The arena variant must not be freed.
unsigned char *inp_get_binary(const char *path, size_t *fsz);
unsigned char *inp_get_binary_arena(ArenaAllocator *arena, const char *path,
size_t *fsz);inp_get_input#
Reads path in text mode ("r") and returns a buffer of the file size plus one, with a NUL stored after the bytes. The caller frees the heap result. An empty file returns a non-NULL empty string. Returns NULL if path is NULL, the file cannot be opened, fseek or ftell fails, the size is SIZE_MAX, allocation fails, or the read is short. A NUL byte inside the file stays in the buffer, so C-string functions stop there. The arena variant must not be freed.
char *inp_get_input(const char *path);
char *inp_get_input_arena(ArenaAllocator *arena, const char *path);inp_get_lines#
Reads path with inp_get_input and splits on \n into out. Each piece is a NUL-terminated copy. One trailing \r on a piece is stripped, so \r\n and \n both yield the same line. A bare \r is not a line break. A trailing newline does not add an extra empty line. A file that is only a newline yields one empty line. Any other empty piece is kept. An empty file, or a file whose first byte is NUL, returns 0 with out->n == 0, out->lines == NULL, and out->base == NULL. Text after an embedded NUL is omitted, because the split uses C-string search. Returns 0 on success. Returns -1 if path or out is NULL, the file cannot be read, or allocation fails, and then out is unchanged. On success heap is 1 for inp_get_lines and 0 for the arena variant, including an empty file. Call inp_lines_free on a previous result before reusing the handle.
int inp_get_lines(const char *path, LfLines *out);
int inp_get_lines_arena(ArenaAllocator *arena, const char *path, LfLines *out);inp_lines_free#
When heap is nonzero, frees each string, the pointer table, and base, then zeros the struct. When heap is 0, only zeros the struct and does not free the arena bytes. A NULL pointer is a no-op.
void inp_lines_free(LfLines *lines);inp_get_ints#
Reads path as lines and parses each with strtonum in base 10, bounded to int. On success *sz is the count. Leading whitespace and an optional sign are accepted. Trailing whitespace, a blank line, and any other non-decimal text are rejected, and the call returns NULL. An empty file returns a non-NULL pointer and sets *sz to 0. The caller frees that heap pointer. Returns NULL if path or sz is NULL, the file cannot be read, a line is out of range or not a decimal integer, or allocation fails. *sz is unchanged on failure. The arena array must not be freed.
int *inp_get_ints(const char *path, size_t *sz);
int *inp_get_ints_arena(ArenaAllocator *arena, const char *path, size_t *sz);inp_split#
Splits s on the string delim into NUL-terminated copies in out. Does not modify s. Empty pieces are kept, so "a,,b" split on "," yields three strings. An empty s yields one empty piece. An empty delim yields one piece, a copy of s. Returns 0 on success. Returns -1 if s, delim, or out is NULL, or if allocation fails, and then out is unchanged. On success base is NULL, and heap is 1 for inp_split and 0 for the arena variant. Release a heap result with inp_lines_free.
int inp_split(const char *s, const char *delim, LfLines *out);
int inp_split_arena(ArenaAllocator *arena, const char *s, const char *delim,
LfLines *out);
Process helpers (lf_capture_system) live in process, not this header.