-
Notifications
You must be signed in to change notification settings - Fork 2
/
str_buffer.c
66 lines (55 loc) · 1.43 KB
/
str_buffer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "str_buffer.h"
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#ifdef S_SPLINT_S
typedef int8_t char;
typedef uint16_t int;
#endif
str_buffer_t* str_buffer_init( int len )
{
str_buffer_t* self = malloc( sizeof( str_buffer_t ) );
if( !self ){ printf("str_buffer malloc failed\n"); return NULL; }
self->length = len;
self->contents = malloc(sizeof(char) * len+1);
if( !self->contents ){ printf("str_buffer contents!\n"); return NULL; }
for( int i=0; i<(len+1); i++ ){ self->contents[i] = '\0'; }
// empty when read == write ix
self->ix_read = 0;
self->ix_write = 0;
self->count = 0;
return self;
}
void str_buffer_deinit( str_buffer_t* buf )
{
free( buf->contents ); buf->contents = NULL;
free( buf ); buf = NULL;
}
void str_buffer_enqueue(str_buffer_t* buf, char* s)
{
while(*s != '\0') { // until we reach a NULL
buf->contents[buf->ix_write++] = *s++;
if( buf->ix_write >= buf->length ){ buf->ix_write = 0; }
buf->count++;
}
}
char* str_buffer_dequeue(str_buffer_t* buf, uint16_t size)
{
char* ret_str = &buf->contents[buf->ix_read];
buf->count -= size;
buf->ix_read += size;
if(buf->ix_read >= buf->length) {buf->ix_read = 0;}
return ret_str;
}
uint16_t str_buffer_len(str_buffer_t* buf)
{
uint16_t ret_len = buf->count;
if( (ret_len + buf->ix_read) > buf->length){
ret_len = buf->length - buf->ix_read;
}
return ret_len;
}
uint8_t str_buffer_empty(str_buffer_t* buf)
{
return (!buf->count);
}