2023-10-12 14:25:48 -07:00
|
|
|
#include "csv_parser.h"
|
|
|
|
|
2023-10-13 01:39:26 -07:00
|
|
|
struct node* parse_csv(char* name) {
|
2023-10-12 14:25:48 -07:00
|
|
|
|
|
|
|
FILE * file;
|
2023-10-13 01:39:26 -07:00
|
|
|
file = fopen(name, "r");
|
|
|
|
if(!file) {
|
|
|
|
printf("Your File was Stolen by the Gremlins\n");
|
|
|
|
exit(66);
|
2023-10-12 14:25:48 -07:00
|
|
|
|
2023-10-13 01:39:26 -07:00
|
|
|
}
|
|
|
|
// Buffer File For chars
|
2023-10-12 14:25:48 -07:00
|
|
|
char* buffer;
|
|
|
|
size_t buff_size = 1000;
|
|
|
|
size_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);
|
|
|
|
|
2023-10-13 01:39:26 -07:00
|
|
|
printf("Processed file %s and processed data for %d movies.\n\n", name, count_nodes(head));
|
|
|
|
|
2023-10-12 14:25:48 -07:00
|
|
|
return head;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
struct csv* parse_line(char* line) {
|
|
|
|
|
|
|
|
struct csv* mov = malloc(sizeof(struct csv));
|
|
|
|
|
2023-10-13 00:55:11 -07:00
|
|
|
// Deal with title
|
2023-10-12 14:25:48 -07:00
|
|
|
char* sub = strtok(line, ",");
|
2023-10-13 00:55:11 -07:00
|
|
|
mov->title = malloc(sizeof(char) *(strlen(sub) + 1));
|
|
|
|
strcpy(mov->title, sub);
|
2023-10-12 14:25:48 -07:00
|
|
|
|
|
|
|
mov->year = atoi(strtok(NULL, ","));
|
|
|
|
|
2023-10-13 00:55:11 -07:00
|
|
|
// Allocate number of languages and save string
|
2023-10-12 14:25:48 -07:00
|
|
|
sub = strtok(NULL, ",");
|
2023-10-13 00:55:11 -07:00
|
|
|
mov->numlang = 1 + count_char(sub, ';');
|
|
|
|
mov->languages = malloc(mov->numlang * sizeof(char*));
|
|
|
|
|
|
|
|
char* lang = malloc(sizeof(char) * (strlen(sub) + 1));
|
|
|
|
strcpy(lang, sub);
|
2023-10-12 14:25:48 -07:00
|
|
|
|
|
|
|
mov->rating = atof(strtok(NULL, ""));
|
|
|
|
|
2023-10-13 00:55:11 -07:00
|
|
|
|
|
|
|
// 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';
|
|
|
|
|
2023-10-12 14:25:48 -07:00
|
|
|
return mov;
|
|
|
|
}
|
|
|
|
|
2023-10-15 16:11:39 -07:00
|
|
|
// Debugging; not used
|
2023-10-12 14:25:48 -07:00
|
|
|
void print_movies(struct node* head) {
|
|
|
|
|
|
|
|
if(!head) {
|
2023-10-14 16:57:09 -07:00
|
|
|
printf("Yo F'd up mate (print_movies)");
|
2023-10-12 14:25:48 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct node* node = head;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
|
|
|
|
print_line(node->data);
|
|
|
|
|
|
|
|
if(node->node)
|
|
|
|
node = node->node;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-15 16:11:39 -07:00
|
|
|
// Debugging; not used
|
2023-10-12 14:25:48 -07:00
|
|
|
void print_line(struct csv* printer) {
|
|
|
|
|
2023-10-13 00:55:11 -07:00
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-15 16:11:39 -07:00
|
|
|
// Counts number of times given char appears in string
|
2023-10-13 00:55:11 -07:00
|
|
|
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++;
|
|
|
|
}
|
2023-10-12 14:25:48 -07:00
|
|
|
|
2023-10-13 00:55:11 -07:00
|
|
|
return num;
|
2023-10-12 14:25:48 -07:00
|
|
|
}
|