Process last line

This commit is contained in:
Shav Kinderlehrer 2023-04-17 18:37:39 -04:00
parent 15986b04ff
commit ab10a9c217

View File

@ -28,7 +28,7 @@ void loadlines(struct filedata *f) {
for (size_t i = 0; i < f->buflen; i++) { for (size_t i = 0; i < f->buflen; i++) {
char c = f->buf[i]; char c = f->buf[i];
if (c == '\n') { if (c == '\n') {
if (offset < linelen) { if (offset < linelen) { // shrink
char *new_line = realloc(line, offset); char *new_line = realloc(line, offset);
if (new_line == NULL) if (new_line == NULL)
die("realloc"); die("realloc");
@ -43,7 +43,6 @@ void loadlines(struct filedata *f) {
line = malloc(linelen); line = malloc(linelen);
if (line == NULL) if (line == NULL)
die("malloc"); die("malloc");
} else { } else {
if (offset == linelen) { if (offset == linelen) {
linelen *= 2; linelen *= 2;
@ -58,6 +57,19 @@ void loadlines(struct filedata *f) {
line[offset++] = c; line[offset++] = c;
} }
} }
// capture last line
if (offset < linelen) { // shrink
char *new_line = realloc(line, offset);
if (new_line == NULL)
die("realloc");
line = new_line;
}
appendline(f, line, offset);
f->lc++;
free(line);
} }
char *linepad(int lc, int total) { char *linepad(int lc, int total) {