x-engine-c v0.2.13
The C engine for x-lang
Loading...
Searching...
No Matches
buffer.c File Reference

Byte buffer type – construction, cursor management, and character I/O. More...

#include "x-type/buffer.h"
#include "x-type/char.h"
#include "x-eval.h"
#include "x-heap.h"

Functions

x_obj_tx_make_buffer (x_obj_t *p_base, x_obj_flag_t flags, void *p)
 
x_obj_tx_type_buffer_mark (x_obj_t *p_base, x_obj_t *p_args)
 
x_obj_tx_type_buffer_save (x_obj_t *p_base, x_obj_t *p_args)
 
x_obj_tx_type_buffer_load (x_obj_t *p_base, x_obj_t *p_args)
 
x_obj_tx_type_buffer_struct (x_obj_t *p_base, x_obj_t *p_args)
 
x_obj_tx_type_buffer_register (x_obj_t *p_base, x_obj_t *p_args)
 
x_obj_tx_type_buffer_make (x_obj_t *p_base, x_obj_t *p_args)
 
x_obj_tx_type_buffer_reset (x_obj_t *p_base, x_obj_t *p_args)
 
x_obj_tx_type_buffer_retain (x_obj_t *p_base, x_obj_t *p_args)
 
x_obj_tx_type_buffer_append (x_obj_t *p_base, x_obj_t *p_args)
 
x_obj_tx_type_buffer_read (x_obj_t *p_base, x_obj_t *p_args)
 
x_obj_tx_type_buffer_read_text (x_obj_t *p_base, x_obj_t *p_args)
 

Variables

x_satom_t x_type_buffer_name = x_obj_set(x_type_atom_obj, X_OBJ_FLAG_NONE, { .s = (x_char_t *)X_TYPE_BUFFER_NAME })
 
x_satom_t x_type_buffer_mark_prim = x_obj_set(x_type_atom_obj, X_OBJ_FLAG_NONE, { (x_obj_t *)&x_type_buffer_mark })
 
x_satom_t x_type_buffer_make_prim = x_obj_set(x_type_atom_obj, X_OBJ_FLAG_NONE, { (x_obj_t *)&x_type_buffer_make })
 
x_satom_t x_type_buffer_struct_prim = x_obj_set(x_type_atom_obj, X_OBJ_FLAG_NONE, { (x_obj_t *)&x_type_buffer_struct })
 
x_satom_t x_type_buffer_save_prim = x_obj_set(x_type_atom_obj, X_OBJ_FLAG_NONE, { (x_obj_t *)&x_type_buffer_save })
 
x_satom_t x_type_buffer_load_prim = x_obj_set(x_type_atom_obj, X_OBJ_FLAG_NONE, { (x_obj_t *)&x_type_buffer_load })
 

Detailed Description

Byte buffer type – construction, cursor management, and character I/O.

Author
Jon Ruttan (jonru.nosp@m.ttan.nosp@m.@gmai.nosp@m.l.co.nosp@m.m)

Function Documentation

◆ x_make_buffer()

x_obj_t * x_make_buffer ( x_obj_t p_base,
x_obj_flag_t  flags,
void *  p 
)

Allocate a BUFFER wrapping the character array at p.

Both read and write cursors are initialized to point at p.

Parameters
p_baseBase (execution context).
flagsObject flags (e.g. X_OBJ_FLAG_OWN, X_OBJ_FLAG_RO).
pPointer to the underlying character array.
Returns
Newly allocated BUFFER object.

◆ x_type_buffer_append()

x_obj_t * x_type_buffer_append ( x_obj_t p_base,
x_obj_t p_args 
)

Append a single character at the write cursor.

Writes the character from the second argument and advances the write cursor by one.

Parameters
p_baseBase (execution context).
p_argsArgument list: (buffer char-object).
Returns
The buffer object.

◆ x_type_buffer_load()

x_obj_t * x_type_buffer_load ( x_obj_t p_base,
x_obj_t p_args 
)

