Turnstone Operating System
Loading...
Searching...
No Matches
buffer.h File Reference

buffer interface More...

#include <types.h>
#include <memory.h>

Macros

#define ___BUFFER_H   0
 
#define buffer_new()   buffer_new_with_capacity(NULL, 128)
 buffer_new creates a new buffer with default heap More...
 
#define buffer_create_with_heap(h, c)   buffer_new_with_capacity(h, c)
 
#define buffer_write_all_into(b, d)   buffer_write_slice_into(b, 0, buffer_get_length(b), d)
 
#define BUFFER_IO_INPUT   0
 
#define BUFFER_IO_OUTPUT   1
 
#define BUFFER_IO_ERROR   2
 
#define buffer_read_line(b)   buffer_read_line_ext(b, '\\', '\n', NULL)
 

Typedefs

typedef struct buffer_t buffer_t
 opaque buffer_t struct
 
typedef enum buffer_seek_direction_t buffer_seek_direction_t
 buffer_seek_direction_t is a enum that buffer seek direction
 

Enumerations

enum  buffer_seek_direction_t { BUFFER_SEEK_DIRECTION_START , BUFFER_SEEK_DIRECTION_CURRENT , BUFFER_SEEK_DIRECTION_END }
 buffer_seek_direction_t is a enum that buffer seek direction More...
 

Functions

buffer_tbuffer_new_with_capacity (memory_heap_t *heap, uint64_t capacity)
 buffer_new_with_capacity creates a new buffer with capacity More...
 
memory_heap_tbuffer_get_heap (buffer_t *buffer)
 buffer_get_heap returns heap of buffer More...
 
buffer_tbuffer_append_byte (buffer_t *buffer, uint8_t data)
 buffer_append_byte appends a byte to buffer More...
 
uint64_t buffer_get_length (buffer_t *buffer)
 returns length of buffer More...
 
boolean_t buffer_reset (buffer_t *buffer)
 resets buffer, sets length and position to 0 and all bytes to 0, capacity stays same More...
 
uint64_t buffer_get_capacity (buffer_t *buffer)
 returns capacity of buffer More...
 
uint64_t buffer_get_position (buffer_t *buffer)
 returns position of buffer More...
 
buffer_tbuffer_append_bytes (buffer_t *buffer, uint8_t *data, uint64_t length)
 appends bytes to buffer More...
 
buffer_tbuffer_append_buffer (buffer_t *buffer, buffer_t *appenden)
 appends buffer to buffer More...
 
uint8_tbuffer_get_bytes (buffer_t *buffer, uint64_t length)
 returns a byte array from buffer with length, byte array is copied, position is advanced with length More...
 
uint8_t buffer_get_byte (buffer_t *buffer)
 returns a byte array from buffer with length, byte array is copied, position is advanced 1 More...
 
uint8_tbuffer_get_all_bytes (buffer_t *buffer, uint64_t *length)
 returns a byte array containing all bytes from buffer, byte array is copied, position is not advanced More...
 
uint8_tbuffer_get_all_bytes_and_reset (buffer_t *buffer, uint64_t *length)
 returns a byte array containing all bytes from buffer, byte array is copied, position is not advanced, buffer is resetted More...
 
uint8_tbuffer_get_all_bytes_and_destroy (buffer_t *buffer, uint64_t *length)
 returns a byte array containing all bytes from buffer, byte array is copied, position is not advanced, buffer is destroyed More...
 
boolean_t buffer_seek (buffer_t *buffer, int64_t position, buffer_seek_direction_t direction)
 changes position of buffer More...
 
int8_t buffer_destroy (buffer_t *buffer)
 destroys buffer More...
 
buffer_tbuffer_encapsulate (uint8_t *data, uint64_t length)
 returns a buffer encapsulating a byte array, byte array is not copied More...
 
uint64_t buffer_remaining (buffer_t *buffer)
 returns how many bytes are remaining in buffer (length - position) More...
 
uint8_t buffer_peek_byte_at_position (buffer_t *buffer, uint64_t position)
 returns a byte from buffer at position, position is not changed More...
 
uint8_t buffer_peek_byte (buffer_t *buffer)
 returns a byte from buffer at current position, position is not changed More...
 
