Import libraries (I know they should be submodules but no one cares
This commit is contained in:
parent
cf0a50dcf0
commit
a5f54c2528
6 changed files with 278 additions and 0 deletions
129
directories/csv_parser.c
Normal file
129
directories/csv_parser.c
Normal file
|
@ -0,0 +1,129 @@
|
|||
#include "csv_parser.h"
|
||||
|
||||
struct node* parse_csv(char* name) {
|
||||
|
||||
FILE * file;
|
||||
file = fopen(name, "r");
|
||||
if(!file) {
|
||||
printf("Your File was Stolen by the Gremlins\n");
|
||||
exit(66);
|
||||
|
||||
}
|
||||
// Buffer File For chars
|
||||
char* buffer;
|
||||
size_t buff_size = 1000;
|
||||
ssize_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);
|
||||
|
||||
printf("Processed file %s and processed data for %d movies.\n\n", name, count_nodes(head));
|
||||
|
||||
return head;
|
||||
|
||||
}
|
||||
|
||||
struct csv* parse_line(char* line) {
|
||||
|
||||
struct csv* mov = malloc(sizeof(struct csv));
|
||||
|
||||
// Deal with title
|
||||
char* sub = strtok(line, ",");
|
||||
mov->title = malloc(sizeof(char) *(strlen(sub) + 1));
|
||||
strcpy(mov->title, sub);
|
||||
|
||||
mov->year = atoi(strtok(NULL, ","));
|
||||
|
||||
// Allocate number of languages and save string
|
||||
sub = strtok(NULL, ",");
|
||||
mov->numlang = 1 + count_char(sub, ';');
|
||||
mov->languages = malloc(mov->numlang * sizeof(char*));
|
||||
|
||||
char* lang = malloc(sizeof(char) * (strlen(sub) + 1));
|
||||
strcpy(lang, sub);
|
||||
|
||||
mov->rating = atof(strtok(NULL, ""));
|
||||
|
||||
|
||||
// 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';
|
||||
|
||||
return mov;
|
||||
}
|
||||
|
||||
// Debugging; not used
|
||||
void print_movies(struct node* head) {
|
||||
|
||||
if(!head) {
|
||||
printf("Yo F'd up mate (print_movies)");
|
||||
return;
|
||||
}
|
||||
|
||||
struct node* node = head;
|
||||
|
||||
while(1) {
|
||||
|
||||
print_line(node->data);
|
||||
|
||||
if(node->node)
|
||||
node = node->node;
|
||||
else
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Debugging; not used
|
||||
void print_line(struct csv* printer) {
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
// Counts number of times given char appears in string
|
||||
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++;
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
30
directories/csv_parser.h
Normal file
30
directories/csv_parser.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
#include "stdio.h"
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
#include "node.h"
|
||||
|
||||
#ifndef CSV_PARSER
|
||||
#define CSV_PARSER
|
||||
|
||||
|
||||
struct csv {
|
||||
char* title;
|
||||
int year;
|
||||
int numlang;
|
||||
char** languages;
|
||||
float rating;
|
||||
struct node* node;
|
||||
};
|
||||
|
||||
struct node* parse_csv(char*);
|
||||
|
||||
struct csv* parse_line(char*);
|
||||
|
||||
void print_movies(struct node*);
|
||||
|
||||
void print_line(struct csv*);
|
||||
|
||||
int count_char(char*, char);
|
||||
|
||||
#endif
|
21
directories/input.c
Normal file
21
directories/input.c
Normal file
|
@ -0,0 +1,21 @@
|
|||
#include "input.h"
|
||||
|
||||
int integer_input(char* val) {
|
||||
|
||||
int num;
|
||||
char *error = "";
|
||||
|
||||
do {
|
||||
printf("%s\n%s",error, val);
|
||||
fflush(stdout);
|
||||
|
||||
char buf[128];
|
||||
read(STDIN_FILENO, buf, 127);
|
||||
num = atoi(buf);
|
||||
|
||||
error = "\nInput Error, try again.";
|
||||
|
||||
} while(!num);
|
||||
|
||||
return num;
|
||||
}
|
12
directories/input.h
Normal file
12
directories/input.h
Normal file
|
@ -0,0 +1,12 @@
|
|||
|
||||
#include "stdio.h"
|
||||
#include "stdlib.h"
|
||||
#include "unistd.h"
|
||||
|
||||
#ifndef INPUT
|
||||
#define INPUT
|
||||
|
||||
// Error checking integer input
|
||||
int integer_input(char*);
|
||||
|
||||
#endif
|
62
directories/node.c
Normal file
62
directories/node.c
Normal file
|
@ -0,0 +1,62 @@
|
|||
|
||||
#include "node.h"
|
||||
|
||||
void append_node(struct node* head, struct node* node_app) {
|
||||
|
||||
if(!head) {
|
||||
head = node_app;
|
||||
return;
|
||||
}
|
||||
|
||||
struct node* temp = head;
|
||||
|
||||
while(temp->node != NULL) {
|
||||
temp = temp->node;
|
||||
}
|
||||
temp->node = node_app;
|
||||
|
||||
}
|
||||
|
||||
struct node* appendv_node(struct node* head, void* data) {
|
||||
|
||||
struct node* node = malloc(sizeof(struct node));
|
||||
node->data = data;
|
||||
node->node = NULL;
|
||||
|
||||
append_node(head, node);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
int count_nodes(struct node* head) {
|
||||
|
||||
int num = 1;
|
||||
|
||||
if(!head)
|
||||
return -1;
|
||||
|
||||
struct node* temp = head;
|
||||
while(temp->node) {
|
||||
|
||||
temp = temp->node;
|
||||
num++;
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
void iterate_nodes(struct node* head, void *func (void*, void*), void* val) {
|
||||
|
||||
if(!head)
|
||||
return;
|
||||
|
||||
func(head->data, val);
|
||||
|
||||
struct node* temp = head;
|
||||
|
||||
while(temp->node) {
|
||||
temp = temp->node;
|
||||
func(temp->data, val);
|
||||
}
|
||||
}
|
||||
|
24
directories/node.h
Normal file
24
directories/node.h
Normal file
|
@ -0,0 +1,24 @@
|
|||
#include "stdlib.h"
|
||||
|
||||
#ifndef NODES
|
||||
#define NODES
|
||||
|
||||
struct node {
|
||||
struct node* node;
|
||||
void* data;
|
||||
};
|
||||
|
||||
// Appends Node to list
|
||||
void append_node(struct node*, struct node*);
|
||||
|
||||
// Creates node with value and appends
|
||||
// Returns node that was created
|
||||
struct node* appendv_node(struct node*, void*);
|
||||
|
||||
// Counts the number of nodes in a list
|
||||
int count_nodes(struct node*);
|
||||
|
||||
// Iterates over every node in a list and runs the given function
|
||||
void iterate_nodes(struct node*, void *func(void*, void*), void*);
|
||||
|
||||
#endif
|
Loading…
Reference in a new issue