mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 18:22:07 +00:00
Handle non-seekable input correctly
This commit is contained in:
@@ -31,7 +31,7 @@ struct yy_buffer_state {
|
|||||||
/* Address where the data is initially written after a safety margin */
|
/* Address where the data is initially written after a safety margin */
|
||||||
char *pBufferStart;
|
char *pBufferStart;
|
||||||
char *pBuffer;
|
char *pBuffer;
|
||||||
uint32_t nBufferSize;
|
size_t nBufferSize;
|
||||||
uint32_t oAtLineStart;
|
uint32_t oAtLineStart;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -156,25 +156,39 @@ YY_BUFFER_STATE yy_create_buffer(FILE *f)
|
|||||||
if (pBuffer == NULL)
|
if (pBuffer == NULL)
|
||||||
fatalerror("%s: Out of memory!", __func__);
|
fatalerror("%s: Out of memory!", __func__);
|
||||||
|
|
||||||
uint32_t size;
|
size_t size = 0, capacity;
|
||||||
|
char *buf = NULL;
|
||||||
|
|
||||||
fseek(f, 0, SEEK_END);
|
fseek(f, 0, SEEK_END);
|
||||||
size = ftell(f);
|
capacity = ftell(f);
|
||||||
fseek(f, 0, SEEK_SET);
|
rewind(f);
|
||||||
|
|
||||||
/* Give extra room for 2 newlines and terminator */
|
if (capacity == -1)
|
||||||
uint32_t capacity = size + 3;
|
capacity = 4096;
|
||||||
|
|
||||||
pBuffer->pBufferRealStart = malloc(capacity + SAFETYMARGIN);
|
while (!feof(f)) {
|
||||||
|
if (buf == NULL || size >= capacity) {
|
||||||
|
capacity *= 2;
|
||||||
|
/* Give extra room for 2 newlines and terminator */
|
||||||
|
buf = realloc(buf, capacity + SAFETYMARGIN + 3);
|
||||||
|
|
||||||
if (pBuffer->pBufferRealStart == NULL)
|
if (buf == NULL)
|
||||||
fatalerror("%s: Out of memory for buffer!", __func__);
|
fatalerror("%s: Out of memory for buffer!",
|
||||||
|
__func__);
|
||||||
|
}
|
||||||
|
|
||||||
pBuffer->pBufferStart = pBuffer->pBufferRealStart + SAFETYMARGIN;
|
char *bufpos = buf + SAFETYMARGIN + size;
|
||||||
pBuffer->pBuffer = pBuffer->pBufferRealStart + SAFETYMARGIN;
|
size_t read_count = fread(bufpos, 1, capacity - size, f);
|
||||||
|
|
||||||
size = fread(pBuffer->pBuffer, sizeof(uint8_t), size, f);
|
if (read_count == 0)
|
||||||
|
fatalerror("%s: fread error", __func__);
|
||||||
|
|
||||||
|
size += read_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
pBuffer->pBufferRealStart = buf;
|
||||||
|
pBuffer->pBufferStart = buf + SAFETYMARGIN;
|
||||||
|
pBuffer->pBuffer = buf + SAFETYMARGIN;
|
||||||
pBuffer->pBuffer[size] = 0;
|
pBuffer->pBuffer[size] = 0;
|
||||||
pBuffer->nBufferSize = size;
|
pBuffer->nBufferSize = size;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user