diff --git a/directories/csv_parser.c b/directories/csv_parser.c new file mode 100644 index 0000000..bfbf168 --- /dev/null +++ b/directories/csv_parser.c @@ -0,0 +1,129 @@ +#include "csv_parser.h" + +struct node* parse_csv(char* name) { + + FILE * file; + file = fopen(name, "r"); + if(!file) { + printf("Your File was Stolen by the Gremlins\n"); + exit(66); + + } +// Buffer File For chars + char* buffer; + size_t buff_size = 1000; + ssize_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); + + printf("Processed file %s and processed data for %d movies.\n\n", name, count_nodes(head)); + + return head; + +} + +struct csv* parse_line(char* line) { + + struct csv* mov = malloc(sizeof(struct csv)); + + // Deal with title + char* sub = strtok(line, ","); + mov->title = malloc(sizeof(char) *(strlen(sub) + 1)); + strcpy(mov->title, sub); + + mov->year = atoi(strtok(NULL, ",")); + + // Allocate number of languages and save string + sub = strtok(NULL, ","); + mov->numlang = 1 + count_char(sub, ';'); + mov->languages = malloc(mov->numlang * sizeof(char*)); + + char* lang = malloc(sizeof(char) * (strlen(sub) + 1)); + strcpy(lang, sub); + + mov->rating = atof(strtok(NULL, "")); + + + // Finish parsing Languages + for(int i = 0; i < mov->numlang; i++) { + + if(!i) + sub = strtok( (lang+1), ";"); + else + sub = strtok(NULL, ";"); + + mov->languages[i] = malloc(sizeof(char) * (strlen(sub) + 1)); + strcpy(mov->languages[i], sub); + + } + + // Remove icky char from end of last string + mov->languages[mov->numlang - 1][strlen(mov->languages[mov->numlang - 1]) - 1] = '\0'; + + return mov; +} + +// Debugging; not used +void print_movies(struct node* head) { + + if(!head) { + printf("Yo 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; + } +} + +// Debugging; not used +void print_line(struct csv* printer) { + + printf("%s, %i, ", printer->title, printer->year); + for(int i = 0; i < printer->numlang; i++) { + printf("%s:", printer->languages[i]); + } + printf(", %f\n", printer->rating); + +} + +// Counts number of times given char appears in string +int count_char(char* text, char cha) { + + int length = strlen(text); + int num = 0; + + for(int i = 0; i < length; i++) { + if(text[i] == cha) + num++; + } + + return num; +} diff --git a/directories/csv_parser.h b/directories/csv_parser.h new file mode 100644 index 0000000..1301c9a --- /dev/null +++ b/directories/csv_parser.h @@ -0,0 +1,30 @@ +#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(char*); + +struct csv* parse_line(char*); + +void print_movies(struct node*); + +void print_line(struct csv*); + +int count_char(char*, char); + +#endif diff --git a/directories/input.c b/directories/input.c new file mode 100644 index 0000000..0c9b6f9 --- /dev/null +++ b/directories/input.c @@ -0,0 +1,21 @@ +#include "input.h" + +int integer_input(char* val) { + + int num; + char *error = ""; + + do { + printf("%s\n%s",error, val); + fflush(stdout); + + char buf[128]; + read(STDIN_FILENO, buf, 127); + num = atoi(buf); + + error = "\nInput Error, try again."; + + } while(!num); + + return num; +} diff --git a/directories/input.h b/directories/input.h new file mode 100644 index 0000000..a2d4906 --- /dev/null +++ b/directories/input.h @@ -0,0 +1,12 @@ + +#include "stdio.h" +#include "stdlib.h" +#include "unistd.h" + +#ifndef INPUT +#define INPUT + +// Error checking integer input +int integer_input(char*); + +#endif diff --git a/directories/node.c b/directories/node.c new file mode 100644 index 0000000..698180c --- /dev/null +++ b/directories/node.c @@ -0,0 +1,62 @@ + +#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; +} + +int count_nodes(struct node* head) { + + int num = 1; + + if(!head) + return -1; + + struct node* temp = head; + while(temp->node) { + + temp = temp->node; + num++; + } + + return num; +} + +void iterate_nodes(struct node* head, void *func (void*, void*), void* val) { + + if(!head) + return; + + func(head->data, val); + + struct node* temp = head; + + while(temp->node) { + temp = temp->node; + func(temp->data, val); + } +} + diff --git a/directories/node.h b/directories/node.h new file mode 100644 index 0000000..71f4d6d --- /dev/null +++ b/directories/node.h @@ -0,0 +1,24 @@ +#include "stdlib.h" + +#ifndef NODES +#define NODES + +struct node { + struct node* node; + void* data; +}; + +// Appends Node to list +void append_node(struct node*, struct node*); + +// Creates node with value and appends +// Returns node that was created +struct node* appendv_node(struct node*, void*); + +// Counts the number of nodes in a list +int count_nodes(struct node*); + +// Iterates over every node in a list and runs the given function +void iterate_nodes(struct node*, void *func(void*, void*), void*); + +#endif