OS1/directories/movies.c

191 lines
3.6 KiB
C
Raw Normal View History

2023-10-30 03:25:48 -07:00
#include "movies.h"
int haschanged = 0;
int main(int argc, char** argv) {
if(argc < 2) {
printf("Please Specify Filename\n");
return 64;
}
struct node* head = parse_csv(argv[1]);
// menu logic
enum OPTIONS menu_select = 0;
while(1) {
menu_select = menu();
switch(menu_select) {
case YEAR:
movie_year(head);
break;
case RATE:
movie_rate(head);
break;
case LANGUAGE:
movie_lang(head);
break;
case EXIT:
printf("Goodbye~\n");
return 0;
default:
printf("You naughty~ Pick a correct option next time.\n");
}
}
}
int menu() {
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");
int option;
do {
option = integer_input("Enter a choice from 1 to 4: ");
} while(option > 4 || option < 1);
return option;
}
void year_print(struct csv* data, void* val) {
if(data->year != *(int*)val)
return;
printf("%s\n", data->title);
haschanged = 1;
}
void movie_year(struct node* head) {
void* f = &year_print;
int* val = malloc(sizeof(int));
*val = integer_input("Enter year to search movies from: ");
haschanged = 0;
// Prints each node if movie year the same as specified
iterate_nodes(head, f, val);
if(!haschanged) {
printf("No data about movies released in the year %d", *val);
}
printf("\n\n");
}
void lang_print(struct csv* data, void* val) {
for(int i = 0; i < data->numlang; i++) {
// Prints if movie is in given language
if(!strcmp(val, data->languages[i])) {
printf("%d %s\n", data->year, data->title);
haschanged = 1;
}
}
}
void movie_lang(struct node* head) {
void* f = &lang_print;
char val[41];
printf("Enter Language to search for: ");
fflush(stdout);
fgets(val, 40, stdin);
// Replace newline character with the null terminator
val[strlen(val)-1] = '\0';
haschanged = 0;
iterate_nodes(head, f, &val);
if(!haschanged)
printf("No data about movies released in %s\n", val);
printf("\n");
}
void check_year(int* data, int* year) {
if(*data == *year)
haschanged = 1;
}
void find_year(struct csv* data, void* year) {
void* f = &check_year;
haschanged = 0;
// Finds every year that has a movie produced in our list
iterate_nodes(year, f, &data->year);
if(!haschanged)
appendv_node(year, &data->year);
haschanged = 0;
}
void print_rating(struct csv* data, struct csv** val) {
// Null case handled here
if(val[0] == NULL) {
if(data->year == haschanged)
val[0] = data;
return;
}
// Sets pointer to movie struct of new highest rating
if(data->rating > val[0]->rating
&& data->year == haschanged)
val[0] = data;
}
void index_nodes(int* val, void* head) {
haschanged = *val;
struct csv ** cur_val = malloc(sizeof(struct csv*));
cur_val[0] = NULL;
void* f = &print_rating;
// Find the node of highest rating for specified year
iterate_nodes(head, f, cur_val);
printf("%d %2.1f %s\n", cur_val[0]->year, cur_val[0]->rating, cur_val[0]->title);
haschanged = 0;
}
void movie_rate(struct node* head) {
struct csv* temp = head->data;
struct node* year = appendv_node(NULL, &temp->year);
void* f = &find_year;
// Make a list of every year that has a movie
iterate_nodes(head, f, year);
void* tempish = &index_nodes;
// Iterate though every year and find the highest rating
iterate_nodes(year, tempish, head);
printf("\n");
}