9 static void sbuf_extend(struct sbuf
*sbuf
, int amount
)
12 sbuf
->sz
= (MAX(1, amount
) + SBUF_SZ
- 1) & ~(SBUF_SZ
- 1);
13 sbuf
->s
= malloc(sbuf
->sz
);
15 memcpy(sbuf
->s
, s
, sbuf
->n
);
19 void sbuf_init(struct sbuf
*sbuf
)
21 memset(sbuf
, 0, sizeof(*sbuf
));
22 sbuf_extend(sbuf
, SBUF_SZ
);
25 void sbuf_add(struct sbuf
*sbuf
, int c
)
27 if (sbuf
->n
+ 2 >= sbuf
->sz
)
28 sbuf_extend(sbuf
, sbuf
->sz
* 2);
29 sbuf
->s
[sbuf
->n
++] = c
;
32 void sbuf_append(struct sbuf
*sbuf
, char *s
)
35 if (sbuf
->n
+ len
+ 1 >= sbuf
->sz
)
36 sbuf_extend(sbuf
, sbuf
->n
+ len
+ 1);
37 memcpy(sbuf
->s
+ sbuf
->n
, s
, len
);
41 void sbuf_printf(struct sbuf
*sbuf
, char *s
, ...)
48 sbuf_append(sbuf
, buf
);
51 int sbuf_empty(struct sbuf
*sbuf
)
56 char *sbuf_buf(struct sbuf
*sbuf
)
58 sbuf
->s
[sbuf
->n
] = '\0';
62 int sbuf_len(struct sbuf
*sbuf
)
67 /* shorten the sbuf */
68 void sbuf_cut(struct sbuf
*sbuf
, int n
)
74 void sbuf_done(struct sbuf
*sbuf
)