Make tokenizer keep a copy of string in normal memory

This commit is contained in:
Alam Ed Arias 2024-02-21 21:23:38 -05:00
parent a8a92a57a7
commit 8c7fe2ae95
7 changed files with 28 additions and 13 deletions

View file

@ -552,7 +552,7 @@ void *M_Memcpy(void* dest, const void* src, size_t n);
char *va(const char *format, ...) FUNCPRINTF; char *va(const char *format, ...) FUNCPRINTF;
char *M_GetToken(const char *inputString); char *M_GetToken(const char *inputString);
void M_UnGetToken(void); void M_UnGetToken(void);
void M_TokenizerOpen(const char *inputString); void M_TokenizerOpen(const char *inputString, size_t len);
void M_TokenizerClose(void); void M_TokenizerClose(void);
const char *M_TokenizerRead(UINT32 i); const char *M_TokenizerRead(UINT32 i);
const char *M_TokenizerReadZDoom(UINT32 i); const char *M_TokenizerReadZDoom(UINT32 i);

View file

@ -1978,9 +1978,9 @@ void M_UnGetToken(void)
static tokenizer_t *globalTokenizer = NULL; static tokenizer_t *globalTokenizer = NULL;
void M_TokenizerOpen(const char *inputString) void M_TokenizerOpen(const char *inputString, size_t len)
{ {
globalTokenizer = Tokenizer_Open(inputString, 2); globalTokenizer = Tokenizer_Open(inputString, len, 2);
} }
void M_TokenizerClose(void) void M_TokenizerClose(void)

View file

@ -12,11 +12,13 @@
#include "m_tokenizer.h" #include "m_tokenizer.h"
#include "z_zone.h" #include "z_zone.h"
tokenizer_t *Tokenizer_Open(const char *inputString, unsigned numTokens) tokenizer_t *Tokenizer_Open(const char *inputString, size_t len, unsigned numTokens)
{ {
tokenizer_t *tokenizer = Z_Malloc(sizeof(tokenizer_t), PU_STATIC, NULL); tokenizer_t *tokenizer = Z_Malloc(sizeof(tokenizer_t), PU_STATIC, NULL);
tokenizer->input = inputString; tokenizer->zdup = malloc(len);
tokenizer->input = M_Memcpy(tokenizer->zdup, inputString, len);
tokenizer->startPos = 0; tokenizer->startPos = 0;
tokenizer->endPos = 0; tokenizer->endPos = 0;
tokenizer->inputLength = 0; tokenizer->inputLength = 0;
@ -51,6 +53,7 @@ void Tokenizer_Close(tokenizer_t *tokenizer)
Z_Free(tokenizer->token[i]); Z_Free(tokenizer->token[i]);
Z_Free(tokenizer->capacity); Z_Free(tokenizer->capacity);
Z_Free(tokenizer->token); Z_Free(tokenizer->token);
free(tokenizer->zdup);
Z_Free(tokenizer); Z_Free(tokenizer);
} }

View file

@ -16,6 +16,7 @@
typedef struct Tokenizer typedef struct Tokenizer
{ {
void *zdup;
const char *input; const char *input;
unsigned numTokens; unsigned numTokens;
UINT32 *capacity; UINT32 *capacity;
@ -29,7 +30,7 @@ typedef struct Tokenizer
const char *(*get)(struct Tokenizer*, UINT32); const char *(*get)(struct Tokenizer*, UINT32);
} tokenizer_t; } tokenizer_t;
tokenizer_t *Tokenizer_Open(const char *inputString, unsigned numTokens); tokenizer_t *Tokenizer_Open(const char *inputString, size_t len, unsigned numTokens);
void Tokenizer_Close(tokenizer_t *tokenizer); void Tokenizer_Close(tokenizer_t *tokenizer);
const char *Tokenizer_Read(tokenizer_t *tokenizer, UINT32 i); const char *Tokenizer_Read(tokenizer_t *tokenizer, UINT32 i);

View file

