1
0
mirror of https://github.com/rene-dev/stmbl.git synced 2024-12-24 17:42:10 +00:00
stmbl/shared/ringbuf.h

64 lines
1.4 KiB
C
Raw Permalink Normal View History

#pragma once
2017-01-10 00:56:32 +00:00
#include <string.h>
/**
* Ringbuffer structure
*
*/
struct ringbuf {
char *buf; ///< Pointer to buffer memory
unsigned bufsize; ///< Size of buffer memory
volatile unsigned pos; ///< Current read position
volatile unsigned len; ///< Length of data in buffer
};
2017-01-10 08:48:07 +00:00
#define RINGBUF(size) { .buf = (char[(size)]) {0}, .bufsize = (size) }
/**
* Read a single byte from a buffer
*
* \param rb pointer to ringbuffer struct
* \param data pointer to data byte
* \return number of bytes read (0 if buffer was empty)
*/
2017-01-10 00:56:32 +00:00
int rb_getc(struct ringbuf *rb, char *data);
/**
* Write a single byte to a buffer
*
* \param rb pointer to ringbuffer struct
* \param data pointer to data byte
* \return number of bytes written (0 if buffer was full)
*/
2017-01-10 00:56:32 +00:00
int rb_putc(struct ringbuf *rb, const char data);
/**
* Read from a buffer
*
*/
2017-01-10 00:56:32 +00:00
int rb_read(struct ringbuf *rb, void *data, int len);
/**
* Write to a buffer
*
* \param rb pointer to ringbuffer struct
* \param data pointer to data byte
* \return number of bytes written (0 if buffer was full)
*
*/
2017-01-10 00:56:32 +00:00
int rb_write(struct ringbuf *rb, const void *data, int len);
2017-01-10 00:56:32 +00:00
/**
* undo rb_read
*
* \param rb pointer to ringbuffer struct
* \param len length to reset read pointer
* \return 0 on error
*
*/
int rb_undo(struct ringbuf *rb, int len);
2017-01-10 00:56:32 +00:00
int rb_getline(struct ringbuf *rb, char *ptr, int len);