-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcbuffer.c
44 lines (35 loc) · 797 Bytes
/
cbuffer.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
#include <cbuffer.h>
void init_cbuffer(struct cbuffer *b) {
b -> count = 0;
b -> head = b -> tail = b -> buffer;
}
int cbuffer_push(struct cbuffer *b, char value) {
if (b -> count == CBUFFER_SIZE) {
return 0;
}
b -> count++;
*(b -> tail) = value;
b -> tail++;
if (b -> tail == &(b -> buffer[CBUFFER_SIZE]))
b -> tail = b -> buffer;
return 1;
}
int cbuffer_empty(struct cbuffer *b) {
return b -> count == 0;
}
int cbuffer_full(struct cbuffer *b) {
return b -> count == CBUFFER_SIZE;
}
char cbuffer_front(struct cbuffer *b) {
return *(b -> head);
}
char cbuffer_pop(struct cbuffer *b) {
char value = *(b -> head);
if (b -> head != b -> tail) {
b -> count--;
b -> head++;
if (b -> head == &(b -> buffer[CBUFFER_SIZE]))
b -> head = b -> buffer;
}
return value;
}