diff --git a/BuildDataBase.c b/BuildDataBase.c index f2d665b067a46259e1acecd7848f49dd0d5a35e5..06c20b7fb62569d1aa6605688923acf2ec4fcd48 100644 --- a/BuildDataBase.c +++ b/BuildDataBase.c @@ -77,7 +77,7 @@ int main(int argc, char* argv[]) { write(1, "\n\n\n\n\n", 6); //print_all_songs(); //write(1, "\nSorting...\n\n\n", 15); - //sort_array(); + sort_array(); //print_all_songs(); save_array(); close(file_descriptor); @@ -370,15 +370,14 @@ void print_song_info(songs_struct* song) { * Saves array to file. Supposed to act as a permanent "database" of sorts. */ void save_array() { - //int file_descriptor = open_file("Data/SongCSV_Small.csv", O_RDONLY); - int song_directory_descriptor = open_file("Data/Song_Directory_Small", O_CREAT | O_TRUNC | O_WRONLY); + 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); song_index = 0; - //while (songs_array[song_index] != NULL) { + while (songs_array[song_index] != NULL) { save_line(song_directory_descriptor, song_binary_descriptor); song_index++; - //} + } close(song_directory_descriptor); close(song_binary_descriptor); @@ -428,12 +427,12 @@ void save_line(int directory_descriptor, int binary_descriptor) { current_line_size += hotttnesss_size; current_line_size += year_size; - printf("%d\n", current_line_size); + printf("Song Struct Size: %d\n", current_line_size); printf("\n\n"); // Save directory values to file. - ssize_t write_size = write(directory_descriptor, current_line_size_ptr, sizeof(int) + 1); - if (write_size < sizeof(int) + 1) { + ssize_t write_size = write(directory_descriptor, current_line_size_ptr, sizeof(int)); + if (write_size < sizeof(int)) { err_sys("Failed to write to song_directory file."); } @@ -494,6 +493,7 @@ void save_line(int directory_descriptor, int binary_descriptor) { index++; } + write(1, "Buffer Value: ", 17); write(1, line_buffer, current_line_size); write(1, "\n\n", 3); diff --git a/UseDataBase.c b/UseDataBase.c index 780ee52c2de3368a7b8706ac9b82519dfe5e46dc..a114c1bff7aa0c8023b26313704debdaef13cd92 100644 --- a/UseDataBase.c +++ b/UseDataBase.c @@ -67,12 +67,28 @@ void exit_program(); // Frees memory and closes program. */ int main(int argc, char* argv[]) { songs_array = calloc(songs_array_max, sizeof(songs_struct *)); - int song_directory_descriptor = open_file("Data/Song_Directory_Small", O_RDONLY); + int song_directory_descriptor = open_file("Data/SongDirectory_Small", O_RDONLY); int song_binary_descriptor = open_file("Data/BinarySongData_Small", O_RDONLY); - read_songs(song_directory_descriptor, song_binary_descriptor); + int* song_size = calloc(1, (sizeof(int) + 1)); + off_t read_value; + off_t offset_size; + + while ((read_value = read(song_directory_descriptor, song_size, sizeof(int))) != 0) { + if (read_value < 0) { + err_sys("Failed to read line."); + } + + offset_size = lseek(song_directory_descriptor, sizeof(int), SEEK_CUR); + if (offset_size < 0) { + err_sys("Failed to update pointer on read in."); + } + + read_songs(song_binary_descriptor, song_size); + } //print_all_songs(); + free(song_size); close(song_directory_descriptor); close(song_binary_descriptor); @@ -93,40 +109,35 @@ int open_file(char* file_location, int operator_flags) { } - /** * Reads in songs from database files. */ -void read_songs(int directory_descriptor, int binary_descriptor) { - int* song_size; +void read_songs(int binary_descriptor, int* song_size) { + void* read_buffer; - song_size = calloc(1, (sizeof(int) + 1)); read_buffer = calloc(1, BUFFER_SIZE); - off_t read_value = read(directory_descriptor, song_size, (sizeof(int) + 1)); + printf("Song Struct Size: %d\n", *song_size); + + ssize_t read_value = read(binary_descriptor, read_buffer, *song_size); if (read_value < 0) { - err_sys("Failed to read line."); + err_sys("Failed to read in song."); } - off_t offset_size = lseek(directory_descriptor, (sizeof(int) + 1), SEEK_CUR); + off_t offset_size = lseek(binary_descriptor, *song_size, SEEK_CUR); if (offset_size < 0) { err_sys("Failed to update pointer on read in."); } - printf("%d\n", *song_size); - - read_value = read(binary_descriptor, read_buffer, *song_size); - populate_array(song_size, read_buffer); - free(song_size); + //free(song_size); free(read_buffer); } -void populate_array(int song_size, void* read_buffer) { +void populate_array(int song_size, ssize_t read_buffer) { int index; - int field_size; int* int_buffer; int* int_pointer; char* char_buffer; @@ -140,13 +151,13 @@ void populate_array(int song_size, void* read_buffer) { songs_array[song_index] = calloc(1, sizeof(songs_struct)); // Get Song Name. - printf("Getting song name...\n"); + //printf("Getting song name...\n"); char_pointer = ((char*)read_buffer); char_buffer = calloc(1, BUFFER_SIZE); index = 0; while (char_pointer[0] != '\0') { - printf("%c\n", *char_pointer); + //printf("%c\n", *char_pointer); char_buffer[index] = *char_pointer; ++char_pointer; index++; @@ -161,12 +172,12 @@ void populate_array(int song_size, void* read_buffer) { // Get Album. - printf("Getting album name...\n"); + //printf("Getting album name...\n"); char_buffer = calloc(1, BUFFER_SIZE); index = 0; while (char_pointer[0] != '\0') { - printf("%c\n", *char_pointer); + //printf("%c\n", *char_pointer); char_buffer[index] = *char_pointer; ++char_pointer; index++; @@ -181,12 +192,12 @@ void populate_array(int song_size, void* read_buffer) { // Get Artist. - printf("Getting artist name...\n"); + //printf("Getting artist name...\n"); char_buffer = calloc(1, BUFFER_SIZE); index = 0; while (char_pointer[0] != '\0') { - printf("%c\n", *char_pointer); + //printf("%c\n", *char_pointer); char_buffer[index] = *char_pointer; ++char_pointer; index++; @@ -197,10 +208,10 @@ void populate_array(int song_size, void* read_buffer) { // Get Duration. - printf("Getting duration...\n"); + //printf("Getting duration...\n"); float_buffer = calloc(1, BUFFER_SIZE); float_pointer = (float*)char_pointer; - printf("%.2f\n", *float_pointer); + //printf("%.2f\n", *float_pointer); *float_buffer = *float_pointer; index = 0; while (index < sizeof(float)) { @@ -213,10 +224,10 @@ void populate_array(int song_size, void* read_buffer) { // Get Hotttnesss. - printf("Getting hotttnesss...\n"); + //printf("Getting hotttnesss...\n"); double_buffer = calloc(1, BUFFER_SIZE); double_pointer = (double*)float_pointer; - printf("%.2f\n", *double_pointer); + //printf("%.2f\n", *double_pointer); *double_buffer = *double_pointer; index = 0; while (index < sizeof(double)) { @@ -229,10 +240,10 @@ void populate_array(int song_size, void* read_buffer) { // Get Year. - printf("Getting year...\n"); + //printf("Getting year...\n"); int_buffer = calloc(1, BUFFER_SIZE); int_pointer = (int*)double_pointer; - printf("%d\n", *int_pointer); + //printf("%d\n", *int_pointer); *int_buffer = *int_pointer; index = 0; while (index < sizeof(int)) { @@ -245,6 +256,8 @@ void populate_array(int song_size, void* read_buffer) { printf("\n\n"); + song_index++; + }