libgslutil
1.2.3
|
Functions | |
struct membuf_t * | membuf_new (void)) |
Allocate a new memory buffer. | |
struct membuf_t * | membuf_realloc (struct membuf_t *old)) |
Allocate new buffer or clear existing buffer. | |
void | membuf_use (const struct membuf_t *m, const char **buf, uint32_t *len)) |
Get details of memory buffer. More... | |
void | membuf_free (struct membuf_t *m) |
Free used memory buffer. | |
void | membuf_consume (struct membuf_t *m, uint32_t amt)) |
Mark some bytes in buffer as consumed. More... | |
void | membuf_truncate (struct membuf_t *m, uint32_t new_len)) |
Truncate the memory buffer. More... | |
void | writem_i8 (struct membuf_t *m, uint8_t i)) |
Write integer to memory buffer (8-bit). More... | |
void | writem_i16 (struct membuf_t *m, uint16_t i)) |
Write integer to memory buffer (16-bit). | |
void | writem_i24 (struct membuf_t *m, uint32_t i)) |
Write integer to memory buffer (24-bit). | |
void | writem_i32 (struct membuf_t *m, uint32_t i)) |
Write integer to memory buffer (32-bit). | |
void | writem_i64 (struct membuf_t *m, uint64_t i)) |
Write integer to memory buffer (64-bit). | |
void | writem_f32 (struct membuf_t *m, float f)) |
Write 32-bit IEEE float to memory buffer. | |
void | writem_f64 (struct membuf_t *m, double f)) |
Write 64-bit IEEE float to memory buffer. | |
void | writem_str (struct membuf_t *m, const char *str)) |
Write null-terminated string to memory buffer. More... | |
void | writem_mem (struct membuf_t *m, const char *mem, uint32_t amt)) |
Write raw data to memory buffer. More... | |
int | readm_i8 (struct membuf_t *m, uint8_t *out)) |
Read 8-bit integer from memory buffer. More... | |
int | readm_i16 (struct membuf_t *m, uint16_t *out)) |
Read 16-bit integer from memory buffer. | |
int | readm_i24 (struct membuf_t *m, uint32_t *out)) |
Read 24-bit integer from memory buffer. | |
int | readm_i24s (struct membuf_t *m, int32_t *out)) |
Read 24-bit integer from memory buffer, and sign-extend it. | |
int | readm_i32 (struct membuf_t *m, uint32_t *out)) |
Read 32-bit integer from memory buffer. | |
int | readm_i64 (struct membuf_t *m, uint64_t *out)) |
Read 64-bit integer from memory buffer. | |
int | readm_f32 (struct membuf_t *m, float *out)) |
Read a 32-bit float from memory buffer. | |
int | readm_f64 (struct membuf_t *m, double *out)) |
Read a 64-bit float from memory buffer. | |
int | readm_str (struct membuf_t *m, char *str, uint32_t str_size, uint32_t *len_out))) |
Read string from memory buffer. More... | |
int | readm_mem (struct membuf_t *m, char *mem, uint32_t mem_size)) |
Read bytes from memory buffer. More... | |
int | readm_stra (struct membuf_t *m, char **str, uint32_t *amt))) |
Read string from memory buffer, allocating space for result. | |
int | readm_mema (struct membuf_t *m, char **mem, uint32_t amt)) |
Read bytes from memory buffer, allocating space for result. | |
This set of functions is used to perform architecture-independent reads/writes from/to memory. An opaque type, struct membuf
, is used. This must be allocated with membuf_new() or membuf_realloc() and freed with membuf_free().
The write functions will append to the buffer and automatically resize it as required (using safe_realloc()). The read functions will fail if there is not enough data in the buffer for the request to succeed; otherwise, the data will be removed from the buffer. This is implemented with efficient caching/resizing to minimise the number of calls to realloc() or memmove() that occur.
void membuf_use | ( | const struct membuf_t * | m, |
const char ** | buf, | ||
uint32_t * | len | ||
) |
Get details of memory buffer.
m | Memory buffer object. | |
[out] | buf | Pointer to data written here. |
[out] | len | Length of data in bytes written here. |
Use this function once you have written some data into the memory buffer to get at the data. The returned values become invalid as soon as any more data is written into the buffer.
void membuf_consume | ( | struct membuf_t * | m, |
uint32_t | amt | ||
) |
Mark some bytes in buffer as consumed.
m | Memory buffer object. |
amt | Number of bytes consumed. |
This function will mark amt bytes in the buffer m as consumed. This does not involve moving any data in the buffer; moves only occur if required in one of the writem_*
functions. Using this function will invalidate the results of any previous call to membuf_use().
void membuf_truncate | ( | struct membuf_t * | m, |
uint32_t | new_len | ||
) |
Truncate the memory buffer.
m | Memory buffer object. |
new_len | The new length of the buffer. |
This function will truncate the buffer so that it only contains new_len bytes. If the buffer does not contain enough data to be affected, m will not be changed.
void writem_i8 | ( | struct membuf_t * | m, |
uint8_t | i | ||
) |
Write integer to memory buffer (8-bit).
m | Memory buffer object. |
i | Integer to write. |
void writem_str | ( | struct membuf_t * | m, |
const char * | str | ||
) |
Write null-terminated string to memory buffer.
m | Memory buffer object. |
str | String to write. |
The string is written as the length (32-bit integer) followed by the data, without the terminating null.
void writem_mem | ( | struct membuf_t * | m, |
const char * | mem, | ||
uint32_t | amt | ||
) |
Write raw data to memory buffer.
m | Memory buffer object. |
mem | Pointer to bytes to write. |
amt | Number of bytes to write. |
int readm_i8 | ( | struct membuf_t * | m, |
uint8_t * | out | ||
) |
Read 8-bit integer from memory buffer.
m | Memory buffer to read from. | |
[out] | out | Integer to write to. |
0 | on success. |
-1 | on error (not enough data in buffer). |
This function will read and consume an 8-bit integer from a memory buffer. If there is not enough data in the buffer, it will fail and return -1.
int readm_str | ( | struct membuf_t * | m, |
char * | str, | ||
uint32_t | str_size, | ||
uint32_t * | len_out | ||
) |
Read string from memory buffer.
m | Memory buffer to read from. | |
str | Pointer to allocated storage. | |
str_size | Number of bytes in str. | |
[out] | len_out | Length of resultant string (may be 0). |
0 | on success. |
-1 | on error (not enough data in buffer). |
This function will read a string from a buffer. It first reads the 32-bit string length as written by writem_str() or writes_str(), and then the string data itself. If the string is larger than the str array, then str will contain a truncated version of the string. str will always be null terminated.
If there is not enough data in the buffer, it will fail and return -1.
int readm_mem | ( | struct membuf_t * | m, |
char * | mem, | ||
uint32_t | mem_size | ||
) |
Read bytes from memory buffer.
m | Memory buffer to read from. |
mem | Pointer to allocated storage. |
mem_size | Number of bytes to read into mem. |
0 | on success. |
-1 | on error. |
Reads mem_size bytes of data from m into mem. If there is not enough data in the buffer, it will fail and return -1.