-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile.h
34 lines (33 loc) · 963 Bytes
/
file.h
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
//
#include <stdio.h>
#include <malloc.h>
#include <sys/stat.h>
#include <string.h>
static size_t read_file(FILE* fp, unsigned char** output) {
size_t smart_size, count;
size_t length = 0;
//make it faster
if (!fp) { //incase
return 0;
}
else if (fp == stdin) {
smart_size = stdin->_bufsiz;
}
else { //unreliable for stdin!
struct stat filestats;
int fd = fileno(fp);
fstat(fd, &filestats);
smart_size = filestats.st_size + 1; // +1 to get EOF, BIG file
}
//
*output = calloc(1, 1); //just in case
while (!feof(fp)) {
*output = realloc(*output, length + smart_size + sizeof(wchar_t));
count = fread(*output + length, 1, smart_size, fp);
memset(*output + length + count, 0, sizeof(wchar_t)); // append 0, in case of wide char
length += count;
}
*output = realloc(*output, length + sizeof(wchar_t));
//
return length;
}