示例#1
0
void BCConvert(HWND dlg)
// performs the batch conversion on selected files
// TODO: progress dialogue, option to cancel process
//		see SHFileOperation for recycle bin ops, write seperate fn.
{
	int		count;
	int		i, j, iLength;
	char	inBuf[MAX_PATH];
	char	outBuf[MAX_PATH];
	char	statusBuf[MAX_PATH];
	char	FileRoot[MAX_PATH];
	char	FileSuf[4];
	char	fileName[MAX_PATH];
	HWND	fl = GetDlgItem(dlg, IDC_BCFILELIST);
	HWND	sl = GetDlgItem(dlg, IDC_BCSTATUS);
	
	int side,track,nbsect,image_size;
	HXCFE * hxcfe;
	HXCFE_FLOPPY* fp;
	HXCFE_SECTORACCESS* ss;
	HXCFE_IMGLDR * imgldr_ctx;
	int loaderId;
	unsigned char * floppybuffer;
	FILE * f;

	LRESULT	State_ADZ;
	LRESULT	State_ADF;
	LRESULT	State_HFE;

	HANDLE	hFile;
	BOOL	bUsingTemp = FALSE;		// TRUE if using temp directory for intermediate file.
	USHORT	dmsError;
//	char	dmsErrorMessage[MAX_PATH];
	BOOL	bOverwriting = TRUE;


	count = SendMessage(fl, LB_GETCOUNT, 0, 0l);
	
	for (i = 0 ; i < count ; i++) {
		SendMessage(fl, LB_GETTEXT, 0, (LPARAM)&inBuf);		// Get filename from list.

		UpdateWindow(dlg);

		strcpy(outBuf, inBuf);

		iLength = strlen(inBuf);						// Get name length.
		for(j = 0;j < iLength - 3;j++)					// Get name root.
			FileRoot[j] = inBuf[j];
		FileRoot[j] = '\0';

		FileSuf[0] = inBuf[iLength - 3];				// Get name suffix.
		FileSuf[1] = inBuf[iLength - 2];
		FileSuf[2] = inBuf[iLength - 1];
		FileSuf[3] = '\0';
		
		strcpy(outBuf, FileRoot);						// Set the outfile name root.
	
		// Get the check state of the output buttons.
		State_ADZ = SendMessage(GetDlgItem(dlg, IDC_BCADZ), BM_GETSTATE, 0, 0);
		State_ADF = SendMessage(GetDlgItem(dlg, IDC_BCADF), BM_GETSTATE, 0, 0);
		State_HFE = SendMessage(GetDlgItem(dlg, IDC_BCHFE), BM_GETSTATE, 0, 0);

		// if hfe.
		if(strcmp(FileSuf, "hfe") == 0 || strcmp(FileSuf, "HFE") == 0 ){
			// Print decompression status message.
			sprintf(statusBuf, "Unpacking file '%s'...", inBuf);	

			// If dms to adz, use temp directory.
			if(State_ADZ & BST_CHECKED){
				_splitpath(strOFNFileNames, NULL, NULL, fileName, NULL);	// Get filename.
				strcpy(outBuf, dirTemp);
				strcat(outBuf, fileName);
				strcat(outBuf, ".");
				bUsingTemp = TRUE;						// Using temp dir.
			}
			
			strcat(outBuf, "adf");						// Set the outfile name suffix.
			SendMessage(sl, LB_ADDSTRING, 0, (LPARAM)&statusBuf);

			// Check for file overwrite.
			hFile = CreateFile(outBuf, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
			// If file exists and we're not writing to temp dir, ask.
			if(hFile != INVALID_HANDLE_VALUE && !bUsingTemp){
				sprintf(statusBuf, "The file %s already exists.\n Do you want to overwrite this file?", outBuf);
				// Set bool if not overwriting.
				if(MessageBox(dlg, statusBuf, "ADF Opus Warning", MB_YESNO|MB_ICONEXCLAMATION) == IDNO){
					bOverwriting = FALSE;
				}
			}

			if(bOverwriting){
			// Overwrite.
				// Open HFE
				hxcfe =  hxcfe_init();
				imgldr_ctx = hxcfe_imgInitLoader(hxcfe);
				loaderId = hxcfe_imgGetLoaderID(imgldr_ctx,"HXC_HFE");
				fp = hxcfe_imgLoad(imgldr_ctx,(char*)inBuf,loaderId,0);
				if(fp)
				{
					image_size = hxcfe_getFloppySize(hxcfe,fp,0);

					if(image_size)
					{
						floppybuffer=(char*)malloc(image_size);

						nbsect=11;
						switch(image_size)
						{
							case 80*11*2*512:
								nbsect=11;
							break;
							case 80*22*2*512:
								nbsect=22;
							break;
						}

						ss = hxcfe_initSectorAccess(hxcfe,fp);

						for(track=0;track<hxcfe_getNumberOfTrack(hxcfe,fp);track++)
						{
							for(side=0;side<hxcfe_getNumberOfSide(hxcfe,fp);side++)
							{
								hxcfe_readSectorData(ss,track,side,0,nbsect,512,AMIGA_MFM_ENCODING,&floppybuffer[(512*nbsect)*((track*hxcfe_getNumberOfSide(hxcfe,fp))+side)],0);
							}
						}
							
						hxcfe_deinitSectorAccess(ss);

						hxcfe_imgUnload(imgldr_ctx,fp);

						hxcfe_imgDeInitLoader(imgldr_ctx);
						hxcfe_deinit(hxcfe);
							
						f=fopen(outBuf,"wb");
						if(f)
						{
							fwrite(floppybuffer,image_size,1,f);
							fclose(f);
						}
						
						free(floppybuffer);

						strcpy(statusBuf, "...file loaded successfully.");
					}

				}
				else
				{
					hxcfe_deinit(hxcfe);
					strcpy(statusBuf, "...error occured during HFE Loading.");
				}
			}
			else{
				// Abort
				strcpy(statusBuf, "...aborted to avoid overwrite.");
				SendMessage(sl, LB_ADDSTRING, 0, (LPARAM)&statusBuf);
			}
		}
		
		// if dms.				 
		if(strcmp(FileSuf, "dms") == 0 || strcmp(FileSuf, "DMS") == 0 ){
			// Print decompression status message.
			sprintf(statusBuf, "Unpacking file '%s'...", inBuf);	

			// If dms to adz, use temp directory.
			if(State_ADZ & BST_CHECKED){
				_splitpath(strOFNFileNames, NULL, NULL, fileName, NULL);	// Get filename.
				strcpy(outBuf, dirTemp);
				strcat(outBuf, fileName);
				strcat(outBuf, ".");
				bUsingTemp = TRUE;						// Using temp dir.
			}
			
			strcat(outBuf, "adf");						// Set the outfile name suffix.
			SendMessage(sl, LB_ADDSTRING, 0, (LPARAM)&statusBuf);

			// Check for file overwrite.
			hFile = CreateFile(outBuf, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
			// If file exists and we're not writing to temp dir, ask.
			if(hFile != INVALID_HANDLE_VALUE && !bUsingTemp){
				sprintf(statusBuf, "The file %s already exists.\n Do you want to overwrite this file?", outBuf);
				// Set bool if not overwriting.
				if(MessageBox(dlg, statusBuf, "ADF Opus Warning", MB_YESNO|MB_ICONEXCLAMATION) == IDNO){
					bOverwriting = FALSE;
				}
			}

			if(bOverwriting){
			// Overwrite.
				if(dmsError = dmsUnpack(inBuf, outBuf) == NO_PROBLEM)	// Decompress.
					strcpy(statusBuf, "...file unpacked successfully.");
				else
					strcpy(statusBuf, "...error occured during DMS decompression.");
			}
			else{
				// Abort
				strcpy(statusBuf, "...aborted to avoid overwrite.");

// ************************** TRYING TO ACCESS DMS ERRORS IN XDMS.C - WON'T COMPILE
//				dmsErrMsg(dmsError, inBuf, outBuf, dmsErrorMessage);
//				strcat(statusBuf, dmsErrorMessage);
				SendMessage(sl, LB_ADDSTRING, 0, (LPARAM)&statusBuf);
			}
		}
		

		//if adz and adf button selected.
		if((strcmp(FileSuf, "adz") == 0 || strcmp(FileSuf, "ADZ") == 0) && (State_ADF & BST_CHECKED) ){
			// Print decompression status message.
			sprintf(statusBuf, "Unpacking file '%s'...", inBuf);	
			strcat(outBuf, "adf");						// Set the outfile name suffix.
			SendMessage(sl, LB_ADDSTRING, 0, (LPARAM)&statusBuf);

			if(GZDecompress(dlg, inBuf, outBuf))
				strcpy(statusBuf, "...file unpacked successfully.");
			else
				strcpy(statusBuf, "...failed to unpack file.");
		}

		//if adf or decompressed dms and adz button selected.
		if(
			(   ( strcmp(FileSuf, "adf") == 0 || strcmp(FileSuf, "ADF") == 0 ) 
			||  ( strcmp(FileSuf, "dms") == 0 || strcmp(FileSuf, "DMS") == 0 ) ) 
			&&  (State_ADZ & BST_CHECKED) )
		{
			
			// If recompressing an adf'd dms, remove the suffixes and
			// write last status message.
			if(strcmp(FileSuf, "dms") == 0 || strcmp(FileSuf, "DMS") == 0){
				SendMessage(sl, LB_ADDSTRING, 0, (LPARAM)&statusBuf);	
				strcpy(inBuf, outBuf);
				strcpy(outBuf, FileRoot);
			}

			// Print compression status message.
			sprintf(statusBuf, "Compressing file '%s'...", inBuf);	

			strcat(outBuf, "adz");						// Set the outfile name suffix.
			SendMessage(sl, LB_ADDSTRING, 0, (LPARAM)&statusBuf);

			if(GZCompress(dlg, inBuf, outBuf))
				strcpy(statusBuf, "...file compressed successfully.");
			else
				strcpy(statusBuf, "...failed to compress file.");

			// Delete intermediate adf.
			if(strcmp(FileSuf, "dms") == 0 || strcmp(FileSuf, "DMS") == 0)
				remove(inBuf);
		}


		// hfe button selected.
		if(  ( ( strcmp(FileSuf, "adf") == 0 || strcmp(FileSuf, "ADF") == 0 )
			|| ( strcmp(FileSuf, "dms") == 0 || strcmp(FileSuf, "DMS") == 0 ) 
			|| ( strcmp(FileSuf, "adz") == 0 || strcmp(FileSuf, "ADZ") == 0 ) )
			&& (State_HFE & BST_CHECKED) ){
			
			// If recompressing an adf'd dms, remove the suffixes and
			// write last status message.
			if(strcmp(FileSuf, "dms") == 0 || strcmp(FileSuf, "DMS") == 0){
				SendMessage(sl, LB_ADDSTRING, 0, (LPARAM)&statusBuf);	
				strcpy(inBuf, outBuf);
				strcpy(outBuf, FileRoot);
			}

			// Print compression status message.
			sprintf(statusBuf, "Compressing file '%s'...", inBuf);	

			strcat(outBuf, "hfe");						// Set the outfile name suffix.
			SendMessage(sl, LB_ADDSTRING, 0, (LPARAM)&statusBuf);

			hxcfe=hxcfe_init();
			imgldr_ctx = hxcfe_imgInitLoader(hxcfe);

			fp=0;
			loaderId = hxcfe_imgAutoSetectLoader(imgldr_ctx,inBuf,0);
			// Load the image
			if(loaderId>=0)
				fp = hxcfe_imgLoad(imgldr_ctx,inBuf,loaderId,0);
			if(fp)
			{
				// Select the HFE loader/exporter.
				loaderId=hxcfe_imgGetLoaderID(imgldr_ctx,"HXC_HFE");
				// Save the file...
				hxcfe_imgExport(imgldr_ctx,fp,outBuf,loaderId);
				// Free the loaded image
				hxcfe_imgUnload(imgldr_ctx,fp);
				strcpy(statusBuf, "...file compressed successfully.");
			}
			else
			{
				strcpy(statusBuf, "...failed to compress file.");
			}

			hxcfe_imgDeInitLoader(imgldr_ctx);
			hxcfe_deinit(hxcfe);

			// Delete intermediate adf.
			if(strcmp(FileSuf, "dms") == 0 || strcmp(FileSuf, "DMS") == 0)
				remove(inBuf);
		}
		//no dms compression at this stage.

		SendMessage(sl, LB_ADDSTRING, 0, (LPARAM)&statusBuf);		// Write final status message.
		SendMessage(fl, LB_DELETESTRING, 0, 0l);					// Delete file from lister. 
		bUsingTemp = FALSE;											// Reset temp dir flag.

		if(SendDlgItemMessage(dlg, IDC_BCDELETE_ORIGINAL, BM_GETCHECK, 0, 0L) == BST_CHECKED){
		//Delete the original file.
			remove(inBuf);
			sprintf(statusBuf, "Deleted %s.", inBuf);
			SendMessage(sl, LB_ADDSTRING, 0, (LPARAM)&statusBuf);	// Write deletion message.
		}
	}
	// Write a divider line to improve readability.
	SendMessage(sl, LB_ADDSTRING, 0, 
		(LPARAM)"----------------------------------------------------------------------------------------------");

	// Re-enable output buttons once operations are complete.
	SendMessage(GetDlgItem(dlg, IDC_BCADF), BM_SETCHECK, BST_CHECKED, 0);
	SendMessage(GetDlgItem(dlg, IDC_BCADZ), BM_SETCHECK, BST_UNCHECKED, 0);
	SendMessage(GetDlgItem(dlg, IDC_BCHFE), BM_SETCHECK, BST_UNCHECKED, 0);
	EnableWindow(GetDlgItem(dlg, IDC_BCADF), TRUE);
	EnableWindow(GetDlgItem(dlg, IDC_BCADZ), TRUE);
	EnableWindow(GetDlgItem(dlg, IDC_BCHFE), TRUE);

	BCUpdateButtons(dlg);						// Dis/enable buttons.
}
int TI99V9T9_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy,char * filename)
{
	int i,j,k;
	FILE * ti99v9t9file;
	int32_t nbsector,imagesize;

	int32_t numberofsector,numberofside,numberoftrack;
	int32_t density = ISOIBM_FM_ENCODING;
	int file_offset;
	int32_t sectorsize = 256;
	unsigned char * diskimage;
	int error = 0;
	HXCFE_SECTORACCESS* ss;
	HXCFE_SECTCFG* sc;

	imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"Write TI99 V9T9 file %s...",filename);

	imagesize=hxcfe_getFloppySize(imgldr_ctx->hxcfe,floppy,&nbsector);

	imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"Disk size : %d Bytes %d Sectors",imagesize,nbsector);

	ss = hxcfe_initSectorAccess(imgldr_ctx->hxcfe, floppy);
	if (ss)
	{
		sc = hxcfe_searchSector(ss, 0, 0, 0, density);
		if (!sc)
		{
			density = ISOIBM_MFM_ENCODING;
			sc = hxcfe_searchSector(ss, 0, 0, 0, density);
			if (!sc)
			{
				imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR, "This disk is neither FM nor MFM.  Exiting.");
				return HXCFE_FILECORRUPTED;
			}
		}

		// sc->input_data should contain the disk geometry

		numberofside = sc->input_data[0x12];
		numberofsector = sc->input_data[0x0c];
		numberoftrack = sc->input_data[0x11];

		if ( (numberofside < 1) && (numberofside > 2))
		{
			imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR, "Image claims it has %i sides, which is clearly wrong.  Exiting.", numberofside);
			return HXCFE_FILECORRUPTED;
		}
 
		if ( (numberoftrack != 40) && (numberoftrack != 80))
		{
			imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR, "Image claims each side has %i tracks, which is clearly wrong.  Exiting.", numberoftrack);
			return HXCFE_FILECORRUPTED;
		}
 
		if ( (numberofsector != 9) && (numberofsector != 16) && (numberofsector != 18) && (numberofsector != 36))
		{
			imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR, "Image claims each track has %i sectors, which is clearly wrong.  Exiting.", numberofsector);
			return HXCFE_FILECORRUPTED;
		}

		if ( (numberofsector * numberoftrack * numberofside) != nbsector )
		{
			imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR, "Disk geometry %i/%i/%i does not match disk length of %i sectors.  Exiting.", numberofside, numberoftrack, numberofsector, nbsector);
			return HXCFE_FILECORRUPTED;
		}


 
		imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1, "Disk geometry is %i sides, %i tracks per side, %i sectors per track.", numberofside, numberoftrack, numberofsector);

		imagesize = numberofsector * numberoftrack * numberofside * sectorsize;
		diskimage = malloc(imagesize);
		if (!diskimage)
			return HXCFE_INTERNALERROR;
		memset(diskimage, 0xF6, imagesize);

		for(i=0;i<numberofside;i++)
		{
			for(j=0;j<numberoftrack;j++)
			{
				hxcfe_imgCallProgressCallback(imgldr_ctx, j + (i*numberoftrack),numberofside*numberoftrack);

				for(k=0;k<numberofsector;k++)
				{
					sc = hxcfe_searchSector(ss,j,i,k,density);
					if(sc)
					{
						if(sc->use_alternate_data_crc)
							imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Warning : Bad Data CRC : T:%d H:%d S:%d Size :%dB",j,i,k,sc->sectorsize);

						if(sc->sectorsize == sectorsize)
						{
							if(i==0)
								file_offset=(j*numberofsector)*sectorsize + ( k * sectorsize );
							else
								file_offset=(  numberoftrack      *numberofsector*sectorsize) +
												(((numberoftrack-1)-j)*numberofsector*sectorsize) +
												( k * sectorsize );
							memcpy(&diskimage[file_offset], sc->input_data, sectorsize);
						} else {
							error++;
							imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Bad Sector Size : T:%d H:%d S:%d Size :%dB, Should be %dB",j,i,k,sc->sectorsize,sectorsize);
						}

						hxcfe_freeSectorConfig(ss,sc);
					} else {
						imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Sector not found : T:%d H:%d S:%d",j,i,k);
					}
				}
			}
		}

		if(!error)
		{
			ti99v9t9file=hxc_fopen(filename,"wb");
			if(ti99v9t9file)
			{
				fwrite(diskimage,imagesize,1,ti99v9t9file);
				hxc_fclose(ti99v9t9file);
			} else {
				free(diskimage);
				hxcfe_deinitSectorAccess(ss);
				return HXCFE_ACCESSERROR;
			}
		}
	}

	free(diskimage);
	hxcfe_deinitSectorAccess(ss);

	if(!error)
		return HXCFE_NOERROR;
	else
	{
		imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Errors were found in this disk image.");
		return HXCFE_FILECORRUPTED;
	}
}