aboutsummaryrefslogtreecommitdiffstats
path: root/src/xstdlib/xstdlib.cpp
blob: 4264e68e0e8abd01bb7a854b8644db6ee60ecc61 (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
#include <stdio.h>

#include "xstdlib.hpp"

char *xfread(const char *path, const char *mode)
{
	char *source = 0;
	size_t file_size, bytes_read;
	FILE *file;

	file = fopen(path, mode);
	if(!file)
		return 0;

	do {
		if(fseek(file, 0, SEEK_END) < 0)
			break;

		file_size = ftell(file);
		if(file_size < 0)
			break;

		if(fseek(file, 0, SEEK_SET) < 0)
			break;

		source = new char[file_size + 1];
		bytes_read = fread(source, 1, file_size, file);
		if(bytes_read != file_size) {
			delete[] source;
			source = 0;
		}
		source[file_size] = '\0';
	} while(0);

	fclose(file);
	return source;
}