uint64_t buffer_peek_ints_at_position (buffer_t *buffer, uint64_t position, uint8_t bc)
 returns an int from buffer at position, position is not changed More...
 
uint64_t buffer_peek_ints (buffer_t *buffer, uint8_t bc)
 returns an int from buffer at current position, position is not changed More...
 
boolean_t buffer_set_readonly (buffer_t *buffer, boolean_t ro)
 sets readonly flag of buffer More...
 
boolean_t buffer_write_slice_into (buffer_t *buffer, uint64_t pos, uint64_t len, uint8_t *dest)
 copies bytes from buffer to dest (should be at least length long), position is not changed More...
 
uint8_tbuffer_get_view_at_position (buffer_t *buffer, uint64_t position, uint64_t length)
 returns a byte array from buffer with length, byte array is not copied, position is not changed More...
 
buffer_tbuffer_get_io_buffer (uint64_t buffer_io_id)
 returns a buffer for io buffer id, first three buffers are stdin, stdout, stderr More...
 
int64_t buffer_printf (buffer_t *buffer, const char *format,...)
 prints to buffer, returns number of bytes written More...
 
int64_t buffer_vprintf (buffer_t *buffer, const char *format, va_list args)
 prints to buffer, returns number of bytes written More...
 
buffer_tbuffer_get_tmp_buffer_for_printf (void)
 returns a temporary buffer for printf, before gui is initialized, this is a static buffer, after gui is initialized, this is a buffer allocated with malloc More...
 
void buffer_reset_tmp_buffer_for_printf (void)
 resets temporary buffer for printf
 
char_tbuffer_read_line_ext (buffer_t *buffer, char_t line_continuation_char, char_t delimiter_char, uint64_t *length)
 reads a line from buffer, line is null terminated, position is advanced More...
 
uint64_t buffer_get_mark_position (buffer_t *buffer)
 
boolean_t buffer_set_mark_position (buffer_t *buffer, uint64_t position)
 

Detailed Description

buffer interface

This work is licensed under TURNSTONE OS Public License. Please read and understand latest version of Licence.

Macro Definition Documentation

◆ ___BUFFER_H

#define ___BUFFER_H   0

macro for avoiding multiple inclusion

◆ BUFFER_IO_ERROR

#define BUFFER_IO_ERROR   2

default io buffer id for stderr

◆ BUFFER_IO_INPUT

#define BUFFER_IO_INPUT   0

default io buffer id for stdin

◆ BUFFER_IO_OUTPUT

#define BUFFER_IO_OUTPUT   1

default io buffer id for stdout

◆ buffer_new

buffer_t * buffer_new (   void)    buffer_new_with_capacity(NULL, 128)

buffer_new creates a new buffer with default heap

Returns
buffer_t* pointer to buffer

◆ buffer_read_line

#define buffer_read_line (   b)    buffer_read_line_ext(b, '\\', '\n', NULL)

macro to read a line from buffer, line is null terminated, position is advanced

◆ buffer_write_all_into

#define buffer_write_all_into (   b,
 
)    buffer_write_slice_into(b, 0, buffer_get_length(b), d)

macro to write all bytes from buffer into dest

See also
buffer_write_slice_into

Enumeration Type Documentation

◆ buffer_seek_direction_t

buffer_seek_direction_t is a enum that buffer seek direction

Enumerator
BUFFER_SEEK_DIRECTION_START 

seek from start

BUFFER_SEEK_DIRECTION_CURRENT 

seek from current position

BUFFER_SEEK_DIRECTION_END 

seek from end

Function Documentation

◆ buffer_append_buffer()

buffer_t * buffer_append_buffer ( buffer_t buffer,
buffer_t appenden 
)

appends buffer to buffer

Parameters
[in]bufferbuffer to append
[in]appendenbuffer to append
Returns
buffer_t* pointer to buffer

◆ buffer_append_byte()

buffer_t * buffer_append_byte ( buffer_t buffer,
uint8_t  data 
)

buffer_append_byte appends a byte to buffer

Parameters
[in]bufferbuffer to append
[in]databyte to append
Returns
buffer_t* pointer to buffer

◆ buffer_append_bytes()

