From bd201202cd432da69f8732b6beaa67fbc80a3c03 Mon Sep 17 00:00:00 2001 From: Andrea Odetti Date: Sat, 7 Sep 2019 19:37:19 +0100 Subject: [PATCH] Some more std::string changes. Signed-off-by: Andrea Odetti --- source/Applewin.cpp | 8 ++-- source/Configuration/PageAdvanced.cpp | 4 +- source/Configuration/PageDisk.cpp | 6 +-- source/Configuration/PropertySheetHelper.cpp | 44 ++++++++++---------- source/Configuration/PropertySheetHelper.h | 12 +++--- source/Disk.cpp | 2 +- source/Harddisk.cpp | 4 +- source/ParallelPrinter.cpp | 2 +- source/Registry.cpp | 6 +-- source/Registry.h | 2 +- source/SaveState.cpp | 2 +- source/SaveState.h | 2 +- source/SerialComms.cpp | 18 ++++---- source/SerialComms.h | 4 +- 14 files changed, 60 insertions(+), 56 deletions(-) diff --git a/source/Applewin.cpp b/source/Applewin.cpp index 08186357..14021084 100644 --- a/source/Applewin.cpp +++ b/source/Applewin.cpp @@ -81,8 +81,8 @@ AppMode_e g_nAppMode = MODE_LOGO; static bool g_bLoadedSaveState = false; std::string g_sProgramDir; // Directory of where AppleWin executable resides -TCHAR g_sDebugDir [MAX_PATH] = TEXT(""); // TODO: Not currently used -TCHAR g_sScreenShotDir[MAX_PATH] = TEXT(""); // TODO: Not currently used +std::string g_sDebugDir; // TODO: Not currently used +std::string g_sScreenShotDir; // TODO: Not currently used bool g_bCapturePrintScreenKey = true; static bool g_bHookSystemKey = true; static bool g_bHookAltTab = false; @@ -1689,8 +1689,8 @@ int APIENTRY WinMain(HINSTANCE passinstance, HINSTANCE, LPSTR lpCmdLine, int) int nIdx = strPathname.find_last_of('\\'); if (nIdx >= 0 && nIdx+1 < (int)strPathname.length()) { - std::string strPath = strPathname.substr(0, nIdx+1); - SetCurrentImageDir(strPath.c_str()); + const std::string strPath = strPathname.substr(0, nIdx+1); + SetCurrentImageDir(strPath); } // Override value just loaded from Registry by LoadConfiguration() diff --git a/source/Configuration/PageAdvanced.cpp b/source/Configuration/PageAdvanced.cpp index 7e3d1ff8..81b89315 100644 --- a/source/Configuration/PageAdvanced.cpp +++ b/source/Configuration/PageAdvanced.cpp @@ -85,7 +85,7 @@ BOOL CPageAdvanced::DlgProcInternal(HWND hWnd, UINT message, WPARAM wparam, LPAR break; case IDC_SAVESTATE_BROWSE: if(m_PropertySheetHelper.SaveStateSelectImage(hWnd, TEXT("Select Save State file"), true)) - SendDlgItemMessage(hWnd, IDC_SAVESTATE_FILENAME, WM_SETTEXT, 0, (LPARAM)m_PropertySheetHelper.GetSSNewFilename()); + SendDlgItemMessage(hWnd, IDC_SAVESTATE_FILENAME, WM_SETTEXT, 0, (LPARAM)m_PropertySheetHelper.GetSSNewFilename().c_str()); break; case IDC_PRINTER_DUMP_FILENAME_BROWSE: { @@ -182,7 +182,7 @@ void CPageAdvanced::DlgOK(HWND hWnd) szFilename[nLineLength] = 0x00; Printer_SetFilename(szFilename); - RegSaveString(TEXT(REG_CONFIG), REGVALUE_PRINTER_FILENAME, 1, Printer_GetFilename().c_str()); + RegSaveString(TEXT(REG_CONFIG), REGVALUE_PRINTER_FILENAME, 1, Printer_GetFilename()); } g_bSaveStateOnExit = IsDlgButtonChecked(hWnd, IDC_SAVESTATE_ON_EXIT) ? true : false; diff --git a/source/Configuration/PageDisk.cpp b/source/Configuration/PageDisk.cpp index c483c8e0..b0f7b286 100644 --- a/source/Configuration/PageDisk.cpp +++ b/source/Configuration/PageDisk.cpp @@ -121,7 +121,7 @@ BOOL CPageDisk::DlgProcInternal(HWND hWnd, UINT message, WPARAM wparam, LPARAM l case IDC_CIDERPRESS_BROWSE: { std::string CiderPressLoc = m_PropertySheetHelper.BrowseToFile(hWnd, TEXT("Select path to CiderPress"), REGVALUE_CIDERPRESSLOC, TEXT("Applications (*.exe)\0*.exe\0") TEXT("All Files\0*.*\0") ); - RegSaveString(TEXT(REG_CONFIG), REGVALUE_CIDERPRESSLOC, 1, CiderPressLoc.c_str()); + RegSaveString(TEXT(REG_CONFIG), REGVALUE_CIDERPRESSLOC, 1, CiderPressLoc); SendDlgItemMessage(hWnd, IDC_CIDERPRESS_FILENAME, WM_SETTEXT, 0, (LPARAM) CiderPressLoc.c_str()); } break; @@ -199,8 +199,8 @@ void CPageDisk::DlgOK(HWND hWnd) m_PropertySheetHelper.GetConfigNew().m_bEnableHDD = bNewHDDIsEnabled; } - RegSaveString(TEXT(REG_PREFS), TEXT(REGVALUE_PREF_LAST_HARDDISK_1), 1, HD_GetFullPathName(HARDDISK_1).c_str()); - RegSaveString(TEXT(REG_PREFS), TEXT(REGVALUE_PREF_LAST_HARDDISK_2), 1, HD_GetFullPathName(HARDDISK_2).c_str()); + RegSaveString(TEXT(REG_PREFS), TEXT(REGVALUE_PREF_LAST_HARDDISK_1), 1, HD_GetFullPathName(HARDDISK_1)); + RegSaveString(TEXT(REG_PREFS), TEXT(REGVALUE_PREF_LAST_HARDDISK_2), 1, HD_GetFullPathName(HARDDISK_2)); m_PropertySheetHelper.PostMsgAfterClose(hWnd, m_Page); } diff --git a/source/Configuration/PropertySheetHelper.cpp b/source/Configuration/PropertySheetHelper.cpp index 5c6979f1..040d6b30 100644 --- a/source/Configuration/PropertySheetHelper.cpp +++ b/source/Configuration/PropertySheetHelper.cpp @@ -137,8 +137,8 @@ void CPropertySheetHelper::SetSlot5(SS_CARDTYPE NewCardType) // . CPageAdvanced: IDC_PRINTER_DUMP_FILENAME_BROWSE std::string CPropertySheetHelper::BrowseToFile(HWND hWindow, TCHAR* pszTitle, TCHAR* REGVALUE, TCHAR* FILEMASKS) { - static char PathToFile[MAX_PATH] = {0}; //This is a really awkward way to prevent mixing CiderPress and SaveStated values (RAPCS), but it seem the quickest. Here is its Line 1. - strcpy(PathToFile, Snapshot_GetFilename().c_str()); //RAPCS, line 2. + static std::string PathToFile; //This is a really awkward way to prevent mixing CiderPress and SaveStated values (RAPCS), but it seem the quickest. Here is its Line 1. + PathToFile = Snapshot_GetFilename(); //RAPCS, line 2. TCHAR szDirectory[MAX_PATH] = TEXT(""); TCHAR szFilename[MAX_PATH]; RegLoadString(TEXT("Configuration"), REGVALUE, 1, szFilename, MAX_PATH, TEXT("")); @@ -163,11 +163,11 @@ std::string CPropertySheetHelper::BrowseToFile(HWND hWindow, TCHAR* pszTitle, TC int nRes = GetOpenFileName(&ofn); if(nRes) // Okay is pressed { - strcpy(m_szNewFilename, &szFilename[ofn.nFileOffset]); // TODO:TC: m_szNewFilename not used! (Was g_szNewFilename) + m_szNewFilename = &szFilename[ofn.nFileOffset]; // TODO:TC: m_szNewFilename not used! (Was g_szNewFilename) szFilename[ofn.nFileOffset] = 0; if (_tcsicmp(szDirectory, szFilename)) - strcpy(m_szSSNewDirectory, szFilename); // TODO:TC: m_szSSNewDirectory looks dodgy! (Was g_szSSNewDirectory) + m_szSSNewDirectory = szFilename; // TODO:TC: m_szSSNewDirectory looks dodgy! (Was g_szSSNewDirectory) PathName = szFilename; PathName.append (m_szNewFilename); @@ -178,7 +178,7 @@ std::string CPropertySheetHelper::BrowseToFile(HWND hWindow, TCHAR* pszTitle, TC PathName = szFilename; } - strcpy(m_szNewFilename, PathToFile); //RAPCS, line 3 (last). + m_szNewFilename = PathToFile; //RAPCS, line 3 (last). return PathName; } @@ -190,7 +190,7 @@ void CPropertySheetHelper::SaveStateUpdate() RegSaveString(TEXT(REG_CONFIG), REGVALUE_SAVESTATE_FILENAME, 1, m_szSSNewPathname); - if(m_szSSNewDirectory[0]) + if(!m_szSSNewDirectory.empty()) RegSaveString(TEXT(REG_PREFS), REGVALUE_PREF_START_DIR, 1, m_szSSNewDirectory); } } @@ -208,26 +208,26 @@ void CPropertySheetHelper::GetDiskBaseNameWithAWS(std::string & pszFilename) int CPropertySheetHelper::SaveStateSelectImage(HWND hWindow, TCHAR* pszTitle, bool bSave) { std::string szDirectory; - std::string szFilename; + std::string tempFilename; if (bSave) { // Attempt to use drive1's image name as the name for the .aws file // Else Attempt to use the Prop Sheet's filename - GetDiskBaseNameWithAWS(szFilename); - if (szFilename.empty()) + GetDiskBaseNameWithAWS(tempFilename); + if (tempFilename.empty()) { - szFilename = Snapshot_GetFilename(); + tempFilename = Snapshot_GetFilename(); } } else // Load (or Browse) { // Attempt to use the Prop Sheet's filename first // Else attempt to use drive1's image name as the name for the .aws file - szFilename = Snapshot_GetFilename(); - if (szFilename.empty()) + tempFilename = Snapshot_GetFilename(); + if (tempFilename.empty()) { - GetDiskBaseNameWithAWS(szFilename); + GetDiskBaseNameWithAWS(tempFilename); } szDirectory = Snapshot_GetPath(); @@ -236,14 +236,16 @@ int CPropertySheetHelper::SaveStateSelectImage(HWND hWindow, TCHAR* pszTitle, bo if (szDirectory.empty()) szDirectory = g_sCurrentDir; + // convert tempFilename to char * for the rest of the function + TCHAR szFilename[MAX_PATH] = {0}; + strcpy(szFilename, tempFilename.c_str()); + tempFilename.clear(); // do NOT use this any longer + // OPENFILENAME ofn; ZeroMemory(&ofn,sizeof(OPENFILENAME)); - TCHAR localFilename[MAX_PATH]; - strcpy(localFilename, szFilename.c_str()); - ofn.lStructSize = sizeof(OPENFILENAME); ofn.hwndOwner = hWindow; ofn.hInstance = g_hInstance; @@ -257,7 +259,7 @@ int CPropertySheetHelper::SaveStateSelectImage(HWND hWindow, TCHAR* pszTitle, bo ofn.lpstrFilter = TEXT("Save State files (*.aws,*.aws.yaml)\0*.aws;*.aws.yaml\0"); TEXT("All Files\0*.*\0"); } - ofn.lpstrFile = localFilename; // Dialog strips the last .EXT from this string (eg. file.aws.yaml is displayed as: file.aws + ofn.lpstrFile = szFilename; // Dialog strips the last .EXT from this string (eg. file.aws.yaml is displayed as: file.aws ofn.nMaxFile = MAX_PATH; ofn.lpstrInitialDir = szDirectory.c_str(); ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY; @@ -299,12 +301,12 @@ int CPropertySheetHelper::SaveStateSelectImage(HWND hWindow, TCHAR* pszTitle, bo } } - strcpy(m_szSSNewFilename, &szFilename[ofn.nFileOffset]); - strcpy(m_szSSNewPathname, szFilename.c_str()); + m_szSSNewFilename = &szFilename[ofn.nFileOffset]; + m_szSSNewPathname = szFilename; szFilename[ofn.nFileOffset] = 0; - if (_tcsicmp(szDirectory.c_str(), szFilename.c_str())) - strcpy(m_szSSNewDirectory, szFilename.c_str()); + if (_tcsicmp(szDirectory.c_str(), szFilename)) + m_szSSNewDirectory = szFilename; } m_bSSNewFilename = nRes ? true : false; diff --git a/source/Configuration/PropertySheetHelper.h b/source/Configuration/PropertySheetHelper.h index 2de14919..b27cda28 100644 --- a/source/Configuration/PropertySheetHelper.h +++ b/source/Configuration/PropertySheetHelper.h @@ -30,8 +30,8 @@ public: } void SaveCurrentConfig(void); - char* GetSSNewFilename(void) { return &m_szSSNewFilename[0]; } - void ClearSSNewDirectory(void) { m_szSSNewDirectory[0] = 0; } + const std::string & GetSSNewFilename(void) { return m_szSSNewFilename; } + void ClearSSNewDirectory(void) { m_szSSNewDirectory.clear(); } // const CConfigNeedingRestart& GetConfigOld(void) { return m_ConfigOld; } CConfigNeedingRestart& GetConfigNew(void) { return m_ConfigNew; } bool IsConfigChanged(void) { return m_ConfigNew != m_ConfigOld; } @@ -54,11 +54,11 @@ private: PAGETYPE m_LastPage; UINT32 m_bmPages; - char m_szNewFilename[MAX_PATH]; + std::string m_szNewFilename; bool m_bSSNewFilename; - char m_szSSNewDirectory[MAX_PATH]; - char m_szSSNewFilename[MAX_PATH]; - char m_szSSNewPathname[MAX_PATH]; + std::string m_szSSNewDirectory; + std::string m_szSSNewFilename; + std::string m_szSSNewPathname; CConfigNeedingRestart m_ConfigOld; CConfigNeedingRestart m_ConfigNew; bool m_bDoBenchmark; diff --git a/source/Disk.cpp b/source/Disk.cpp index e5ef21d3..4b61b1ca 100644 --- a/source/Disk.cpp +++ b/source/Disk.cpp @@ -177,7 +177,7 @@ void Disk2InterfaceCard::SaveLastDiskImage(const int drive) if (!m_saveDiskImage) return; - const TCHAR *pFileName = m_floppyDrive[drive].m_disk.m_fullname.c_str(); + const std::string & pFileName = m_floppyDrive[drive].m_disk.m_fullname; if (drive == DRIVE_1) RegSaveString(TEXT(REG_PREFS), TEXT(REGVALUE_PREF_LAST_DISK_1), TRUE, pFileName); diff --git a/source/Harddisk.cpp b/source/Harddisk.cpp index 1a020ac7..3ef166d1 100644 --- a/source/Harddisk.cpp +++ b/source/Harddisk.cpp @@ -233,7 +233,7 @@ static void HD_SaveLastDiskImage(const int iDrive) if (!g_bSaveDiskImage) return; - const char *pFileName = g_HardDisk[iDrive].fullname.c_str(); + const std::string & pFileName = g_HardDisk[iDrive].fullname; if (iDrive == HARDDISK_1) RegSaveString(TEXT(REG_PREFS), REGVALUE_PREF_LAST_HARDDISK_1, TRUE, pFileName); @@ -846,7 +846,7 @@ bool HD_LoadSnapshot(YamlLoadHelper& yamlLoadHelper, UINT slot, UINT version, co bool bResSelectImage2 = HD_LoadSnapshotHDDUnit(yamlLoadHelper, HARDDISK_2); if (!bResSelectImage1 && !bResSelectImage2) - RegSaveString(TEXT(REG_PREFS), TEXT(REGVALUE_PREF_HDV_START_DIR), 1, strSaveStatePath.c_str()); + RegSaveString(TEXT(REG_PREFS), TEXT(REGVALUE_PREF_HDV_START_DIR), 1, strSaveStatePath); HD_SetEnabled(true); diff --git a/source/ParallelPrinter.cpp b/source/ParallelPrinter.cpp index 47cb7300..9eb8a669 100644 --- a/source/ParallelPrinter.cpp +++ b/source/ParallelPrinter.cpp @@ -242,7 +242,7 @@ void Printer_SetFilename(const std::string & prtFilename) else //No registry entry is available { g_szPrintFilename = g_sProgramDir + DEFAULT_PRINT_FILENAME; - RegSaveString(REG_CONFIG, REGVALUE_PRINTER_FILENAME, 1, g_szPrintFilename.c_str()); + RegSaveString(REG_CONFIG, REGVALUE_PRINTER_FILENAME, 1, g_szPrintFilename); } } diff --git a/source/Registry.cpp b/source/Registry.cpp index 83adb8ab..6ab3ad34 100644 --- a/source/Registry.cpp +++ b/source/Registry.cpp @@ -87,7 +87,7 @@ BOOL RegLoadValue (LPCTSTR section, LPCTSTR key, BOOL peruser, DWORD* value, DWO } //=========================================================================== -void RegSaveString (LPCTSTR section, LPCTSTR key, BOOL peruser, LPCTSTR buffer) { +void RegSaveString (LPCTSTR section, LPCTSTR key, BOOL peruser, const std::string & buffer) { TCHAR fullkeyname[256]; StringCbPrintf(fullkeyname, 256, TEXT("Software\\AppleWin\\CurrentVersion\\%s"), section); @@ -110,8 +110,8 @@ void RegSaveString (LPCTSTR section, LPCTSTR key, BOOL peruser, LPCTSTR buffer) key, 0, REG_SZ, - (CONST LPBYTE)buffer, - (_tcslen(buffer) + 1) * sizeof(TCHAR)); + (CONST LPBYTE)buffer.c_str(), + (buffer.size() + 1) * sizeof(TCHAR)); RegCloseKey(keyhandle); } } diff --git a/source/Registry.h b/source/Registry.h index d4dbb891..e691a248 100644 --- a/source/Registry.h +++ b/source/Registry.h @@ -8,5 +8,5 @@ BOOL RegLoadString (LPCTSTR section, LPCTSTR key, BOOL peruser, LPTSTR buffer, D BOOL RegLoadString (LPCTSTR section, LPCTSTR key, BOOL peruser, LPTSTR buffer, DWORD chars, LPCTSTR defaultValue); BOOL RegLoadValue (LPCTSTR section, LPCTSTR key, BOOL peruser, DWORD* value); BOOL RegLoadValue (LPCTSTR section, LPCTSTR key, BOOL peruser, DWORD* value, DWORD defaultValue); -void RegSaveString (LPCTSTR section, LPCTSTR key, BOOL peruser, LPCTSTR buffer); +void RegSaveString (LPCTSTR section, LPCTSTR key, BOOL peruser, const std::string & buffer); void RegSaveValue (LPCTSTR section, LPCTSTR key, BOOL peruser, DWORD value); diff --git a/source/SaveState.cpp b/source/SaveState.cpp index 8e249b26..064cd18e 100644 --- a/source/SaveState.cpp +++ b/source/SaveState.cpp @@ -74,7 +74,7 @@ static YamlHelper yamlHelper; //----------------------------------------------------------------------------- -void Snapshot_SetFilename(std::string strPathname) +void Snapshot_SetFilename(const std::string & strPathname) { if (strPathname.empty()) { diff --git a/source/SaveState.h b/source/SaveState.h index 6d50fe5b..aac842c6 100644 --- a/source/SaveState.h +++ b/source/SaveState.h @@ -2,7 +2,7 @@ extern bool g_bSaveStateOnExit; -void Snapshot_SetFilename(std::string strPathname); +void Snapshot_SetFilename(const std::string & strPathname); const std::string & Snapshot_GetFilename(); const std::string & Snapshot_GetPath(); void Snapshot_LoadState(); diff --git a/source/SerialComms.cpp b/source/SerialComms.cpp index 9489c1b1..36742e5e 100644 --- a/source/SerialComms.cpp +++ b/source/SerialComms.cpp @@ -70,7 +70,7 @@ CSuperSerialCard::CSuperSerialCard() : m_uSlot(0), m_bCfgSupportDCD(false) { - memset(m_ayCurrentSerialPortName, 0, sizeof(m_ayCurrentSerialPortName)); + m_ayCurrentSerialPortName.clear(); m_dwSerialPortItem = 0; m_hCommHandle = INVALID_HANDLE_VALUE; @@ -966,11 +966,14 @@ void CSuperSerialCard::CommSetSerialPort(HWND hWindow, DWORD dwNewSerialPortItem m_dwSerialPortItem = dwNewSerialPortItem; if (m_dwSerialPortItem == m_uTCPChoiceItemIdx) - strcpy(m_ayCurrentSerialPortName, TEXT_SERIAL_TCP); - else if (m_dwSerialPortItem != 0) - sprintf(m_ayCurrentSerialPortName, TEXT_SERIAL_COM"%d", m_vecSerialPortsItems[m_dwSerialPortItem]); + m_ayCurrentSerialPortName = TEXT_SERIAL_TCP; + else if (m_dwSerialPortItem != 0) { + TCHAR temp[SIZEOF_SERIALCHOICE_ITEM]; + sprintf(temp, TEXT_SERIAL_COM"%d", m_vecSerialPortsItems[m_dwSerialPortItem]); + m_ayCurrentSerialPortName = temp; + } else - m_ayCurrentSerialPortName[0] = 0; // "None" + m_ayCurrentSerialPortName.clear(); // "None" } //=========================================================================== @@ -1315,8 +1318,7 @@ char* CSuperSerialCard::GetSerialPortChoices() // Called by LoadConfiguration() void CSuperSerialCard::SetSerialPortName(const char* pSerialPortName) { - strncpy(m_ayCurrentSerialPortName, pSerialPortName, SIZEOF_SERIALCHOICE_ITEM); - m_ayCurrentSerialPortName[SIZEOF_SERIALCHOICE_ITEM-1] = 0; + m_ayCurrentSerialPortName = pSerialPortName; // Init m_aySerialPortChoices, so that we have choices to show if serial is active when we 1st open Config dialog GetSerialPortChoices(); @@ -1348,7 +1350,7 @@ void CSuperSerialCard::SetSerialPortName(const char* pSerialPortName) } else { - m_ayCurrentSerialPortName[0] = 0; // "None" + m_ayCurrentSerialPortName.clear(); // "None" m_dwSerialPortItem = 0; } } diff --git a/source/SerialComms.h b/source/SerialComms.h index 6a5eecc4..922cd02f 100644 --- a/source/SerialComms.h +++ b/source/SerialComms.h @@ -38,7 +38,7 @@ public: char* GetSerialPortChoices(); DWORD GetSerialPort() { return m_dwSerialPortItem; } // Drop-down list item - char* GetSerialPortName() { return m_ayCurrentSerialPortName; } + const std::string & GetSerialPortName() { return m_ayCurrentSerialPortName; } void SetSerialPortName(const char* pSerialPortName); bool IsActive() { return (m_hCommHandle != INVALID_HANDLE_VALUE) || (m_hCommListenSocket != INVALID_SOCKET); } void SupportDCD(bool bEnable) { m_bCfgSupportDCD = bEnable; } // Status @@ -86,7 +86,7 @@ public: static const UINT SIZEOF_SERIALCHOICE_ITEM = 8*sizeof(char); private: - char m_ayCurrentSerialPortName[SIZEOF_SERIALCHOICE_ITEM]; + std::string m_ayCurrentSerialPortName; DWORD m_dwSerialPortItem; static const UINT SERIALPORTITEM_INVALID_COM_PORT = 0;