From 367b12484f5316e0002b44456f0525d99f751ba1 Mon Sep 17 00:00:00 2001 From: stitchy Date: Thu, 12 Oct 2023 14:25:48 -0700 Subject: [PATCH] Add initial program with LL implementation --- movies/csv_parser.c | 84 +++++++++++++++++++++++++++++++++++++++++++++ movies/csv_parser.h | 28 +++++++++++++++ movies/movies.c | 12 +++++++ movies/movies.h | 13 +++++++ movies/node.c | 30 ++++++++++++++++ movies/node.h | 16 +++++++++ 6 files changed, 183 insertions(+) create mode 100644 movies/csv_parser.c create mode 100644 movies/csv_parser.h create mode 100644 movies/movies.c create mode 100644 movies/movies.h create mode 100644 movies/node.c create mode 100644 movies/node.h diff --git a/movies/csv_parser.c b/movies/csv_parser.c new file mode 100644 index 0000000..cdb990f --- /dev/null +++ b/movies/csv_parser.c @@ -0,0 +1,84 @@ +#include "csv_parser.h" + +struct node* parse_csv() { + + FILE * file; + file = fopen("movies_sample_1.csv", "r"); + + // Buffer File For chars + char* buffer; + size_t buff_size = 1000; + size_t chars = -2; + buffer = malloc(buff_size * sizeof(char)); + + // Deal with Top of CSV + getline(&buffer, &buff_size, file); + chars = getline(&buffer, &buff_size, file); + + struct node* head = appendv_node(NULL, parse_line(buffer)); + + chars = getline(&buffer, &buff_size, file); + + // Actually Store the CSV + while(chars != -1) { + + appendv_node(head, parse_line(buffer)); + + chars = getline(&buffer, &buff_size, file); + } + + // Free unecessary buffer files + free(buffer); + fclose(file); + + return head; + +} + +struct csv* parse_line(char* line) { + + struct csv* mov = malloc(sizeof(struct csv)); + + char* sub = strtok(line, ","); + char* title = malloc(sizeof(char) *(strlen(sub) + 1)); + strcpy(title,sub); + mov->title = title; + + mov->year = atoi(strtok(NULL, ",")); + + sub = strtok(NULL, ","); + char* languages = malloc(sizeof(char) *(strlen(sub) + 1)); + strcpy(languages, sub); + mov->languages = languages; + //printf("%s\n", csv->languages); + + mov->rating = atof(strtok(NULL, "")); + + return mov; +} + +void print_movies(struct node* head) { + + if(!head) { + printf("You F'd up mate (print_movies)"); + return; + } + + struct node* node = head; + + while(1) { + + print_line(node->data); + + if(node->node) + node = node->node; + else + return; + } +} + +void print_line(struct csv* printer) { + + printf("%s, %i, %s, %f\n", printer->title, printer->year, printer->languages, printer->rating); + +} diff --git a/movies/csv_parser.h b/movies/csv_parser.h new file mode 100644 index 0000000..679ddfb --- /dev/null +++ b/movies/csv_parser.h @@ -0,0 +1,28 @@ +#include "stdio.h" +#include "stdlib.h" +#include "string.h" + +#include "node.h" + +#ifndef CSV_PARSER +#define CSV_PARSER + + +struct csv { + char* title; + int year; + int numlang; + char* languages; + float rating; + struct node* node; +}; + +struct node* parse_csv(); + +struct csv* parse_line(char*); + +void print_movies(struct node*); + +void print_line(struct csv*); + +#endif diff --git a/movies/movies.c b/movies/movies.c new file mode 100644 index 0000000..ee706ef --- /dev/null +++ b/movies/movies.c @@ -0,0 +1,12 @@ +#include "movies.h" + +int main() { + + struct node* head = parse_csv(); + + printf("Test\n"); + + print_movies(head); + + return 0; +} diff --git a/movies/movies.h b/movies/movies.h new file mode 100644 index 0000000..a6d11ee --- /dev/null +++ b/movies/movies.h @@ -0,0 +1,13 @@ + + +#ifndef MOVIES +#define MOVIES + +#include "stdio.h" +#include "stdlib.h" + +#include "csv_parser.h" +#include "node.h" + + +#endif diff --git a/movies/node.c b/movies/node.c new file mode 100644 index 0000000..e70c87d --- /dev/null +++ b/movies/node.c @@ -0,0 +1,30 @@ + +#include "node.h" + +void append_node(struct node* head, struct node* node_app) { + + if(!head) { + head = node_app; + return; + } + + struct node* temp = head; + + while(temp->node != NULL) { + temp = temp->node; + } + temp->node = node_app; + +} + +struct node* appendv_node(struct node* head, void* data) { + + struct node* node = malloc(sizeof(struct node)); + node->data = data; + node->node = NULL; + + append_node(head, node); + + return node; +} + diff --git a/movies/node.h b/movies/node.h new file mode 100644 index 0000000..db2e628 --- /dev/null +++ b/movies/node.h @@ -0,0 +1,16 @@ +#include "stdlib.h" + +#ifndef NODES +#define NODES + +struct node { + struct node* node; + void* data; +}; + +void append_node(struct node*, struct node*); + +struct node* appendv_node(struct node*, void*); + + +#endif