aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHolden Rohrer <hr@hrhr.dev>2020-03-29 17:11:47 -0400
committerHolden Rohrer <hr@hrhr.dev>2020-03-29 17:11:47 -0400
commitf60b2add323ddb21675383b5461aa03e95755d2b (patch)
treeefe0252ffb87682c5dc1f4ba499449dcee1d29fc
parent52efabab2ad12d22cd5aa5a3d2d08ee88ce2b20e (diff)
string buffer start
-rw-r--r--buf.c82
-rw-r--r--buf.h21
2 files changed, 103 insertions, 0 deletions
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 <stdlib.h>
+#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