1 |
/* |
2 |
resource-manager.c - handles embedded files |
3 |
Copyright (C) 2007 siliconforks.com |
4 |
|
5 |
This program is free software; you can redistribute it and/or modify |
6 |
it under the terms of the GNU General Public License as published by |
7 |
the Free Software Foundation; either version 2 of the License, or |
8 |
(at your option) any later version. |
9 |
|
10 |
This program is distributed in the hope that it will be useful, |
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
GNU General Public License for more details. |
14 |
|
15 |
You should have received a copy of the GNU General Public License along |
16 |
with this program; if not, write to the Free Software Foundation, Inc., |
17 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
18 |
*/ |
19 |
|
20 |
#include "resource-manager.h" |
21 |
|
22 |
#include <stdlib.h> |
23 |
#include <string.h> |
24 |
|
25 |
#include "util.h" |
26 |
|
27 |
#include "resources.c" |
28 |
|
29 |
const struct Resource * get_resource(const char * name) { |
30 |
int num_resources = sizeof(RESOURCES) / sizeof(struct Resource); |
31 |
for (int i = 0; i < num_resources; i++) { |
32 |
if (strcmp(RESOURCES[i].name, name) == 0) { |
33 |
return &RESOURCES[i]; |
34 |
} |
35 |
} |
36 |
abort(); |
37 |
} |
38 |
|
39 |
void copy_resource_to_stream(const char * resource, FILE * stream) { |
40 |
const struct Resource * r = get_resource(resource); |
41 |
if (fwrite(r->data, 1, r->length, stream) != r->length) { |
42 |
fatal("cannot write to stream"); |
43 |
} |
44 |
} |
45 |
|
46 |
void copy_resource(const char * resource, const char * destination_directory) { |
47 |
char * file = make_path(destination_directory, resource); |
48 |
char * directory = make_dirname(file); |
49 |
mkdirs(directory); |
50 |
free(directory); |
51 |
FILE * f = xfopen(file, "wb"); |
52 |
copy_resource_to_stream(resource, f); |
53 |
fclose(f); |
54 |
free(file); |
55 |
} |