Set default directories "battery" and "savestates".

Code cleanup.
Automatically & silently create directories in a relative path.

git-svn-id: https://svn.code.sf.net/p/vbam/code/trunk@774 a31d4220-a93d-0410-bf67-fe4944624d44
This commit is contained in:
spacy51 2008-10-21 19:00:37 +00:00
parent 00163834e1
commit 83a3996168
8 changed files with 81 additions and 268 deletions

View File

@ -23,9 +23,6 @@
#include "Reg.h"
#include "WinResUtil.h"
#include <shlobj.h>
#include <shlwapi.h>
#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;
}

View File

@ -50,5 +50,4 @@ private:
CString initialFolderDir;
CString browseForDir(CString title);
bool directoryDoesExist(const char *directory);
};

View File

@ -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;
}

View File

@ -24,7 +24,6 @@
#include "MainWnd.h"
#include <winsock.h>
#include <shlwapi.h>
#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;

View File

@ -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);

View File

@ -19,8 +19,6 @@
#include "stdafx.h"
#include "MainWnd.h"
#include <shlwapi.h>
#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);

View File

@ -25,6 +25,7 @@
#include "stdafx.h"
#include <intrin.h>
#include <shlobj.h> // 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 );
}
}
*/
}
}
}

View File

@ -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 );