OS1/directories/movies_by_year.c

183 lines
3.2 KiB
C
Raw Normal View History

2023-10-25 13:09:11 -07:00
#include "movies_by_year.h"
int haschanged = 0;
int main() {
// seed random seed
srand(time(NULL));
2023-10-30 03:25:48 -07:00
enum OPTIONS menu_select = 0;
while(1) {
if(menu_select == 0)
menu_select = menu();
switch(menu_select) {
case SMALL:
movie_by_small();
menu_select = 0;
break;
case LARGE:
movie_by_large();
menu_select = 0;
break;
case SELECT:
movie_by_select();
menu_select = 0;
break;
case PROCCESS:
menu_select = select_ftype();
break;
case EXIT:
printf("Goodbye~\n");
return 0;
default:
printf("You naughty~ Pick a correct option next time.\n");
}
}
}
void movie_by_small() {
char* name = smallest_val();
choose_movie(name);
}
void movie_by_large() {
char* name = largest_val();
choose_movie(name);
}
int select_ftype() {
while(1) {
printf("Enter 1 to pick the largest file\n");
printf("Enter 2 to pick the smallest file\n");
printf("Enter 3 to specify the name of a file\n");
switch(integer_input("Enter a choice from 1 to 3: ")) {
case 1:
return 4;
case 2:
return 5;
case 3:
return 3;
default:
break;
}
}
}
int menu() {
printf("1. Select file to process\n");
printf("2. Exit program\n");
int choice = 0;
while((choice > 2) || (choice < 1)) {
choice = integer_input("Enter Choice 1 or 2: ");
}
return choice;
}
void movie_by_select() {
char* dir_str = string_input("Enter CSV to parse: ");
choose_movie(dir_str);
}
void choose_movie(char* dir_str) {
//printf("\nDirectory: %s\n", dir_str);
struct node* head = parse_csv(dir_str);
2023-10-25 13:09:11 -07:00
2023-10-30 14:15:49 -07:00
if(!head)
return;
2023-10-25 13:09:11 -07:00
// Generate the String for movies
char* dir = malloc(sizeof(char)*100);
sprintf(dir, "temp.movies.%d", rand() % 100000);
2023-10-30 14:15:49 -07:00
printf("Created directory with the name %s\n\n", dir);
2023-10-30 03:25:48 -07:00
print_csv(head, dir);
free(dir_str);
free(dir);
2023-10-25 13:09:11 -07:00
}
void write_file(struct csv* node, FILE* mov_file) {
if(haschanged == node->year)
fprintf(mov_file, "%s\n", node->title);
}
void index_nodes(int* val, void* head) {
haschanged = *val;
char* textfile = malloc(sizeof(char)*30);
sprintf(textfile, "%d.txt", *val);
FILE* movie_file = fopen(textfile, "w");
void* f = &write_file;
// Find the node of highest rating for specified year
iterate_nodes(head, f, movie_file);
fclose(movie_file);
haschanged = 0;
}
void print_csv(struct node* head, char* dir) {
2023-10-30 14:15:49 -07:00
if(!head)
return;
2023-10-25 13:09:11 -07:00
int result = mkdir(dir, 0750);
chdir(dir);
struct csv* temp = head->data;
struct node* year = appendv_node(NULL, &temp->year);
void* f = &find_year;
// Do the node list
iterate_nodes(head, f, year);
void* tempish = &index_nodes;
// Iterate through every year year
iterate_nodes(year, tempish, head);
2023-10-30 03:25:48 -07:00
chdir("..");
2023-10-25 13:09:11 -07:00
}
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;
}