diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | Makefile | 14 | ||||
-rw-r--r-- | README | 1 | ||||
-rw-r--r-- | buf.c | 55 | ||||
-rw-r--r-- | buf.h | 11 | ||||
-rw-r--r-- | buf_misc.c | 191 | ||||
-rw-r--r-- | buf_misc.h | 38 | ||||
-rw-r--r-- | test_circ.c | 95 |
8 files changed, 394 insertions, 14 deletions
@@ -2,3 +2,6 @@ log.txt *.so *.o *.a +test +test_circ +log.txt* @@ -1,15 +1,17 @@ CC=gcc -CFLAGS=-g3 +CFLAGS=-fPIC -g3 make: - $(CC) -c buf.c - $(CC) $(CFLAGS) -fPIC -lc -ldl buf.c -shared -o libbuf.so + $(CC) $(CFLAGS) -c buf.c + $(CC) $(CFLAGS) -c buf_misc.c + $(CC) $(CFLAGS) -fPIC -lc -ldl buf_misc.o buf.o -shared -o libbuf.so -test: buf.c - $(CC) $(CFLAGS) buf.o test.c -o test +t: buf.c buf_misc.c + $(CC) $(CFLAGS) buf.o buf_misc.o test.c -o test + $(CC) $(CFLAGS) buf.o buf_misc.o test_circ.c -o test_circ leak: - valgrind --leak-check=full --track-origins=yes --log-file=log.txt ./test + valgrind --leak-check=full --track-origins=yes --log-file=log.txt ./test_circ clean: rm -rf *.so *.o @@ -0,0 +1 @@ +bbuf - allocate buffer in heap @@ -114,6 +114,12 @@ int bbuf_realloc(bbuf *buf, int size) return ret; } +int bbuf_reduce(bbuf *buf) +{ + printf("Not implemented\n"); + return -1; +} + //increase buffer for size int bbuf_inc(bbuf *a, char *b, int size) @@ -131,6 +137,48 @@ int bbuf_dec(bbuf *a, char *b, int size) } + +int bbuf_concat(bbuf *a, bbuf *b) +{ + printf("Not implemented\n"); + return -1; +} + +int bbuf_size(bbuf *a) +{ + if (a == NULL) + { + return -1; + } + + //return used size + return a->buf_size; +} + +int bbuf_print(bbuf *a) +{ + int i; + + if (NULL == a) + { + return -1; + } + + for (i=0;i<a->buf_size;i++) + { + char ch = a->buf[i]; + if (isalpha(ch)) + { + printf("%c",ch); + } else + { + printf("."); + } + } + + return 0; +} + //free buffer void bbuf_free(bbuf *buf) { @@ -145,12 +193,7 @@ void bbuf_free(bbuf *buf) buf->buf = NULL; memset(buf, 0, sizeof(bbuf)); free(buf); + buf = NULL; } } -int bbuf_concat(bbuf *a, bbuf *b) -{ - printf("Not implemented\n"); - return -1; -} - @@ -4,10 +4,11 @@ #include <stdlib.h> #include <stdio.h> #include <string.h> +#include <ctype.h> typedef struct bbuf { - int size; - int buf_size; + int size; //total size + int buf_size; //used size char *buf; } bbuf; @@ -25,6 +26,8 @@ bbuf *bbuf_copy(bbuf *buf); int bbuf_get(bbuf *buf, char **val, int *size); //resize buffer int bbuf_realloc(bbuf *buf, int size); +//set to minimal size +int bbuf_reduce(bbuf *buf); //increase buffer for size int bbuf_inc(bbuf *a, char *b, int size); //decrease buffer for size @@ -34,6 +37,10 @@ void bbuf_free(bbuf *buf); int bbuf_concat(bbuf *a, bbuf *b); +int bbuf_size(bbuf *a); + +int bbuf_print(bbuf *a); + void bbuf_free(bbuf *buf); #endif
\ No newline at end of file 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; +} + diff --git a/buf_misc.h b/buf_misc.h new file mode 100644 index 0000000..c0d8aab --- /dev/null +++ b/buf_misc.h @@ -0,0 +1,38 @@ +#ifndef __LIBBUF_MISC_H +#define __LIBBUF_MISC_H + +#include "buf.h" + +//line buffer, put data get line out +typedef struct bbuf_line +{ + bbuf *buf; + char sep; //seperator character +} bbuf_line; + +int bbuf_line_new(bbuf_line *buf, char sep, int size); +int bbuf_line_add(bbuf_line *buf_line, bbuf *new_data); +int bbuf_line_get_line(bbuf_line *buf_line, bbuf *line); +int bbuf_line_free(bbuf_line *buf); + +//formating buf ops + +//circular buffer +typedef struct bbuf_circ +{ + bbuf *buf; + int head; + int tail; +} bbuf_circ; + +int bbuf_circ_new(bbuf_circ **circ, int size); +int bbuf_circ_add(bbuf_circ *circ, bbuf *new_data); +int bbuf_circ_get_line(bbuf_circ *circ, bbuf *line); +int bbuf_circ_reset(bbuf_circ *circ); +int bbuf_circ_get(bbuf_circ *circ, char *data); +int bbuf_circ_put(bbuf_circ *circ, char data); +int bbuf_circ_empty(bbuf_circ *circ); +int bbuf_circ_full(bbuf_circ *circ); +int bbuf_circ_free(bbuf_circ *circ); + +#endif diff --git a/test_circ.c b/test_circ.c new file mode 100644 index 0000000..60746f0 --- /dev/null +++ b/test_circ.c @@ -0,0 +1,95 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "buf.h" +#include "buf_misc.h" +#include "debug.h" + +void blow_if_error(int err) +{ + if (err < 0) + { + printf("Should be no error but there is\n"); + exit(1); + } +} + +//#define B(X) PNL();blow_if_error(X); +#define B(X) blow_if_error(X); + +void error_is_good(int err) +{ + if (err >= 0) + { + printf("Should be error but there is no\n"); + exit(1); + } +} +#define E(X) error_is_good(X); + +int main() +{ + + bbuf_circ *bc=NULL; + bbuf *b1=NULL; + char ch; + int i; + + B(bbuf_circ_new(&bc, 8)); + bbuf_print(bc->buf); printf("\r\n"); + + B(bbuf_circ_put(bc, 'A')); + bbuf_print(bc->buf); printf("\r\n"); + + B(bbuf_circ_put(bc, 'B')); + bbuf_print(bc->buf); printf("\r\n"); + + B(bbuf_circ_put(bc, 'C')); + B(bbuf_circ_put(bc, 'D')); + B(bbuf_circ_put(bc, 'E')); + B(bbuf_circ_put(bc, 'F')); + B(bbuf_circ_put(bc, 'G')); + B(bbuf_circ_put(bc, 'H')); + bbuf_print(bc->buf); printf("\r\n"); + + B(bbuf_circ_put(bc, 'I')); + bbuf_print(bc->buf); printf("\r\n"); + + B(bbuf_circ_reset(bc)); + E(bbuf_circ_get(bc, &ch)); + + B(bbuf_circ_put(bc, 'M')); + B(bbuf_circ_put(bc, 'O')); + B(bbuf_circ_put(bc, 'S')); + B(bbuf_circ_put(bc, 'S')); + B(bbuf_circ_put(bc, 'A')); + B(bbuf_circ_put(bc, 'D')); + for (i=0;i<10;i++) + { + if (0 == bbuf_circ_get(bc, &ch)) + { + printf("%c",ch); + } + } + printf("\r\n"); + bbuf_circ_reset(bc); + + char name1[] = "FREDOME"; + b1 = bbuf_new(strlen(name1)); + bbuf_set(b1, name1, strlen(name1)); + bbuf_print(b1); printf("\r\n"); + printf("Added =%d\r\n",bbuf_circ_add(bc, b1)); + for (i=0;i<10;i++) + { + if (0 == bbuf_circ_get(bc, &ch)) + { + printf("!%c",ch); + } + } + printf("\r\n"); + + bbuf_circ_free(bc); + bbuf_free(b1); + + return 0; +}
\ No newline at end of file |