-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrbuf.c
201 lines (185 loc) · 4.11 KB
/
strbuf.c
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* SPDX-License-Identifier: GPL-2.0
*/
/*
* most of this code is taken from the hashlink/buffer.c by Haxe Foundation
*/
#include <float.h>
#include <stdlib.h>
#include <string.h>
#include "strbuf.h"
struct chunk {
int pos;
int len; // length(chunk->mem)
union {
struct chunk *next;
double __x;
};
char data[0];
};
#define chk_data(chk) ((chk)->data)
#define chk_head(buf) ((buf)->chunks)
#define chk_next(chk) ((chk)->next)
void strbuf_init(struct strbuf *buf)
{
buf->csize = 128;
buf->length = 0;
buf->chunks = NULL;
}
void strbuf_reset(struct strbuf *buf)
{
struct chunk *next;
struct chunk *chk = chk_head(buf);
if (!chk)
return;
next = chk_next(chk);
// Keep the first chunk
chk_next(chk) = NULL;
chk->pos = 0;
buf->length = 0;
// Release the remaining chunks
chk = next;
while (chk) {
next = chk_next(next);
rb_free(chk);
chk = next;
}
}
void strbuf_release(struct strbuf *buf)
{
struct chunk *next;
struct chunk *chk = chk_head(buf);
while (chk) {
next = chk_next(chk);
rb_free(chk);
chk = next;
}
strbuf_init(buf);
}
static void strbuf_append_new(struct strbuf *buf, char *src, int len)
{
while (buf->length >= (buf->csize << 2))
buf->csize <<= 1;
int size = len < buf->csize ? buf->csize : len;
struct chunk *chk = rb_malloc(sizeof(struct chunk) + size);
if (!chk) {
// TODO
}
memcpy(chk_data(chk), src, len);
chk->len = size;
chk->pos = len;
chk_next(chk) = chk_head(buf);
chk_head(buf) = chk;
}
void strbuf_append_char(struct strbuf *buf, char c)
{
struct chunk *chk = chk_head(buf);
buf->length++;
if (chk && chk->pos < chk->len) {
chk->data[chk->pos++] = c;
return;
}
strbuf_append_new(buf, &c, 1);
}
void strbuf_append_string(struct strbuf *buf, char *string, int len)
{
if (!string)
return;
if (len < 0)
len = strlen(string);
buf->length += len;
struct chunk *chk = chk_head(buf);
if (chk) {
int free = chk->len - chk->pos;
if (free >= len) {
memcpy(chk_data(chk) + chk->pos, string, len);
chk->pos += len;
return;
} else {
memcpy(chk_data(chk) + chk->pos, string, free);
chk->pos += free;
string += free;
len -= free;
}
}
strbuf_append_new(buf, string, len);
}
void strbuf_append_int(struct strbuf *buf, int i)
{
char array[16];
int len = snprintf(array, 16, "%d", i);
strbuf_append_string(buf, array, len);
}
static int trim_tail_zero(char *ptr, int len)
{
int i = 0;
while (i < len && ptr[i++] != '.') {
}
i += 2; // Keep at least 2 zeros
int count = 0;
while (i < len) {
if (ptr[i++] != '0') {
count = 0;
continue;
}
count++;
if (i == len || count == 3)
return i - count;
}
return len;
}
void strbuf_append_float(struct strbuf *buf, float f, int fixed)
{
char array[16];
int len;
if (fixed < 0) {
len = snprintf(array, 16, "%f" , f);
} else {
len = snprintf(array, 16, "%.*f", fixed, f);
}
strbuf_append_string(buf, array, trim_tail_zero(array, len));
}
void strbuf_append_double(struct strbuf *buf, double lf, int fixed)
{
char array[32];
int len;
if (fixed < 0) {
len = snprintf(array, 32, "%g" , lf + DBL_EPSILON);
} else {
len = snprintf(array, 32, "%.*g", fixed, lf + DBL_EPSILON);
}
strbuf_append_string(buf, array, trim_tail_zero(array, len));
}
/*
* example:
* ```c
* int len = buf->length;
* char *ptr = malloc(len + 1);
* strbuf_to_string(buf, ptr);
* ```
*/
void strbuf_to_string(struct strbuf *buf, char *out)
{
char *ptr = out + buf->length;
*ptr = 0;
struct chunk *chk = chk_head(buf);
while (chk) {
ptr -= chk->pos;
memcpy(ptr, chk_data(chk), chk->pos);
chk = chk_next(chk);
}
}
static int stream_write_rec(struct chunk *chk, FILE *stream)
{
if (!chk)
return 0;
int size = stream_write_rec(chk_next(chk), stream);
return size + fwrite(chk_data(chk), sizeof(char), chk->pos, stream);
}
int strbuf_to_file(struct strbuf *buf, FILE *stream)
{
struct chunk *chk = chk_head(buf);
int size = stream_write_rec(chk, stream);
fflush(stream);
return size;
}