summaryrefslogtreecommitdiff
path: root/buf_misc.c
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2018-02-27 22:32:49 +0000
committerFreeArtMan <dos21h@gmail.com>2018-02-27 22:32:49 +0000
commit03bfeedc5f4c04c20764c8a9a58bd02604f27b2c (patch)
tree7ab09ac16649d80ca9e6397fc1b19aa8e184e2db /buf_misc.c
parenta8b9b15b4f02ce3c6c7eac0d9393c44cb3fc668d (diff)
downloadlibbuf-03bfeedc5f4c04c20764c8a9a58bd02604f27b2c.tar.gz
libbuf-03bfeedc5f4c04c20764c8a9a58bd02604f27b2c.zip
Created circular buffer
Diffstat (limited to 'buf_misc.c')
-rw-r--r--buf_misc.c191
1 files changed, 191 insertions, 0 deletions
diff --git a/buf_misc.c b/buf_misc.c
new file mode 100644
index 0000000..938e8a3
--- /dev/null
+++ b/buf_misc.c
@@ -0,0 +1,191 @@
+#include "buf_misc.h"
+
+int bbuf_line_new(bbuf_line *buf, char sep, int size)
+{
+ bbuf *newbuf=NULL;
+ bbuf_line *bline=NULL;
+
+ newbuf = bbuf_new(size);
+ if (newbuf == NULL)
+ {
+
+ return -1;
+ }
+
+ bline = malloc(sizeof(bbuf_line));
+ if (bline == NULL)
+ {
+ return -1;
+ }
+
+ memset(bline, 0, sizeof(bbuf_line));
+ bline->sep = 0x0;
+
+ bline->buf = newbuf;
+
+ buf = bline;
+
+ return 0;
+}
+
+
+int bbuf_line_add(bbuf_line *buf_line, bbuf *new_data)
+{
+
+ return -1;
+}
+
+
+int bbuf_line_get_line(bbuf_line *buf_line, bbuf *line)
+{
+
+ return -1;
+}
+
+
+int bbuf_line_free(bbuf_line *buf)
+{
+
+ return -1;
+}
+
+int bbuf_circ_new(bbuf_circ **circ, int size)
+{
+ bbuf *buf = NULL;
+ bbuf_circ *cbuf = NULL;
+
+ buf = bbuf_new(size);
+ if (buf == NULL)
+ {
+ return -1;
+ }
+
+ cbuf = malloc(sizeof(bbuf_circ));
+ if (cbuf == NULL)
+ {
+ return -1;
+ }
+
+ cbuf->buf = buf;
+ cbuf->head = 0;
+ cbuf->tail = 0;
+
+ *circ = cbuf;
+
+ return 0;
+}
+
+int bbuf_circ_add(bbuf_circ *circ, bbuf *new_data)
+{
+ int i=0;
+ int cnt=0;
+
+ if (circ == NULL)
+ {
+ return -1;
+ }
+
+ if (new_data == NULL)
+ {
+ return -1;
+ }
+
+ if (bbuf_size(new_data) <= 0)
+ {
+ return -1;
+ }
+
+ for (i=0;i<bbuf_size(new_data);i++)
+ {
+ if (0 == bbuf_circ_put(circ, new_data->buf[i]))
+ {
+ cnt++;
+ }
+ }
+
+ return cnt;
+}
+
+int bbuf_circ_get_line(bbuf_circ *circ, bbuf *line)
+{
+
+}
+
+int bbuf_circ_reset(bbuf_circ *circ)
+{
+ if (circ == NULL)
+ {
+ return -1;
+ }
+
+ circ->head = 0;
+ circ->tail = 0;
+
+ return 0;
+}
+
+
+int bbuf_circ_get(bbuf_circ *circ, char *data)
+{
+ if (circ == NULL)
+ {
+ return -1;
+ }
+
+ if (data == NULL)
+ {
+ return -1;
+ }
+
+ if (0 == bbuf_circ_empty(circ))
+ {
+ *data = circ->buf->buf[circ->tail];
+ circ->tail = (circ->tail+1)%bbuf_size(circ->buf);
+ return 0;
+ }
+
+ return -1;
+}
+
+int bbuf_circ_put(bbuf_circ *circ, char data)
+{
+ if (NULL == circ)
+ {
+ return -1;
+ }
+
+ circ->buf->buf[circ->head] = data;
+ circ->head = (circ->head+1)%bbuf_size(circ->buf);
+
+ if (circ->head == circ->tail)
+ {
+ circ->tail = (circ->tail+1)%bbuf_size(circ->buf);
+ }
+
+ return 0;
+}
+
+int bbuf_circ_empty(bbuf_circ *circ)
+{
+ if (circ == NULL)
+ {
+ return -1;
+ }
+
+ return (circ->tail == circ->head);
+}
+
+int bbuf_circ_full(bbuf_circ *circ)
+{
+ //one lost byte
+ return ((circ->head+1)%bbuf_size(circ->buf) == (circ->tail));
+}
+
+int bbuf_circ_free(bbuf_circ *circ)
+{
+ bbuf_free(circ->buf);
+ circ->buf = NULL;
+ free(circ);
+ circ = NULL;
+}
+