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