get_line() fixes for rebuild.c

This commit is contained in:
Bobbi Webber-Manners 2020-07-08 17:11:53 -04:00
parent 9c85bf5d78
commit 56874574f3

View File

@ -53,29 +53,28 @@ void error_exit() {
* Converts line endings from CRLF -> CR (Apple ][ style)
*/
int16_t get_line(FILE *fp) {
static uint16_t rd = 0;
static uint16_t buflen = 0;
static uint16_t rd = 0; // Read
static uint16_t wt = 0; // Write
uint8_t found = 0;
uint16_t j = 0;
uint16_t i;
while (1) {
for (i = rd; i < buflen; ++i) {
linebuf[j++] = buf[i];
while (rd < wt) {
linebuf[j++] = buf[rd++];
if (linebuf[j - 1] == '\r') {
found = 1;
break;
}
}
if (found) {
rd = i + 1;
linebuf[j] = '\0';
if (rd == wt) // Empty buf[]
rd = wt = 0;
if (found)
return j;
}
buflen = fread(buf, 1, READSZ, fp);
if (buflen == 0) {
rd = 0;
return -1; // Hit EOF before we found EOL
}
rd = 0;
if (feof(fp))
return -1;
i = fread(&buf[wt], 1, READSZ - wt, fp);
wt += i;
}
}