feat: day 2
This commit is contained in:
30
input_handler.c
Normal file
30
input_handler.c
Normal file
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// Created by Lennart on 02/12/2023.
|
||||
//
|
||||
|
||||
#include "input_handler.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define INITIAL_CAPACITY 64
|
||||
|
||||
const 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;
|
||||
}
|
||||
Reference in New Issue
Block a user