From 4e2b080be5fced857594a61300273fe69d8af740 Mon Sep 17 00:00:00 2001 From: ehouse Date: Tue, 25 Apr 2006 13:31:15 +0000 Subject: [PATCH] Add, and use, stream_putString. Saves 500 bytes on win32. --- common/mempool.c | 4 ++-- common/memstream.c | 8 ++++++++ common/model.c | 2 +- common/mscore.c | 6 +++--- common/server.c | 22 +++++++++++----------- common/xwstream.h | 4 ++++ linux/filestream.c | 7 +++++++ wince/cedict.c | 8 ++++---- wince/cemain.c | 10 ++++------ 9 files changed, 44 insertions(+), 27 deletions(-) diff --git a/common/mempool.c b/common/mempool.c index e7b30ad95..1442b824f 100644 --- a/common/mempool.c +++ b/common/mempool.c @@ -221,13 +221,13 @@ mpool_stats( MemPoolCtx* mpool, XWStreamCtxt* stream ) "Number of free blocks: %d\n" "Total number of blocks allocated: %d\n", mpool->nUsed, mpool->nFree, mpool->nAllocs ); - stream_putBytes( stream, buf, (XP_U16)XP_STRLEN(buf) ); + stream_putString( stream, buf ); for ( entry = mpool->usedList; !!entry; entry = entry->next ) { XP_SNPRINTF( buf, sizeof(buf), (XP_UCHAR*)"%ld byte block allocated %s: line %ld\n", entry->size, entry->fileName, entry->lineNo ); - stream_putBytes( stream, buf, (XP_U16)XP_STRLEN(buf) ); + stream_putString( stream, buf ); } } /* mpool_stats */ diff --git a/common/memstream.c b/common/memstream.c index 279a42787..c4648c725 100644 --- a/common/memstream.c +++ b/common/memstream.c @@ -212,6 +212,13 @@ mem_stream_putBytes( XWStreamCtxt* p_sctx, void* whence, XP_U16 count ) stream->curWritePos += count; } /* mem_stream_putBytes */ +static void +mem_stream_putString( XWStreamCtxt* p_sctx, const char* whence ) +{ + XP_U16 len = XP_STRLEN( whence ); + mem_stream_putBytes( p_sctx, (void*)whence, len ); +} + static void mem_stream_putU8( XWStreamCtxt* p_sctx, XP_U8 data ) { @@ -413,6 +420,7 @@ make_vtable( MemStreamCtxt* stream ) SET_VTABLE_ENTRY( vtable, stream_putU8, mem ); SET_VTABLE_ENTRY( vtable, stream_putBytes, mem ); + SET_VTABLE_ENTRY( vtable, stream_putString, mem ); SET_VTABLE_ENTRY( vtable, stream_putU16, mem ); SET_VTABLE_ENTRY( vtable, stream_putU32, mem ); SET_VTABLE_ENTRY( vtable, stream_putBits, mem ); diff --git a/common/model.c b/common/model.c index 87715d702..9cf1e654c 100644 --- a/common/model.c +++ b/common/model.c @@ -1370,7 +1370,7 @@ notifyTrayListeners( ModelCtxt* model, XP_U16 turn, TileBit bits ) static void printString( XWStreamCtxt* stream, XP_UCHAR* str ) { - stream_putBytes( stream, str, (XP_U16)XP_STRLEN((char*)str) ); + stream_putString( stream, str ); } /* printString */ static XP_UCHAR* diff --git a/common/mscore.c b/common/mscore.c index aa4b84a41..be82233bc 100644 --- a/common/mscore.c +++ b/common/mscore.c @@ -516,7 +516,7 @@ figureMoveScore( ModelCtxt* model, MoveInfo* moveInfo, EngineCtxt* engine, if ( !!stream ) { XP_UCHAR* bstr = util_getUserString( model->vol.util, STR_BONUS_ALL ); - stream_putBytes( stream, bstr, (XP_U8)XP_STRLEN(bstr) ); + stream_putString( stream, bstr ); } } @@ -832,7 +832,7 @@ formatWordScore( XWStreamCtxt* stream, XP_U16 wordScore, } XP_ASSERT( XP_STRLEN(tmpBuf) < sizeof(tmpBuf) ); - stream_putBytes( stream, tmpBuf, (XP_U16)XP_STRLEN(tmpBuf) ); + stream_putString( stream, tmpBuf ); } } /* formatWordScore */ @@ -844,7 +844,7 @@ formatSummary( XWStreamCtxt* stream, ModelCtxt* model, XP_U16 score ) util_getUserString(model->vol.util, STRD_TURN_SCORE), score); XP_ASSERT( XP_STRLEN(buf) < sizeof(buf) ); - stream_putBytes( stream, buf, (XP_U16)XP_STRLEN(buf) ); + stream_putString( stream, buf ); } /* formatSummary */ #ifdef CPLUS diff --git a/common/server.c b/common/server.c index 8a6f74bb8..5fb0610eb 100644 --- a/common/server.c +++ b/common/server.c @@ -658,7 +658,7 @@ makeRobotMove( ServerCtxt* server ) str = util_getUserString(util, STRD_ROBOT_TRADED); XP_SNPRINTF( buf, sizeof(buf), str, MAX_TRAY_TILES ); - stream_putBytes( stream, buf, (XP_U16)XP_STRLEN(buf) ); + stream_putString( stream, buf ); XP_ASSERT( !server->vol.prevMoveStream ); server->vol.prevMoveStream = stream; } @@ -733,7 +733,7 @@ showPrevScore( ServerCtxt* server ) stream = mkServerStream( server ); str = util_getUserString( util, strCode ); - stream_putBytes( stream, str, (XP_U16)XP_STRLEN(str) ); + stream_putString( stream, str ); if ( !!server->vol.prevMoveStream ) { XWStreamCtxt* prevStream = server->vol.prevMoveStream; @@ -1762,7 +1762,7 @@ reflectMoveAndInform( ServerCtxt* server, XWStreamCtxt* stream ) XP_SNPRINTF( tradeBuf, sizeof(tradeBuf), tradeStr, tradedTiles.nTiles ); mvStream = mkServerStream( server ); - stream_putBytes( mvStream, tradeBuf, XP_STRLEN(tradeBuf) ); + stream_putString( mvStream, tradeBuf ); } } else { @@ -2343,7 +2343,7 @@ server_formatDictCounts( ServerCtxt* server, XWStreamCtxt* stream, dict = model_getDictionary( server->vol.model ); dname = dict_getShortName( dict ); XP_SNPRINTF( buf, sizeof(buf), fmt, dname ); - stream_putBytes( stream, buf, XP_STRLEN(buf) ); + stream_putString( stream, buf ); nChars = dict_numTileFaces( dict ); @@ -2360,16 +2360,16 @@ server_formatDictCounts( ServerCtxt* server, XWStreamCtxt* stream, XP_SNPRINTF( buf, sizeof(buf), (XP_UCHAR*)"%s: %d/%d", face, count, value ); - stream_putBytes( stream, buf, (XP_U16)XP_STRLEN(buf) ); + stream_putString( stream, buf ); } if ( ++tile >= nChars ) { break; } else if ( count > 0 ) { if ( ++nPrinted % nCols == 0 ) { - stream_putBytes( stream, XP_CR, (XP_U16)XP_STRLEN(XP_CR) ); + stream_putString( stream, XP_CR ); } else { - stream_putBytes( stream, (void*)" ", 3 ); + stream_putString( stream, (void*)" " ); } } } @@ -2429,13 +2429,13 @@ server_formatRemainingTiles( ServerCtxt* server, XWStreamCtxt* stream, buf[0] = '\0'; putNTiles( buf, face, count ); - stream_putBytes( stream, buf, (XP_U16)XP_STRLEN(buf) ); + stream_putString( stream, buf ); } if ( ++tile >= nChars ) { break; } else if ( count > 0 ) { - stream_putBytes( stream, (void*)" ", 3 ); + stream_putString( stream, (void*)" " ); } } } /* server_formatRemainingTiles */ @@ -2483,7 +2483,7 @@ server_writeFinalScores( ServerCtxt* server, XWStreamCtxt* stream ) if ( highestIndex == -1 ) { break; /* we're done */ } else if ( place > 1 ) { - stream_putBytes( stream, XP_CR, (XP_U16)XP_STRLEN(XP_CR) ); + stream_putString( stream, XP_CR ); } scores[highestIndex] = IMPOSSIBLY_LOW_SCORE; @@ -2514,7 +2514,7 @@ server_writeFinalScores( ServerCtxt* server, XWStreamCtxt* stream ) place, emptyStringIfNull(gi->players[highestIndex].name), highestScore, curScore, tmpbuf, timeStr ); - stream_putBytes( stream, buf, (XP_U16)XP_STRLEN(buf) ); + stream_putString( stream, buf ); } } /* server_writeFinalScores */ diff --git a/common/xwstream.h b/common/xwstream.h index b3b17ad9f..05c2cb3e7 100644 --- a/common/xwstream.h +++ b/common/xwstream.h @@ -44,6 +44,7 @@ typedef struct StreamCtxVTable { void (*m_stream_putU8)( XWStreamCtxt* dctx, XP_U8 byt ); void (*m_stream_putBytes)( XWStreamCtxt* dctx, void* whence, XP_U16 count ); + void (*m_stream_putString)( XWStreamCtxt* dctx, const char* whence ); void (*m_stream_putU16)( XWStreamCtxt* dctx, XP_U16 data ); void (*m_stream_putU32)( XWStreamCtxt* dctx, XP_U32 data ); void (*m_stream_putBits)( XWStreamCtxt* dctx, XP_U16 nBits, XP_U32 bits ); @@ -103,6 +104,9 @@ struct XWStreamCtxt { #define stream_putBytes( sc, w, c ) \ (sc)->vtable->m_stream_putBytes((sc), (w), (c)) +#define stream_putString( sc, w ) \ + (sc)->vtable->m_stream_putString((sc), (w)) + #define stream_putU16(sc, d) \ (sc)->vtable->m_stream_putU16((sc), (d)) diff --git a/linux/filestream.c b/linux/filestream.c index ce75ce667..e14a6f24e 100644 --- a/linux/filestream.c +++ b/linux/filestream.c @@ -90,6 +90,12 @@ linux_file_stream_putBytes( XWStreamCtxt* p_sctx, void* where, XP_ASSERT( written != 0 ); } /* linux_file_stream_putBytes */ +static void +linux_file_stream_putString( XWStreamCtxt* p_sctx, const char* where ) +{ + linux_file_stream_putBytes( p_sctx, (void*)where, XP_STRLEN(where) ); +} + static void linux_file_stream_putU8( XWStreamCtxt* p_sctx, XP_U8 data ) { @@ -152,6 +158,7 @@ make_vtable( LinuxFileStreamCtxt* stream ) SET_VTABLE_ENTRY( stream, stream_putU8, linux_file ); SET_VTABLE_ENTRY( stream, stream_putBytes, linux_file ); + SET_VTABLE_ENTRY( stream, stream_putString, linux_file ); SET_VTABLE_ENTRY( stream, stream_putU16, linux_file ); SET_VTABLE_ENTRY( stream, stream_putU32, linux_file ); diff --git a/wince/cedict.c b/wince/cedict.c index 67cbff577..843e8c41a 100755 --- a/wince/cedict.c +++ b/wince/cedict.c @@ -706,7 +706,7 @@ formatDirsCB( wchar_t* dir, void* ctxt ) int len; if ( datap->firstPassDone ) { - stream_putBytes( datap->stream, ", ", 2 ); + stream_putString( datap->stream, ", " ); } else { datap->firstPassDone = XP_TRUE; } @@ -714,7 +714,7 @@ formatDirsCB( wchar_t* dir, void* ctxt ) len = WideCharToMultiByte( CP_ACP, 0, dir, -1, narrow, sizeof(narrow)/sizeof(narrow[0]), NULL, NULL ); - stream_putBytes( datap->stream, narrow, len-1 ); /* skip null */ + stream_putString( datap->stream, narrow ); return XP_FALSE; } /* formatDirsCB */ @@ -770,12 +770,12 @@ ceFormatDictDirs( XWStreamCtxt* stream, HINSTANCE hInstance ) } if ( id != IDS_DICTDIRS ) { - stream_putBytes( stream, ", ", 2 ); + stream_putString( stream, ", " ); } len = WideCharToMultiByte( CP_ACP, 0, wide, -1, narrow, sizeof(narrow)/sizeof(narrow[0]), NULL, NULL ); - stream_putBytes( stream, narrow, len-1 ); /* skip null */ + stream_putString( stream, narrow ); } } #endif /* USE_FOREACH */ diff --git a/wince/cemain.c b/wince/cemain.c index 3cd667e44..d2f809202 100755 --- a/wince/cemain.c +++ b/wince/cemain.c @@ -975,13 +975,11 @@ InitInstance(HINSTANCE hInstance, int nCmdShow) globals ); if ( !result ) { XWStreamCtxt* stream = make_generic_stream( globals ); - char* str = "Please install a Crosswords dictionary " - "in one of these directories: "; - stream_putBytes( stream, str, XP_STRLEN(str) ); + stream_putString( stream, "Please install a Crosswords dictionary " + "in one of these directories: " ); ceFormatDictDirs( stream, hInstance ); - str = ". Download dictionaries from http://xwords.sf.net."; - stream_putBytes( stream, str, XP_STRLEN(str) ); - + stream_putString( stream, ". Download dictionaries from " + "http://xwords.sf.net." ); messageBoxStream( globals, stream, L"Dictionary Not Found" ); stream_destroy( stream ); return FALSE;