aboutsummaryrefslogtreecommitdiff
path: root/strbuf.h
blob: d21fa1175f07e7591ff209806620bc7d2f5f3769 (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
#ifndef STRBUF_H
#define STRBUF_H

#include <stdlib.h>

typedef struct {
	char* mem;
	size_t ptr;
	size_t alloc;
	size_t len;
} string;

/*
 * TODO rename everything to be on the form
 *	strbuf_.*
 *
 * TODO Check memmory allocation for last +1 byte for null.
 */

/*
 * Constructor
 */
int init_string(string* str, size_t len);

/*
 * Like realloc, but for strbuf
 */
int realloc_string(string* str, size_t len);

/*
 * Free's contents of str, but keeps str.
 */
int free_string(string* str);

/*
 * Copy contents from src to dest.
 * Assumes that dest is already initialized.
 *
 * also see: strbuf_alloc_copy
 */
int copy_strbuf(string* dest, string* src);
int strbuf_cmp(string* a, string* b);
int strbuf_c(string* a, char* b);

/*
 * Append char to end of string, determined by s->len.
 */
int strbuf_append(string* s, char c);

/*
 * Calls strbuf_append with NULL.
 */
int strbuf_cap(string* s);
int strbuf_reset(string* s);
char* charat(string* s, int idx);
char* strbuf_cur(string* s);

/*
 * Sets the length and seek ptr to 0, but doesn't touch the memmory. 
 */
int strbuf_soft_reset(string* s);

/*
 * Returns the character after the last, so where null hopefully is.
 */
char* strbuf_end(string* s);

/*
 * Copies contents from src to dest, also allocating dest in the
 * process. dest should not be initialized before this call. 
 */
int strbuf_init_copy(string* dest, string* src);

#endif /* STRBUF_H */