|
@@ -1,34 +1,51 @@ |
|
|
/* Copyright (C) 2014-2015 Ben Kurtovic <ben.kurtovic@gmail.com> |
|
|
/* Copyright (C) 2014-2015 Ben Kurtovic <ben.kurtovic@gmail.com> |
|
|
Released under the terms of the MIT License. See LICENSE for details. */ |
|
|
Released under the terms of the MIT License. See LICENSE for details. */ |
|
|
|
|
|
|
|
|
|
|
|
#include <errno.h> |
|
|
#include <stdio.h> |
|
|
#include <stdio.h> |
|
|
#include <stdlib.h> |
|
|
#include <stdlib.h> |
|
|
#include <string.h> |
|
|
#include <string.h> |
|
|
|
|
|
#include <sys/stat.h> |
|
|
|
|
|
|
|
|
#include "rom.h" |
|
|
#include "rom.h" |
|
|
|
|
|
|
|
|
/* Create and return a ROM object located at the given path. Return NULL if |
|
|
/* Create and return a ROM object located at the given path. Return NULL if |
|
|
there was an error; errno will be set appropriately. */ |
|
|
there was an error; errno will be set appropriately. */ |
|
|
rom_type* open_rom(const char *path) |
|
|
|
|
|
|
|
|
rom_type* rom_open(const char *path) |
|
|
{ |
|
|
{ |
|
|
rom_type *rom; |
|
|
rom_type *rom; |
|
|
FILE* fp; |
|
|
FILE* fp; |
|
|
|
|
|
struct stat s; |
|
|
|
|
|
|
|
|
if (!(fp = fopen(path, "r"))) |
|
|
if (!(fp = fopen(path, "r"))) |
|
|
return NULL; |
|
|
return NULL; |
|
|
if (!(rom = malloc(sizeof(rom_type)))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fstat(fileno(fp), &s)) { |
|
|
|
|
|
fclose(fp); |
|
|
|
|
|
return NULL; |
|
|
|
|
|
} |
|
|
|
|
|
if (!(s.st_mode & S_IFREG)) { |
|
|
|
|
|
if (s.st_mode & S_IFDIR) |
|
|
|
|
|
errno = EISDIR; |
|
|
|
|
|
fclose(fp); |
|
|
|
|
|
return NULL; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!(rom = malloc(sizeof(rom_type)))) { |
|
|
|
|
|
fclose(fp); |
|
|
return NULL; |
|
|
return NULL; |
|
|
|
|
|
} |
|
|
rom->name = malloc(sizeof(char) * (strlen(path) + 1)); |
|
|
rom->name = malloc(sizeof(char) * (strlen(path) + 1)); |
|
|
strcpy(rom->name, path); |
|
|
strcpy(rom->name, path); |
|
|
|
|
|
|
|
|
// load data from file into a buffer |
|
|
|
|
|
|
|
|
// TODO: load data from file into a buffer |
|
|
|
|
|
|
|
|
fclose(fp); |
|
|
fclose(fp); |
|
|
return rom; |
|
|
return rom; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/* Free a ROM object previously created with open_rom(). */ |
|
|
|
|
|
void close_rom(rom_type *rom) |
|
|
|
|
|
|
|
|
/* Free a ROM object previously created with rom_open(). */ |
|
|
|
|
|
void rom_close(rom_type *rom) |
|
|
{ |
|
|
{ |
|
|
free(rom); |
|
|
free(rom); |
|
|
} |
|
|
} |