Refactor: Get rid of HIMAGE handle for disk images

This commit is contained in:
tomcw 2016-10-25 21:09:48 +01:00
parent d7937dc58c
commit 31483b33b9
5 changed files with 58 additions and 73 deletions

View File

@ -143,8 +143,6 @@ enum AppMode_e
// TODO-TC: Refactor codebase by renaming /nCyclesLeft/ to /uExecutedCycles/
typedef BYTE (__stdcall *iofunction)(WORD nPC, WORD nAddr, BYTE nWriteFlag, BYTE nWriteValue, ULONG nCyclesLeft);
typedef struct _IMAGE__ { int unused; } *HIMAGE; // DiskImage's /ImageInfo/ is hidden behind HIMAGE
enum eIRQSRC {IS_6522=0, IS_SPEECH, IS_SSC, IS_MOUSE};
//

View File

@ -76,7 +76,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
TCHAR imagename[ MAX_DISK_IMAGE_NAME + 1 ]; // <FILENAME> (ie. no extension)
TCHAR fullname [ MAX_DISK_FULL_NAME + 1 ]; // <FILENAME.EXT> or <FILENAME.zip> : This is persisted to the snapshot file
std::string strFilenameInZip; // "" or <FILENAME.EXT>
HIMAGE imagehandle; // Init'd by DiskInsert() -> ImageOpen()
ImageInfo* imagehandle; // Init'd by DiskInsert() -> ImageOpen()
bool bWriteProtected;
//
int track;
@ -327,7 +327,7 @@ static void RemoveDisk(const int iDrive)
WriteTrack( iDrive);
ImageClose(pFloppy->imagehandle);
pFloppy->imagehandle = (HIMAGE)0;
pFloppy->imagehandle = NULL;
}
if (pFloppy->trackimage)

View File

