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.
 
 
 
 
 

52 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. /* Create and return a ROM object located at the given path. Return NULL if
  10. there was an error; errno will be set appropriately. */
  11. rom_type* rom_open(const char *path)
  12. {
  13. rom_type *rom;
  14. FILE* fp;
  15. struct stat s;
  16. if (!(fp = fopen(path, "r")))
  17. return NULL;
  18. if (fstat(fileno(fp), &s)) {
  19. fclose(fp);
  20. return NULL;
  21. }
  22. if (!(s.st_mode & S_IFREG)) {
  23. if (s.st_mode & S_IFDIR)
  24. errno = EISDIR;
  25. fclose(fp);
  26. return NULL;
  27. }
  28. if (!(rom = malloc(sizeof(rom_type)))) {
  29. fclose(fp);
  30. return NULL;
  31. }
  32. rom->name = malloc(sizeof(char) * (strlen(path) + 1));
  33. strcpy(rom->name, path);
  34. // TODO: load data from file into a buffer
  35. fclose(fp);
  36. return rom;
  37. }
  38. /* Free a ROM object previously created with rom_open(). */
  39. void rom_close(rom_type *rom)
  40. {
  41. free(rom);
  42. }