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 ac03c4be61
commit f021964213
4 changed files with 93 additions and 60 deletions

View file

@ -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, &params->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,
&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
if ( g_globals.cGlobals.game.comms ) {

View file

@ -29,7 +29,6 @@
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/stat.h>
#include <ctype.h>
#include <gdk/gdkkeysyms.h>
#include <errno.h>
@ -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 )

View file

@ -20,6 +20,7 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <netdb.h> /* gethostbyname */
#include <errno.h>
@ -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) )
{

View file

@ -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,