fix ncurses version so it can save games and read them back in: make existing gtk code run on both.

This commit is contained in:
ehouse 2008-09-18 03:46:27 +00:00
parent 78c5574bce
commit c6da802dc9
4 changed files with 93 additions and 60 deletions

View file

@ -444,6 +444,22 @@ showStatus( CursesAppGlobals* globals )
static XP_Bool static XP_Bool
handleQuit( CursesAppGlobals* globals ) 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; globals->timeToExit = XP_TRUE;
return XP_TRUE; return XP_TRUE;
} /* handleQuit */ } /* handleQuit */
@ -1336,12 +1352,25 @@ cursesmain( XP_Bool isServer, LaunchParams* params )
g_globals.draw = (struct CursesDrawCtx*) g_globals.draw = (struct CursesDrawCtx*)
cursesDrawCtxtMake( g_globals.boardWin ); cursesDrawCtxtMake( g_globals.boardWin );
gameID = (XP_U16)util_getCurSeconds( g_globals.cGlobals.params->util ); if ( !!params->fileName && file_exists( params->fileName ) ) {
game_makeNewGame( MEMPOOL &g_globals.cGlobals.game, &params->gi, XWStreamCtxt* stream;
params->util, (DrawCtx*)g_globals.draw, stream = streamFromFile( &g_globals.cGlobals, params->fileName, &g_globals );
gameID, &g_globals.cp, LINUX_SEND,
IF_CH(linux_reset) &g_globals ); (void)game_makeFromStream( MEMPOOL stream, &g_globals.cGlobals.game,
&params->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, &params->gi,
params->util, (DrawCtx*)g_globals.draw,
gameID, &g_globals.cp, LINUX_SEND,
IF_CH(linux_reset) &g_globals );
}
#ifndef XWFEATURE_STANDALONE_ONLY #ifndef XWFEATURE_STANDALONE_ONLY
if ( g_globals.cGlobals.game.comms ) { if ( g_globals.cGlobals.game.comms ) {

View file

@ -29,7 +29,6 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h>
#include <ctype.h> #include <ctype.h>
#include <gdk/gdkkeysyms.h> #include <gdk/gdkkeysyms.h>
#include <errno.h> #include <errno.h>
@ -62,7 +61,6 @@
#ifndef XWFEATURE_STANDALONE_ONLY #ifndef XWFEATURE_STANDALONE_ONLY
static void sendOnClose( XWStreamCtxt* stream, void* closure ); static void sendOnClose( XWStreamCtxt* stream, void* closure );
#endif #endif
static XP_Bool file_exists( const char* fileName );
static void setCtrlsForTray( GtkAppGlobals* globals ); static void setCtrlsForTray( GtkAppGlobals* globals );
static void printFinalScores( GtkAppGlobals* globals ); static void printFinalScores( GtkAppGlobals* globals );
@ -289,29 +287,6 @@ key_release_event( GtkWidget* XP_UNUSED(widget), GdkEventKey* event,
# define MEMPOOL # define MEMPOOL
#endif #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 static void
createOrLoadObjects( GtkAppGlobals* globals ) createOrLoadObjects( GtkAppGlobals* globals )
{ {
@ -329,7 +304,7 @@ createOrLoadObjects( GtkAppGlobals* globals )
if ( !!params->fileName && file_exists( params->fileName ) ) { 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, opened = game_makeFromStream( MEMPOOL stream, &globals->cGlobals.game,
&globals->cGlobals.params->gi, &globals->cGlobals.params->gi,
@ -549,25 +524,6 @@ handle_client_event( GtkWidget *widget, GdkEventClient *event,
} /* handle_client_event */ } /* handle_client_event */
#endif #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 static void
quit( void* XP_UNUSED(dunno), GtkAppGlobals* globals ) quit( void* XP_UNUSED(dunno), GtkAppGlobals* globals )
{ {
@ -1469,15 +1425,6 @@ gtk_util_userQuery( XW_UtilCtxt* XP_UNUSED(uc), UtilQueryID id,
return result; return result;
} /* gtk_util_userQuery */ } /* 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* static GtkWidget*
makeShowButtonFromBitmap( void* closure, const gchar* filename, makeShowButtonFromBitmap( void* closure, const gchar* filename,
const gchar* alt, GCallback func ) const gchar* alt, GCallback func )

View file

@ -20,6 +20,7 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/stat.h>
#include <netdb.h> /* gethostbyname */ #include <netdb.h> /* gethostbyname */
#include <errno.h> #include <errno.h>
@ -63,6 +64,57 @@
#define DEFAULT_PORT 10999 #define DEFAULT_PORT 10999
#define DEFAULT_LISTEN_PORT 4998 #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 void
catOnClose( XWStreamCtxt* stream, void* XP_UNUSED(closure) ) catOnClose( XWStreamCtxt* stream, void* XP_UNUSED(closure) )
{ {

View file

@ -58,6 +58,11 @@ XP_UCHAR* strFromStream( XWStreamCtxt* stream );
void catGameHistory( CommonGlobals* cGlobals ); void catGameHistory( CommonGlobals* cGlobals );
void catOnClose( XWStreamCtxt* stream, void* closure ); 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 #ifdef KEYBOARD_NAV
XP_Bool linShiftFocus( CommonGlobals* cGlobals, XP_Key key, XP_Bool linShiftFocus( CommonGlobals* cGlobals, XP_Key key,