include/frx/frx_buf.h

filter: Statementstmt 100% 36 / 36 Branchbran 72.2% 13 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 90.7% 49 / 54 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/frx/frx_write.h ↑ Summary include/frx/frx_xmlattr.h »
1 #ifndef FRX_BUF_H
2 #define FRX_BUF_H
3  
4 /* Output bytes.
5 *
6 * A growable byte buffer for canonicalisation and text output: the core
7 * writes into it and the SV layer wraps the finished bytes once. Doubling
8 * realloc, always NUL-terminated at len, and a failure flag the caller
9 * checks once at the end rather than on every append - after a failed
10 * grow every later append is a no-op, so the flag is the whole story.
11 *
12 * malloc, realloc and free by rule 3. Needs nothing. */
13  
14 typedef struct frx_buf {
15 char *p;
16 size_t len;
17 size_t cap;
18 int failed;
19 } frx_buf;
20  
21 static void
22 157111 frx_buf_init(frx_buf *b)
23 {
24 157111 b->p = NULL;
25 157111 b->len = 0;
26 157111 b->cap = 0;
27 157111 b->failed = 0;
28 157111 }
29  
30 /* room for extra more bytes plus the NUL; 0 on failure, and failed is set */
31 static int
32 169613908 frx_buf_reserve(frx_buf *b, size_t extra)
33 {
34 size_t need;
35 169613908 ▶ if (b->failed) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (b->failed) return 0; 0 169613908 169613908
36 169613908 need = b->len + extra + 1;
37 169613908 ▶ if (need < extra) { b->failed = 1; return 0; } /* wrapped */
Branch: 1 branch
BranchTrueFalseTotal
if (need < extra) { b->failed = 1; return 0; } /* wrapped */ 0 169613908 169613908
38 169613908 ▶ if (need > b->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (need > b->cap) { 66477 169547431 169613908
39 66477 ▶ size_t ncap = b->cap ? b->cap : 256;
Branch: 1 branch
BranchTrueFalseTotal
size_t ncap = b->cap ? b->cap : 256; 1495 64982 66477
40 char *np;
41 69865 ▶ while (ncap < need) {
Branch: 1 branch
BranchTrueFalseTotal
while (ncap < need) { 3388 66477 69865
42 3388 size_t grown = ncap * 2;
43 3388 ▶ if (grown < ncap) { b->failed = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (grown < ncap) { b->failed = 1; return 0; } 0 3388 3388
44 3388 ncap = grown;
45 }
46 66477 np = (char *)realloc(b->p, ncap);
47 66477 ▶ if (!np) { b->failed = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!np) { b->failed = 1; return 0; } 0 66477 66477
48 66477 b->p = np;
49 66477 b->cap = ncap;
50 }
51 169613908 return 1;
52 }
53  
54 static void
55 169613908 frx_buf_append_n(frx_buf *b, const char *s, size_t n)
56 {
57 169613908 ▶ if (!frx_buf_reserve(b, n)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_buf_reserve(b, n)) return; 0 169613908 169613908
58 169613908 ▶ if (n) memcpy(b->p + b->len, s, n);
Branch: 1 branch
BranchTrueFalseTotal
if (n) memcpy(b->p + b->len, s, n); 167440397 2173511 169613908
59 169613908 b->len += n;
60 169613908 b->p[b->len] = '\0';
61 }
62  
63 static void
64 433503 frx_buf_append(frx_buf *b, const char *s)
65 {
66 433503 frx_buf_append_n(b, s, strlen(s));
67 433503 }
68  
69 static void
70 160920148 frx_buf_append_ch(frx_buf *b, char c)
71 {
72 160920148 frx_buf_append_n(b, &c, 1);
73 160920148 }
74  
75 static void
76 78561 frx_buf_free(frx_buf *b)
77 {
78 78561 free(b->p);
79 78561 frx_buf_init(b);
80 78561 }
81  
82 #endif /* FRX_BUF_H */
« include/frx/frx_write.h ↑ Summary include/frx/frx_xmlattr.h »