aboutsummaryrefslogtreecommitdiff
path: root/src/fbuf.c
blob: 69a3475fbd614917df32d8b52478df324fa88d3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "fbuf.h"

void bputc(struct TMPL_buf* b,char s){
	if(b->head == NULL){
		struct LLNode* new = (struct LLNode*)malloc(sizeof(struct LLNode));
		new->length = 1;
		new->data = (char*)malloc(sizeof(char));
		new->data[0] = s;
		new->next = NULL;
		b->total_len = 1;
		b->head = new;
		b->tail = new;
	}else{
		struct LLNode* last = b->tail;
		struct LLNode* new = (struct LLNode*)malloc(sizeof(struct LLNode));
		new->length = 1;
		new->data = (char*)malloc(sizeof(char));
		new->data[0] = s;
		new->next = NULL;
		b->total_len += new->length;
		last->next = new;
		b->tail = new;
	}
}

void bputsn(struct TMPL_buf* b, const char* s, size_t size){
	if(b->head == NULL){
		struct LLNode* new = (struct LLNode*)malloc(sizeof(struct LLNode));
		new->length = size;
		new->data = (char*)malloc(sizeof(char)*size);
		memcpy(new->data,s,size);
		b->total_len = size;
		b->head = new;
		b->tail = new;
		new->next = NULL;
	}else{
		struct LLNode* last = b->tail;
		struct LLNode* new = (struct LLNode*)malloc(sizeof(struct LLNode));
		new->length = size;
		new->data = (char*)malloc(sizeof(char)*size);
		memcpy(new->data,s,size);
		last->next = new;
		new->next = NULL;
		b->tail = new;
		b->total_len += size;
	}
}

void bputs(struct TMPL_buf* b, const char* s){
	size_t len = strlen(s);
	bputsn(b,s,len);
}

struct TMPL_buf* alloc_tmpl_buf(){
	struct TMPL_buf* ret = (struct TMPL_buf*)malloc(sizeof(struct TMPL_buf));
	ret->total_len = 0;
	ret->head = NULL;
	ret->tail = NULL;
	return ret;
}

void free_llnodes(struct LLNode* l){
	struct LLNode* cursor = l;
	struct LLNode* next;
	while(cursor != NULL){
		next = cursor->next;
		free(cursor->data);
		free(cursor);
		cursor = next;
	}
}

void free_tmpl_buf(struct TMPL_buf* b){
	free_llnodes(b->head);
	free(b);
}

void bprint(struct TMPL_buf* b){
	printf("-------------\n");
	struct LLNode* cursor = b->head;
	while(cursor != NULL){
		printf("Cursor is %p\n",(void*)cursor);
		printf("Length:%d\nData:",(int)cursor->length);
		char* cb = cursor->data;
		size_t cn;
		for(cn = 0; cn < cursor->length; cn++){
			printf("%c",*cb);
			cb++;
		}
		cursor = cursor->next;
		printf("\n-------------\n");
	}
	printf("Done printing\n");
	printf("-----------------------\n");

}

char* bstringify(struct TMPL_buf* b, size_t* size){
	struct LLNode* cursora = b->head;
	size_t cursorb = 0;

	struct LLNode* single = (struct LLNode*)malloc(sizeof(struct LLNode));
	single->length = b->total_len;
	single->data = (char*)malloc(sizeof(char)*(b->total_len + 1));
	single->data[b->total_len] = '\0';
	single->next = NULL;
	
	while(cursora != NULL){
		memcpy(single->data + cursorb,cursora->data,cursora->length);
		cursorb += cursora->length;
		cursora = cursora->next;
	}

	free_llnodes(b->head);

	b->head = single;
	b->tail = single;
	*size = b->total_len;

	return single->data;
}