-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdiox.h
More file actions
34 lines (31 loc) · 913 Bytes
/
stdiox.h
File metadata and controls
34 lines (31 loc) · 913 Bytes
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
// https://spacetoast.dev/tech.html
// = header
#ifndef BREAD_STDIOX_H
#define BREAD_STDIOX_H
#include <stdio.h>
#ifndef READALL_BUFSIZE
#define READALL_BUFSIZE 1024
#endif
// read from src into dst until hitting EOF
// dst will be allocated using malloc and realloc, READALL_BUFSIZE at a time
// does not call fseek, so you can use this with pipes/sockets/etc
// note that as size increases, this will slow down significantly
size_t readall(char **dst, FILE *src);
#endif // BREAD_STDIOX_H
// = implementation
#ifdef BREAD_STDIOX_IMPLEMENTATION
#include <stdio.h>
#include <stdlib.h>
size_t readall(char **dst, FILE *src) {
size_t size = READALL_BUFSIZE, read = 0;
*dst = malloc(size);
while(!feof(src)) {
if (size - read < READALL_BUFSIZE) {
size = (size - read) + READALL_BUFSIZE;
*dst = realloc(*dst, size);
}
read += fread((*dst) + read, 1, READALL_BUFSIZE, src);
}
return read;
}
#endif