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

#include <iostream>

#include <stdlib.h>
#include "macro.h"

/*
 * A high level string type which holds it's own length, how much
 * memmory it has allocated for itself, and a seek pointer into the
 * string.
 *
 * Also comes with a number of functions which allow for safe(er)
 * access to the memmory.
 */
struct strbuf {
	char* mem;
	/* TODO add support for negative ptr */
	int ptr;
	unsigned int alloc = 0;
	unsigned int len;

	// strbuf (size_t len);
	strbuf (strbuf& other);
	strbuf ();

	~strbuf ();

	strbuf& operator=(strbuf& other);

	bool operator==(strbuf& other);
	bool operator==(const char* other);
	bool operator!=(strbuf& other)
		{ return ! (*this == other); }
	bool operator!=(const char* other)
		{ return ! (*this == other); }

};

std::ostream& operator<<(std::ostream&, strbuf&);

/*
 * Init strbuf to size of 0
 * Doesnt't call malloc.
 */
// INIT_F(strbuf);

/* Constructor */
// INIT_F(strbuf, size_t len);

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

/*
 * Free's contents of str, but keeps str.
 */
// FREE_F(strbuf);

int strbuf_cmp(strbuf* a, strbuf* b);
int strbuf_c(strbuf* a, const char* b);

/*
 * Copy contents from src to dest.
 * Assumes that dest is already initialized.
 */
int DEEP_COPY(strbuf)(strbuf*, strbuf*);

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

/*
 * Calls strbuf_append with NULL.
 */
int strbuf_cap(strbuf* s);

/*
 * Returns a pointer to character at index. Allows mutation of the
 * value pointed to by the return address.
 */
char* charat(strbuf* s, unsigned int idx);

/*
 * Same as `charat`, But returns the current character.
 */
char* strbuf_cur(strbuf* s);

/*
 * Resets the seek for strbuf to 0.
 */
int strbuf_reset(strbuf* s);

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

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

/*
 * Reallocs dest to be the same size as src, and copies the contents
 * of src into dest.
 */
int strbuf_realloc_copy(strbuf* dest, strbuf* src);

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

strbuf* RESOLVE(strbuf)(strbuf*, strbuf*);

// FMT_F(strbuf);

int SIZE(strbuf)(strbuf*);

#endif /* STRBUF_H */