aboutsummaryrefslogtreecommitdiff
path: root/sources/TZR_LoadResourceTyped.c
blob: 126737b3c4c1880cb904f60ccb93ebb1d34beeca (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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "TZR_resource.h"
#include "TZR_globals.h"
#include "drain.h"
#include <string.h>
#include <sys/stat.h>

TZR_Uint
TZR_LoadResourceTyped(TZR_ResourceType type, const char *path)
{
	for (TZR_Uint i = 0; i < ___tzr_resources_size; i++)
		if (___tzr_resources[i].type == type &&
		    strcmp(___tzr_resources[i].path, path) == 0)
			return i + 1;
	FILE *const fp = fopen(path, "rb");
	if (fp == NULL) {
		perror(path);
		return 0;
	}
	size_t size;
	char *const data = drain(fp, &size);
	fclose(fp);
	if (data == NULL)
		return 0;
	const TZR_Uint id = TZR_LoadResourceFromMemory(type, data, size);
	free(data);
	if (id == 0)
		return 0;
	TZR_Resource *const res = TZR_GetResourcePointer(id);

	const size_t path_len = strlen(path);
	struct stat st = {0};
	(void)stat(path, &st);
	res->path = malloc(path_len + 1);
	res->mtime = 0;
	if (res->path == NULL)
		perror("TZR_LoadResourceTyped");
	else
		strcpy(res->path, path);
	return id;
}