@ -1525,6 +1525,12 @@ static boolean TextmapCount(size_t size)
numvertexes = 0; numvertexes = 0;
numsectors = 0; numsectors = 0;
if(!tkn)
{
CONS_Alert(CONS_ERROR, "No text in lump!\n");
return true;
}
// Look for namespace at the beginning. // Look for namespace at the beginning.
if (!fastcmp(tkn, "namespace")) if (!fastcmp(tkn, "namespace"))
{ {
@ -3109,7 +3115,12 @@ static boolean P_LoadMapData(const virtres_t *virt)
if (udmf) // Count how many entries for each type we got in textmap. if (udmf) // Count how many entries for each type we got in textmap.
{ {
virtlump_t *textmap = vres_Find(virt, "TEXTMAP"); virtlump_t *textmap = vres_Find(virt, "TEXTMAP");
M_TokenizerOpen((char *)textmap->data); if (textmap->size == 0)
{
CONS_Alert(CONS_ERROR, "Emtpy TEXTMAP Lump!\n");
return false;
}
M_TokenizerOpen((char *)textmap->data, textmap->size);
if (!TextmapCount(textmap->size)) if (!TextmapCount(textmap->size))
{ {
M_TokenizerClose(); M_TokenizerClose();

View file

@ -784,9 +784,9 @@ static struct PaletteRemapParseResult *PaletteRemap_ParseString(tokenizer_t *sc)
return NULL; return NULL;
} }
static struct PaletteRemapParseResult *PaletteRemap_ParseTranslation(const char *translation) static struct PaletteRemapParseResult *PaletteRemap_ParseTranslation(const char *translation, size_t len)
{ {
tokenizer_t *sc = Tokenizer_Open(translation, 1); tokenizer_t *sc = Tokenizer_Open(translation, len, 1);
struct PaletteRemapParseResult *result = PaletteRemap_ParseString(sc); struct PaletteRemapParseResult *result = PaletteRemap_ParseString(sc);
Tokenizer_Close(sc); Tokenizer_Close(sc);
return result; return result;
@ -918,7 +918,7 @@ void R_ParseTrnslate(INT32 wadNum, UINT16 lumpnum)
text[lumpLength] = '\0'; text[lumpLength] = '\0';
Z_Free(lumpData); Z_Free(lumpData);
sc = Tokenizer_Open(text, 1); sc = Tokenizer_Open(text, lumpLength, 1);
tkn = sc->get(sc, 0); tkn = sc->get(sc, 0);
struct NewTranslation *list = NULL; struct NewTranslation *list = NULL;
@ -963,7 +963,7 @@ void R_ParseTrnslate(INT32 wadNum, UINT16 lumpnum)
// Parse all of the translations // Parse all of the translations
do { do {
struct PaletteRemapParseResult *parse_result = PaletteRemap_ParseTranslation(tkn); struct PaletteRemapParseResult *parse_result = PaletteRemap_ParseTranslation(tkn, strlen(tkn));
if (parse_result->error) if (parse_result->error)
{ {
PrintError(name, "%s", parse_result->error); PrintError(name, "%s", parse_result->error);

View file

@ -2704,7 +2704,7 @@ virtres_t* vres_GetMap(lumpnum_t lumpnum)
UINT8 *wadData = W_CacheLumpNum(lumpnum, PU_LEVEL); UINT8 *wadData = W_CacheLumpNum(lumpnum, PU_LEVEL);
filelump_t *fileinfo = (filelump_t *)(wadData + ((wadinfo_t *)wadData)->infotableofs); filelump_t *fileinfo = (filelump_t *)(wadData + ((wadinfo_t *)wadData)->infotableofs);
numlumps = ((wadinfo_t *)wadData)->numlumps; numlumps = ((wadinfo_t *)wadData)->numlumps;
vlumps = Z_Malloc(sizeof(virtlump_t)*numlumps, PU_LEVEL, NULL); vlumps = Z_Calloc(sizeof(virtlump_t)*numlumps, PU_LEVEL, NULL);
// Build the lumps. // Build the lumps.
for (i = 0; i < numlumps; i++) for (i = 0; i < numlumps; i++)
@ -2728,7 +2728,7 @@ virtres_t* vres_GetMap(lumpnum_t lumpnum)
break; break;
numlumps++; numlumps++;
vlumps = Z_Malloc(sizeof(virtlump_t)*numlumps, PU_LEVEL, NULL); vlumps = Z_Calloc(sizeof(virtlump_t)*numlumps, PU_LEVEL, NULL);
for (i = 0; i < numlumps; i++, lumpnum++) for (i = 0; i < numlumps; i++, lumpnum++)
{ {
vlumps[i].size = W_LumpLength(lumpnum); vlumps[i].size = W_LumpLength(lumpnum);