From fecc2a50d1f3d1c5aeaad13093dae243dad63f9f Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Sun, 27 Apr 2008 15:27:39 +0000 Subject: [PATCH] Change function levFindDataSet: * Make it return it's result rather than write it in an output pointer (NULL will indicate failure instead of `false`) * Don't use two nested if-statements, use a single if-statement with double conditions using logical and (&&) git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@4819 4a71c877-e1ca-e34f-864e-861f7616d084 --- src/game.c | 8 +++++--- src/levels.c | 43 +++++++++++++++++++++-------------------- src/levels.h | 2 +- src/multiint.c | 2 +- src/scriptfuncs.c | 3 ++- src/scriptobj.c | 3 ++- src/scriptvals_parser.y | 3 ++- 7 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/game.c b/src/game.c index 29f346894..c4c47590e 100644 --- a/src/game.c +++ b/src/game.c @@ -4295,7 +4295,8 @@ bool gameLoadV7(PHYSFS_file* fileHandle) return false; } // find the level dataset - if (!levFindDataSet(aLevelName, &psNewLevel)) + psNewLevel = levFindDataSet(aLevelName); + if (psNewLevel == NULL) { debug( LOG_ERROR, "gameLoadV7: couldn't find level data" ); abort(); @@ -4771,7 +4772,8 @@ bool gameLoadV(PHYSFS_file* fileHandle, unsigned int version) return false; } // find the level dataset -/* if (!levFindDataSet(aLevelName, &psNewLevel)) +/* psNewLevel = levFindDataSet(aLevelName); + if (psNewLevel == NULL) { DBERROR(("gameLoadV6: couldn't find level data")); return false; @@ -11855,7 +11857,7 @@ BOOL plotStructurePreview16(char *backDropSprite, UBYTE scale, UDWORD offX, UDWO LEVEL_DATASET *psLevel; PIELIGHT color = WZCOL_BLACK ; - levFindDataSet(game.map, &psLevel); + psLevel = levFindDataSet(game.map); strcpy(aFileName,psLevel->apDataFiles[0]); aFileName[strlen(aFileName)-4] = '\0'; strcat(aFileName, "/struct.bjo"); diff --git a/src/levels.c b/src/levels.c index c509c071b..784c4a2c9 100644 --- a/src/levels.c +++ b/src/levels.c @@ -137,24 +137,25 @@ void levError(const char *pError) #endif } -// find the level dataset -BOOL levFindDataSet(const char* name, LEVEL_DATASET **ppsDataSet) +/** Find a level dataset with the given name. + * @param name the name of the dataset to search for. + * @return a dataset with associated with the given @c name, or NULL if none + * could be found. + */ +LEVEL_DATASET* levFindDataSet(const char* name) { - LEVEL_DATASET *psNewLevel; + LEVEL_DATASET* psNewLevel; - for(psNewLevel = psLevels; psNewLevel; psNewLevel = psNewLevel->psNext) + for (psNewLevel = psLevels; psNewLevel; psNewLevel = psNewLevel->psNext) { - if (psNewLevel->pName != NULL) + if (psNewLevel->pName != NULL + && strcmp(psNewLevel->pName, name) == 0) { - if (strcmp(psNewLevel->pName, name) == 0) - { - *ppsDataSet = psNewLevel; - return true; - } + return psNewLevel; } } - return false; + return NULL; } // parse a level description data file @@ -162,13 +163,11 @@ BOOL levParse(char *pBuffer, SDWORD size, searchPathMode datadir) { SDWORD token, state, currData=0; LEVEL_DATASET *psDataSet = NULL; - LEVEL_DATASET *psFoundData; levSetInputBuffer(pBuffer, size); state = LP_START; - token = lev_lex(); - while (token != 0) + for (token = lev_lex(); token != 0; token = lev_lex()) { switch (token) { @@ -343,8 +342,10 @@ BOOL levParse(char *pBuffer, SDWORD size, searchPathMode datadir) { if (psDataSet->type == LDS_CAMCHANGE) { - // campaign change dataset - need to find the full data set - if (!levFindDataSet(pLevToken, &psFoundData)) + // This is a campaign change dataset, we need to find the full data set. + LEVEL_DATASET * const psFoundData = levFindDataSet(pLevToken); + + if (psFoundData == NULL) { levError("Cannot find full data set for camchange"); return false; @@ -372,7 +373,9 @@ BOOL levParse(char *pBuffer, SDWORD size, searchPathMode datadir) else if (state == LP_DATASET) { // find the dataset - if (!levFindDataSet(pLevToken, &psDataSet->psBaseData)) + psDataSet->psBaseData = levFindDataSet(pLevToken); + + if (psDataSet->psBaseData == NULL) { levError("Unknown dataset"); return false; @@ -425,9 +428,6 @@ BOOL levParse(char *pBuffer, SDWORD size, searchPathMode datadir) levError("Unexpected token"); break; } - - // get the next token - token = lev_lex(); } lev_lex_destroy(); @@ -581,7 +581,8 @@ BOOL levLoadData(const char* name, char *pSaveName, SDWORD saveType) levelLoadType = saveType; // find the level dataset - if (!levFindDataSet(name, &psNewLevel)) + psNewLevel = levFindDataSet(name); + if (psNewLevel == NULL) { debug(LOG_NEVER, "levLoadData: dataset %s not found - trying to load as WRF", name); return levLoadSingleWRF(name); diff --git a/src/levels.h b/src/levels.h index ae3f6fae6..a454967b6 100644 --- a/src/levels.h +++ b/src/levels.h @@ -84,7 +84,7 @@ extern BOOL levInitialise(void); extern BOOL levLoadData(const char* name, char *pSaveName, SDWORD saveType); // find the level dataset -extern BOOL levFindDataSet(const char* name, LEVEL_DATASET **ppsDataSet); +extern LEVEL_DATASET* levFindDataSet(const char* name); // free the currently loaded dataset extern BOOL levReleaseAll(void); diff --git a/src/multiint.c b/src/multiint.c index 78ed02042..ef53dff6d 100644 --- a/src/multiint.c +++ b/src/multiint.c @@ -195,7 +195,7 @@ void loadMapPreview(void) } // load the terrain types - levFindDataSet(game.map, &psLevel); + psLevel = levFindDataSet(game.map); rebuildSearchPath(psLevel->dataDir, false); strcpy(aFileName,psLevel->apDataFiles[0]); aFileName[strlen(aFileName)-4] = '\0'; diff --git a/src/scriptfuncs.c b/src/scriptfuncs.c index 231ffb4b1..2ca255708 100644 --- a/src/scriptfuncs.c +++ b/src/scriptfuncs.c @@ -3687,7 +3687,8 @@ BOOL scrStartMission(void) strlcpy(aLevelName, pGame, sizeof(aLevelName)); // find the level dataset - if (!levFindDataSet(pGame, &psNewLevel)) + psNewLevel = levFindDataSet(pGame); + if (psNewLevel == NULL) { debug( LOG_ERROR, "scrStartMission: couldn't find level data" ); abort(); diff --git a/src/scriptobj.c b/src/scriptobj.c index 358340d74..df4f7c3dc 100644 --- a/src/scriptobj.c +++ b/src/scriptobj.c @@ -1340,7 +1340,8 @@ BOOL scrValDefLoad(SDWORD version, INTERP_VAL *psVal, char *pBuffer, UDWORD size } else { - if (!levFindDataSet(pBuffer, &psLevel)) + psLevel = levFindDataSet(pBuffer); + if (psLevel == NULL) { debug( LOG_ERROR, "scrValDefLoad: couldn't find level dataset %s", pBuffer ); abort(); diff --git a/src/scriptvals_parser.y b/src/scriptvals_parser.y index 280d8f357..032b80b91 100644 --- a/src/scriptvals_parser.y +++ b/src/scriptvals_parser.y @@ -638,7 +638,8 @@ var_init: var_entry TYPE var_value YYABORT; } // just check the level exists - if (!levFindDataSet($3.pString, &psLevel)) + psLevel = levFindDataSet($3.pString); + if (psLevel == NULL) { scrv_error("Level %s not found", $3.pString); YYABORT;