mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 18:22:07 +00:00
Only add newlines to file if necessary
This commit is contained in:
@@ -155,10 +155,7 @@ YY_BUFFER_STATE yy_create_buffer(FILE *f)
|
|||||||
|
|
||||||
size = fread(pBuffer->pBuffer, sizeof(uint8_t), size, f);
|
size = fread(pBuffer->pBuffer, sizeof(uint8_t), size, f);
|
||||||
|
|
||||||
pBuffer->pBuffer[size] = '\n';
|
pBuffer->pBuffer[size] = 0;
|
||||||
pBuffer->pBuffer[size + 1] = '\n'; /* in case the file ends with \ */
|
|
||||||
pBuffer->pBuffer[size + 2] = 0;
|
|
||||||
pBuffer->nBufferSize = size + 2;
|
|
||||||
|
|
||||||
/* Convert all line endings to LF and spaces */
|
/* Convert all line endings to LF and spaces */
|
||||||
|
|
||||||
@@ -219,6 +216,30 @@ YY_BUFFER_STATE yy_create_buffer(FILE *f)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pBuffer->nBufferSize = size;
|
||||||
|
|
||||||
|
/* Add newline if file doesn't end with one */
|
||||||
|
if (size == 0 || pBuffer->pBuffer[size - 1] != '\n') {
|
||||||
|
pBuffer->pBuffer[pBuffer->nBufferSize] = '\n';
|
||||||
|
pBuffer->nBufferSize++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Add newline if \ will eat the last newline */
|
||||||
|
if (pBuffer->nBufferSize >= 2) {
|
||||||
|
size_t pos = pBuffer->nBufferSize - 2;
|
||||||
|
|
||||||
|
/* Skip spaces */
|
||||||
|
while (pos > 0 && pBuffer->pBuffer[pos] == ' ')
|
||||||
|
pos--;
|
||||||
|
|
||||||
|
if (pBuffer->pBuffer[pos] == '\\') {
|
||||||
|
pBuffer->pBuffer[pBuffer->nBufferSize] = '\n';
|
||||||
|
pBuffer->nBufferSize++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pBuffer->pBuffer[pBuffer->nBufferSize] = 0;
|
||||||
|
|
||||||
pBuffer->oAtLineStart = 1;
|
pBuffer->oAtLineStart = 1;
|
||||||
return pBuffer;
|
return pBuffer;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user