hat-util  0.6.16
Utility library
buff.h
Go to the documentation of this file.
1 #ifndef HAT_BUFF_H
2 #define HAT_BUFF_H
3 
17 #include <stdint.h>
18 #include <string.h>
19 
20 #define HAT_BUFF_SUCCESS 0
21 #define HAT_BUFF_ERROR (-1)
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
28 typedef struct {
30  uint8_t *data;
32  size_t size;
34  size_t pos;
35 } hat_buff_t;
36 
37 
45 static inline size_t hat_buff_available(hat_buff_t *buff) {
46  return (buff && buff->size > buff->pos) ? buff->size - buff->pos : 0;
47 }
48 
58 static inline int hat_buff_write(hat_buff_t *buff, uint8_t *data,
59  size_t data_len) {
60  if (hat_buff_available(buff) < data_len)
61  return HAT_BUFF_ERROR;
62  memcpy(buff->data + buff->pos, data, data_len);
63  buff->pos += data_len;
64  return HAT_BUFF_SUCCESS;
65 }
66 
76 static inline uint8_t *hat_buff_read(hat_buff_t *buff, size_t size) {
77  if (hat_buff_available(buff) < size)
78  return NULL;
79  uint8_t *data = buff->data + buff->pos;
80  buff->pos += size;
81  return data;
82 }
83 
84 #ifdef __cplusplus
85 }
86 #endif
87 
88 #endif
static uint8_t * hat_buff_read(hat_buff_t *buff, size_t size)
Read data from buffer.
Definition: buff.h:76
static size_t hat_buff_available(hat_buff_t *buff)
Available capacity.
Definition: buff.h:45
#define HAT_BUFF_ERROR
Definition: buff.h:21
#define HAT_BUFF_SUCCESS
Definition: buff.h:20
static int hat_buff_write(hat_buff_t *buff, uint8_t *data, size_t data_len)
Write data to buffer.
Definition: buff.h:58
Data buffer.
Definition: buff.h:28
size_t pos
current position
Definition: buff.h:34
uint8_t * data
data
Definition: buff.h:30
size_t size
buffer size
Definition: buff.h:32