From ddb01d8930efaafb45b8bc80242e505abb94cb70 Mon Sep 17 00:00:00 2001 From: Eric House Date: Sun, 19 Apr 2020 10:44:57 -0700 Subject: [PATCH] refactor: struct ptr replaces a bunch of params --- xwords4/common/board.c | 16 +++++--------- xwords4/common/engine.c | 12 +++------- xwords4/common/model.c | 49 ++++++++++++----------------------------- xwords4/common/model.h | 16 +++++++++----- xwords4/common/mscore.c | 9 +++++--- xwords4/common/server.c | 18 +++++---------- 6 files changed, 45 insertions(+), 75 deletions(-) diff --git a/xwords4/common/board.c b/xwords4/common/board.c index 90f893824..4e0f067b9 100644 --- a/xwords4/common/board.c +++ b/xwords4/common/board.c @@ -1040,19 +1040,13 @@ typedef struct _BadWordList { } BadWordList; static void -saveBadWords( const XP_UCHAR* word, XP_Bool isLegal, - const DictionaryCtxt* XP_UNUSED(dict), -#ifdef XWFEATURE_BOARDWORDS - const MoveInfo* XP_UNUSED(movei), - XP_U16 XP_UNUSED(start), XP_U16 XP_UNUSED(end), -#endif - void* closure ) +saveBadWords( const WNParams* wnp ) { - if ( !isLegal ) { - BadWordList* bwlp = (BadWordList*)closure; + if ( !wnp->isLegal ) { + BadWordList* bwlp = (BadWordList*)wnp->closure; bwlp->bwi.words[bwlp->bwi.nWords] = &bwlp->buf[bwlp->index]; - XP_STRCAT( &bwlp->buf[bwlp->index], word ); - bwlp->index += XP_STRLEN(word) + 1; + XP_STRCAT( &bwlp->buf[bwlp->index], wnp->word ); + bwlp->index += XP_STRLEN(wnp->word) + 1; ++bwlp->bwi.nWords; } } /* saveBadWords */ diff --git a/xwords4/common/engine.c b/xwords4/common/engine.c index 8ba23d751..de909d7f3 100644 --- a/xwords4/common/engine.c +++ b/xwords4/common/engine.c @@ -1104,16 +1104,10 @@ considerMove( EngineCtxt* engine, Tile* tiles, XP_S16 tileLength, } /* considerMove */ static void -countWords( const XP_UCHAR* XP_UNUSED(word), XP_Bool isLegal, - const DictionaryCtxt* XP_UNUSED(dict), -#ifdef XWFEATURE_BOARDWORDS - const MoveInfo* XP_UNUSED(movei), XP_U16 XP_UNUSED(start), - XP_U16 XP_UNUSED(end), -#endif - void* closure ) +countWords( const WNParams* wnp ) { - XP_U16* wcp = (XP_U16*)closure; - if ( isLegal ) { + XP_U16* wcp = (XP_U16*)wnp->closure; + if ( wnp->isLegal ) { ++*wcp; } } diff --git a/xwords4/common/model.c b/xwords4/common/model.c index a493a2d6a..50ad3ffeb 100644 --- a/xwords4/common/model.c +++ b/xwords4/common/model.c @@ -77,13 +77,7 @@ static void loadPlayerCtxt( const ModelCtxt* model, XWStreamCtxt* stream, static void writePlayerCtxt( const ModelCtxt* model, XWStreamCtxt* stream, const PlayerCtxt* pc ); static XP_U16 model_getRecentPassCount( ModelCtxt* model ); -static void recordWord( const XP_UCHAR* word, XP_Bool isLegal, - const DictionaryCtxt* dict, - -#ifdef XWFEATURE_BOARDWORDS - const MoveInfo* movei, XP_U16 start, XP_U16 end, -#endif - void* clsur ); +static void recordWord( const WNParams* wnp ); #ifdef DEBUG typedef struct _DiffTurnState { XP_S16 lastPlayerNum; @@ -2465,17 +2459,11 @@ typedef struct _FirstWordData { } FirstWordData; static void -getFirstWord( const XP_UCHAR* word, XP_Bool XP_UNUSED(isLegal), - const DictionaryCtxt* XP_UNUSED(dict), -#ifdef XWFEATURE_BOARDWORDS - const MoveInfo* XP_UNUSED(movei), XP_U16 XP_UNUSED(start), - XP_U16 XP_UNUSED(end), -#endif - void* closure ) +getFirstWord( const WNParams* wnp ) { - FirstWordData* data = (FirstWordData*)closure; - if ( '\0' == data->word[0] && '\0' != word[0] ) { - XP_STRCAT( data->word, word ); + FirstWordData* data = (FirstWordData*)wnp->closure; + if ( '\0' == data->word[0] && '\0' != wnp->word[0] ) { + XP_STRCAT( data->word, wnp->word ); } } @@ -2560,17 +2548,10 @@ appendWithCR( XWStreamCtxt* stream, const XP_UCHAR* word, XP_U16* counter ) } static void -recordWord( const XP_UCHAR* word, XP_Bool XP_UNUSED(isLegal), - const DictionaryCtxt* XP_UNUSED(dict), -#ifdef XWFEATURE_BOARDWORDS - const MoveInfo* XP_UNUSED(movei), XP_U16 XP_UNUSED(start), - XP_U16 XP_UNUSED(end), -#endif - void* closure ) - +recordWord( const WNParams* wnp ) { - RecordWordsInfo* info = (RecordWordsInfo*)closure; - appendWithCR( info->stream, word, &info->nWords ); + RecordWordsInfo* info = (RecordWordsInfo*)wnp->closure; + appendWithCR( info->stream, wnp->word, &info->nWords ); } WordNotifierInfo* @@ -2591,22 +2572,20 @@ typedef struct _ListWordsThroughInfo { } ListWordsThroughInfo; static void -listWordsThrough( const XP_UCHAR* word, XP_Bool XP_UNUSED(isLegal), - const DictionaryCtxt* XP_UNUSED(dict), - const MoveInfo* movei, XP_U16 start, XP_U16 end, - void* closure ) +listWordsThrough( const WNParams* wnp ) { - ListWordsThroughInfo* info = (ListWordsThroughInfo*)closure; + ListWordsThroughInfo* info = (ListWordsThroughInfo*)wnp->closure; + const MoveInfo* movei = wnp->movei; XP_Bool contained = XP_FALSE; if ( movei->isHorizontal && movei->commonCoord == info->row ) { - contained = start <= info->col && end >= info->col; + contained = wnp->start <= info->col && wnp->end >= info->col; } else if ( !movei->isHorizontal && movei->commonCoord == info->col ) { - contained = start <= info->row && end >= info->row; + contained = wnp->start <= info->row && wnp->end >= info->row; } if ( contained ) { - appendWithCR( info->stream, word, &info->nWords ); + appendWithCR( info->stream, wnp->word, &info->nWords ); } } diff --git a/xwords4/common/model.h b/xwords4/common/model.h index 078dd9bf7..20074c963 100644 --- a/xwords4/common/model.h +++ b/xwords4/common/model.h @@ -282,13 +282,19 @@ void model_countAllTrayTiles( ModelCtxt* model, XP_U16* counts, /********************* scoring ********************/ -typedef void (*WordNotifierProc)( const XP_UCHAR* word, XP_Bool isLegal, - const DictionaryCtxt* dict, +typedef struct _WNParams { + const XP_UCHAR* word; + XP_Bool isLegal; + const DictionaryCtxt* dict; #ifdef XWFEATURE_BOARDWORDS - const MoveInfo* movei, XP_U16 start, - XP_U16 end, + const MoveInfo* movei; + XP_U16 start; + XP_U16 end; #endif - void* closure ); + void* closure; +} WNParams; + +typedef void (*WordNotifierProc)( const WNParams* wnp ); typedef struct WordNotifierInfo { WordNotifierProc proc; void* closure; diff --git a/xwords4/common/mscore.c b/xwords4/common/mscore.c index 2d568767c..10cdc293c 100644 --- a/xwords4/common/mscore.c +++ b/xwords4/common/mscore.c @@ -695,11 +695,14 @@ scoreWord( const ModelCtxt* model, XP_U16 turn, XP_UCHAR buf[(MAX_ROWS*2)+1]; dict_tilesToString( dict, checkWordBuf, len, buf, sizeof(buf) ); - (void)(*notifyInfo->proc)( buf, legal, dict, + + WNParams wnp = { .word = buf, .isLegal =legal, .dict = dict, #ifdef XWFEATURE_BOARDWORDS - movei, start, end, + .movei = movei, .start = start, .end = end, #endif - notifyInfo->closure ); + .closure = notifyInfo->closure, + }; + (void)(*notifyInfo->proc)( &wnp ); } if ( !!stream ) { diff --git a/xwords4/common/server.c b/xwords4/common/server.c index 476b769d3..4541920a4 100644 --- a/xwords4/common/server.c +++ b/xwords4/common/server.c @@ -2613,24 +2613,18 @@ server_setGameOverListener( ServerCtxt* server, GameOverListener gol, } /* server_setGameOverListener */ static void -storeBadWords( const XP_UCHAR* word, XP_Bool isLegal, - const DictionaryCtxt* dict, -#ifdef XWFEATURE_BOARDWORDS - const MoveInfo* XP_UNUSED(movei), XP_U16 XP_UNUSED(start), - XP_U16 XP_UNUSED(end), -#endif - void* closure ) +storeBadWords( const WNParams* wnp ) { - if ( !isLegal ) { - ServerCtxt* server = (ServerCtxt*)closure; - const XP_UCHAR* name = dict_getShortName( dict ); + if ( !wnp->isLegal ) { + ServerCtxt* server = (ServerCtxt*)wnp->closure; + const XP_UCHAR* name = dict_getShortName( wnp->dict ); - XP_LOGF( "storeBadWords called with \"%s\" (name=%s)", word, name ); + XP_LOGF( "storeBadWords called with \"%s\" (name=%s)", wnp->word, name ); if ( NULL == server->illegalWordInfo.dictName ) { server->illegalWordInfo.dictName = copyString( server->mpool, name ); } server->illegalWordInfo.words[server->illegalWordInfo.nWords++] - = copyString( server->mpool, word ); + = copyString( server->mpool, wnp->word ); } } /* storeBadWords */