From 3af612125553338d6dbd773d3c5e7b7dcc674800 Mon Sep 17 00:00:00 2001 From: Preston Baxter Date: Sat, 2 Dec 2023 20:32:40 -0600 Subject: [PATCH] init --- day1/day1.c | 26 ++++++++++++++ libs/buffered_reader.c | 80 ++++++++++++++++++++++++++++++++++++++++++ libs/buffered_reader.h | 21 +++++++++++ 3 files changed, 127 insertions(+) create mode 100644 day1/day1.c create mode 100644 libs/buffered_reader.c create mode 100644 libs/buffered_reader.h diff --git a/day1/day1.c b/day1/day1.c new file mode 100644 index 0000000..28d3f81 --- /dev/null +++ b/day1/day1.c @@ -0,0 +1,26 @@ +#include +#include +#include + +int main(int argc, char *argv[]) { + // Define input file + FILE *file; + file = fopen(argv[1], "r"); + + //Define loop things + bool loop = true; + char *buffer; + size_t bufsize = 128; + size_t characters; + + //loop over file + if (file) { + while (loop) { + buffer = (char *)malloc(bufsize * sizeof(char)); + characters = getline(&buffer, &bufsize, file); + + } + } + + return 0; +} diff --git a/libs/buffered_reader.c b/libs/buffered_reader.c new file mode 100644 index 0000000..6ee41ba --- /dev/null +++ b/libs/buffered_reader.c @@ -0,0 +1,80 @@ +#include "buffered_reader.h" + +#include +#include +#include +#include + +filelines* flines(const char* filename) { + // Define input file + FILE *file; + file = fopen(filename, "r"); + + //Define loop things + bool loop = true; + char *buffer; + size_t bufsize = 128; + size_t characters; + + filelines *lines; + initFileLines(&lines); + + //loop over file + if (file) { + while (loop) { + buffer = (char *)malloc(bufsize * sizeof(char)); + characters = getline(&buffer, &bufsize, file); + + } + } + + return lines; +} + +void initFileLines(filelines **flines_ptr) { + filelines *flines; + flines = (filelines*)malloc(sizeof(filelines)); + if (!flines) { + printf("Failed to allocate memory for filelines\n"); + exit(1); + } + + flines->size = 0; + flines->capacity = STARTING_SIZE; + flines->lines = (char**)malloc(STARTING_SIZE * sizeof(char*)); + flines = (filelines*)malloc(sizeof(filelines)); + if (!flines->lines) { + printf("Failed to allocate memory for filelines\n"); + exit(1); + } + + *flines_ptr = flines; +} + +//inserts copy of string into array +void insert(filelines *flines_ptr, char *str){ + if (flines_ptr->size == flines_ptr->capacity) { + char **temp = flines_ptr->lines; + flines_ptr->capacity <<= 1; + flines_ptr->lines = realloc(flines_ptr->lines, flines_ptr->capacity * sizeof(char*)); + if (!flines_ptr->lines) { + printf("Out of mem\n"); + return; + } + } + flines_ptr->size += 1; + flines_ptr->lines[flines_ptr->size] = str; +} + +char* get(filelines *flines_ptr, int index) { + if(index >=flines_ptr->size) { + printf("index out of bounds"); + return NULL; + } + return flines_ptr->lines[index]; +} + +void freeLines(filelines* flines_ptr){ + free(flines_ptr->lines); + free(flines_ptr); +} diff --git a/libs/buffered_reader.h b/libs/buffered_reader.h new file mode 100644 index 0000000..cacc70e --- /dev/null +++ b/libs/buffered_reader.h @@ -0,0 +1,21 @@ +#ifndef buffred_reader +#define buffred_reader + +#define STARTING_SIZE 16 + +#include + +typedef struct { + size_t size; + size_t capacity; + char** lines; +}filelines; + +filelines* flines(const char* filename); + +void initFileLines(filelines** flines_ptr); +void insert(filelines* flines_ptr, char* str); +char* get(filelines* flines_ptr, int index); +void free(filelines* flines_ptr); + +#endif