buffer_t * buffer_append_bytes ( buffer_t buffer,
uint8_t data,
uint64_t  length 
)

appends bytes to buffer

Parameters
[in]bufferbuffer to append
[in]databytes to append
[in]lengthlength of bytes
Returns
buffer_t* pointer to buffer

◆ buffer_destroy()

int8_t buffer_destroy ( buffer_t buffer)

destroys buffer

Parameters
[in]bufferbuffer to destroy
Returns
0 if success

◆ buffer_encapsulate()

buffer_t * buffer_encapsulate ( uint8_t data,
uint64_t  length 
)

returns a buffer encapsulating a byte array, byte array is not copied

Parameters
[in]databyte array to encapsulate
[in]lengthlength of byte array
Returns
buffer_t* pointer to buffer

◆ buffer_get_all_bytes()

uint8_t * buffer_get_all_bytes ( buffer_t buffer,
uint64_t length 
)

returns a byte array containing all bytes from buffer, byte array is copied, position is not advanced

Parameters
[in]bufferbuffer to get bytes
[out]lengthlength of bytes returned
Returns
uint8_t* pointer to byte array

◆ buffer_get_all_bytes_and_destroy()

uint8_t * buffer_get_all_bytes_and_destroy ( buffer_t buffer,
uint64_t length 
)

returns a byte array containing all bytes from buffer, byte array is copied, position is not advanced, buffer is destroyed

Parameters
[in]bufferbuffer to get bytes
[out]lengthlength of bytes returned
Returns
uint8_t* pointer to byte array

◆ buffer_get_all_bytes_and_reset()

uint8_t * buffer_get_all_bytes_and_reset ( buffer_t buffer,
uint64_t length 
)

returns a byte array containing all bytes from buffer, byte array is copied, position is not advanced, buffer is resetted

Parameters
[in]bufferbuffer to get bytes
[out]lengthlength of bytes returned
Returns
uint8_t* pointer to byte array

◆ buffer_get_byte()

uint8_t buffer_get_byte ( buffer_t buffer)

returns a byte array from buffer with length, byte array is copied, position is advanced 1

Parameters
[in]bufferbuffer to get bytes
Returns
a single byte

◆ buffer_get_bytes()

uint8_t * buffer_get_bytes ( buffer_t buffer,
uint64_t  length 
)

returns a byte array from buffer with length, byte array is copied, position is advanced with length

Parameters
[in]bufferbuffer to get bytes
[in]lengthlength of bytes
Returns
uint8_t* pointer to byte array

◆ buffer_get_capacity()

uint64_t buffer_get_capacity ( buffer_t buffer)

returns capacity of buffer

Parameters
[in]bufferbuffer to get capacity
Returns
uint64_t capacity of buffer

◆ buffer_get_heap()

memory_heap_t * buffer_get_heap ( buffer_t buffer)

buffer_get_heap returns heap of buffer

Parameters
[in]bufferbuffer to get heap
Returns
memory_heap_t* pointer to heap

◆ buffer_get_io_buffer()

buffer_t * buffer_get_io_buffer ( uint64_t  buffer_io_id)

returns a buffer for io buffer id, first three buffers are stdin, stdout, stderr

Parameters
[in]buffer_io_idio buffer id
Returns
buffer_t* pointer to buffer

◆ buffer_get_length()

uint64_t buffer_get_length ( buffer_t buffer)

returns length of buffer

Parameters
[in]bufferbuffer to get length
Returns
uint64_t length of buffer

◆ buffer_get_position()

uint64_t buffer_get_position ( buffer_t buffer)

returns position of buffer

Parameters
[in]bufferbuffer to get position
Returns
uint64_t position of buffer

◆ buffer_get_tmp_buffer_for_printf()

buffer_t * buffer_get_tmp_buffer_for_printf ( void  )

returns a temporary buffer for printf, before gui is initialized, this is a static buffer, after gui is initialized, this is a buffer allocated with malloc

Returns
buffer_t* pointer to buffer

◆ buffer_get_view_at_position()

uint8_t * buffer_get_view_at_position ( buffer_t buffer,
uint64_t  position,
uint64_t  length 
)

returns a byte array from buffer with length, byte array is not copied, position is not changed

