From f021964213fd009e5a6b46160a4dc9f43c9921f9 Mon Sep 17 00:00:00 2001 From: ehouse Date: Thu, 18 Sep 2008 03:46:27 +0000 Subject: [PATCH] fix ncurses version so it can save games and read them back in: make existing gtk code run on both. --- xwords4/linux/cursesmain.c | 41 +++++++++++++++++++++++----- xwords4/linux/gtkmain.c | 55 +------------------------------------- xwords4/linux/linuxmain.c | 52 +++++++++++++++++++++++++++++++++++ xwords4/linux/linuxmain.h | 5 ++++ 4 files changed, 93 insertions(+), 60 deletions(-) diff --git a/xwords4/linux/cursesmain.c b/xwords4/linux/cursesmain.c index c554098b4..2821788f3 100644 --- a/xwords4/linux/cursesmain.c +++ b/xwords4/linux/cursesmain.c @@ -444,6 +444,22 @@ showStatus( CursesAppGlobals* globals ) static XP_Bool handleQuit( CursesAppGlobals* globals ) { + if ( !!globals->cGlobals.params->fileName ) { + XWStreamCtxt* outStream; + + outStream = mem_stream_make( + MPPARM(globals->cGlobals.params->util->mpool) + globals->cGlobals.params->vtMgr, + &globals->cGlobals, 0, writeToFile ); + stream_open( outStream ); + + game_saveToStream( &globals->cGlobals.game, + &globals->cGlobals.params->gi, + outStream ); + + stream_destroy( outStream ); + } + globals->timeToExit = XP_TRUE; return XP_TRUE; } /* handleQuit */ @@ -1336,12 +1352,25 @@ cursesmain( XP_Bool isServer, LaunchParams* params ) g_globals.draw = (struct CursesDrawCtx*) cursesDrawCtxtMake( g_globals.boardWin ); - - gameID = (XP_U16)util_getCurSeconds( g_globals.cGlobals.params->util ); - game_makeNewGame( MEMPOOL &g_globals.cGlobals.game, ¶ms->gi, - params->util, (DrawCtx*)g_globals.draw, - gameID, &g_globals.cp, LINUX_SEND, - IF_CH(linux_reset) &g_globals ); + + if ( !!params->fileName && file_exists( params->fileName ) ) { + XWStreamCtxt* stream; + stream = streamFromFile( &g_globals.cGlobals, params->fileName, &g_globals ); + + (void)game_makeFromStream( MEMPOOL stream, &g_globals.cGlobals.game, + ¶ms->gi, dict, params->util, + (DrawCtx*)g_globals.draw, + &g_globals.cp, + LINUX_SEND, IF_CH(linux_reset) &g_globals ); + + stream_destroy( stream ); + } else { + gameID = (XP_U16)util_getCurSeconds( g_globals.cGlobals.params->util ); + game_makeNewGame( MEMPOOL &g_globals.cGlobals.game, ¶ms->gi, + params->util, (DrawCtx*)g_globals.draw, + gameID, &g_globals.cp, LINUX_SEND, + IF_CH(linux_reset) &g_globals ); + } #ifndef XWFEATURE_STANDALONE_ONLY if ( g_globals.cGlobals.game.comms ) { diff --git a/xwords4/linux/gtkmain.c b/xwords4/linux/gtkmain.c index df568ab1b..c812e9f63 100644 --- a/xwords4/linux/gtkmain.c +++ b/xwords4/linux/gtkmain.c @@ -29,7 +29,6 @@ #include #include -#include #include #include #include @@ -62,7 +61,6 @@ #ifndef XWFEATURE_STANDALONE_ONLY static void sendOnClose( XWStreamCtxt* stream, void* closure ); #endif -static XP_Bool file_exists( const char* fileName ); static void setCtrlsForTray( GtkAppGlobals* globals ); static void printFinalScores( GtkAppGlobals* globals ); @@ -289,29 +287,6 @@ key_release_event( GtkWidget* XP_UNUSED(widget), GdkEventKey* event, # define MEMPOOL #endif -static XWStreamCtxt* -streamFromFile( GtkAppGlobals* globals, char* name ) -{ - XP_U8* buf; - struct stat statBuf; - FILE* f; - XWStreamCtxt* stream; - - (void)stat( name, &statBuf ); - buf = malloc( statBuf.st_size ); - f = fopen( name, "r" ); - fread( buf, statBuf.st_size, 1, f ); - fclose( f ); - - stream = mem_stream_make( MEMPOOL - globals->cGlobals.params->vtMgr, - globals, CHANNEL_NONE, NULL ); - stream_putBytes( stream, buf, statBuf.st_size ); - free( buf ); - - return stream; -} /* streamFromFile */ - static void createOrLoadObjects( GtkAppGlobals* globals ) { @@ -329,7 +304,7 @@ createOrLoadObjects( GtkAppGlobals* globals ) if ( !!params->fileName && file_exists( params->fileName ) ) { - stream = streamFromFile( globals, params->fileName ); + stream = streamFromFile( &globals->cGlobals, params->fileName, globals ); opened = game_makeFromStream( MEMPOOL stream, &globals->cGlobals.game, &globals->cGlobals.params->gi, @@ -549,25 +524,6 @@ handle_client_event( GtkWidget *widget, GdkEventClient *event, } /* handle_client_event */ #endif -static void -writeToFile( XWStreamCtxt* stream, void* closure ) -{ - void* buf; - FILE* file; - XP_U16 len; - GtkAppGlobals* globals = (GtkAppGlobals*)closure; - - len = stream_getSize( stream ); - buf = malloc( len ); - stream_getBytes( stream, buf, len ); - - file = fopen( globals->cGlobals.params->fileName, "w" ); - fwrite( buf, 1, len, file ); - fclose( file ); - - free( buf ); -} /* writeToFile */ - static void quit( void* XP_UNUSED(dunno), GtkAppGlobals* globals ) { @@ -1469,15 +1425,6 @@ gtk_util_userQuery( XW_UtilCtxt* XP_UNUSED(uc), UtilQueryID id, return result; } /* gtk_util_userQuery */ -static XP_Bool -file_exists( const char* fileName ) -{ - struct stat statBuf; - - int statResult = stat( fileName, &statBuf ); - return statResult == 0; -} /* file_exists */ - static GtkWidget* makeShowButtonFromBitmap( void* closure, const gchar* filename, const gchar* alt, GCallback func ) diff --git a/xwords4/linux/linuxmain.c b/xwords4/linux/linuxmain.c index e21963456..ae0c6197c 100644 --- a/xwords4/linux/linuxmain.c +++ b/xwords4/linux/linuxmain.c @@ -20,6 +20,7 @@ #include #include #include +#include #include /* gethostbyname */ #include @@ -63,6 +64,57 @@ #define DEFAULT_PORT 10999 #define DEFAULT_LISTEN_PORT 4998 +XP_Bool +file_exists( const char* fileName ) +{ + struct stat statBuf; + + int statResult = stat( fileName, &statBuf ); + return statResult == 0; +} /* file_exists */ + +XWStreamCtxt* +streamFromFile( CommonGlobals* cGlobals, char* name, void* closure ) +{ + XP_U8* buf; + struct stat statBuf; + FILE* f; + XWStreamCtxt* stream; + + (void)stat( name, &statBuf ); + buf = malloc( statBuf.st_size ); + f = fopen( name, "r" ); + fread( buf, statBuf.st_size, 1, f ); + fclose( f ); + + stream = mem_stream_make( MPPARM(cGlobals->params->util->mpool) + cGlobals->params->vtMgr, + closure, CHANNEL_NONE, NULL ); + stream_putBytes( stream, buf, statBuf.st_size ); + free( buf ); + + return stream; +} /* streamFromFile */ + +void +writeToFile( XWStreamCtxt* stream, void* closure ) +{ + void* buf; + FILE* file; + XP_U16 len; + CommonGlobals* cGlobals = (CommonGlobals*)closure; + + len = stream_getSize( stream ); + buf = malloc( len ); + stream_getBytes( stream, buf, len ); + + file = fopen( cGlobals->params->fileName, "w" ); + fwrite( buf, 1, len, file ); + fclose( file ); + + free( buf ); +} /* writeToFile */ + void catOnClose( XWStreamCtxt* stream, void* XP_UNUSED(closure) ) { diff --git a/xwords4/linux/linuxmain.h b/xwords4/linux/linuxmain.h index 4fdc77e41..a4751ea8c 100644 --- a/xwords4/linux/linuxmain.h +++ b/xwords4/linux/linuxmain.h @@ -58,6 +58,11 @@ XP_UCHAR* strFromStream( XWStreamCtxt* stream ); void catGameHistory( CommonGlobals* cGlobals ); void catOnClose( XWStreamCtxt* stream, void* closure ); +XP_Bool file_exists( const char* fileName ); +XWStreamCtxt* streamFromFile( CommonGlobals* cGlobals, char* name, + void* closure ); +void writeToFile( XWStreamCtxt* stream, void* closure ); + #ifdef KEYBOARD_NAV XP_Bool linShiftFocus( CommonGlobals* cGlobals, XP_Key key,