From f1638f63a7e0ffe854ec6bb940c4ed1e4a175215 Mon Sep 17 00:00:00 2001
From: Holden Rohrer <hr@hrhr.dev>
Date: Fri, 27 Mar 2020 00:42:31 -0400
Subject: initial commit

---
 Makefile  |  9 +++++++++
 badroff.c | 18 +++++++++++++++++
 sb.c      | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 sb.h      | 11 +++++++++++
 4 files changed, 105 insertions(+)
 create mode 100644 Makefile
 create mode 100644 badroff.c
 create mode 100644 sb.c
 create mode 100644 sb.h

diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..32e5f37
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,9 @@
+.POSIX:
+.PHONY: all clean
+OBJS = badroff.o sb.o
+badroff: $(OBJS)
+	$(CC) $(OBJS)
+badroff.o: badroff.c sb.h
+sb.o: sb.c sb.h
+clean:
+	rm -f badroff $(OBJS)
diff --git a/badroff.c b/badroff.c
new file mode 100644
index 0000000..5149c23
--- /dev/null
+++ b/badroff.c
@@ -0,0 +1,18 @@
+#include <stdio.h>
+#include "sb.h"
+
+int main(int argc, char** argv){
+    FILE* in;
+    int c;
+    if (argc >= 2)
+        in = fopen(argv[1], "r");
+    else
+        in = stdin;
+    if (in == NULL){
+        perror(argv[1]);
+        return 1;
+    }
+    while ( (c = fgetc(in)) != EOF){
+        
+    }
+}
diff --git a/sb.c b/sb.c
new file mode 100644
index 0000000..0353545
--- /dev/null
+++ b/sb.c
@@ -0,0 +1,67 @@
+#include <stdlib.h>
+#include <string.h>
+#include "sb.h"
+
+struct node{ // linked list, but strings don't have to be max size
+    size_t cap;
+    size_t len;
+    char* text;
+    node* next;
+};
+
+// malloc might fail, but the entire program should just crash if it does
+node* newsb(size_t cap){
+    node* sb;
+    sb = malloc(sizeof(node));
+    sb->text = malloc(sizeof(char)*cap);
+    sb->len = 0; sb->cap = cap;
+    sb->next = NULL;
+    return sb;
+}
+
+node* extendsb(node* tail){
+    tail->next = newsb(tail->cap*2);
+    return tail->next;
+}
+
+node* appendsb(node* tail, const char* str){
+    for (int i=0; str[i] != '\0'; i++){
+        if (tail->len == tail->cap) tail = extendsb(tail); // if full, extend
+        *(tail->text + tail->len) = str[i]; // copy str[i] to last open spot
+        tail->len++;
+    }
+    return tail;
+}
+///////// i want to prevent this duplication but cant figure out how :(
+
+node* addchrsb(node* tail, char chr){
+    if (tail->len == tail->cap) tail = extendsb(tail); // if full, extend
+    *(tail->text + tail->len) = chr; // copy str[i] to last open spot
+    tail->len++;
+    return tail;
+}
+
+char* tostr(node* head){ // destructive; frees the whole chain
+    int len;
+    char* str;
+    len = lensb(head);
+    str = malloc((len+1)*sizeof(char)); // need +1 for null terminator
+    str[len] = '\0';
+
+    len = 0; //controls progression through str
+    do {
+        strncpy(str+len, head->text, head->len);
+        len += head->len;
+        head = head->next;
+        free(head);
+    } while ( head != NULL);
+    return str;
+}
+
+int lensb(node* head){ // how much space do i need to concat all strings
+    int len = 0;
+    do {
+        len += head->len;
+    } while ( (head = head->next) != NULL);
+    return len;
+}
diff --git a/sb.h b/sb.h
new file mode 100644
index 0000000..e3785fd
--- /dev/null
+++ b/sb.h
@@ -0,0 +1,11 @@
+#ifndef _SB_INCLUDE
+#define _SB_INCLUDE
+
+typedef struct node node;
+node* newsb(size_t cap);
+node* extendsb(node* tail);
+node* appendsb(node* tail, const char* str);
+char* tostr(node* head);
+int lensb(node* head);
+
+#endif
-- 
cgit