aboutsummaryrefslogtreecommitdiff
path: root/strbuf.c
blob: 0d7300d3df3a47fa4e3b8a24cbe176fa32b166c4 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "strbuf.h"

#include <string.h>
#include <stdio.h>

#ifdef SAFE_STR
#include "err.h"
#endif

INIT_F(strbuf) {
	INIT(strbuf, this, 1);
	return 0;
}

/*
 * Giving len < 1 is an error.
 */
INIT_F(strbuf, size_t len) {
	this->mem   = calloc(sizeof(*this->mem), len);
	this->alloc = len;
	this->ptr   = 0;
	this->len   = 0;
	return 0;
}

int strbuf_realloc(strbuf* str, size_t len) {
	str->mem = realloc(str->mem, len);
	str->alloc = len;
	return 0;
}

FREE_F(strbuf) {
#ifdef SAFE_STR
	/* has already been freed */
	if (this->mem == NULL) return 1;
#endif
	free (this->mem);
	this->mem = NULL;
	this->alloc = 0;
	this->len = 0;
	return 0;
}

/*
 * Reallocates memmory for you. Returns 1 if memory was reallocated.
 */
int strbuf_append(strbuf* s, char c) {
	int retval = 0;
#ifdef SAFE_STR
	if (s->len + 1 > s->alloc) {
		s->alloc <<= 1;
		s->mem = realloc(s->mem, s->alloc);
		retval = 1;
	}
#endif
	s->mem[s->len] = c;
	s->ptr = ++s->len;
	return retval;
}

int strbuf_cap(strbuf* s) {
	/*
	 * TODO check memmory usage
	 */
	return strbuf_append(s, 0);
}


int strbuf_copy(strbuf* dest, strbuf* src) {
	int retval = 0;
#ifdef SAFE_STR
	if (dest->alloc < src->len) {
		/*
		 * one extra octet allocated to have space for a finishing
		 * '\0'.
		 * */
		strbuf_realloc(dest, src->len + 1);
		retval = 1;
	}
#endif
	dest->len = src->len;
	memcpy(dest->mem, src->mem, src->len);
	return retval;
}

int strbuf_cmp(strbuf* a, strbuf* b) {
#ifdef SAFE_STR
	if (a->alloc == 0 || b->alloc == 0) {
		ERR("a or b not alloced");
		return -1;
	}
#endif
	return strncmp(a->mem, b->mem, a->len);
}

int strbuf_c(strbuf* a, char* b) {
#ifdef SAFE_STR
	if (a->alloc == 0) {
		ERR("a not allocated");
		return -1;
	}
#endif
	return strcmp(a->mem, b) == 0;
}

char* charat(strbuf* s, unsigned int idx) {
#ifdef SAFE_STR
	if (idx > s->len) {
		ERR("Index out of bounds");
		return (char*) -1;
	}
#endif
	return &s->mem[idx];
}

char* strbuf_cur(strbuf* s) {
	return &s->mem[s->ptr];
}

/* TODO merge this and strbuf_copy */
int DEEP_COPY(strbuf)(strbuf* dest, strbuf* src) {
	return strbuf_copy(dest, src);
}

char* strbuf_end(strbuf* s) {
	return &s->mem[s->len];
}

int strbuf_reset(strbuf* s) {
	s->ptr = 0;
	return 0;
}


int strbuf_soft_reset(strbuf* s) {
	s->ptr = s->len = 0;
	return 0;
}

strbuf* RESOLVE(strbuf)(strbuf* dest, strbuf* new) {
	if (dest == NULL) return new;
	else return dest;
}

FMT_F(strbuf) {
	return sprintf(buf, "%s", this->mem);
}