-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassets.c
170 lines (83 loc) · 2.98 KB
/
assets.c
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "assets.h"
void *assets_get_file_content(const char *file_path, long *file_size)
{
FILE *file = fopen(file_path, "rb");
if (file == NULL)
{
fprintf(stderr, "Failed to read file: %s, in \"assets_get_file_content\".", file_path);
return NULL;
}
fseek(file, 0, SEEK_END);
*file_size = ftell(file);
fseek(file, 0, SEEK_SET);
void *file_content = malloc(*file_size);
if (file_content == NULL)
{
fprintf(stderr, "Failed to allocate memory for file content in \"assets_get_file_content\".");
fclose(file);
return NULL;
}
fread(file_content, 1, *file_size, file);
fclose(file);
return file_content;
}
char *assets_get_file_content_null(const char *file_path, long *file_size)
{
void *file_content = assets_get_file_content(file_path, file_size);
if (file_content == NULL)
{
fprintf(stderr, "");
return NULL;
}
char *file_content_null = (char *)malloc(*file_size + 1);
if (file_content_null == NULL)
{
fprintf(stderr, "Failed to allocate memory for file content in \"assets_get_file_content_null\".");
free(file_content);
return NULL;
}
memcpy(file_content_null, file_content, *file_size);
file_content_null[*file_size] = '\0';
free(file_content);
return file_content_null;
}
char *get_path_from_assets(char *folder, char *file_name)
{
size_t folder_str_len = strlen(folder);
size_t file_name_str_len = strlen(file_name);
size_t out_str_len = sizeof(ASSETS_FOLDER) + folder_str_len + file_name_str_len + 1;
char *out_str = (char *)malloc(out_str_len);
if (out_str == NULL)
{
fprintf(stderr, "Failed to allocate memory when getting path from assets: %s, %s.", folder, file_name);
return NULL;
}
strcpy(out_str, ASSETS_FOLDER);
strcat(out_str, folder);
strcat(out_str, file_name);
out_str[out_str_len] = '\0';
return out_str;
}
char *get_file_path_from_assets(char *folder, char *subfolder, char *file_name)
{
size_t folder_str_len = strlen(folder);
size_t subfolder_str_len = strlen(subfolder);
size_t fss_str_len = sizeof(ASSETS_FOLDER) + folder_str_len + subfolder_str_len + 1;
char *fss_str = (char *)malloc(fss_str_len);
if (fss_str == NULL)
{
fprintf(stderr, ": %s, %s.", folder, subfolder);
return NULL;
}
strcpy(fss_str, folder);
strcat(fss_str, subfolder);
fss_str[fss_str_len] = '\0';
char *file_path = get_path_from_assets(fss_str, file_name);
free(fss_str);
if (file_path == NULL)
{
fprintf(stderr, "Failed to get image file path: %s, %s.", fss_str, file_name);
return NULL;
}
return file_path;
}