This commit is contained in:
Preston Baxter 2023-12-02 20:32:40 -06:00
commit 3af6121255
3 changed files with 127 additions and 0 deletions

26
day1/day1.c Normal file
View File

@ -0,0 +1,26 @@
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
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;
}

80
libs/buffered_reader.c Normal file
View File

@ -0,0 +1,80 @@
#include "buffered_reader.h"
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
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);
}

21
libs/buffered_reader.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef buffred_reader
#define buffred_reader
#define STARTING_SIZE 16
#include <stdlib.h>
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