summaryrefslogtreecommitdiff
path: root/src/til_str.c
blob: 442e9236673cc67995b5ed29a195a707245ae2f7 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <assert.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#include "til_str.h"
#include "til_util.h"

/* This implements very rudimentary growable strings, and hasn't been optimized at all.
 *
 * The impetus for adding this is to get rid of the open_memstream() usage which is mostly
 * just being used as a convenient way to build up a buffer from format strings.  So this
 * basically implements just that ability using variadic functions...
 *
 * Why no open_memstream()?  Because Windows, that's why.  For some reason even mingw doesn't
 * have open_memstream().  FILE* I keep forgetting that you're dead to me.
 */

struct til_str_t {
	struct {
		size_t	allocated, used; /* used is length, including '\0' terminator */
	} size;
	char	*buf;
};


#define TIL_STR_MIN_SIZE	64


/* alloc always returns a buf w/nul terminator present */
static til_str_t * til_str_nulstr(size_t minsize)
{
	til_str_t	*str;

	str = calloc(1, sizeof(*str));
	if (!str)
		return NULL;

	str->size.used = 1;
	str->size.allocated = MAX(minsize, TIL_STR_MIN_SIZE);

	str->buf = calloc(1, str->size.allocated);
	if (!str->buf) {
		free(str);
		return NULL;
	}

	return str;
}


/* allocate a new til_str, starting with a dup of seed, just use "" for an empty str, there is no NULL str */
til_str_t * til_str_new(const char *seed)
{
	til_str_t	*str;
	size_t		len;

	assert(seed);

	len = strlen(seed);
	str = til_str_nulstr(len + 1);
	if (!str)
		return NULL;

	memcpy(str->buf, seed, len);
	str->size.used += len;

	return str;
}


void * til_str_free(til_str_t *str)
{
	if (str) {
		free(str->buf);
		free(str);
	}

	return NULL;
}


/* allocate a new til_str from a fmt string + args */
til_str_t * til_str_newf(const char *format, ...)
{
	til_str_t	*str;
	va_list		ap;

	assert(format);

	va_start(ap, format);

	str = til_str_nulstr(vsnprintf(NULL, 0, format, ap) + 1);
	if (!str)
		return NULL;

	str->size.used += vsnprintf(str->buf, str->size.allocated, format, ap);

	va_end(ap);

	assert(str->size.used <= str->size.allocated);

	return str;
}


/* append to an existing til_str_t from a fmt string + args */
int til_str_appendf(til_str_t *str, const char *format, ...)
{
	size_t	len;
	va_list	ap;

	assert(str);
	assert(format);

	va_start(ap, format);
	len = vsnprintf(NULL, 0, format, ap);
	va_end(ap);

	if (str->size.used + len > str->size.allocated) {
		char	*new;

		new = realloc(str->buf, str->size.used + len);
		if (!new)
			return -ENOMEM;

		str->buf = new;
		str->size.allocated = str->size.used + len;
	}

	va_start(ap, format);
	str->size.used += vsnprintf(&str->buf[str->size.used - 1],
				    str->size.allocated - (str->size.used - 1),
				    format, ap);
	va_end(ap);

	assert(str->size.used <= str->size.allocated);

	return 0;
}


/* strdup() the /used/ contents of str */
char * til_str_strdup(const til_str_t *str)
{
	assert(str);

	return strdup(str->buf);
}


/* a valid \0-terminated string is _always_ maintained @ str->buf so callers can just use it as a string..
 * but must not hang onto that pointer across more til_str() calls on the same str.
 */
const char * til_str_buf(const til_str_t *str, size_t *res_len)
{
	assert(str);

	if (res_len)
		*res_len = str->size.used;

	return str->buf;
}


/* for when you just want the buf without the dup overhead, and don't need the str anymore */
char * til_str_to_buf(til_str_t *str, size_t *res_len)
{
	char	*buf;

	assert(str);

	if (res_len)
		*res_len = str->size.used;

	buf = str->buf;
	free(str);

	return buf;
}
© All Rights Reserved