@ -27,7 +27,6 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "StdAfx.h"
#include "Common.h"
#include "DiskImage.h"
#include "DiskImageHelper.h"
@ -40,23 +39,23 @@ static CHardDiskImageHelper sg_HardDiskImageHelper;
// Pre: *pWriteProtected_ already set to file's r/w status - see DiskInsert()
ImageError_e ImageOpen( LPCTSTR pszImageFilename,
HIMAGE* hDiskImage,
ImageInfo** ppImageInfo,
bool* pWriteProtected,
const bool bCreateIfNecessary,
std::string& strFilenameInZip,
const bool bExpectFloppy /*=true*/,
const bool bIsHarddisk /*=false*/)
{
if (! (pszImageFilename && hDiskImage && pWriteProtected && sg_DiskImageHelper.GetWorkBuffer()))
if (! (pszImageFilename && ppImageInfo && pWriteProtected && sg_DiskImageHelper.GetWorkBuffer()))
return eIMAGE_ERROR_BAD_POINTER;
// CREATE A RECORD FOR THE FILE, AND RETURN AN IMAGE HANDLE
*hDiskImage = (HIMAGE) VirtualAlloc(NULL, sizeof(ImageInfo), MEM_COMMIT, PAGE_READWRITE);
if (*hDiskImage == NULL)
// CREATE A RECORD FOR THE FILE
*ppImageInfo = (ImageInfo*) VirtualAlloc(NULL, sizeof(ImageInfo), MEM_COMMIT, PAGE_READWRITE);
if (*ppImageInfo == NULL)
return eIMAGE_ERROR_BAD_POINTER;
ZeroMemory(*hDiskImage, sizeof(ImageInfo));
ImageInfo* pImageInfo = (ImageInfo*) *hDiskImage;
ZeroMemory(*ppImageInfo, sizeof(ImageInfo));
ImageInfo* pImageInfo = *ppImageInfo;
pImageInfo->bWriteProtected = *pWriteProtected;
ImageError_e Err = (!bIsHarddisk)
@ -65,8 +64,8 @@ ImageError_e ImageOpen( LPCTSTR pszImageFilename,
if (Err != eIMAGE_ERROR_NONE)
{
ImageClose(*hDiskImage, true);
*hDiskImage = (HIMAGE)0;
ImageClose(*ppImageInfo, true);
*ppImageInfo = NULL;
return Err;
}
@ -91,16 +90,15 @@ ImageError_e ImageOpen( LPCTSTR pszImageFilename,
//===========================================================================
void ImageClose(const HIMAGE hDiskImage, const bool bOpenError /*=false*/)
void ImageClose(ImageInfo* const pImageInfo, const bool bOpenError /*=false*/)
{
ImageInfo* ptr = (ImageInfo*) hDiskImage;
bool bDeleteFile = false;
if (!bOpenError)
{
for (UINT uTrack = 0; uTrack < ptr->uNumTracks; uTrack++)
for (UINT uTrack = 0; uTrack < pImageInfo->uNumTracks; uTrack++)
{
if (!ptr->ValidTrack[uTrack])
if (!pImageInfo->ValidTrack[uTrack])
{
// TODO: Comment using info from this URL:
// http://groups.google.de/group/comp.emulators.apple2/msg/7a1b9317e7905152
@ -110,23 +108,22 @@ void ImageClose(const HIMAGE hDiskImage, const bool bOpenError /*=false*/)
}
}
sg_DiskImageHelper.Close(ptr, bDeleteFile);
sg_DiskImageHelper.Close(pImageInfo, bDeleteFile);
VirtualFree(ptr, 0, MEM_RELEASE);
VirtualFree(pImageInfo, 0, MEM_RELEASE);
}
//===========================================================================
BOOL ImageBoot(const HIMAGE hDiskImage)
BOOL ImageBoot(ImageInfo* const pImageInfo)
{
ImageInfo* ptr = (ImageInfo*) hDiskImage;
BOOL result = 0;
if (ptr->pImageType->AllowBoot())
result = ptr->pImageType->Boot(ptr);
if (pImageInfo->pImageType->AllowBoot())
result = pImageInfo->pImageType->Boot(pImageInfo);
if (result)
ptr->bWriteProtected = 1;
pImageInfo->bWriteProtected = 1;
return result;
}
@ -149,7 +146,7 @@ void ImageInitialize(void)
//===========================================================================
void ImageReadTrack( const HIMAGE hDiskImage,
void ImageReadTrack( ImageInfo* const pImageInfo,
const int nTrack,
const int nQuarterTrack,
LPBYTE pTrackImageBuffer,
@ -159,10 +156,9 @@ void ImageReadTrack( const HIMAGE hDiskImage,
if (nTrack < 0)
return;
ImageInfo* ptr = (ImageInfo*) hDiskImage;
if (ptr->pImageType->AllowRW() && ptr->ValidTrack[nTrack])
if (pImageInfo->pImageType->AllowRW() && pImageInfo->ValidTrack[nTrack])
{
ptr->pImageType->Read(ptr, nTrack, nQuarterTrack, pTrackImageBuffer, pNibbles);
pImageInfo->pImageType->Read(pImageInfo, nTrack, nQuarterTrack, pTrackImageBuffer, pNibbles);
}
else
{
@ -173,7 +169,7 @@ void ImageReadTrack( const HIMAGE hDiskImage,
//===========================================================================
void ImageWriteTrack( const HIMAGE hDiskImage,
void ImageWriteTrack( ImageInfo* const pImageInfo,
const int nTrack,
const int nQuarterTrack,
LPBYTE pTrackImage,
@ -183,75 +179,65 @@ void ImageWriteTrack( const HIMAGE hDiskImage,
if (nTrack < 0)
return;
ImageInfo* ptr = (ImageInfo*) hDiskImage;
if (ptr->pImageType->AllowRW() && !ptr->bWriteProtected)
if (pImageInfo->pImageType->AllowRW() && !pImageInfo->bWriteProtected)
{
ptr->pImageType->Write(ptr, nTrack, nQuarterTrack, pTrackImage, nNibbles);
ptr->ValidTrack[nTrack] = 1;
pImageInfo->pImageType->Write(pImageInfo, nTrack, nQuarterTrack, pTrackImage, nNibbles);
pImageInfo->ValidTrack[nTrack] = 1;
}
}
//===========================================================================
bool ImageReadBlock( const HIMAGE hDiskImage,
bool ImageReadBlock( ImageInfo* const pImageInfo,
UINT nBlock,
LPBYTE pBlockBuffer)
{
ImageInfo* ptr = (ImageInfo*) hDiskImage;
bool bRes = false;
if (ptr->pImageType->AllowRW())
bRes = ptr->pImageType->Read(ptr, nBlock, pBlockBuffer);
if (pImageInfo->pImageType->AllowRW())
bRes = pImageInfo->pImageType->Read(pImageInfo, nBlock, pBlockBuffer);
return bRes;
}
//===========================================================================
bool ImageWriteBlock( const HIMAGE hDiskImage,
bool ImageWriteBlock( ImageInfo* const pImageInfo,
UINT nBlock,
LPBYTE pBlockBuffer)
{
ImageInfo* ptr = (ImageInfo*) hDiskImage;
bool bRes = false;
if (ptr->pImageType->AllowRW() && !ptr->bWriteProtected)
bRes = ptr->pImageType->Write(ptr, nBlock, pBlockBuffer);
if (pImageInfo->pImageType->AllowRW() && !pImageInfo->bWriteProtected)
bRes = pImageInfo->pImageType->Write(pImageInfo, nBlock, pBlockBuffer);
return bRes;
}
//===========================================================================
int ImageGetNumTracks(const HIMAGE hDiskImage)
int ImageGetNumTracks(ImageInfo* const pImageInfo)
{
ImageInfo* ptr = (ImageInfo*) hDiskImage;
return ptr ? ptr->uNumTracks : 0;
return pImageInfo ? pImageInfo->uNumTracks : 0;
}
bool ImageIsWriteProtected(const HIMAGE hDiskImage)
bool ImageIsWriteProtected(ImageInfo* const pImageInfo)
{
ImageInfo* ptr = (ImageInfo*) hDiskImage;
return ptr ? ptr->bWriteProtected : true;
return pImageInfo ? pImageInfo->bWriteProtected : true;
}
bool ImageIsMultiFileZip(const HIMAGE hDiskImage)
bool ImageIsMultiFileZip(ImageInfo* const pImageInfo)
{
ImageInfo* ptr = (ImageInfo*) hDiskImage;
return ptr ? (ptr->uNumEntriesInZip > 1) : false;
return pImageInfo ? (pImageInfo->uNumEntriesInZip > 1) : false;
}
const char* ImageGetPathname(const HIMAGE hDiskImage)
const char* ImageGetPathname(ImageInfo* const pImageInfo)
{
static char* szEmpty = "";
ImageInfo* ptr = (ImageInfo*) hDiskImage;
return ptr ? ptr->szFilename : szEmpty;
return pImageInfo ? pImageInfo->szFilename : szEmpty;
}
UINT ImageGetImageSize(const HIMAGE hDiskImage)
UINT ImageGetImageSize(ImageInfo* const pImageInfo)
{
ImageInfo* ptr = (ImageInfo*) hDiskImage;
return ptr ? ptr->uImageSize : 0;
return pImageInfo ? pImageInfo->uImageSize : 0;
}
void GetImageTitle(LPCTSTR pPathname, TCHAR* pImageName, TCHAR* pFullName)

View File

@ -61,22 +61,23 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
const int MAX_DISK_IMAGE_NAME = 15;
const int MAX_DISK_FULL_NAME = 127;
struct ImageInfo;
ImageError_e ImageOpen(LPCTSTR pszImageFilename, HIMAGE* hDiskImage, bool* pWriteProtected, const bool bCreateIfNecessary, std::string& strFilenameInZip, const bool bExpectFloppy=true, const bool bIsHarddisk=false);
void ImageClose(const HIMAGE hDiskImage, const bool bOpenError=false);
BOOL ImageBoot(const HIMAGE hDiskImage);
ImageError_e ImageOpen(LPCTSTR pszImageFilename, ImageInfo** ppImageInfo, bool* pWriteProtected, const bool bCreateIfNecessary, std::string& strFilenameInZip, const bool bExpectFloppy=true, const bool bIsHarddisk=false);
void ImageClose(ImageInfo* const pImageInfo, const bool bOpenError=false);
BOOL ImageBoot(ImageInfo* const pImageInfo);
void ImageDestroy(void);
void ImageInitialize(void);
void ImageReadTrack(const HIMAGE hDiskImage, int nTrack, int nQuarterTrack, LPBYTE pTrackImageBuffer, int* pNibbles);
void ImageWriteTrack(const HIMAGE hDiskImage, int nTrack, int nQuarterTrack, LPBYTE pTrackImage, int nNibbles);
bool ImageReadBlock(const HIMAGE hDiskImage, UINT nBlock, LPBYTE pBlockBuffer);
bool ImageWriteBlock(const HIMAGE hDiskImage, UINT nBlock, LPBYTE pBlockBuffer);
void ImageReadTrack(ImageInfo* const pImageInfo, int nTrack, int nQuarterTrack, LPBYTE pTrackImageBuffer, int* pNibbles);
void ImageWriteTrack(ImageInfo* const pImageInfo, int nTrack, int nQuarterTrack, LPBYTE pTrackImage, int nNibbles);
bool ImageReadBlock(ImageInfo* const pImageInfo, UINT nBlock, LPBYTE pBlockBuffer);
bool ImageWriteBlock(ImageInfo* const pImageInfo, UINT nBlock, LPBYTE pBlockBuffer);
int ImageGetNumTracks(const HIMAGE hDiskImage);
bool ImageIsWriteProtected(const HIMAGE hDiskImage);
bool ImageIsMultiFileZip(const HIMAGE hDiskImage);
const char* ImageGetPathname(const HIMAGE hDiskImage);
UINT ImageGetImageSize(const HIMAGE hDiskImage);
int ImageGetNumTracks(ImageInfo* const pImageInfo);
bool ImageIsWriteProtected(ImageInfo* const pImageInfo);
bool ImageIsMultiFileZip(ImageInfo* const pImageInfo);
const char* ImageGetPathname(ImageInfo* const pImageInfo);
UINT ImageGetImageSize(ImageInfo* const pImageInfo);
void GetImageTitle(LPCTSTR pPathname, TCHAR* pImageName, TCHAR* pFullName);

View File

@ -118,7 +118,7 @@ struct HDD
TCHAR imagename[ MAX_DISK_IMAGE_NAME + 1 ]; // <FILENAME> (ie. no extension) [not used]
TCHAR fullname[ MAX_DISK_FULL_NAME + 1 ]; // <FILENAME.EXT> or <FILENAME.zip>
std::string strFilenameInZip; // "" or <FILENAME.EXT> [not used]
HIMAGE imagehandle; // Init'd by HD_Insert() -> ImageOpen()
ImageInfo* imagehandle; // Init'd by HD_Insert() -> ImageOpen()
bool bWriteProtected; // Needed for ImageOpen() [otherwise not used]
//
BYTE hd_error;
@ -157,7 +157,7 @@ static void HD_CleanupDrive(const int iDrive)
if (g_HardDisk[iDrive].imagehandle)
{
ImageClose(g_HardDisk[iDrive].imagehandle);
g_HardDisk[iDrive].imagehandle = (HIMAGE)0;
g_HardDisk[iDrive].imagehandle = NULL;
}
g_HardDisk[iDrive].hd_imageloaded = false;