diff options
author | Holden Rohrer <hr@hrhr.dev> | 2020-03-30 15:02:20 -0400 |
---|---|---|
committer | Holden Rohrer <hr@hrhr.dev> | 2020-03-30 15:07:44 -0400 |
commit | 463bf27d90a66ffe027b252cbce0d0167d1bfc0a (patch) | |
tree | 71d7d470348c09a9215ab6fa1d9f1535f924d51b | |
parent | 0c32b528c20f02b72a0842575d586ee63b490632 (diff) |
removed sb.*
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | badroff.c | 1 | ||||
-rw-r--r-- | sb.c | 67 | ||||
-rw-r--r-- | sb.h | 10 |
4 files changed, 2 insertions, 81 deletions
@@ -1,10 +1,9 @@ .POSIX: .PHONY: all clean -OBJS = badroff.o sb.o buf.o +OBJS = badroff.o buf.o badroff: $(OBJS) $(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) -o $@ -badroff.o: badroff.c sb.h -sb.o: sb.c sb.h +badroff.o: badroff.c buf.h buf.o: buf.c buf.h clean: rm -f badroff $(OBJS) @@ -1,5 +1,4 @@ #include <stdio.h> -#include "sb.h" #include "buf.h" #include <string.h> #include <stdlib.h> @@ -1,67 +0,0 @@ -#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; -} @@ -1,10 +0,0 @@ -#ifndef _SB_INCLUDE -#define _SB_INCLUDE - -typedef struct node node; -node* newsb(size_t cap); -node* appendsb(node* tail, const char* str); -char* tostr(node* head); -int lensb(node* head); - -#endif |