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 157275 frx_buf_init(frx_buf *b)
23 {
24 157275 b->p = NULL;
25 157275 b->len = 0;
26 157275 b->cap = 0;
27 157275 b->failed = 0;
28 157275 }
29  
30 /* room for extra more bytes plus the NUL; 0 on failure, and failed is set */
31 static int
32 169615941 frx_buf_reserve(frx_buf *b, size_t extra)
33 {
34 size_t need;
35 169615941 ▶ if (b->failed) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (b->failed) return 0; 0 169615941 169615941
36 169615941 need = b->len + extra + 1;
37 169615941 ▶ if (need < extra) { b->failed = 1; return 0; } /* wrapped */
Branch: 1 branch
BranchTrueFalseTotal
if (need < extra) { b->failed = 1; return 0; } /* wrapped */ 0 169615941 169615941
38 169615941 ▶ if (need > b->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (need > b->cap) { 66558 169549383 169615941
39 66558 ▶ size_t ncap = b->cap ? b->cap : 256;
Branch: 1 branch
BranchTrueFalseTotal
size_t ncap = b->cap ? b->cap : 256; 1495 65063 66558
40 char *np;
41 70306 ▶ while (ncap < need) {
Branch: 1 branch
BranchTrueFalseTotal
while (ncap < need) { 3748 66558 70306
42 3748 size_t grown = ncap * 2;
43 3748 ▶ if (grown < ncap) { b->failed = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (grown < ncap) { b->failed = 1; return 0; } 0 3748 3748
44 3748 ncap = grown;
45 }
46 66558 np = (char *)realloc(b->p, ncap);
47 66558 ▶ if (!np) { b->failed = 1; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!np) { b->failed = 1; return 0; } 0 66558 66558
48 66558 b->p = np;
49 66558 b->cap = ncap;
50 }
51 169615941 return 1;
52 }
53  
54 static void
55 169615941 frx_buf_append_n(frx_buf *b, const char *s, size_t n)
56 {
57 169615941 ▶ if (!frx_buf_reserve(b, n)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!frx_buf_reserve(b, n)) return; 0 169615941 169615941
58 169615941 ▶ if (n) memcpy(b->p + b->len, s, n);
Branch: 1 branch
BranchTrueFalseTotal
if (n) memcpy(b->p + b->len, s, n); 167442430 2173511 169615941
59 169615941 b->len += n;
60 169615941 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 160922075 frx_buf_append_ch(frx_buf *b, char c)
71 {
72 160922075 frx_buf_append_n(b, &c, 1);
73 160922075 }
74  
75 static void
76 78643 frx_buf_free(frx_buf *b)
77 {
78 78643 free(b->p);
79 78643 frx_buf_init(b);
80 78643 }
81  
82 #endif /* FRX_BUF_H */
« include/frx/frx_write.h ↑ Summary include/frx/frx_xmlattr.h »