diff --git a/src/win32/Directories.cpp b/src/win32/Directories.cpp index 781760cd..4df6418b 100644 --- a/src/win32/Directories.cpp +++ b/src/win32/Directories.cpp @@ -23,9 +23,6 @@ #include "Reg.h" #include "WinResUtil.h" -#include -#include - #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE @@ -97,11 +94,11 @@ BOOL Directories::OnInitDialog() if(!p.IsEmpty()) GetDlgItem(IDC_GBROM_PATH)->SetWindowText(p); - p = regQueryStringValue("batteryDir", NULL); + p = regQueryStringValue("batteryDir", DEFAULT_BATTERY_DIR); if(!p.IsEmpty()) GetDlgItem(IDC_BATTERY_PATH)->SetWindowText( p); - p = regQueryStringValue("saveDir", NULL); + p = regQueryStringValue("saveDir", DEFAULT_SAVESTATES_DIR); if(!p.IsEmpty()) GetDlgItem(IDC_SAVE_PATH)->SetWindowText(p); @@ -171,86 +168,37 @@ void Directories::OnOK() { CDialog::OnOK(); - char baseDir[MAX_PATH+1]; - char temp[MAX_PATH+1]; - GetModuleFileName( NULL, baseDir, MAX_PATH ); - baseDir[MAX_PATH] = '\0'; // for security reasons - PathRemoveFileSpec( baseDir ); // removes the trailing file name and backslash - - CString buffer; m_romPath.GetWindowText(buffer); if( !buffer.IsEmpty() ) regSetStringValue( "romdir", buffer ); - if( buffer[0] == '.' ) { - strcpy( temp, baseDir ); - strcat( temp, "\\" ); - strcat( temp, buffer ); - buffer = temp; - } - if( !directoryDoesExist( buffer ) ) - SHCreateDirectoryEx( NULL, buffer, NULL ); + treatRelativePath( buffer ); m_gbcromPath.GetWindowText(buffer); if( !buffer.IsEmpty() ) regSetStringValue( "gbcromdir", buffer ); - if( buffer[0] == '.' ) { - strcpy( temp, baseDir ); - strcat( temp, "\\" ); - strcat( temp, buffer ); - buffer = temp; - } - if( !directoryDoesExist( buffer ) ) - SHCreateDirectoryEx( NULL, buffer, NULL ); + treatRelativePath( buffer ); m_gbromPath.GetWindowText(buffer); if( !buffer.IsEmpty() ) regSetStringValue( "gbromdir", buffer ); - if( buffer[0] == '.' ) { - strcpy( temp, baseDir ); - strcat( temp, "\\" ); - strcat( temp, buffer ); - buffer = temp; - } - if( !directoryDoesExist( buffer ) ) - SHCreateDirectoryEx( NULL, buffer, NULL ); + treatRelativePath( buffer ); m_batteryPath.GetWindowText(buffer); if( !buffer.IsEmpty() ) regSetStringValue( "batteryDir", buffer ); - if( buffer[0] == '.' ) { - strcpy( temp, baseDir ); - strcat( temp, "\\" ); - strcat( temp, buffer ); - buffer = temp; - } - if( !directoryDoesExist( buffer ) ) - SHCreateDirectoryEx( NULL, buffer, NULL ); + treatRelativePath( buffer ); m_savePath.GetWindowText(buffer); if( !buffer.IsEmpty() ) regSetStringValue( "saveDir", buffer ); - if( buffer[0] == '.' ) { - strcpy( temp, baseDir ); - strcat( temp, "\\" ); - strcat( temp, buffer ); - buffer = temp; - } - if( !directoryDoesExist( buffer ) ) - SHCreateDirectoryEx( NULL, buffer, NULL ); + treatRelativePath( buffer ); m_capturePath.GetWindowText(buffer); if( !buffer.IsEmpty() ) regSetStringValue( "captureDir", buffer ); - if( buffer[0] == '.' ) { - strcpy( temp, baseDir ); - strcat( temp, "\\" ); - strcat( temp, buffer ); - buffer = temp; - } - if( !directoryDoesExist( buffer ) ) - SHCreateDirectoryEx( NULL, buffer, NULL ); + treatRelativePath( buffer ); EndDialog(TRUE); } @@ -285,20 +233,3 @@ CString Directories::browseForDir(CString title) } return res; } - -// returns true if the directory does exist -bool Directories::directoryDoesExist(const char *directory) -{ - HANDLE hDir; - hDir = CreateFile( - directory, - GENERIC_READ, - FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, - NULL, - OPEN_EXISTING, - FILE_FLAG_BACKUP_SEMANTICS, - NULL ); - bool retval = (hDir == INVALID_HANDLE_VALUE) ? false : true; - CloseHandle( hDir ); - return retval; -} diff --git a/src/win32/Directories.h b/src/win32/Directories.h index 332d9fa4..5e6518f7 100644 --- a/src/win32/Directories.h +++ b/src/win32/Directories.h @@ -50,5 +50,4 @@ private: CString initialFolderDir; CString browseForDir(CString title); - bool directoryDoesExist(const char *directory); }; diff --git a/src/win32/FileDlg.cpp b/src/win32/FileDlg.cpp index 8091d93d..6abb77af 100644 --- a/src/win32/FileDlg.cpp +++ b/src/win32/FileDlg.cpp @@ -189,7 +189,7 @@ int FileDlg::getFilterIndex() int FileDlg::DoModal() { BOOL res = isSave ? GetSaveFileName(&m_ofn) : - GetOpenFileName(&m_ofn); + GetOpenFileName(&m_ofn); return res ? IDOK : IDCANCEL; } diff --git a/src/win32/MainWnd.cpp b/src/win32/MainWnd.cpp index c04eff39..90892c13 100644 --- a/src/win32/MainWnd.cpp +++ b/src/win32/MainWnd.cpp @@ -24,7 +24,6 @@ #include "MainWnd.h" #include -#include #include "FileDlg.h" #include "Reg.h" @@ -818,17 +817,8 @@ void MainWnd::winSaveCheatListDefault() name = theApp.filename.Right(theApp.filename.GetLength()-index-1); else name = theApp.filename; - CString dir = regQueryStringValue("saveDir", NULL); - if( dir[0] == '.' ) { - // handle as relative path - char baseDir[MAX_PATH+1]; - GetModuleFileName( NULL, baseDir, MAX_PATH ); - baseDir[MAX_PATH] = '\0'; // for security reasons - PathRemoveFileSpec( baseDir ); // removes the trailing file name and backslash - strcat( baseDir, "\\" ); - strcat( baseDir, dir ); - dir = baseDir; - } + CString dir = regQueryStringValue("saveDir", DEFAULT_SAVESTATES_DIR); + treatRelativePath( dir ); if(!dir.GetLength()) dir = getDirFromFile(filename); @@ -860,17 +850,8 @@ void MainWnd::winLoadCheatListDefault() name = theApp.filename.Right(theApp.filename.GetLength()-index-1); else name = theApp.filename; - CString dir = regQueryStringValue("saveDir", NULL); - if( dir[0] == '.' ) { - // handle as relative path - char baseDir[MAX_PATH+1]; - GetModuleFileName( NULL, baseDir, MAX_PATH ); - baseDir[MAX_PATH] = '\0'; // for security reasons - PathRemoveFileSpec( baseDir ); // removes the trailing file name and backslash - strcat( baseDir, "\\" ); - strcat( baseDir, dir ); - dir = baseDir; - } + CString dir = regQueryStringValue("saveDir", DEFAULT_SAVESTATES_DIR); + treatRelativePath( dir ); if(!dir.GetLength()) dir = getDirFromFile(filename); @@ -932,17 +913,8 @@ void MainWnd::writeBatteryFile() else buffer = theApp.filename; - CString saveDir = regQueryStringValue("batteryDir", NULL); - if( saveDir[0] == '.' ) { - // handle as relative path - char baseDir[MAX_PATH+1]; - GetModuleFileName( NULL, baseDir, MAX_PATH ); - baseDir[MAX_PATH] = '\0'; // for security reasons - PathRemoveFileSpec( baseDir ); // removes the trailing file name and backslash - strcat( baseDir, "\\" ); - strcat( baseDir, saveDir ); - saveDir = baseDir; - } + CString saveDir = regQueryStringValue("batteryDir", DEFAULT_BATTERY_DIR); + treatRelativePath( saveDir ); if(saveDir.IsEmpty()) saveDir = getDirFromFile(theApp.filename); @@ -969,17 +941,8 @@ void MainWnd::readBatteryFile() else buffer = theApp.filename; - CString saveDir = regQueryStringValue("batteryDir", NULL); - if( saveDir[0] == '.' ) { - // handle as relative path - char baseDir[MAX_PATH+1]; - GetModuleFileName( NULL, baseDir, MAX_PATH ); - baseDir[MAX_PATH] = '\0'; // for security reasons - PathRemoveFileSpec( baseDir ); // removes the trailing file name and backslash - strcat( baseDir, "\\" ); - strcat( baseDir, saveDir ); - saveDir = baseDir; - } + CString saveDir = regQueryStringValue("batteryDir", DEFAULT_BATTERY_DIR); + treatRelativePath( saveDir ); if(saveDir.IsEmpty()) saveDir = getDirFromFile(theApp.filename); @@ -1069,16 +1032,7 @@ bool MainWnd::fileOpenSelect( int system ) theApp.dir = initialDir; } - if( initialDir[0] == '.' ) { - // handle as relative path - char baseDir[MAX_PATH+1]; - GetModuleFileName( NULL, baseDir, MAX_PATH ); - baseDir[MAX_PATH] = '\0'; // for security reasons - PathRemoveFileSpec( baseDir ); // removes the trailing file name and backslash - strcat( baseDir, "\\" ); - strcat( baseDir, initialDir ); - initialDir = baseDir; - } + treatRelativePath( initialDir ); theApp.szFile = _T(""); @@ -1130,16 +1084,8 @@ void MainWnd::screenCapture(int captureNumber) CString buffer; CString captureDir = regQueryStringValue("captureDir", ""); - if( captureDir[0] == '.' ) { - // handle as relative path - char baseDir[MAX_PATH+1]; - GetModuleFileName( NULL, baseDir, MAX_PATH ); - baseDir[MAX_PATH] = '\0'; // for security reasons - PathRemoveFileSpec( baseDir ); // removes the trailing file name and backslash - strcat( baseDir, "\\" ); - strcat( baseDir, captureDir ); - captureDir = baseDir; - } + treatRelativePath( captureDir ); + int index = theApp.filename.ReverseFind('\\'); CString name; diff --git a/src/win32/MainWndCheats.cpp b/src/win32/MainWndCheats.cpp index a295e582..46a5831e 100644 --- a/src/win32/MainWndCheats.cpp +++ b/src/win32/MainWndCheats.cpp @@ -85,7 +85,7 @@ void MainWnd::OnCheatsLoadcheatlist() else buffer = theApp.filename; - CString saveDir = regQueryStringValue("saveDir", NULL); + CString saveDir = regQueryStringValue("saveDir", DEFAULT_SAVESTATES_DIR); if(saveDir.IsEmpty()) saveDir = getDirFromFile(theApp.filename); @@ -123,7 +123,7 @@ void MainWnd::OnCheatsSavecheatlist() else buffer = theApp.filename; - CString saveDir = regQueryStringValue("saveDir", NULL); + CString saveDir = regQueryStringValue("saveDir", DEFAULT_SAVESTATES_DIR); if(saveDir.IsEmpty()) saveDir = getDirFromFile(theApp.filename); diff --git a/src/win32/MainWndFile.cpp b/src/win32/MainWndFile.cpp index 4a443887..d50e10ed 100644 --- a/src/win32/MainWndFile.cpp +++ b/src/win32/MainWndFile.cpp @@ -19,8 +19,6 @@ #include "stdafx.h" #include "MainWnd.h" -#include - #include "ExportGSASnapshot.h" #include "FileDlg.h" #include "GSACodeSelect.h" @@ -193,17 +191,8 @@ void MainWnd::OnFileLoad() else buffer = theApp.filename; - CString saveDir = regQueryStringValue("saveDir", NULL); - if( saveDir[0] == '.' ) { - // handle as relative path - char baseDir[MAX_PATH+1]; - GetModuleFileName( NULL, baseDir, MAX_PATH ); - baseDir[MAX_PATH] = '\0'; // for security reasons - PathRemoveFileSpec( baseDir ); // removes the trailing file name and backslash - strcat( baseDir, "\\" ); - strcat( baseDir, saveDir ); - saveDir = baseDir; - } + CString saveDir = regQueryStringValue("saveDir", DEFAULT_SAVESTATES_DIR); + treatRelativePath( saveDir ); if(saveDir.IsEmpty()) saveDir = getDirFromFile(theApp.filename); @@ -250,17 +239,8 @@ BOOL MainWnd::OnFileLoadSlot(UINT nID) else buffer = theApp.filename; - CString saveDir = regQueryStringValue("saveDir", NULL); - if( saveDir[0] == '.' ) { - // handle as relative path - char baseDir[MAX_PATH+1]; - GetModuleFileName( NULL, baseDir, MAX_PATH ); - baseDir[MAX_PATH] = '\0'; // for security reasons - PathRemoveFileSpec( baseDir ); // removes the trailing file name and backslash - strcat( baseDir, "\\" ); - strcat( baseDir, saveDir ); - saveDir = baseDir; - } + CString saveDir = regQueryStringValue("saveDir", DEFAULT_SAVESTATES_DIR); + treatRelativePath( saveDir ); if(saveDir.IsEmpty()) saveDir = getDirFromFile(theApp.filename); @@ -301,17 +281,8 @@ void MainWnd::OnFileSave() else buffer = theApp.filename; - CString saveDir = regQueryStringValue("saveDir", NULL); - if( saveDir[0] == '.' ) { - // handle as relative path - char baseDir[MAX_PATH+1]; - GetModuleFileName( NULL, baseDir, MAX_PATH ); - baseDir[MAX_PATH] = '\0'; // for security reasons - PathRemoveFileSpec( baseDir ); // removes the trailing file name and backslash - strcat( baseDir, "\\" ); - strcat( baseDir, saveDir ); - saveDir = baseDir; - } + CString saveDir = regQueryStringValue("saveDir", DEFAULT_SAVESTATES_DIR); + treatRelativePath( saveDir ); if(saveDir.IsEmpty()) saveDir = getDirFromFile(theApp.filename); @@ -353,17 +324,8 @@ BOOL MainWnd::OnFileSaveSlot(UINT nID) else buffer = theApp.filename; - CString saveDir = regQueryStringValue("saveDir", NULL); - if( saveDir[0] == '.' ) { - // handle as relative path - char baseDir[MAX_PATH+1]; - GetModuleFileName( NULL, baseDir, MAX_PATH ); - baseDir[MAX_PATH] = '\0'; // for security reasons - PathRemoveFileSpec( baseDir ); // removes the trailing file name and backslash - strcat( baseDir, "\\" ); - strcat( baseDir, saveDir ); - saveDir = baseDir; - } + CString saveDir = regQueryStringValue("saveDir", DEFAULT_SAVESTATES_DIR); + treatRelativePath( saveDir ); if(saveDir.IsEmpty()) saveDir = getDirFromFile(theApp.filename); @@ -391,17 +353,8 @@ void MainWnd::OnFileImportBatteryfile() CString filter = winLoadFilter(IDS_FILTER_SAV); CString title = winResLoadString(IDS_SELECT_BATTERY_FILE); - CString saveDir = regQueryStringValue("batteryDir", NULL); - if( saveDir[0] == '.' ) { - // handle as relative path - char baseDir[MAX_PATH+1]; - GetModuleFileName( NULL, baseDir, MAX_PATH ); - baseDir[MAX_PATH] = '\0'; // for security reasons - PathRemoveFileSpec( baseDir ); // removes the trailing file name and backslash - strcat( baseDir, "\\" ); - strcat( baseDir, saveDir ); - saveDir = baseDir; - } + CString saveDir = regQueryStringValue("batteryDir", DEFAULT_BATTERY_DIR); + treatRelativePath( saveDir ); if(saveDir.IsEmpty()) saveDir = getDirFromFile(theApp.filename); @@ -515,17 +468,8 @@ void MainWnd::OnFileExportBatteryfile() CString filter = winLoadFilter(IDS_FILTER_SAV); CString title = winResLoadString(IDS_SELECT_BATTERY_FILE); - CString saveDir = regQueryStringValue("batteryDir", NULL); - if( saveDir[0] == '.' ) { - // handle as relative path - char baseDir[MAX_PATH+1]; - GetModuleFileName( NULL, baseDir, MAX_PATH ); - baseDir[MAX_PATH] = '\0'; // for security reasons - PathRemoveFileSpec( baseDir ); // removes the trailing file name and backslash - strcat( baseDir, "\\" ); - strcat( baseDir, saveDir ); - saveDir = baseDir; - } + CString saveDir = regQueryStringValue("batteryDir", DEFAULT_BATTERY_DIR); + treatRelativePath( saveDir ); if(saveDir.IsEmpty()) saveDir = getDirFromFile(theApp.filename); @@ -621,16 +565,8 @@ void MainWnd::OnFileScreencapture() name = theApp.filename; CString capdir = regQueryStringValue("captureDir", ""); - if( capdir[0] == '.' ) { - // handle as relative path - char baseDir[MAX_PATH+1]; - GetModuleFileName( NULL, baseDir, MAX_PATH ); - baseDir[MAX_PATH] = '\0'; // for security reasons - PathRemoveFileSpec( baseDir ); // removes the trailing file name and backslash - strcat( baseDir, "\\" ); - strcat( baseDir, capdir ); - capdir = baseDir; - } + treatRelativePath( capdir ); + if(capdir.IsEmpty()) capdir = getDirFromFile(theApp.filename); @@ -775,17 +711,8 @@ void MainWnd::OnFileSavegameOldestslot() else filename = theApp.filename; - CString saveDir = regQueryStringValue("saveDir", NULL); - if( saveDir[0] == '.' ) { - // handle as relative path - char baseDir[MAX_PATH+1]; - GetModuleFileName( NULL, baseDir, MAX_PATH ); - baseDir[MAX_PATH] = '\0'; // for security reasons - PathRemoveFileSpec( baseDir ); // removes the trailing file name and backslash - strcat( baseDir, "\\" ); - strcat( baseDir, saveDir ); - saveDir = baseDir; - } + CString saveDir = regQueryStringValue("saveDir", DEFAULT_SAVESTATES_DIR); + treatRelativePath( saveDir ); if(saveDir.IsEmpty()) saveDir = getDirFromFile(theApp.filename); @@ -829,17 +756,8 @@ void MainWnd::OnUpdateFileSavegameOldestslot(CCmdUI* pCmdUI) else filename = theApp.filename; - CString saveDir = regQueryStringValue("saveDir", NULL); - if( saveDir[0] == '.' ) { - // handle as relative path - char baseDir[MAX_PATH+1]; - GetModuleFileName( NULL, baseDir, MAX_PATH ); - baseDir[MAX_PATH] = '\0'; // for security reasons - PathRemoveFileSpec( baseDir ); // removes the trailing file name and backslash - strcat( baseDir, "\\" ); - strcat( baseDir, saveDir ); - saveDir = baseDir; - } + CString saveDir = regQueryStringValue("saveDir", DEFAULT_SAVESTATES_DIR); + treatRelativePath( saveDir ); if(saveDir.IsEmpty()) saveDir = getDirFromFile(theApp.filename); @@ -881,17 +799,8 @@ void MainWnd::OnFileLoadgameMostrecent() else filename = theApp.filename; - CString saveDir = regQueryStringValue("saveDir", NULL); - if( saveDir[0] == '.' ) { - // handle as relative path - char baseDir[MAX_PATH+1]; - GetModuleFileName( NULL, baseDir, MAX_PATH ); - baseDir[MAX_PATH] = '\0'; // for security reasons - PathRemoveFileSpec( baseDir ); // removes the trailing file name and backslash - strcat( baseDir, "\\" ); - strcat( baseDir, saveDir ); - saveDir = baseDir; - } + CString saveDir = regQueryStringValue("saveDir", DEFAULT_SAVESTATES_DIR); + treatRelativePath( saveDir ); if(saveDir.IsEmpty()) saveDir = getDirFromFile(theApp.filename); @@ -935,17 +844,8 @@ void MainWnd::OnUpdateFileLoadgameMostrecent(CCmdUI* pCmdUI) else filename = theApp.filename; - CString saveDir = regQueryStringValue("saveDir", NULL); - if( saveDir[0] == '.' ) { - // handle as relative path - char baseDir[MAX_PATH+1]; - GetModuleFileName( NULL, baseDir, MAX_PATH ); - baseDir[MAX_PATH] = '\0'; // for security reasons - PathRemoveFileSpec( baseDir ); // removes the trailing file name and backslash - strcat( baseDir, "\\" ); - strcat( baseDir, saveDir ); - saveDir = baseDir; - } + CString saveDir = regQueryStringValue("saveDir", DEFAULT_SAVESTATES_DIR); + treatRelativePath( saveDir ); if(saveDir.IsEmpty()) saveDir = getDirFromFile(theApp.filename); diff --git a/src/win32/VBA.cpp b/src/win32/VBA.cpp index ec5fa506..4b96902b 100644 --- a/src/win32/VBA.cpp +++ b/src/win32/VBA.cpp @@ -25,6 +25,7 @@ #include "stdafx.h" #include +#include // for SHCreateDirectoryEx #include "AVIWrite.h" #include "LangSelect.h" @@ -2780,3 +2781,34 @@ void Sm60FPS_Sleep() } } } + + +void treatRelativePath( CString & path ) +{ + if( path.GetLength() > 1 ) { + if( path.GetAt( 0 ) == '.' ) { + // treat as relative path + char baseDir[MAX_PATH + 1]; + GetModuleFileName( NULL, baseDir, MAX_PATH ); + baseDir[MAX_PATH] = '\0'; // for security reasons + CString temp = baseDir; + int pos = temp.ReverseFind( '\\' ); + temp.Truncate( pos + 1 ); + path.Insert( 0, temp ); + + // create directories if path does not exist + SHCreateDirectoryEx( NULL, path, NULL ); + /* Spacy's implementation: + while( ERROR_PATH_NOT_FOUND == CreateDirectory( path, NULL ) ) { + temp = path; + while( ERROR_PATH_NOT_FOUND == CreateDirectory( temp, NULL ) ) { + pos = temp.ReverseFind( '\\' ); + ASSERT( pos != -1 ); + if( pos == -1 ) break; // something has gone wrong, abort + temp.Truncate( pos ); + } + } + */ + } + } +} diff --git a/src/win32/VBA.h b/src/win32/VBA.h index a5f9592c..92b42432 100644 --- a/src/win32/VBA.h +++ b/src/win32/VBA.h @@ -64,6 +64,9 @@ enum pixelFilterType #define REWIND_SIZE 400000 +#define DEFAULT_BATTERY_DIR ".\\battery" +#define DEFAULT_SAVESTATES_DIR ".\\savestates" + class AVIWrite; class WavWriter; @@ -284,3 +287,5 @@ extern int emulating; #ifdef MMX extern "C" bool cpu_mmx; #endif + +void treatRelativePath( CString & path );