Exemplo n.º 1
0
Arquivo: zip.c Projeto: r-type/hatari
/**
 * Return the first matching file in a zip, or NULL on failure.
 * String buffer size is ZIP_PATH_MAX
 */
static char *ZIP_FirstFile(const char *filename, const char * const ppsExts[])
{
	zip_dir *files;
	int i, j;
	char *name;

	files = ZIP_GetFiles(filename);
	if (files == NULL)
		return NULL;

	name = malloc(ZIP_PATH_MAX);
	if (!name)
	{
		perror("ZIP_FirstFile");
		ZIP_FreeZipDir(files);
		return NULL;
	}

	/* Do we have to scan for a certain extension? */
	if (ppsExts)
	{
		name[0] = '\0';
		for(i = files->nfiles-1; i >= 0; i--)
		{
			for (j = 0; ppsExts[j] != NULL; j++)
			{
				if (File_DoesFileExtensionMatch(files->names[i], ppsExts[j]))
				{
					strncpy(name, files->names[i], ZIP_PATH_MAX);
					break;
				}
			}
		}
	}
	else
	{
		/* There was no extension given -> use the very first name */
		strncpy(name, files->names[0], ZIP_PATH_MAX);
	}

	/* free the files */
	ZIP_FreeZipDir(files);

	if (name[0] == '\0')
	{
		free(name);
		return NULL;
	}

	return name;
}
Exemplo n.º 2
0
/**
 * Save file to disk, return FALSE if errors
 */
bool File_Save(const char *pszFileName, const Uint8 *pAddress, size_t Size, bool bQueryOverwrite)
{
	bool bRet = false;

	/* Check if need to ask user if to overwrite */
	if (bQueryOverwrite)
	{
		/* If file exists, ask if OK to overwrite */
		if (!File_QueryOverwrite(pszFileName))
			return false;
	}

#if HAVE_LIBZ
	/* Normal file or gzipped file? */
	if (File_DoesFileExtensionMatch(pszFileName, ".gz"))
	{
		gzFile hGzFile;
		/* Create a gzipped file: */
		hGzFile = gzopen(pszFileName, "wb");
		if (hGzFile != NULL)
		{
			/* Write data, set success flag */
			if (gzwrite(hGzFile, pAddress, Size) == (int)Size)
				bRet = true;

			gzclose(hGzFile);
		}
	}
	else
#endif  /* HAVE_LIBZ */
	{
		FILE *hDiskFile;
		/* Create a normal file: */
		hDiskFile = fopen(pszFileName, "wb");
		if (hDiskFile != NULL)
		{
			/* Write data, set success flag */
			if (fwrite(pAddress, 1, Size, hDiskFile) == Size)
				bRet = true;

			fclose(hDiskFile);
		}
	}

	return bRet;
}
Exemplo n.º 3
0
/**
 * Does filename end with a .IPF or .RAW or .CTR extension ? If so, return true.
 * .RAW and .CTR support requires caps lib >= 5.1
 */
bool IPF_FileNameIsIPF(const char *pszFileName, bool bAllowGZ)
{
	return ( File_DoesFileExtensionMatch(pszFileName,".ipf" )
		|| ( bAllowGZ && File_DoesFileExtensionMatch(pszFileName,".ipf.gz") )
#if CAPS_LIB_REL_REV >= 501
		|| File_DoesFileExtensionMatch(pszFileName,".raw" )
		|| ( bAllowGZ && File_DoesFileExtensionMatch(pszFileName,".raw.gz") )
		|| File_DoesFileExtensionMatch(pszFileName,".ctr" )
		|| ( bAllowGZ && File_DoesFileExtensionMatch(pszFileName,".ctr.gz") )
#endif
		);
}
Exemplo n.º 4
0
/**
 * Does filename end with a .ST extension? If so, return true.
 */
bool ST_FileNameIsST(const char *pszFileName, bool bAllowGZ)
{
	return(File_DoesFileExtensionMatch(pszFileName,".st")
	       || (bAllowGZ && File_DoesFileExtensionMatch(pszFileName,".st.gz")));
}
Exemplo n.º 5
0
Arquivo: msa.c Projeto: denizt/hatari
/**
 * Does filename end with a .MSA extension? If so, return true
 */
