Files
2026-09-29 20:42:04 +01:00

66 lines
1.6 KiB
C

#ifndef NX_BUF_H
#define NX_BUF_H
#include "nx_types.h"
#if defined(__cplusplus)
extern "C" {
#endif
typedef struct nx_buf {
uint8_t *data;
size_t len;
size_t pos;
size_t cap;
} nx_buf;
#define NX_BUF_INIT {NULL, 0, 0, 0}
void nx_buf_init(nx_buf *b);
void nx_buf_free(nx_buf *b);
void nx_buf_reset(nx_buf *b);
int nx_buf_reserve(nx_buf *b, size_t extra);
int nx_buf_append(nx_buf *b, const void *p, size_t n);
int nx_buf_append_str(nx_buf *b, const char *s);
int nx_buf_append_char(nx_buf *b, char c);
int nx_buf_appendf(nx_buf *b, const char *fmt, ...);
static inline size_t nx_buf_avail(const nx_buf *b) { return b->len - b->pos; }
static inline uint8_t *nx_buf_head(nx_buf *b) { return b->data + b->pos; }
void nx_buf_consume(nx_buf *b, size_t n);
void nx_buf_compact(nx_buf *b);
int nx_buf_read_line(nx_buf *b, size_t max_line, size_t *pos,
const uint8_t **line, size_t *line_len);
typedef struct nx_ring {
uint8_t *data;
size_t cap;
size_t head;
size_t tail;
} nx_ring;
#define NX_RING_INIT {NULL, 0, 0, 0}
int nx_ring_init(nx_ring *r, size_t cap);
void nx_ring_free(nx_ring *r);
static inline size_t nx_ring_used(const nx_ring *r) { return r->head - r->tail; }
static inline size_t nx_ring_free_space(const nx_ring *r) { return r->cap - nx_ring_used(r); }
static inline int nx_ring_is_empty(const nx_ring *r) { return r->head == r->tail; }
size_t nx_ring_write(nx_ring *r, const void *p, size_t n);
size_t nx_ring_read(nx_ring *r, void *p, size_t n);
size_t nx_ring_peek(const nx_ring *r, const void **p, size_t n);
#if defined(__cplusplus)
}
#endif
#endif