int mame_faccess(const char *filename, int filetype) { const char *extension = get_extension_for_filetype(filetype); int pathcount = osd_get_path_count(filetype); char modified_filename[256]; int pathindex; /* copy the filename and add an extension */ strcpy(modified_filename, filename); if (extension) { char *p = strchr(modified_filename, '.'); if (p) strcpy(p, extension); else { strcat(modified_filename, "."); strcat(modified_filename, extension); } } /* loop over all paths */ for (pathindex = 0; pathindex < pathcount; pathindex++) { char name[256]; /* first check the raw filename, in case we're looking for a directory */ sprintf(name, "%s", filename); LOG(("mame_faccess: trying %s\n", name)); if (osd_get_path_info(filetype, pathindex, name) != PATH_NOT_FOUND) return 1; /* try again with a .zip extension */ sprintf(name, "%s.zip", filename); LOG(("mame_faccess: trying %s\n", name)); if (osd_get_path_info(filetype, pathindex, name) != PATH_NOT_FOUND) return 1; /* does such a directory (or file) exist? */ sprintf(name, "%s", modified_filename); LOG(("mame_faccess: trying %s\n", name)); if (osd_get_path_info(filetype, pathindex, name) != PATH_NOT_FOUND) return 1; } /* no match */ return 0; }
const char *image_filedir(mess_image *img) { char *s; if (!img->dir) { img->dir = image_strdup(img, img->name); if (img->dir) { s = img->dir + strlen(img->dir); while(--s > img->dir) { if (strchr("\\/:", *s)) { *s = '\0'; if (osd_get_path_info(FILETYPE_IMAGE, 0, img->dir) == PATH_IS_DIRECTORY) break; } } } } return img->dir; }
static mame_file *generic_fopen(int pathtype, const char *gamename, const char *filename, const char* hash, UINT32 flags) { static const char *access_modes[] = { "rb", "rb", "wb", "r+b" }; const char *extension = get_extension_for_filetype(pathtype); int pathcount = osd_get_path_count(pathtype); int pathindex, pathstart, pathstop, pathinc; mame_file file, *newfile; char tempname[256]; LOG(("generic_fopen(%d, %s, %s, %s, %X)\n", pathc, gamename, filename, extension, flags)); /* reset the file handle */ memset(&file, 0, sizeof(file)); /* check for incompatible flags */ if ((flags & FILEFLAG_OPENWRITE) && (flags & FILEFLAG_HASH)) fprintf(stderr, "Can't use HASH option with WRITE option in generic_fopen!\n"); /* determine start/stop based on reverse search flag */ if (!(flags & FILEFLAG_REVERSE_SEARCH)) { pathstart = 0; pathstop = pathcount; pathinc = 1; } else { pathstart = pathcount - 1; pathstop = -1; pathinc = -1; } /* loop over paths */ for (pathindex = pathstart; pathindex != pathstop; pathindex += pathinc) { char name[1024]; /* ----------------- STEP 1: OPEN THE FILE RAW -------------------- */ /* first look for path/gamename as a directory */ compose_path(name, gamename, NULL, NULL); LOG(("Trying %s\n", name)); /* if the directory exists, proceed */ if (*name == 0 || osd_get_path_info(pathtype, pathindex, name) == PATH_IS_DIRECTORY) { /* now look for path/gamename/filename.ext */ compose_path(name, gamename, filename, extension); /* if we need checksums, load it into RAM and compute it along the way */ if (flags & FILEFLAG_HASH) { if (checksum_file(pathtype, pathindex, name, &file.data, &file.length, file.hash) == 0) { file.type = RAM_FILE; break; } } /* otherwise, just open it straight */ else { file.type = PLAIN_FILE; file.file = osd_fopen(pathtype, pathindex, name, access_modes[flags & 3]); if (file.file == NULL && (flags & 3) == 3) file.file = osd_fopen(pathtype, pathindex, name, "w+b"); if (file.file != NULL) break; } } /* ----------------- STEP 2: OPEN THE FILE IN A ZIP -------------------- */ /* now look for it within a ZIP file */ if (!(flags & (FILEFLAG_OPENWRITE | FILEFLAG_NOZIP))) { /* first look for path/gamename.zip */ compose_path(name, gamename, NULL, "zip"); LOG(("Trying %s file\n", name)); /* if the ZIP file exists, proceed */ if (osd_get_path_info(pathtype, pathindex, name) == PATH_IS_FILE) { UINT32 ziplength; /* if the file was able to be extracted from the ZIP, continue */ compose_path(tempname, NULL, filename, extension); /* verify-only case */ if (flags & FILEFLAG_VERIFY_ONLY) { UINT8 crcs[4]; UINT32 crc = 0; /* Since this is a .ZIP file, we extract the CRC from the expected hash (if any), so that we can load by CRC if needed. We must check that the hash really contains a CRC, because it could be a NO_DUMP rom for which we do not know the CRC yet. */ if (hash && hash_data_extract_binary_checksum(hash, HASH_CRC, crcs) != 0) { /* Store the CRC in a single DWORD */ crc = ((unsigned long)crcs[0] << 24) | ((unsigned long)crcs[1] << 16) | ((unsigned long)crcs[2] << 8) | ((unsigned long)crcs[3] << 0); } hash_data_clear(file.hash); if (checksum_zipped_file(pathtype, pathindex, name, tempname, &ziplength, &crc) == 0) { file.length = ziplength; file.type = UNLOADED_ZIPPED_FILE; crcs[0] = (UINT8)(crc >> 24); crcs[1] = (UINT8)(crc >> 16); crcs[2] = (UINT8)(crc >> 8); crcs[3] = (UINT8)(crc >> 0); hash_data_insert_binary_checksum(file.hash, HASH_CRC, crcs); break; } }