An emulator, assembler, and disassembler for the Sega Game Gear
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

56 lines
1.1 KiB

  1. /* Copyright (C) 2014-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. Released under the terms of the MIT License. See LICENSE for details. */
  3. #include <errno.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/stat.h>
  8. #include "rom.h"
  9. /*
  10. Create and return a ROM object located at the given path. Return NULL if
  11. there was an error; errno will be set appropriately.
  12. */
  13. ROM* rom_open(const char *path)
  14. {
  15. ROM *rom;
  16. FILE* fp;
  17. struct stat s;
  18. if (!(fp = fopen(path, "r")))
  19. return NULL;
  20. if (fstat(fileno(fp), &s)) {
  21. fclose(fp);
  22. return NULL;
  23. }
  24. if (!(s.st_mode & S_IFREG)) {
  25. if (s.st_mode & S_IFDIR)
  26. errno = EISDIR;
  27. fclose(fp);
  28. return NULL;
  29. }
  30. if (!(rom = malloc(sizeof(ROM)))) {
  31. fclose(fp);
  32. return NULL;
  33. }
  34. rom->name = malloc(sizeof(char) * (strlen(path) + 1));
  35. strcpy(rom->name, path);
  36. // TODO: load data from file into a buffer
  37. fclose(fp);
  38. return rom;
  39. }
  40. /*
  41. Free a ROM object previously created with rom_open().
  42. */
  43. void rom_close(ROM *rom)
  44. {
  45. free(rom);
  46. }