aboutsummaryrefslogtreecommitdiff
path: root/sources/drain.h
blob: 07db117437f363da290ade8b46d08a78c3946ceb (plain)
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
#pragma once
#include <stdio.h>
#include <stdlib.h>

static void *drain_error(void)
{
	perror("TZR_LoadResourceTyped:drain");
	return NULL;
}

static char *drain(FILE *fp, size_t *size)
{
	if (fseek(fp, 0, SEEK_END) < 0)
		return drain_error();
	*size = ftell(fp);
	if (fseek(fp, 0, SEEK_SET) < 0)
		return drain_error();
	char *data = malloc(*size);
	if (data == NULL)
		return drain_error();
	if (fread(data, 1, *size, fp) != *size)
		return free(data), drain_error();
	return data;
}