From f60b2add323ddb21675383b5461aa03e95755d2b Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Sun, 29 Mar 2020 17:11:47 -0400 Subject: string buffer start --- buf.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ buf.h | 21 +++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 buf.c create mode 100644 buf.h diff --git a/buf.c b/buf.c new file mode 100644 index 0000000..eb1655e --- /dev/null +++ b/buf.c @@ -0,0 +1,82 @@ +#include +#include "buf.h" + +int max(int a, int b){ + if (a > b) return a; + return b; +} +int min(int a, int b){ + if (a < b) return a; + return b; +} + +struct buf_node{ + char* text; + size_t space; + struct buf_node* next; + struct buf_node* prev; +}; + +struct buf{ + buf_node* head; + buf_node* tail; + size_t cap; +}; + +buf* newbuf(size_t cap){ + buf* buffer = malloc(sizeof(buf)); + buffer->cap = cap; + buffer->tail = buffer->head = newbufnode(cap); + return buffer; +} + +buf_node* newbufnode(size_t cap){ + buf_node* bufnode = calloc(sizeof(buf_node)); + bufnode->text = malloc(sizeof(char)*cap); + return bufnode; +} + +void bextendbuf(buf* buffer){ + buffer->tail = buffer->tail->prev->next + = buffer->tail->prev = newbufnode(); +} + +void fextendbuf(buf* buffer){ + buffer->head = buffer->head->next->prev + = buffer->head->next = newbufnode(); +} + +void addchrbuf(buf* buffer, char chr){ // front + buf_node* head = buffer->head + if (head->space < 0 || head->space == buffer->cap) + fextendbuf(buffer); + *(head->text + head->space) = chr; + head->space++; +} +void addstrbuf(buf* buffer, char* str){ + +} +char remchrbuf(buf* buffer){ + +} +char* remchrbuf(buf* buffer, int ct){ + +} + +void prechrbuf(buf* buffer, char chr){ // back + +} +void prestrbuf(buf* buffer, char* str){ + +} +char shfchrbuf(buf* buffer){ + +} +char* shfstrbuf(buf* buffer, int ct){ + +} + +char* strpeekbuf(buf* buffer, int loc, int len){ + +} + diff --git a/buf.h b/buf.h new file mode 100644 index 0000000..7323b83 --- /dev/null +++ b/buf.h @@ -0,0 +1,21 @@ +#ifndef _BUF_INCLUDE +#define _BUF_INCLUDE + +typedef struct buf_node buf_node; +typedef struct buf buf; + +buf* newbuf(size_t cap); + +void addchrbuf(buf* buffer, char chr); // front +void addstrbuf(buf* buffer, char* str); +char remchrbuf(buf* buffer); +char* remstrbuf(buf* buffer, int ct); + +void prechrbuf(buf* buffer, char chr); // back +void prestrbuf(buf* buffer, char* str); +char shfchrbuf(buf* buffer); +char* shfstrbuf(buf* buffer, int ct); + +char* strpeekbuf(buf* buffer, int loc); + +#endif -- cgit