PageRenderTime 45ms CodeModel.GetById 41ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/resource-manager.c

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C | 59 lines | 33 code | 8 blank | 18 comment | 6 complexity | 2a229827bc9c7cd9df2ab3c37d62f584 MD5 | raw file
 1/*
 2    resource-manager.c - handles embedded files
 3    Copyright (C) 2007, 2008 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 <config.h>
21
22#include "resource-manager.h"
23
24#include <assert.h>
25#include <stdlib.h>
26#include <string.h>
27
28#include "util.h"
29
30#include "resources.c"
31
32const struct Resource * get_resource(const char * name) {
33  int num_resources = sizeof(RESOURCES) / sizeof(struct Resource);
34  for (int i = 0; i < num_resources; i++) {
35    if (strcmp(RESOURCES[i].name, name) == 0) {
36      return &RESOURCES[i];
37    }
38  }
39  return NULL;
40}
41
42void copy_resource_to_stream(const char * resource, FILE * stream) {
43  const struct Resource * r = get_resource(resource);
44  assert(r != NULL);
45  if (fwrite(r->data, 1, r->length, stream) != r->length) {
46    fatal("cannot write to stream");
47  }
48}
49
50void copy_resource(const char * resource, const char * destination_directory) {
51  char * file = make_path(destination_directory, resource);
52  char * directory = make_dirname(file);
53  mkdirs(directory);
54  free(directory);
55  FILE * f = xfopen(file, "wb");
56  copy_resource_to_stream(resource, f);
57  fclose(f);
58  free(file);
59}