Project Should be working

This commit is contained in:
stitchy 2023-10-30 03:25:48 -07:00
parent 9136bc7740
commit 8eb004e775
Signed by: stitchy
SSH key fingerprint: SHA256:yz2SoxdnY67tfY5Jzb0f2v8f5W3o/IF359kbcquWip8
10 changed files with 491 additions and 8 deletions

View file

@ -5,7 +5,7 @@ struct node* parse_csv(char* name) {
FILE * file;
file = fopen(name, "r");
if(!file) {
printf("Your File was Stolen by the Gremlins\n");
perror("Error");
exit(66);
}

View file

@ -0,0 +1,121 @@
#include <stdio.h>
#include "stdlib.h"
#include <dirent.h>
#include <sys/stat.h>
#include "string.h"
static int
filter_movies (const struct dirent *unused)
{
if(strstr(unused->d_name, "movies_") == unused->d_name) {
char* temp = strchr(unused->d_name, '.');
if(temp != NULL)
if( !strcmp(temp, ".csv"))
return 1;
}
return 0;
}
void dir_free(struct dirent** ent, int n) {
int i = 0;
while(n--) {
free(ent[n]);
i++;
}
free(ent);
}
char* largest_val() {
struct dirent** eps;
int num = scandir ("./", &eps, filter_movies, alphasort);
if(num < 1)
exit(1);
struct dirent* max = eps[0];
struct stat *cur_stat = malloc(sizeof(struct stat));
stat(eps[0]->d_name, cur_stat);
int n = 1;
struct stat *st = malloc(sizeof(struct stat));
while(n < num) {
stat (eps[n]->d_name, st);
if(st->st_size > cur_stat->st_size) {
struct stat* temp = cur_stat;
cur_stat = st;
st = temp;
max = eps[n];
}
n++;
}
char* ret = malloc((strlen(max->d_name) + 1) * sizeof(char));
strcpy(ret, max->d_name);
free(cur_stat);
free(st);
dir_free(eps, n);
return ret;
}
char* smallest_val() {
struct dirent** eps;
int num = scandir ("./", &eps, filter_movies, alphasort);
if(num < 1)
exit(1);
struct dirent* max = eps[0];
struct stat *cur_stat = malloc(sizeof(struct stat));
stat(eps[0]->d_name, cur_stat);
int n = 1;
struct stat *st = malloc(sizeof(struct stat));
while(n < num) {
stat (eps[n]->d_name, st);
if(st->st_size < cur_stat->st_size) {
struct stat* temp = cur_stat;
cur_stat = st;
st = temp;
max = eps[n];
}
n++;
}
char* ret = malloc((strlen(max->d_name) + 1) * sizeof(char));
strcpy(ret, max->d_name);
free(cur_stat);
free(st);
dir_free(eps, n);
return ret;
}
int temp() {
char* large = largest_val();
printf("Largest Moives: %s\n", large);
char* small = smallest_val();
printf("Smallest Moives: %s\n", small);
free(small);
free(large);
return 0;
}

View file

@ -0,0 +1,10 @@
#ifndef DIR_LISTER
#define DIR_LISTER
char* largest_val();
char* smallest_val();
#endif

View file

@ -19,3 +19,16 @@ int integer_input(char* val) {
return num;
}
char* string_input(char* val) {
char* string = malloc(sizeof(char) * 1000);
printf("%s", val);
fflush(stdout);
ssize_t n = read(STDIN_FILENO, string, 1000);
string[n - 1] = '\0';
return string;
}

View file

@ -1,5 +1,6 @@
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "unistd.h"
@ -8,5 +9,6 @@
// Error checking integer input
int integer_input(char*);
char* string_input(char*);
#endif

23
directories/makefile Normal file
View file

@ -0,0 +1,23 @@
CC=gcc --std=gnu99 -g
output=movies_by_year
all: movies_by_year.c movies_by_year.h csv_parser.o node.o input.o directory_list.o
$(CC) movies_by_year.c csv_parser.o node.o input.o directory_list.o -o $(output)
csv_parser.o: csv_parser.c csv_parser.h
$(CC) -c csv_parser.c -o csv_parser.o
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
directory_list.o: directory_list.c directory_list.h
$(CC) -c directory_list.c -o directory_list.o
run:
./movies_by_year movies_sample_1.csv
clean:
rm -fr *.o vgcore.* temp.movies.* $(output)

190
directories/movies.c Normal file
View file

@ -0,0 +1,190 @@
#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");
}

29
directories/movies.h Normal file
View file

@ -0,0 +1,29 @@
#ifndef MOVIES
#define MOVIES
#include "stdio.h"
#include "stdlib.h"
#include "csv_parser.h"
#include "node.h"
#include "input.h"
int menu();
// Headers for the options called by menu
void movie_year(struct node*);
void movie_rate(struct node*);
void movie_lang(struct node*);
// Menu Options
enum OPTIONS {
YEAR = 1,
RATE = 2,
LANGUAGE = 3,
EXIT = 4,
};
#endif

View file

@ -7,13 +7,100 @@ int main() {
// seed random seed
srand(time(NULL));
struct node* head = parse_csv("movies_sample_1.csv");
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);
// Generate the String for movies
char* dir = malloc(sizeof(char)*100);
sprintf(dir, "temp.movies.%d", rand() % 100000);
return 0;
printf("Created directory with the name %s\n", dir);
print_csv(head, dir);
free(dir_str);
free(dir);
}
void write_file(struct csv* node, FILE* mov_file) {
@ -46,7 +133,6 @@ void index_nodes(int* val, void* head) {
void print_csv(struct node* head, char* dir) {
int result = mkdir(dir, 0750);
printf("%d\n", result);
chdir(dir);
@ -62,6 +148,8 @@ void print_csv(struct node* head, char* dir) {
// Iterate through every year year
iterate_nodes(year, tempish, head);
chdir("..");
}
void check_year(int* data, int* year) {

View file

@ -12,19 +12,26 @@
#include "csv_parser.h"
#include "node.h"
#include "input.h"
#include "directory_list.h"
int menu();
int select_ftype();
void print_csv(struct node*, char*);
// Menu Options
enum OPTIONS {
YEAR = 1,
RATE = 2,
LANGUAGE = 3,
EXIT = 4,
PROCCESS = 1,
EXIT = 2,
SELECT = 3,
LARGE = 4,
SMALL = 5,
};
void movie_by_small();
void movie_by_large();
void movie_by_select();
void choose_movie(char*);
void find_year(struct csv*, void*);
#endif