Files
advent-of-code-2023/input_handler.c

31 lines
690 B
C

//
// Created by Lennart on 02/12/2023.
//
#include "input_handler.h"
#include <stdlib.h>
#include <string.h>
#define INITIAL_CAPACITY 64
char* read_line(FILE *f, char **buffer, int *capacity) {
if(*buffer == NULL) {
*buffer = malloc(2 * sizeof (char) * INITIAL_CAPACITY);
*capacity = INITIAL_CAPACITY;
}
int position = 0;
while(!feof(f)) {
char *res = fgets(*buffer + position, *capacity - position, f);
if(res[strlen(res) - 1] == '\n') {
return *buffer;
}
*buffer = realloc(*buffer, 2 * sizeof (char) * *capacity);
position += (int) strlen(res);
*capacity *= 2;
}
return *buffer;
}