tweak file opening and saving (incomplete)

This commit is contained in:
ehouse 2004-01-30 05:48:24 +00:00
parent db9391df36
commit bc4937048c
2 changed files with 105 additions and 19 deletions

View file

@ -16,7 +16,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Derived from code generated by M$'s eVS.
* Derived from code generated by M$'s eVC++.
*/
#include "stdafx.h"
@ -45,8 +45,12 @@
#define MAX_LOADSTRING 100
#define CUR_CE_PREFS_FLAGS 0x0001
#define PREFSFILENAME L"\\My Documents\\Personal\\.xwprefs"
#define UNSAVEDGAMEFILENAME "\\My Documents\\Personal\\_newgame"
/* #define PREFSFILENAME L"\\My Documents\\Personal\\.xwprefs" */
/* #define UNSAVEDGAMEFILENAME "\\My Documents\\Personal\\_newgame" */
#define DEFAULT_DIR_NAME L"\\My Documents\\Crosswords"
/* #define PREFSFILENAME L"\\My Documents\\Crosswords\\.xwprefs" */
#define PREFSFILENAME L"\\My Documents\\Crosswords\\xwprefs"
#define UNSAVEDGAMEFILENAME "\\My Documents\\Crosswords\\_newgame"
#ifdef MEM_DEBUG
# define MEMPOOL globals->mpool,
@ -392,11 +396,51 @@ ceInitAndStartBoard( CEAppGlobals* globals, XP_Bool newGame, CeGamePrefs* gp )
globals->isNewGame = FALSE;
} /* ceInitAndStartBoard */
#ifdef DEBUG
void
logLastError( XP_UCHAR* comment )
{
LPVOID lpMsgBuf;
DWORD lastErr = GetLastError();
XP_UCHAR msg[256];
XP_U16 len;
XP_U16 lenSoFar;
strcpy( msg, comment );
strcat( msg, ": " );
lenSoFar = strlen( msg );
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
lastErr,
0, // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
len = wcslen( lpMsgBuf );
if ( len >= sizeof(msg)-lenSoFar ) {
len = sizeof(msg) - lenSoFar - 1;
}
WideCharToMultiByte( CP_ACP, 0, lpMsgBuf, len + 1,
msg + lenSoFar, len + 1, NULL, NULL );
LocalFree( lpMsgBuf );
XP_LOGF( "system error: %s", msg );
} /* logLastError */
#endif
static void
ceSavePrefs( CEAppGlobals* globals )
{
HANDLE fileH = CreateFile( PREFSFILENAME, GENERIC_WRITE, 0, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
HANDLE fileH;
fileH = CreateFile( PREFSFILENAME, GENERIC_WRITE, 0, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL );
if ( fileH != INVALID_HANDLE_VALUE ) {
XP_U32 nWritten;
XP_U16 nameLen = 0;
@ -418,6 +462,8 @@ ceSavePrefs( CEAppGlobals* globals )
SetEndOfFile( fileH ); /* truncate anything previously there */
XP_DEBUGF( "ceSavePrefs: prefs file written" );
} else {
XP_LOGF( "failed to create prefs file" );
}
} /* ceSavePrefs */
@ -426,8 +472,10 @@ static XP_Bool
ceLoadPrefs( CEAppGlobals* globals )
{
XP_Bool result = XP_FALSE;
HANDLE fileH = CreateFile( PREFSFILENAME, GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
HANDLE fileH;
fileH = CreateFile( PREFSFILENAME, GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
if ( fileH != INVALID_HANDLE_VALUE ) {
XP_U32 fileSize = GetFileSize( fileH, NULL );
XP_DEBUGF( "ceLoadPrefs: prefs file found" );
@ -457,6 +505,8 @@ ceLoadPrefs( CEAppGlobals* globals )
}
}
CloseHandle( fileH );
} else {
XP_LOGF( "ceLoadPrefs: prefs file not found" );
}
return result; /* none found */
} /* ceLoadPrefs */
@ -591,6 +641,7 @@ InitInstance(HINSTANCE hInstance, int nCmdShow)
XP_Bool oldGameLoaded;
XP_Bool prevStateExists;
XP_Bool newDone = XP_FALSE;
XP_U16 len;
MPSLOT;
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
@ -603,6 +654,8 @@ InitInstance(HINSTANCE hInstance, int nCmdShow)
return FALSE;
}
(void)CreateDirectory( DEFAULT_DIR_NAME, 0 );
#ifdef MEM_DEBUG
mpool = mpool_make();
#endif
@ -615,6 +668,11 @@ InitInstance(HINSTANCE hInstance, int nCmdShow)
globals->vtMgr = make_vtablemgr( MPPARM_NOCOMMA(mpool) );
len = wcslen( DEFAULT_DIR_NAME );
len = (len + 1) * sizeof(globals->lastDefaultDir[0]);
globals->lastDefaultDir = XP_MALLOC( mpool, len );
XP_MEMCPY( globals->lastDefaultDir, DEFAULT_DIR_NAME, len );
globals->hInst = hInstance;
// Initialize global strings
MyRegisterClass(hInstance, szWindowClass);
@ -646,7 +704,6 @@ InitInstance(HINSTANCE hInstance, int nCmdShow)
gi_initPlayerInfo( MPPARM(mpool) &globals->gameInfo, "Player %d" );
/* here's where we want to behave differently if there's saved state.
But that's a long ways off. */
prevStateExists = ceLoadPrefs( globals );
@ -856,7 +913,9 @@ ceChooseAndOpen( CEAppGlobals* globals )
if ( globals->curGameName != NULL
&& 0 == XP_STRCMP( name, globals->curGameName ) ){ /*already open*/
XP_FREE( globals->mpool, name );
} else if ( ceSaveCurGame( globals, XP_FALSE ) ) {
} else if ( ceSaveCurGame( globals, XP_FALSE )
|| queryBoxChar( globals, "Do you really want to "
"overwrite the current game?" ) ) {
if ( globals->curGameName != NULL ) {
XP_FREE( globals->mpool, globals->curGameName );
@ -955,23 +1014,30 @@ ceSaveCurGame( CEAppGlobals* globals, XP_Bool autoSave )
XP_U16 len = XP_STRLEN(UNSAVEDGAMEFILENAME) + 1;
newName = XP_MALLOC( globals->mpool, len );
XP_MEMCPY( newName, UNSAVEDGAMEFILENAME, len );
confirmed = XP_TRUE;
} else {
OPENFILENAME saveFileStruct;
OPENFILENAME sfs;
XP_MEMSET( &saveFileStruct, 0, sizeof(saveFileStruct) );
XP_MEMSET( &sfs, 0, sizeof(sfs) );
XP_MEMSET( nameBuf, 0, sizeof(nameBuf) );
saveFileStruct.lStructSize = sizeof(saveFileStruct);
saveFileStruct.hwndOwner = globals->hWnd;
saveFileStruct.lpstrFile = nameBuf;
saveFileStruct.nMaxFile = sizeof(nameBuf)/sizeof(nameBuf[0]);
sfs.lStructSize = sizeof(sfs);
sfs.hwndOwner = globals->hWnd;
sfs.lpstrFile = nameBuf;
sfs.nMaxFile = sizeof(nameBuf)/sizeof(nameBuf[0]);
saveFileStruct.lpstrDefExt = L"xwg";
sfs.lpstrDefExt = L"xwg";
confirmed = GetSaveFileName( &saveFileStruct );
/* Doesn't seem to be working.... */
XP_LOGF( "setting the bleeping title to: Save current game as" );
sfs.lpstrTitle = L"Save current game as";
/* XP_ASSERT( !!globals->lastDefaultDir ); */
sfs.lpstrInitialDir = "hello world";
confirmed = GetSaveFileName( &sfs );
if ( confirmed ) {
XP_U16 len = wcslen(nameBuf);
@ -979,6 +1045,17 @@ ceSaveCurGame( CEAppGlobals* globals, XP_Bool autoSave )
newName = XP_MALLOC( globals->mpool, len + 1 );
WideCharToMultiByte( CP_ACP, 0, nameBuf, len + 1,
newName, len + 1, NULL, NULL );
/* If user picked from a different directory, remember it
as the new starting point. */
nameBuf[sfs.nFileOffset] = 0;
if ( wcscmp( nameBuf, globals->lastDefaultDir ) != 0 ) {
XP_FREE( globals->mpool, globals->lastDefaultDir );
globals->lastDefaultDir =
XP_MALLOC( globals->mpool,
(sfs.nFileOffset + 1) * 2 );
wcscpy( globals->lastDefaultDir, nameBuf );
}
}
}
@ -1422,7 +1499,7 @@ wince_debugf(XP_UCHAR* format, ...)
makeTimeStamp(timeStamp, sizeof(timeStamp));
fileH = CreateFile( L"\\My Documents\\Personal\\xwDbgLog.txt",
fileH = CreateFile( L"\\My Documents\\Crosswords\\xwDbgLog.txt",
GENERIC_WRITE, 0, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
@ -1586,6 +1663,7 @@ ce_util_getSquareBonus( XW_UtilCtxt* uc, ModelCtxt* model,
if ( col > 7 ) col = 14 - col;
if ( row > 7 ) row = 14 - row;
index = (row*8) + col;
if ( !globals->bonusInfo || (index >= 8*8) ) {
XP_ASSERT( 0 );
return (XWBonusType)BONUS_NONE;

View file

@ -48,6 +48,7 @@ typedef struct CEAppGlobals {
XW_UtilCtxt util;
VTableMgr* vtMgr;
XP_U16* bonusInfo;
wchar_t* lastDefaultDir;
CEAppPrefs appPrefs;
@ -113,4 +114,11 @@ typedef struct CEDrawCtx {
DrawCtx* ce_drawctxt_make( MPFORMAL HWND mainWin, CEAppGlobals* globals );
#ifdef DEBUG
void logLastError(XP_UCHAR* comment );
#else
# define logLastError(c)
#endif
#endif /* _CEMAIN_H_ */