From 6cb536c0d123543edd9551fdb9e793ff90678f54 Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Tue, 29 May 2007 18:12:02 +0000 Subject: [PATCH] * Now load the "STR_RES" resource type directly from a file rather than an intermediate memory buffer git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@1760 4a71c877-e1ca-e34f-864e-861f7616d084 --- lib/framework/strres.c | 15 ++++++++++++--- lib/framework/strres.h | 2 +- lib/framework/strres_lexer.l | 29 +++++++++++++++++------------ lib/framework/strresly.h | 4 +++- src/data.c | 6 +++--- 5 files changed, 36 insertions(+), 20 deletions(-) diff --git a/lib/framework/strres.c b/lib/framework/strres.c index 0c9e4bf12..136f694b5 100644 --- a/lib/framework/strres.c +++ b/lib/framework/strres.c @@ -385,15 +385,24 @@ char *strresGetString(STR_RES *psRes, UDWORD id) /* Load a string resource file */ -BOOL strresLoad(STR_RES *psRes, char *pData, UDWORD size) +BOOL strresLoad(STR_RES* psRes, const char* fileName) { psCurrRes = psRes; - strresSetInputBuffer(pData, size); - if (strres_parse() != 0) + PHYSFS_file* fileHandle = PHYSFS_openRead(fileName); + if (!fileHandle) { + debug(LOG_ERROR, "strresLoadFile: PHYSFS_openRead(\"%s\") failed with error: %s\n", fileName, PHYSFS_getLastError()); return FALSE; } + strresSetInputFile(fileHandle); + if (strres_parse() != 0) + { + PHYSFS_close(fileHandle); + return FALSE; + } + PHYSFS_close(fileHandle); + return TRUE; } diff --git a/lib/framework/strres.h b/lib/framework/strres.h index 5ab0528c5..aac89356c 100644 --- a/lib/framework/strres.h +++ b/lib/framework/strres.h @@ -80,7 +80,7 @@ extern BOOL strresGetIDString(STR_RES *psRes, char *pIDStr, char **ppStoredID); extern char *strresGetString(STR_RES *psRes, UDWORD id); /* Load a string resource file */ -extern BOOL strresLoad(STR_RES *psRes, char *pData, UDWORD size); +extern BOOL strresLoad(STR_RES* psRes, const char* fileName); /* Return the the length of a char */ extern UDWORD stringLen(const char *pStr); diff --git a/lib/framework/strres_lexer.l b/lib/framework/strres_lexer.l index 6a7e7a96a..7d6d98c9a 100644 --- a/lib/framework/strres_lexer.l +++ b/lib/framework/strres_lexer.l @@ -37,6 +37,8 @@ #include "lib/framework/strres.h" #include "lib/framework/strresly.h" +#include + /* Get the Yacc definitions */ #include "strres_parser.tab.h" @@ -52,16 +54,21 @@ static UDWORD currText=0; // Note if in a comment static BOOL inComment; -/* Pointer to the input buffer */ -static char *pInputBuffer = NULL; -static char *pEndBuffer = NULL; +/* Handle to the input file */ +static PHYSFS_file* pReadFile = NULL; #define YY_INPUT(buf,result,max_size) \ - if (pInputBuffer != pEndBuffer) { \ - buf[0] = *(pInputBuffer++); result = 1; \ - } else { \ + if (PHYSFS_eof(pReadFile)) \ + { \ + buf[0] = EOF; result = YY_NULL; \ + } \ + else { \ + result = PHYSFS_read(pReadFile, buf, 1, max_size); \ + if (result == -1) \ + { \ buf[0] = EOF; result = YY_NULL; \ - } + } \ + } %} %option nounput @@ -113,14 +120,12 @@ static char *pEndBuffer = NULL; %% -/* Set the current input buffer for the lexer */ -void strresSetInputBuffer(char *pBuffer, UDWORD size) +/* Set the current input file for the lexer */ +void strresSetInputFile(PHYSFS_file* fileHandle) { - pInputBuffer = pBuffer; - pEndBuffer = pBuffer + size; + pReadFile = fileHandle; /* Reset the lexer incase it's been used before */ -// YY_FLUSH_BUFFER; yy_flush_buffer( YY_CURRENT_BUFFER ); } diff --git a/lib/framework/strresly.h b/lib/framework/strresly.h index 4ac5d1e5a..89ca4a23e 100644 --- a/lib/framework/strresly.h +++ b/lib/framework/strresly.h @@ -23,6 +23,8 @@ #ifndef _strresly_h #define _strresly_h +#include + /* Maximum number of characters in a directory entry */ #define FILE_MAXCHAR 255 @@ -33,7 +35,7 @@ extern STR_RES *psCurrRes; /* Set the current input buffer for the lexer - used by strresLoad */ -extern void strresSetInputBuffer(char *pBuffer, UDWORD size); +extern void strresSetInputFile(PHYSFS_file* fileHandle); /* Give access to the line number and current text for error messages */ extern void strresGetErrorData(int *pLine, char **ppText); diff --git a/src/data.c b/src/data.c index f6c56fe7f..1ab6b5c56 100644 --- a/src/data.c +++ b/src/data.c @@ -1025,7 +1025,7 @@ static void dataAnimRelease( void *pData ) } /* Load a string resource file */ -static BOOL dataStrResLoad(char *pBuffer, UDWORD size, void **ppData) +static BOOL dataStrResLoad(const char* fileName, void** ppData) { // recreate the string resource if it was freed by a WRF release if (psStringRes == NULL) @@ -1036,7 +1036,7 @@ static BOOL dataStrResLoad(char *pBuffer, UDWORD size, void **ppData) } } - if (!strresLoad(psStringRes, pBuffer, size)) + if (!strresLoad(psStringRes, fileName)) { return FALSE; } @@ -1155,7 +1155,6 @@ static const RES_TYPE_MIN_BUF BufferResourceTypes[] = {"SMSG", bufferSMSGLoad, dataSMSGRelease}, {"SCRIPT", dataScriptLoad, (RES_FREE)scriptFreeCode}, {"SCRIPTVAL", dataScriptLoadVals, NULL}, - {"STR_RES", dataStrResLoad, dataStrResRelease}, {"IMD", dataIMDBufferLoad, (RES_FREE)iV_IMDRelease}, }; @@ -1176,6 +1175,7 @@ static const RES_TYPE_MIN_FILE FileResourceTypes[] = {"TERTILES", dataTERTILESLoad, dataTERTILESRelease}, // freed by 3d shutdow},// Tertiles Files. This version used when running with hardware renderer. {"IMG", dataIMGLoad, dataIMGRelease}, {"TEXPAGE", dataTexPageLoad, dataTexPageRelease}, + {"STR_RES", dataStrResLoad, dataStrResRelease}, }; /* Pass all the data loading functions to the framework library */