/** * Set given floppy drive image file name and handle * different image extensions. * Return corrected file name on success and NULL on failure. */ const char* Floppy_SetDiskFileName(int Drive, const char *pszFileName, const char *pszZipPath) { char *filename; int i; /* setting to empty or "none" ejects */ if (!*pszFileName || strcasecmp(pszFileName, "none") == 0) { return Floppy_SetDiskFileNameNone(Drive); } /* See if file exists, and if not, get/add correct extension */ if (!File_Exists(pszFileName)) filename = File_FindPossibleExtFileName(pszFileName, pszDiskImageNameExts); else filename = strdup(pszFileName); if (!filename) { Log_AlertDlg((const char *)LOG_INFO, "Image '%s' not found", pszFileName); return NULL; } #if 0 /* If we insert a disk into Drive A, should we try to put disk 2 into drive B? */ if (Drive == 0 && ConfigureParams.DiskImage.bAutoInsertDiskB) { /* Attempt to make up second filename, eg was 'auto_100a' to 'auto_100b' */ char *szDiskBFileName = Floppy_CreateDiskBFileName(filename); if (szDiskBFileName) { /* recurse with Drive B */ Floppy_SetDiskFileName(1, szDiskBFileName, pszZipPath); free(szDiskBFileName); } } #endif /* validity checks */ assert(Drive >= 0 && Drive < MAX_FLOPPYDRIVES); for (i = 0; i < MAX_FLOPPYDRIVES; i++) { if (i == Drive) continue; /* prevent inserting same image to multiple drives */ if (strcmp(filename, /*ConfigureParams.DiskImage.*/szDiskFileName[i]) == 0) { Log_AlertDlg((const char *)LOG_ERROR, "ERROR: Cannot insert same floppy to multiple drives!"); return NULL; } } /* do the changes */ if (pszZipPath) strcpy(szDiskZipPath[Drive], pszZipPath); else szDiskZipPath[Drive][0] = '\0'; strcpy(szDiskFileName[Drive], filename); free(filename); //File_MakeAbsoluteName(ConfigureParams.DiskImage.szDiskFileName[Drive]); return szDiskFileName[Drive]; }
/** * Read file from disk into allocated buffer and return the buffer * or NULL for error. If pFileSize is non-NULL, read file size * is set to that. */ Uint8 *File_Read(const char *pszFileName, long *pFileSize, const char * const ppszExts[]) { char *filepath = NULL; Uint8 *pFile = NULL; long FileSize = 0; /* Does the file exist? If not, see if can scan for other extensions and try these */ if (!File_Exists(pszFileName) && ppszExts) { /* Try other extensions, if succeeds, returns correct one */ filepath = File_FindPossibleExtFileName(pszFileName, ppszExts); } if (!filepath) filepath = strdup(pszFileName); #if HAVE_LIBZ /* Is it a gzipped file? */ if (File_DoesFileExtensionMatch(filepath, ".gz")) { gzFile hGzFile; /* Open and read gzipped file */ hGzFile = gzopen(filepath, "rb"); if (hGzFile != NULL) { /* Find size of file: */ do { /* Seek through the file until we hit the end... */ char tmp[1024]; if (gzread(hGzFile, tmp, sizeof(tmp)) < 0) { fprintf(stderr, "Failed to read gzip file!\n"); free(filepath); return NULL; } } while (!gzeof(hGzFile)); FileSize = gztell(hGzFile); gzrewind(hGzFile); /* Read in... */ pFile = malloc(FileSize); if (pFile) FileSize = gzread(hGzFile, pFile, FileSize); gzclose(hGzFile); } } else if (File_DoesFileExtensionMatch(filepath, ".zip")) { /* It is a .ZIP file! -> Try to load the first file in the archive */ pFile = ZIP_ReadFirstFile(filepath, &FileSize, ppszExts); } else /* It is a normal file */ #endif /* HAVE_LIBZ */ { FILE *hDiskFile; /* Open and read normal file */ hDiskFile = fopen(filepath, "rb"); if (hDiskFile != NULL) { /* Find size of file: */ fseek(hDiskFile, 0, SEEK_END); FileSize = ftell(hDiskFile); fseek(hDiskFile, 0, SEEK_SET); /* Read in... */ pFile = malloc(FileSize); if (pFile) FileSize = fread(pFile, 1, FileSize, hDiskFile); fclose(hDiskFile); } } free(filepath); /* Store size of file we read in (or 0 if failed) */ if (pFileSize) *pFileSize = FileSize; return pFile; /* Return to where read in/allocated */ }