aboutsummaryrefslogtreecommitdiffstats
path: root/xstring.c
blob: 7dcb5f1d53c3486920dcf845f4b13cf794e97dc4 (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
#include <stdlib.h>
#include <string.h>

#include "xstring.h"

string *string_alloc(string *str, const char *text)
{
	return string_nalloc(str, text, 
		(text) ? strlen(text) : INIT_LEN_STRING);
}

string *string_nalloc(string *str, const char *text, size_t text_len)
{
	if(!str) {
		str = malloc(sizeof(string));
		str->s = malloc(sizeof(char) * (text_len + 1));
		str->capacity = text_len;
	}
	else {
		if(text_len >= str->capacity) {
			free(str->s);
			str->s = malloc(sizeof(char) * (text_len + 1));
			str->capacity = text_len;
		}

	}

	if(text) {
		strncpy(str->s, text, text_len);
		str->len = text_len;
	}
	else
		str->len = 0;

	str->s[str->len] = '\0';
	return str;
}

void string_release(string *str)
{
	free(str->s);
	free(str);
}

void string_reset(string *str)
{
	str->s[0] = '\0';
	str->len = 0;
}

static void string_realloc(string *str, int cap)
{
	int i;
	char *tmp_s = str->s;
	str->s = malloc(sizeof(char) * (cap + 1));
	for(i = 0; i < cap; i++)
		str->s[i] = (i >= str->len) ? '\0' : tmp_s[i];

	str->capacity = cap;
	free(tmp_s);
}

void string_addch(string *str, int ch)
{
	if((str->len + 1) >= str->capacity)
		string_realloc(str, str->capacity + INIT_LEN_STRING);

	str->s[str->len] = ch;
	str->len++;
	str->s[str->len] = '\0';
}

void string_addstr(string *str, const char *src, size_t src_len)
{
	if((str->len + src_len) >= str->capacity)
		string_realloc(str, str->capacity + src_len);

	while(src_len > 0 && (str->s[str->len] = *src++)) {
		src_len--;
		str->len++;
	}
	str->s[str->len] = '\0';
}