From 15e004dc7c04085e53faf271f63935a812d279bf Mon Sep 17 00:00:00 2001 From: stitchy Date: Fri, 13 Oct 2023 03:29:13 -0700 Subject: [PATCH] Node Iterator and Start Input Error Checking --- movies/input.c | 21 +++++++++++++++++++++ movies/input.h | 11 +++++++++++ movies/makefile | 7 +++++-- movies/movies.c | 30 +++++++++++++++++++++++------- movies/movies.h | 1 + movies/node.c | 16 ++++++++++++++++ movies/node.h | 2 ++ 7 files changed, 79 insertions(+), 9 deletions(-) create mode 100644 movies/input.c create mode 100644 movies/input.h diff --git a/movies/input.c b/movies/input.c new file mode 100644 index 0000000..a8125c7 --- /dev/null +++ b/movies/input.c @@ -0,0 +1,21 @@ +#include "input.h" + +int integer_input(char* val) { + + int num; + char *error = ""; + + do { + printf("\n%s\n%s",error, val); + fflush(stdout); + + char buf[128]; + read(STDIN_FILENO, buf, 127); + num = atoi(buf); + + error = "Input Error, try again."; + + } while(!num); + + return num; +} diff --git a/movies/input.h b/movies/input.h new file mode 100644 index 0000000..83cc02d --- /dev/null +++ b/movies/input.h @@ -0,0 +1,11 @@ + +#include "stdio.h" +#include "stdlib.h" +#include "unistd.h" + +#ifndef INPUT +#define INPUT + +int integer_input(char*); + +#endif diff --git a/movies/makefile b/movies/makefile index ce5b84e..226dea1 100644 --- a/movies/makefile +++ b/movies/makefile @@ -1,8 +1,8 @@ CC=gcc --std=c99 -g output=movies -all: movies.c csv_parser.o node.o - $(CC) movies.c csv_parser.o node.o -o $(output) +all: movies.c csv_parser.o node.o input.o + $(CC) movies.c csv_parser.o node.o input.o -o $(output) csv_parser.o: csv_parser.c csv_parser.h $(CC) -c csv_parser.c -o csv_parser.o @@ -10,6 +10,9 @@ csv_parser.o: csv_parser.c csv_parser.h node.o: node.c node.h $(CC) -c node.c -o node.o +input.o: input.h input.c + $(CC) -c input.c -o input.o + run: ./movies movies_sample_1.csv diff --git a/movies/movies.c b/movies/movies.c index 31f85e4..ccd8884 100644 --- a/movies/movies.c +++ b/movies/movies.c @@ -39,23 +39,39 @@ int main(int argc, char** argv) { int menu() { - int option = 0; - printf("1. Show movies released in the specified year\n"); printf("2. Show highest rated movie for each year\n"); printf("3. Show the title and year of release of all movies in a specific language\n"); printf("4. Exit from the program\n\n"); - while(option > 4 || option < 1) { - printf("Enter a choice from 1 to 4: "); - scanf("%i", &option); - } + int option; + do { + option = integer_input("Enter a choice from 1 to 4: "); + } while(option > 4 || option < 1); return option; } +int year_print(struct csv* data, void* val) { + + if(data->year != *(int*)val) + return 0; + + printf("%s\n", data->title); + return 1; +} + void movie_year(struct node* head) { - printf("stub\n"); + + void* f = &year_print; + int* val = malloc(sizeof(int)); + + *val = integer_input("Enter year to search movies from: "); + + + iterate_nodes(head, f, val); + + printf("\n\n"); } diff --git a/movies/movies.h b/movies/movies.h index 81607a1..b76503b 100644 --- a/movies/movies.h +++ b/movies/movies.h @@ -8,6 +8,7 @@ #include "csv_parser.h" #include "node.h" +#include "input.h" int menu(); diff --git a/movies/node.c b/movies/node.c index b2cce29..1ccfa7d 100644 --- a/movies/node.c +++ b/movies/node.c @@ -45,3 +45,19 @@ int count_nodes(struct node* head) { return num; } +void iterate_nodes(struct node* head, void *func (void*, void*), void* val) { + + if(!head) + return; + + if(head->node) + func(head->data, val); + + struct node* temp = head; + + while(temp->node) { + temp = temp->node; + func(temp->data, val); + } +} + diff --git a/movies/node.h b/movies/node.h index 8d1cd07..7b0567f 100644 --- a/movies/node.h +++ b/movies/node.h @@ -14,4 +14,6 @@ struct node* appendv_node(struct node*, void*); int count_nodes(struct node*); +void iterate_nodes(struct node*, void *func(void*, void*), void*); + #endif