-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.c
72 lines (59 loc) · 1.26 KB
/
buffer.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* Copyright (C) 2010 Valery V. Vorotyntsev <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include "buffer.h"
int
buffer_resize(struct Buffer *buf, size_t size)
{
uint8_t *p = realloc(buffer_data(buf), size + 1);
if (p == NULL)
return -1;
p[size] = 0; /* hidden null byte ('\0') */
buf->wptr = p;
buf->_max_size = buf->size = size;
return 0;
}
int
buffer_put(struct Buffer *dest, const void *src, size_t n)
{
if (dest->size < n)
return -1;
memcpy(dest->wptr, src, n);
dest->wptr += n;
dest->size -= n;
return 0;
}
int
buffer_putc(struct Buffer *dest, uint8_t c)
{
if (dest->size == 0)
return -1;
*dest->wptr = c;
++dest->wptr;
--dest->size;
return 0;
}
int
buffer_printf(struct Buffer *dest, const char *format, ...)
{
va_list ap;
va_start(ap, format);
size_t n = vsnprintf((char *) dest->wptr, dest->size, format, ap);
va_end(ap);
if (n < dest->size) {
dest->wptr += n; /* point to the trailing '\0' */
dest->size -= n;
return 0;
}
dest->wptr += dest->size;
dest->size = 0;
return -1;
}