Parameters
[in]bufferbuffer to get bytes
[in]positionposition to get bytes
[in]lengthlength of bytes
Returns
uint8_t* pointer to byte array, or null if position + length > buffer length

◆ buffer_new_with_capacity()

buffer_t * buffer_new_with_capacity ( memory_heap_t heap,
uint64_t  capacity 
)

buffer_new_with_capacity creates a new buffer with capacity

Parameters
heapheap to allocate buffer
capacitycapacity of buffer
Returns
buffer_t* pointer to buffer

◆ buffer_peek_byte()

uint8_t buffer_peek_byte ( buffer_t buffer)

returns a byte from buffer at current position, position is not changed

Parameters
[in]bufferbuffer to peek
Returns
uint8_t byte at position

◆ buffer_peek_byte_at_position()

uint8_t buffer_peek_byte_at_position ( buffer_t buffer,
uint64_t  position 
)

returns a byte from buffer at position, position is not changed

Parameters
[in]bufferbuffer to peek
[in]positionposition to peek
Returns
uint8_t byte at position

◆ buffer_peek_ints()

uint64_t buffer_peek_ints ( buffer_t buffer,
uint8_t  bc 
)

returns an int from buffer at current position, position is not changed

Parameters
[in]bufferbuffer to peek
[in]bcbyte count of int
Returns
uint64_t int at position

◆ buffer_peek_ints_at_position()

uint64_t buffer_peek_ints_at_position ( buffer_t buffer,
uint64_t  position,
uint8_t  bc 
)

returns an int from buffer at position, position is not changed

Parameters
[in]bufferbuffer to peek
[in]positionposition to peek
[in]bcbyte count of int
Returns
uint64_t int at position

◆ buffer_printf()

int64_t buffer_printf ( buffer_t buffer,
const char *  format,
  ... 
)

prints to buffer, returns number of bytes written

Parameters
[in]bufferbuffer to print to
[in]formatformat string
[in]...arguments
Returns
int64_t number of bytes written

◆ buffer_read_line_ext()

char_t * buffer_read_line_ext ( buffer_t buffer,
char_t  line_continuation_char,
char_t  delimiter_char,
uint64_t length 
)

reads a line from buffer, line is null terminated, position is advanced

Parameters
[in]bufferbuffer to read line
[in]line_continuation_charline continuation char
[in]delimiter_chardelimiter char
[out]lengthlength of line optional parameter
Returns
char_t* pointer to line

◆ buffer_remaining()

uint64_t buffer_remaining ( buffer_t buffer)

returns how many bytes are remaining in buffer (length - position)

Parameters
[in]bufferbuffer to get remaining bytes
Returns
uint64_t remaining bytes

◆ buffer_reset()

boolean_t buffer_reset ( buffer_t buffer)

resets buffer, sets length and position to 0 and all bytes to 0, capacity stays same

Parameters
[in]bufferbuffer to reset
Returns
boolean_t true if success

◆ buffer_seek()

boolean_t buffer_seek ( buffer_t buffer,
int64_t  position,
buffer_seek_direction_t  direction 
)

changes position of buffer

Parameters
[in]bufferbuffer to change position
[in]positionposition to change
[in]directiondirection to change
Returns
boolean_t true if success

◆ buffer_set_readonly()

boolean_t buffer_set_readonly ( buffer_t buffer,
boolean_t  ro 
)

sets readonly flag of buffer

Parameters
[in]bufferbuffer to set readonly
[in]roreadonly flag
Returns
boolean_t true if success

◆ buffer_vprintf()

int64_t buffer_vprintf ( buffer_t buffer,
const char *  format,
va_list  args 
)

prints to buffer, returns number of bytes written

Parameters
[in]bufferbuffer to print to
[in]formatformat string
[in]argsva_list of arguments
Returns
int64_t number of bytes written

◆ buffer_write_slice_into()

boolean_t buffer_write_slice_into ( buffer_t buffer,
uint64_t  pos,
uint64_t  len,
uint8_t dest 
)

copies bytes from buffer to dest (should be at least length long), position is not changed

Parameters
[in]bufferbuffer to copy
[in]posposition to copy
[in]lenlength to copy
[out]destdestination to copy to, dest should be at least len long
Returns
boolean_t true if success