◆ x_type_buffer_make()

x_obj_t * x_type_buffer_make ( x_obj_t p_base,
x_obj_t p_args 
)

Type-system make handler for BUFFER objects.

Creates a two-level structure: the outer pair holds the base pointer, and the inner pair holds the read and write cursors (both initialized to the base pointer). The X_OBJ_FLAG_OWN flag is set only on the outer object.

Parameters
p_baseBase (execution context).
p_argsArgument list: (buffer-atom [flags-atom]).
Returns
Newly allocated BUFFER object.

◆ x_type_buffer_mark()

x_obj_t * x_type_buffer_mark ( x_obj_t p_base,
x_obj_t p_args 
)

GC mark handler for BUFFER objects.

Flags the inner bookkeeping pair directly without traversing its slots, since those contain raw char* pointers rather than objects.

Parameters
p_baseBase (execution context).
p_argsArgument list: (buffer-object flags).
Returns
NULL (mark handlers have no meaningful return).

◆ x_type_buffer_read()

x_obj_t * x_type_buffer_read ( x_obj_t p_base,
x_obj_t p_args 
)

Read one character from the buffer, extending from stdin if needed.

If the buffer is exhausted and not read-only, reads one character from the base input channel and appends it. Advances the read cursor by one and tracks newlines for error reporting.

Parameters
p_baseBase (execution context).
p_argsArgument list whose first element is the buffer.
Returns
The buffer object after advancing, or NULL on EOF.
Note
Read-only buffers return NULL when exhausted instead of reading from stdin.

◆ x_type_buffer_read_text()

x_obj_t * x_type_buffer_read_text ( x_obj_t p_base,
x_obj_t p_args 
)

Read one text character, treating NUL as EOF.

Delegates to x_type_buffer_read() and returns NULL if the result is NULL or the last character read was '\0'.

Parameters
p_baseBase (execution context).
p_argsArgument list whose first element is the buffer.
Returns
The buffer object after advancing, or NULL on EOF/NUL.
See also
x_type_buffer_read

◆ x_type_buffer_register()

x_obj_t * x_type_buffer_register ( x_obj_t p_base,
x_obj_t p_args 
)

Register (or retrieve) the BUFFER type in the type alist.

Parameters
p_baseBase (execution context).
p_argsUnused.
Returns
The registered BUFFER type object.

◆ x_type_buffer_reset()

x_obj_t * x_type_buffer_reset ( x_obj_t p_base,
x_obj_t p_args 
)

Reset both read and write cursors to the base pointer.

Effectively empties the buffer without deallocating the array.

Parameters
p_baseBase (execution context).
p_argsArgument list whose first element is the buffer.
Returns
The buffer object.

◆ x_type_buffer_retain()

x_obj_t * x_type_buffer_retain ( x_obj_t p_base,
x_obj_t p_args 
)

Compact unread data to the front of the underlying array.

Copies the remaining unread bytes to position 0 and adjusts both cursors accordingly, freeing space at the end of the buffer.

Parameters
p_baseBase (execution context).
p_argsArgument list whose first element is the buffer.
Returns
The buffer object.

◆ x_type_buffer_save()

x_obj_t * x_type_buffer_save ( x_obj_t p_base,
x_obj_t p_args 
)

Build the BUFFER type struct descriptor.

Populates name, mark, and make hooks.

Parameters
p_baseBase (execution context).
p_argsUnused.
Returns
Type struct pair-tree for BUFFER.

◆ x_type_buffer_struct()

x_obj_t * x_type_buffer_struct ( x_obj_t p_base,
x_obj_t p_args 
)

Build the BUFFER type struct descriptor.

Variable Documentation

◆ x_type_buffer_load_prim

◆ x_type_buffer_make_prim

◆ x_type_buffer_mark_prim

◆ x_type_buffer_name

◆ x_type_buffer_save_prim

◆ x_type_buffer_struct_prim