-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCirBBuf.c
36 lines (33 loc) · 858 Bytes
/
CirBBuf.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
#include "cir_internal.h"
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
void
CirBBuf__add(CirBBuf *buf, const uint8_t *src, size_t len)
{
CirBBuf_grow(buf, len);
memcpy(buf->items + buf->len, src, len);
buf->len += len;
}
static void
CirBBuf__read(CirBBuf *buf, FILE *fp)
{
CirBBuf_grow(buf, 8192);
while (!feof(fp)) {
size_t reqLen = buf->alloc - buf->len;
size_t bytesRead = fread(buf->items + buf->len, 1, reqLen, fp);
buf->len += bytesRead;
if (ferror(fp))
cir_fatal("failed to read from file: %s", strerror(errno));
CirBBuf_grow(buf, 8192);
}
}
void
CirBBuf__readFile(CirBBuf *buf, const char *path)
{
FILE *fp = fopen(path, "rb");
if (!fp)
cir_fatal("could not open %s: %s", path, strerror(errno));
CirBBuf__read(buf, fp);
fclose(fp);
}