Repository navigation
Expand file tree
/
Copy pathsbuf.c
More file actions
239 lines (228 loc) · 5.26 KB
/
Copy pathsbuf.c
File metadata and controls
239 lines (228 loc) · 5.26 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// file: sbuf.c
// vim:fileencoding=utf-8:ft=c:tabstop=2
// This is free and unencumbered software released into the public domain.
//
// Author: R.F. Smith <rsmith@xs4all.nl>
// SPDX-License-Identifier: Unlicense
// Created: 2025-08-28 23:49:02 +0200
// Last modified: 2026-04-23T18:53:20+0200
#include "sbuf.h"
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
void sbuf_append(Sbuf *buf, char *str, ptrdiff_t len)
{
assert(buf != 0);
assert(str != 0);
if (buf->error == true) {
return;
}
ptrdiff_t alen = strnlen(str, len);
ptrdiff_t remaining = SBUF_SIZE - buf->used - 1;
if (len < remaining) {
memcpy(buf->data + buf->used, str, alen);
buf->used += alen;
buf->error = false;
} else {
buf->error = true;
}
}
void sbuf_appendc(Sbuf *buf, char c)
{
assert(buf != 0);
if (buf->error == true) {
return;
}
ptrdiff_t remaining = SBUF_SIZE - buf->used - 1;
if (remaining >= 1) {
buf->data[buf->used++] = c;
buf->error = false;
} else {
buf->error = true;
}
}
inline void sbuf_appends(Sbuf *buf, char *str)
{
sbuf_append(buf, str, strlen(str));
}
extern void sbuf_appendi(Sbuf *buf, int64_t i)
{
assert(buf != 0);
if (buf->error == true) {
return;
}
#define BUFLENI 21
#define ORD0 48
int64_t work = 0;
bool negative = false;
if (i < 0) {
work = -i;
negative = true;
} else {
work = i;
}
char tmpbuf[BUFLENI] = {0};
int index = sizeof(tmpbuf) - 1;
do {
ldiv_t result = ldiv(work, 10);
tmpbuf[index--] = ORD0 + result.rem;
work = result.quot;
} while (work > 0 && index > 1);
if (negative) {
assert(index > 0);
tmpbuf[index--] = '-';
}
sbuf_appends(buf, tmpbuf + index + 1);
}
static double frexp10(double arg, int *exp)
{
*exp = (arg == 0) ? 0 : (int)floor(log10(fabs(arg)));
return arg * pow(10, -(*exp));
}
extern void sbuf_appendd(Sbuf *buf, double f)
{
#define BUFLEND 30
#define EXPLEN (BUFLEND-4-1)
assert(buf != 0);
if (buf->error != false) {
return;
}
int exp;
double mantissa = frexp10(f, &exp);
double am = fabs(mantissa);
char tbuf[BUFLEND] = {0};
int bufused = 0;
if (signbit(f)) {
tbuf[bufused++] = '-';
}
if (isinf(f)) {
char inf[4] = {0xE2, 0x88, 0x9E, 0}; // UTF-8 for infinity symbol.
sbuf_appends(buf, inf);
return;
} else if (mantissa == 0 && exp == 0) {
tbuf[bufused++] = '0';
sbuf_appends(buf, tbuf);
return;
} else if (isnan(f)) {
sbuf_appends(buf, "NaN");
return;
}
if (exp > 0 && exp < 10) {
// Format as dddd.dddddd, with 6 decimal places.
// Format integer part
int value = (int)f;
int frac = (int)round((f - (double)value) * 1e6);
char intbuf[11] = {0};
int index = sizeof(intbuf) - 2;
do {
div_t result = div(value, 10);
intbuf[index--] = ORD0 + result.rem;
value = result.quot;
} while (value > 0 && index > 0);
strcpy(tbuf + bufused, intbuf + index + 1);
bufused += strlen(intbuf + index + 1);
if (frac == 0) {
sbuf_appends(buf, tbuf);
return;
}
if (buf->decsep == 0) {
tbuf[bufused++] = '.';
} else {
tbuf[bufused++] = buf->decsep;
}
char fracbuf[8] = {0};
index = sizeof(fracbuf) - 2;
do {
div_t result = div(frac, 10);
fracbuf[index--] = ORD0 + result.rem;
frac = result.quot;
} while (frac > 0 && index > 0);
// Remove trailing 0 decimals
int ridx = sizeof(fracbuf) - 3;
while (fracbuf[ridx] == '0') {
fracbuf[ridx--] = 0;
}
strcpy(tbuf + bufused, fracbuf + index + 1);
sbuf_appends(buf, tbuf);
return;
}
int decimal = (int)floor(am);
tbuf[bufused++] = ORD0 + decimal;
am = (am - decimal) * 10.0;
if (buf->decsep != 0) {
tbuf[bufused++] = buf->decsep;
} else {
tbuf[bufused++] = '.';
}
int loopexp = 0;
do {
decimal = (int)floor(am);
tbuf[bufused++] = ORD0 + decimal;
am = (am - decimal) * 10.0;
frexp10(am, &loopexp);
if (decimal == 9 && am > 9.5) {
break;
}
} while (am > 0 && loopexp == 0 && bufused < EXPLEN);
tbuf[bufused++] = 'e';
if (exp < 0) {
tbuf[bufused++] = '-';
exp = -exp;
} else {
tbuf[bufused++] = '+';
}
if (exp > 100) {
tbuf[bufused++] = ORD0 + 1;
exp -= 100;
}
if (exp > 10) {
tbuf[bufused++] = ORD0 + exp / 10;
exp -= 10;
}
tbuf[bufused++] = ORD0 + exp;
sbuf_appends(buf, tbuf);
}
void sbuf_printf(Sbuf *buf, char *fmt, ...)
{
assert(buf != 0);
assert(fmt != 0);
if (buf->error == true) {
return;
}
ptrdiff_t remaining = SBUF_SIZE - buf->used - 1;
va_list ap;
va_start(ap, fmt);
ptrdiff_t used = vsnprintf(buf->data + buf->used, remaining, fmt, ap);
va_end(ap);
if (used > remaining) { // discard
memset(buf->data + buf->used, 0, remaining);
buf->error = true;
} else {
buf->error = false;
buf->used += used;
}
}
ptrdiff_t sbuf_remaining(Sbuf *buf)
{
assert(buf != 0);
ptrdiff_t remaining = SBUF_SIZE - buf->used - 1;
return remaining;
}
void sbuf_fputs(Sbuf *buf, FILE* stream)
{
assert(buf != 0);
assert(stream != 0);
fputs(buf->data, stream);
fflush(stream);
}
void sbuf_reset(Sbuf *buf)
{
assert(buf != 0);
memset(buf->data, 0, SBUF_SIZE);
buf->used = 0;
buf->error = false;
}