From 4217ded01b4461b5dc6b636b82a1c8048996d74d Mon Sep 17 00:00:00 2001 From: Preston Baxter Date: Mon, 4 Dec 2023 21:46:02 -0600 Subject: [PATCH] create buffered reader --- day1/day1 | Bin 0 -> 16512 bytes day1/day1.c | 26 ++++++++++---------------- day1/input.txt | 3 +++ libs/buffered_reader.c | 25 +++++++++++++++++++++---- libs/buffered_reader.h | 2 +- 5 files changed, 35 insertions(+), 21 deletions(-) create mode 100755 day1/day1 create mode 100644 day1/input.txt diff --git a/day1/day1 b/day1/day1 new file mode 100755 index 0000000000000000000000000000000000000000..bec42429851164e46c052694ad223702bc921b7b GIT binary patch literal 16512 zcmeHOeQ;FO6~7z4qI_f#C|?R&3k;TKAs8vxL6*RV7Yh=Bv|6>#P4*>OO?Kn%+nP8H zjg53jW1RShEw;7AwvJ4#ElkzH=^q4ap|)1TNNYNdGf`{15o%)uCu(K;JNMr6^7fI{ zPTT+7n|XWg`JIn@@45HA-22|W-w1E5Eb{pTrwQU3LENn~6%x{dzO_075)y00bo?$B zi^OE`6D6kPAw?inX`m>V7HfP0DA~=U%sBK?1q-HJL!xAtFZp!=Ood-*lHDZAYTirF zNudx-S?-hGwl9UJpLy}+S09_md9Qn`}sj z68YmHLOkVX;36FrkGqjm7}1l~^UXx_rF9ToGLCR=9{fJwfpPT9^XPvu4^I6#Ub#O5 z9>C>I6o>KjugQZ4^Wc?v@TY+XaCy@e0LClV2mErcGV!Ka*BUYFWAR8+>@G_*N1B=v zbwDwG>dcfKN!n&}Bo-G9mR%?66D?L;w508ns83p!NLotWYK_^jfqLETW?kd$W_={q zBwCWOxLq&E6AipIV$0U5icRL~V7ZgKaxA+#c$F}#c5O4GR?=#SrEDv?Yul!#MBLgH zsco|0zM(l0*Hto=>bQz%R1wCV55E&}5mkRGr-I;vMjf(zpUsZNrqdIM1|MNh-2WX6 z&k5pA&8HW{E+W6*1W$>dhx{B0>7aM$=N;qR8O0%KCKzXaV1vTxUU1(hgD#x!Nz%-? z@F~hv{tmn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>Wn>?Zb&;ks0YktkkL|y>%-6Ai-2+6UFKrATyY0I&beIHrRDc{ P_WM7tG!}RaJgoRP3*yKv literal 0 HcmV?d00001 diff --git a/day1/day1.c b/day1/day1.c index 28d3f81..83b5f6e 100644 --- a/day1/day1.c +++ b/day1/day1.c @@ -1,25 +1,19 @@ -#include #include +#include #include +#include "../libs/buffered_reader.h" + int main(int argc, char *argv[]) { - // Define input file - FILE *file; - file = fopen(argv[1], "r"); + filelines *lines = flines(argv[1]); - //Define loop things - bool loop = true; - char *buffer; - size_t bufsize = 128; - size_t characters; + if (lines == NULL) { + printf("Failed to read file"); + exit(1); + } - //loop over file - if (file) { - while (loop) { - buffer = (char *)malloc(bufsize * sizeof(char)); - characters = getline(&buffer, &bufsize, file); - - } + for (int i = 0; i < lines->size; i++) { + printf("%s\n", get(lines, i)); } return 0; diff --git a/day1/input.txt b/day1/input.txt new file mode 100644 index 0000000..83db48f --- /dev/null +++ b/day1/input.txt @@ -0,0 +1,3 @@ +line1 +line2 +line3 diff --git a/libs/buffered_reader.c b/libs/buffered_reader.c index 6ee41ba..51698b8 100644 --- a/libs/buffered_reader.c +++ b/libs/buffered_reader.c @@ -19,12 +19,30 @@ filelines* flines(const char* filename) { filelines *lines; initFileLines(&lines); + int c; //loop over file if (file) { while (loop) { buffer = (char *)malloc(bufsize * sizeof(char)); - characters = getline(&buffer, &bufsize, file); + int i = 0; + while((c = getc(file)) != EOF) { + if (c == '\n' || i == bufsize) { + printf("breaking\n"); + break; + } + + //TODO: resize string if input is too big + printf("Adding %d\n", c); + buffer[i] = c; + i += 1; + } + if (i > 0){ + insert(lines, buffer); + } + if (c == EOF) { + break; + } } } @@ -42,9 +60,8 @@ void initFileLines(filelines **flines_ptr) { 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"); + printf("Failed to allocate memory for lines in filelines\n"); exit(1); } @@ -62,8 +79,8 @@ void insert(filelines *flines_ptr, char *str){ return; } } - flines_ptr->size += 1; flines_ptr->lines[flines_ptr->size] = str; + flines_ptr->size += 1; } char* get(filelines *flines_ptr, int index) { diff --git a/libs/buffered_reader.h b/libs/buffered_reader.h index cacc70e..e56984f 100644 --- a/libs/buffered_reader.h +++ b/libs/buffered_reader.h @@ -16,6 +16,6 @@ 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); +void freeLines(filelines* flines_ptr); #endif