101 lines
2.9 KiB
C
101 lines
2.9 KiB
C
|
/** See the end of this file for copyright and license terms. */
|
||
|
|
||
|
#pragma once
|
||
|
|
||
|
#include "neo/_toolchain.h"
|
||
|
#include "neo/_types.h"
|
||
|
|
||
|
/**
|
||
|
* Create a new buffer of fixed size.
|
||
|
*
|
||
|
* If allocation fails or `size` is 0, an error is yeeted.
|
||
|
*
|
||
|
* @param size: Size in bytes
|
||
|
* @param err: Error pointer
|
||
|
* @returns The buffer, except if an error occurred
|
||
|
*/
|
||
|
nbuf_t *nbuf_create(usize size, error *err);
|
||
|
|
||
|
/**
|
||
|
* Create a new buffer of fixed size and copy `data` into it.
|
||
|
*
|
||
|
* The original pointer is neither modified nor deallocated.
|
||
|
* If allocation fails, `data` is `nil`, or `size` is 0, an error is yeeted.
|
||
|
*
|
||
|
* @param data: Raw data to fill the buffer with
|
||
|
* @param size: How many bytes are read from `data`, and the buffer size
|
||
|
* @param err: Error pointer
|
||
|
* @returns The buffer, except if an error occurred
|
||
|
*/
|
||
|
nbuf_t *nbuf_from(const void *restrict data, usize size, error *err);
|
||
|
|
||
|
/**
|
||
|
* Return a new copy of `buf`.
|
||
|
*
|
||
|
* If `buf` is `nil` or allocation fails, an error is yeeted.
|
||
|
*
|
||
|
* @param buf: Buffer to create a copy of
|
||
|
* @param err: Error pointer
|
||
|
* @returns A copy of `buf`, unless an error occurred
|
||
|
*/
|
||
|
nbuf_t *nbuf_clone(const nbuf_t *buf, error *err);
|
||
|
|
||
|
/**
|
||
|
* Get the byte at the specified index.
|
||
|
*
|
||
|
* If `buf` is `nil` or `index` is out of bounds, an error is yeeted.
|
||
|
*
|
||
|
* @param buf: `nbuf_t *` to get the byte from
|
||
|
* @param index: Byte index (counting from 0)
|
||
|
* @param err: Error pointer
|
||
|
* @returns The byte at position `index` (as a `u8`), unless an error occurred
|
||
|
*/
|
||
|
#define nbuf_byte(buf, index, err) ({ \
|
||
|
u8 __byte = 0; \
|
||
|
if (nlen(buf) <= index) { \
|
||
|
yeet(err, ERANGE, "Buffer index out of bounds"); \
|
||
|
} else { \
|
||
|
neat(err); \
|
||
|
__byte = buf->_data[index]; \
|
||
|
} \
|
||
|
__byte; \
|
||
|
})
|
||
|
|
||
|
#define nbuf_foreach(cursor, buf) \
|
||
|
for (cursor = &buf->_data[0]; \
|
||
|
cursor != &buf->_data[nlen(buf)]; \
|
||
|
cursor++)
|
||
|
|
||
|
/**
|
||
|
* Compare two buffers.
|
||
|
*
|
||
|
* If the first buffer is found to be greater than the second one, the return
|
||
|
* value is greater than 0.
|
||
|
* If the two buffers are equal, the return value is zero.
|
||
|
* If the first buffer is found to be less than the second one, the return
|
||
|
* value is less than 0.
|
||
|
*
|
||
|
* If `buf1` or `buf2` is `nil`, an error is yeeted.
|
||
|
*
|
||
|
* @param buf1: First buffer to compare
|
||
|
* @param buf2: Second buffer to compare
|
||
|
* @param err: Error pointer
|
||
|
* @returns The difference between the buffers, unless an error occurred
|
||
|
*/
|
||
|
int nbuf_cmp(const nbuf_t *buf1, const nbuf_t *buf2, error *err);
|
||
|
|
||
|
#define nbuf_eq(buf1, buf2, err) ( (bool)(nbuf_cmp(buf1, buf2, err) == 0) )
|
||
|
|
||
|
/*
|
||
|
* This file is part of libneo.
|
||
|
* Copyright (c) 2021 Fefie <owo@fef.moe>.
|
||
|
*
|
||
|
* libneo is non-violent software: you may only use, redistribute,
|
||
|
* and/or modify it under the terms of the CNPLv6+ as found in
|
||
|
* the LICENSE file in the source code root directory or at
|
||
|
* <https://git.pixie.town/thufie/CNPL>.
|
||
|
*
|
||
|
* libneo comes with ABSOLUTELY NO WARRANTY, to the extent
|
||
|
* permitted by applicable law. See the CNPLv6+ for details.
|
||
|
*/
|