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 */
|
||||
char *pBufferStart;
|
||||
char *pBuffer;
|
||||
uint32_t nBufferSize;
|
||||
size_t nBufferSize;
|
||||
uint32_t oAtLineStart;
|
||||
};
|
||||
|
||||
|
||||
@@ -156,25 +156,39 @@ YY_BUFFER_STATE yy_create_buffer(FILE *f)
|
||||
if (pBuffer == NULL)
|
||||
fatalerror("%s: Out of memory!", __func__);
|
||||
|
||||
uint32_t size;
|
||||
size_t size = 0, capacity;
|
||||
char *buf = NULL;
|
||||
|
||||
fseek(f, 0, SEEK_END);
|
||||
size = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
capacity = ftell(f);
|
||||
rewind(f);
|
||||
|
||||
if (capacity == -1)
|
||||
capacity = 4096;
|
||||
|
||||
while (!feof(f)) {
|
||||
if (buf == NULL || size >= capacity) {
|
||||
capacity *= 2;
|
||||
/* Give extra room for 2 newlines and terminator */
|
||||
uint32_t capacity = size + 3;
|
||||
buf = realloc(buf, capacity + SAFETYMARGIN + 3);
|
||||
|
||||
pBuffer->pBufferRealStart = malloc(capacity + SAFETYMARGIN);
|
||||
if (buf == NULL)
|
||||
fatalerror("%s: Out of memory for buffer!",
|
||||
__func__);
|
||||
}
|
||||
|
||||
if (pBuffer->pBufferRealStart == NULL)
|
||||
fatalerror("%s: Out of memory for buffer!", __func__);
|
||||
char *bufpos = buf + SAFETYMARGIN + size;
|
||||
size_t read_count = fread(bufpos, 1, capacity - size, f);
|
||||
|
||||
pBuffer->pBufferStart = pBuffer->pBufferRealStart + SAFETYMARGIN;
|
||||
pBuffer->pBuffer = pBuffer->pBufferRealStart + SAFETYMARGIN;
|
||||
if (read_count == 0)
|
||||
fatalerror("%s: fread error", __func__);
|
||||
|
||||
size = fread(pBuffer->pBuffer, sizeof(uint8_t), size, f);
|
||||
size += read_count;
|
||||
}
|
||||
|
||||
pBuffer->pBufferRealStart = buf;
|
||||
pBuffer->pBufferStart = buf + SAFETYMARGIN;
|
||||
pBuffer->pBuffer = buf + SAFETYMARGIN;
|
||||
pBuffer->pBuffer[size] = 0;
|
||||
pBuffer->nBufferSize = size;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user