blob: feb3c0eb747fcad558ad30b4af673655b106a76d (
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
#include "util.h"
#include <stdio.h>
#include <stdlib.h>
int ecex_file_exists(const char *path) {
if (!path || !path[0]) return 0;
FILE *file = fopen(path, "rb");
if (!file) return 0;
fclose(file);
return 1;
}
char *ecex_read_entire_file(const char *path, size_t *out_size) {
FILE *file = fopen(path, "rb");
if (!file) return NULL;
if (fseek(file, 0, SEEK_END) != 0) {
fclose(file);
return NULL;
}
long size = ftell(file);
if (size < 0) {
fclose(file);
return NULL;
}
rewind(file);
char *data = malloc((size_t)size + 1);
if (!data) {
fclose(file);
return NULL;
}
size_t read_count = fread(data, 1, (size_t)size, file);
fclose(file);
if (read_count != (size_t)size) {
free(data);
return NULL;
}
data[size] = '\0';
if (out_size) {
*out_size = (size_t)size;
}
return data;
}
char *ecex_strdup(const char *s) {
if (!s) return NULL;
size_t len = 0;
while (s[len]) len++;
char *out = malloc(len + 1);
if (!out) return NULL;
for (size_t i = 0; i <= len; i++) {
out[i] = s[i];
}
return out;
}
|