diff --git a/BuildDataBase.c b/BuildDataBase.c index 3f1627e981f1dacb5e46f2df15f74ae265ef7928..5a3806f378901330d25ab12eae2f2decc12346cc 100644 --- a/BuildDataBase.c +++ b/BuildDataBase.c @@ -44,6 +44,7 @@ typedef struct { double* hotttnesss; } songs_struct; // Songs struct. +int test_file_bool = 0; // Check if using smaller test csv or not. int songs_array_max = ARRAY_SIZE; // Saves current size of songs array. int song_index = 0; // Current song index. songs_struct** songs_array; // The array which holds all song pointers. @@ -54,8 +55,8 @@ int open_file(); // Initially opens file to read. void read_file(); // Reads in chunk of file. void read_line(); // Separates file chunk into lines. void tokenize_line(); // Separates lines into fields. +void resize_array(); // Increase buffer size of song array. void populate_array(); // Populates array with fields. -void resize_array(); // Reallocates memory for array. void print_all_songs(); // Prints info for all songs in array. void print_song_info(); // Prints all info for provided song. void sort_array(); // Sorts array by song name. @@ -72,7 +73,15 @@ void exit_program(); // Frees memory and closes program. int main(int argc, char* argv[]) { write(1, "Reading csv...\n", 16); songs_array = calloc(songs_array_max, sizeof(songs_struct *)); - int file_descriptor = open_file("Data/SongCSV_Small.csv", O_RDONLY); + int file_descriptor; + + // Check test csv bool. If true, read smaller csv. + if (test_file_bool) { + file_descriptor = open_file("Data/SongCSV_Small.csv", O_RDONLY); + } else { // Else use full data set. + file_descriptor = open_file("Data/SongCSV.csv", O_RDONLY); + } + read_file(file_descriptor); sort_array(); //print_all_songs(); @@ -200,6 +209,11 @@ void tokenize_line(char *line_buffer) { int field_number = 0; char* field_buffer; + // Check to make sure there are still open spots in songs_array. + if (song_index >= songs_array_max) { + resize_array(); + } + field_buffer = calloc(BUFFER_SIZE, sizeof(char)); songs_array[song_index] = calloc(1, sizeof(songs_struct)); @@ -231,6 +245,22 @@ void tokenize_line(char *line_buffer) { } +/** + * Increase current buffer size and rebuffer songs array. + * Call to initialize, and then anytime the array runs out of space. + */ +void resize_array() { + songs_array_max = songs_array_max * 2; + + songs_array = realloc(songs_array, songs_array_max * sizeof(songs_struct* )); + if (songs_array != NULL) { + printf("Allocation successful.\n"); + } else { + err_dump("Could not allocate memory for realloc."); + } +} + + /** * Takes provided field value and associates with song in struct. */ @@ -344,8 +374,17 @@ void print_song_info(songs_struct* song) { * Saves array to file. Supposed to act as a permanent "database" of sorts. */ void save_array() { - int song_directory_descriptor = open_file("Data/SongDirectory_Small", O_CREAT | O_TRUNC | O_WRONLY); - int song_binary_descriptor = open_file("Data/BinarySongData_Small", O_CREAT | O_TRUNC | O_WRONLY); + int song_directory_descriptor; + int song_binary_descriptor; + + // Check test csv bool. If true, write to smaller csv. + if (test_file_bool) { + song_directory_descriptor = open_file("Data/SongDirectory_Small", O_CREAT | O_TRUNC | O_WRONLY); + song_binary_descriptor = open_file("Data/BinarySongData_Small", O_CREAT | O_TRUNC | O_WRONLY); + } else { // Else use full data set. + song_directory_descriptor = open_file("Data/SongDirectory", O_CREAT | O_TRUNC | O_WRONLY); + song_binary_descriptor = open_file("Data/BinarySongData", O_CREAT | O_TRUNC | O_WRONLY); + } song_index = 0; while (songs_array[song_index] != NULL) { diff --git a/UseDataBase.c b/UseDataBase.c index d9f0f7e58c5357cfa1d2dd67655e671a259f9bd4..359946388a0bc0245308b54463686d93e2aafbaf 100644 --- a/UseDataBase.c +++ b/UseDataBase.c @@ -46,6 +46,7 @@ typedef struct { double* hotttnesss; } songs_struct; // Songs struct. +int test_file_bool = 0; // Check if using smaller test csv or not. int songs_array_max = ARRAY_SIZE; // Saves current size of songs array. int song_index = 0; // Current song index. songs_struct** songs_array; // The array which holds all song pointers. @@ -81,11 +82,18 @@ int main(int argc, char* argv[]) { // Initialize vars. run_program_bool = 1; - song_directory_descriptor = open_file("Data/SongDirectory_Small", O_RDONLY); - song_binary_descriptor = open_file("Data/BinarySongData_Small", O_RDONLY); songs_array = calloc(songs_array_max, sizeof(songs_struct *)); song_size = calloc(1, (sizeof(int) + 1)); + // Check test csv bool. If true, read smaller csv. + if (test_file_bool) { + song_directory_descriptor = open_file("Data/SongDirectory_Small", O_RDONLY); + song_binary_descriptor = open_file("Data/BinarySongData_Small", O_RDONLY); + } else { // Else use full data set. + song_directory_descriptor = open_file("Data/SongDirectory", O_RDONLY); + song_binary_descriptor = open_file("Data/BinarySongData", O_RDONLY); + } + // Read songs until no more directories are present. while ((read_value = read(song_directory_descriptor, song_size, sizeof(int))) != 0) { if (read_value < 0) {