bool MSA_FileNameIsMSA(const char *pszFileName, bool bAllowGZ)
{
	return(File_DoesFileExtensionMatch(pszFileName,".msa")
	       || (bAllowGZ && File_DoesFileExtensionMatch(pszFileName,".msa.gz")));
}
Exemplo n.º 6
0
/**
 * Does filename end with a .ZIP extension? If so, return true.
 */
bool ZIP_FileNameIsZIP(const char *pszFileName)
{
	return File_DoesFileExtensionMatch(pszFileName,".zip");
}
Exemplo n.º 7
0
/**
 * Does filename end with a .DIM extension? If so, return TRUE
 */
bool DIM_FileNameIsDIM(const char *pszFileName, bool bAllowGZ)
{
	return(File_DoesFileExtensionMatch(pszFileName,".dim")
	       || (bAllowGZ && File_DoesFileExtensionMatch(pszFileName,".dim.gz")));
}
Exemplo n.º 8
0
/**
 * 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 */
}
Exemplo n.º 9
0
/**
 * Show and process the floppy disk image dialog.
 */
void DlgFloppy_Main(void)
{
	int but, i;
	char *newdisk;
	char dlgname[MAX_FLOPPYDRIVES][64], dlgdiskdir[64];

	SDLGui_CenterDlg(floppydlg);

	/* Set up dialog to actual values: */
 const char *name;

 floppydlg[FLOPPYDLG_ATTACH2FLIPLIST].state &= ~SG_SELECTED;

 name = file_system_get_disk_name(8); /* Filename */
 if (!name)dlgname[0][0] = '\0';
 else File_ShrinkName(dlgname[0], name,floppydlg[FLOPPYDLG_DISKA].w);
 floppydlg[FLOPPYDLG_DISKA].txt = dlgname[0];

 name = file_system_get_disk_name(9); /* Filename */
 if (!name)dlgname[1][0] = '\0';
 else File_ShrinkName(dlgname[1], name,floppydlg[FLOPPYDLG_DISKB].w);
 floppydlg[FLOPPYDLG_DISKB].txt = dlgname[1];

 name = file_system_get_disk_name(10); /* Filename */
 if (!name)dlgname[2][0] = '\0';
 else File_ShrinkName(dlgname[2], name,floppydlg[FLOPPYDLG_DISK2].w);
 floppydlg[FLOPPYDLG_DISK2].txt = dlgname[2];

 name = file_system_get_disk_name(11); /* Filename */
 if (!name)dlgname[3][0] = '\0';
 else File_ShrinkName(dlgname[3], name,floppydlg[FLOPPYDLG_DISK3].w);
 floppydlg[FLOPPYDLG_DISK3].txt = dlgname[3];

	/* Default image directory: */
	File_ShrinkName(dlgdiskdir,szDiskImageDirectory,
	                floppydlg[FLOPPYDLG_IMGDIR].w);
	floppydlg[FLOPPYDLG_IMGDIR].txt = dlgdiskdir;


	/* Draw and process the dialog */
	do
	{       
		but = SDLGui_DoDialog(floppydlg, NULL);
		switch (but)
		{
		 case FLOPPYDLG_EJECTA:                         /* Eject disk in drive A: */
			Floppy_SetDiskFileNameNone(0);
			dlgname[0][0] = '\0';
			file_system_detach_disk(GET_DRIVE(8));

			break;
		 case FLOPPYDLG_BROWSEA:                        /* Choose a new disk A: */
			DlgDisk_BrowseDisk(dlgname[0], 0, FLOPPYDLG_DISKA);

			if (strlen(szDiskFileName[0]) > 0){

			int drivetype;

			printf("load (%s)-",szDiskFileName[0]);
			resources_get_int_sprintf("Drive%iType", &drivetype, GET_DRIVE(8));
			printf("(Drive%iType)\n",drivetype);

			cartridge_detach_image(-1);
			tape_image_detach(1);
//			file_system_detach_disk(GET_DRIVE(8));

			if(File_DoesFileExtensionMatch(szDiskFileName[0],"CRT"))
				cartridge_attach_image(CARTRIDGE_CRT, szDiskFileName[0]);
			else {
//FIXME
/*
				if(File_DoesFileExtensionMatch(szDiskFileName[0],"D81") && drivetype!=1581)
						resources_set_int_sprintf("Drive%iType", 1581,  GET_DRIVE(8));
				else if (drivetype!=1542 && !File_DoesFileExtensionMatch(szDiskFileName[0],"D81"))
						resources_set_int_sprintf("Drive%iType", 1542,  GET_DRIVE(8));
*/			

				if (floppydlg[FLOPPYDLG_ATTACH2FLIPLIST].state & SG_SELECTED){
					file_system_detach_disk(GET_DRIVE(8));
					printf("Attach to flip list\n");
					file_system_attach_disk(8, szDiskFileName[0]);
					fliplist_add_image(8)	;
				}
				else {
					printf("autostart\n");
					autostart_autodetect(szDiskFileName[0], NULL, 0, AUTOSTART_MODE_RUN);
				}

			}

			}

			break;
		 case FLOPPYDLG_EJECTB:                         /* Eject disk in drive B: */
			Floppy_SetDiskFileNameNone(1);
			dlgname[1][0] = '\0';
			file_system_detach_disk(GET_DRIVE(9));

			break;
		case FLOPPYDLG_BROWSEB:                         /* Choose a new disk B: */
			DlgDisk_BrowseDisk(dlgname[1], 1, FLOPPYDLG_DISKB);

			if (strlen(szDiskFileName[1]) > 0){
				
			file_system_detach_disk(GET_DRIVE(9));
     		file_system_attach_disk(9, szDiskFileName[1]);
	
			}

		 case FLOPPYDLG_EJECT2:                         /* Eject disk in drive A: */
			Floppy_SetDiskFileNameNone(2);
			dlgname[2][0] = '\0';
			file_system_detach_disk(GET_DRIVE(10));
			break;
		 case FLOPPYDLG_BROWSE2:                        /* Choose a new disk A: */
			DlgDisk_BrowseDisk(dlgname[2], 0, FLOPPYDLG_DISK2);

			if (strlen(szDiskFileName[2]) > 0){
					//strcpy(prefs->DrivePath[2], szDiskFileName[2]);
			}

			break;
		 case FLOPPYDLG_EJECT3:                         /* Eject disk in drive B: */
			Floppy_SetDiskFileNameNone(3);
			dlgname[3][0] = '\0';
			file_system_detach_disk(GET_DRIVE(11));
			break;
		case FLOPPYDLG_BROWSE3:                         /* Choose a new disk B: */
			DlgDisk_BrowseDisk(dlgname[3], 1, FLOPPYDLG_DISKB);

			if (strlen(szDiskFileName[3]) > 0){

//					strcpy(prefs->DrivePath[3], szDiskFileName[3]);
			}

			break;
		 case FLOPPYDLG_BROWSEIMG:
			DlgDisk_BrowseDir(dlgdiskdir,szDiskImageDirectory,floppydlg[FLOPPYDLG_IMGDIR].w);
			break;
/*
		 case FLOPPYDLG_CREATEIMG:
			newdisk = DlgNewDisk_Main();
			if (newdisk)
			{
				DlgFloppy_QueryInsert(dlgname[0], FLOPPYDLG_DISKA,
						      dlgname[1], FLOPPYDLG_DISKB,
						      newdisk);
				free(newdisk);
			}
			break;
*/
		}
                gui_poll_events();
	}
	while (but != FLOPPYDLG_EXIT && but != SDLGUI_QUIT
	        && but != SDLGUI_ERROR && !bQuitProgram);

/*
	if (floppydlg[FLOPPYDLG_AUTOSTART].state & SG_SELECTED){

			if(!ThePrefs.Emul1541Proc){
					prefs->Emul1541Proc = !prefs->Emul1541Proc;
			}
	}
	else {
			if(ThePrefs.Emul1541Proc){
					prefs->Emul1541Proc = !prefs->Emul1541Proc;
			}	

	}
*/

}