-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebuf.c
53 lines (42 loc) · 879 Bytes
/
ebuf.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
/*
* Buffer for error messages.
* Different modules can add error messages to it.
* It is common practice to prefix message with module
* short name. We see this buffer as basic analog of
* Java stack trace.
*/
#include <stdarg.h>
#include <stdio.h>
#include "ebuf.h"
void ebuf_init(struct ebuf *ebuf, char *buf, int sz)
{
ebuf->buf = ebuf->p = buf;
ebuf->end = ebuf->buf + sz;
*(ebuf->p) = '\0';
}
void ebuf_clr(struct ebuf *ebuf)
{
ebuf->p = ebuf->buf;
*(ebuf->p) = '\0';
}
void ebuf_add(struct ebuf *ebuf, const char *frmt, ...)
{
va_list ap;
int n, l;
n = ebuf->end - ebuf->p - 1;
va_start(ap, frmt);
l = vsnprintf(ebuf->p, n, frmt, ap);
va_end(ap);
if (l > n)
l = n;
ebuf->p += l;
if (*(ebuf->p - 1) != '\n') {
*(ebuf->p) = '\n';
*(ebuf->p + 1) = '\0';
(ebuf->p) += 2;
}
}
const char *ebuf_s(struct ebuf *ebuf)
{
return ebuf->buf;
}