From 2759527f0dda74e0ad55133863f140d26a3391cc Mon Sep 17 00:00:00 2001 From: Andrea Date: Sat, 17 Jun 2023 20:41:48 +0100 Subject: [PATCH] Remove some StringCb* functions and fix assertion. (PR #1239) . Return an error for an invalid .bin image. --- source/Disk.cpp | 20 ++++++++++---------- source/Disk.h | 2 +- source/DiskImage.cpp | 3 ++- source/Harddisk.cpp | 21 ++++++++++----------- source/Harddisk.h | 2 +- 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/source/Disk.cpp b/source/Disk.cpp index eaab520c..84ab0c7c 100644 --- a/source/Disk.cpp +++ b/source/Disk.cpp @@ -243,13 +243,11 @@ void Disk2InterfaceCard::SaveLastDiskImage(const int drive) if (m_slot != SLOT6 || drive != DRIVE_1) return; - TCHAR szPathName[MAX_PATH]; - StringCbCopy(szPathName, MAX_PATH, pathName.c_str()); - TCHAR* slash = _tcsrchr(szPathName, PATH_SEPARATOR); - if (slash != NULL) + const size_t slash = pathName.find_last_of(PATH_SEPARATOR); + if (slash != std::string::npos) { - slash[1] = '\0'; - RegSaveString(REG_PREFS, REGVALUE_PREF_START_DIR, 1, szPathName); + const std::string dirName = pathName.substr(0, slash + 1); + RegSaveString(REG_PREFS, REGVALUE_PREF_START_DIR, 1, dirName); } } @@ -887,9 +885,10 @@ bool Disk2InterfaceCard::IsConditionForFullSpeed(void) //=========================================================================== -void Disk2InterfaceCard::NotifyInvalidImage(const int drive, LPCTSTR pszImageFilename, const ImageError_e Error) +void Disk2InterfaceCard::NotifyInvalidImage(const int drive, const std::string & szImageFilename, const ImageError_e Error) { std::string strText; + const char * pszImageFilename = szImageFilename.c_str(); switch (Error) { @@ -1842,17 +1841,18 @@ bool Disk2InterfaceCard::UserSelectNewDiskImage(const int drive, LPCSTR pszFilen if (GetOpenFileName(&ofn)) { + std::string openFilename = filename; if ((!ofn.nFileExtension) || !filename[ofn.nFileExtension]) - StringCbCat(filename, MAX_PATH, TEXT(".dsk")); + openFilename += TEXT(".dsk"); - ImageError_e Error = InsertDisk(drive, filename, ofn.Flags & OFN_READONLY, IMAGE_CREATE); + ImageError_e Error = InsertDisk(drive, openFilename, ofn.Flags & OFN_READONLY, IMAGE_CREATE); if (Error == eIMAGE_ERROR_NONE) { bRes = true; } else { - NotifyInvalidImage(drive, filename, Error); + NotifyInvalidImage(drive, openFilename, Error); } } diff --git a/source/Disk.h b/source/Disk.h index de959b69..bc08d218 100644 --- a/source/Disk.h +++ b/source/Disk.h @@ -159,7 +159,7 @@ public: void UnplugDrive(const int drive); bool IsConditionForFullSpeed(void); - void NotifyInvalidImage(const int drive, LPCTSTR pszImageFilename, const ImageError_e Error); + void NotifyInvalidImage(const int drive, const std::string & szImageFilename, const ImageError_e Error); UINT GetCurrentBitOffset(void); UINT GetCurrentFirmware(void) { return m_is13SectorFirmware ? 13 : 16; } diff --git a/source/DiskImage.cpp b/source/DiskImage.cpp index 1f2f48ee..7851f53b 100644 --- a/source/DiskImage.cpp +++ b/source/DiskImage.cpp @@ -78,7 +78,8 @@ ImageError_e ImageOpen( const std::string & pszImageFilename, if (!bExpectFloppy) return eIMAGE_ERROR_UNSUPPORTED; - _ASSERT(pImageInfo->uNumTracks); + if (!pImageInfo->uNumTracks) + return eIMAGE_ERROR_UNSUPPORTED; *pWriteProtected = pImageInfo->bWriteProtected; diff --git a/source/Harddisk.cpp b/source/Harddisk.cpp index 0869673c..a076ade3 100644 --- a/source/Harddisk.cpp +++ b/source/Harddisk.cpp @@ -197,12 +197,12 @@ void HarddiskInterfaceCard::CleanupDrive(const int iDrive) //=========================================================================== -void HarddiskInterfaceCard::NotifyInvalidImage(TCHAR* pszImageFilename) +void HarddiskInterfaceCard::NotifyInvalidImage(const std::string & szImageFilename) { // TC: TO DO - see Disk2InterfaceCard::NotifyInvalidImage() std::string strText = StrFormat("Unable to open the file %s.", - pszImageFilename); + szImageFilename.c_str()); GetFrame().FrameMessageBox(strText.c_str(), g_pAppTitle.c_str(), @@ -263,13 +263,11 @@ void HarddiskInterfaceCard::SaveLastDiskImage(const int drive) if (m_slot != SLOT7 || drive != HARDDISK_1) return; - TCHAR szPathName[MAX_PATH]; - StringCbCopy(szPathName, MAX_PATH, pathName.c_str()); - TCHAR* slash = _tcsrchr(szPathName, PATH_SEPARATOR); - if (slash != NULL) + const size_t slash = pathName.find_last_of(PATH_SEPARATOR); + if (slash != std::string::npos) { - slash[1] = '\0'; - RegSaveString(REG_PREFS, REGVALUE_PREF_HDV_START_DIR, 1, szPathName); + const std::string dirName = pathName.substr(0, slash + 1); + RegSaveString(REG_PREFS, REGVALUE_PREF_HDV_START_DIR, 1, dirName); } } @@ -410,16 +408,17 @@ bool HarddiskInterfaceCard::SelectImage(const int drive, LPCSTR pszFilename) if (GetOpenFileName(&ofn)) { + std::string openFilename = filename; if ((!ofn.nFileExtension) || !filename[ofn.nFileExtension]) - StringCbCat(filename, MAX_PATH, TEXT(".hdv")); + openFilename += TEXT(".hdv"); - if (Insert(drive, filename)) + if (Insert(drive, openFilename)) { bRes = true; } else { - NotifyInvalidImage(filename); + NotifyInvalidImage(openFilename); } } diff --git a/source/Harddisk.h b/source/Harddisk.h index 559d2640..cca84e2e 100644 --- a/source/Harddisk.h +++ b/source/Harddisk.h @@ -112,7 +112,7 @@ public: private: void CleanupDriveInternal(const int iDrive); void CleanupDrive(const int iDrive); - void NotifyInvalidImage(TCHAR* pszImageFilename); + void NotifyInvalidImage(const std::string & szImageFilename); void SaveLastDiskImage(const int drive); const std::string& DiskGetBaseName(const int iDrive); bool SelectImage(const int drive, LPCSTR pszFilename);