diff --git a/BuildDataBase.c b/BuildDataBase.c index 06c20b7fb62569d1aa6605688923acf2ec4fcd48..8b05edf22ecc008aaead0fd786f12ac99c512555 100644 --- a/BuildDataBase.c +++ b/BuildDataBase.c @@ -78,7 +78,7 @@ int main(int argc, char* argv[]) { //print_all_songs(); //write(1, "\nSorting...\n\n\n", 15); sort_array(); - //print_all_songs(); + print_all_songs(); save_array(); close(file_descriptor); exit_program(); @@ -90,18 +90,17 @@ int main(int argc, char* argv[]) { */ int open_file(char* file_location, int operator_flags) { int file_descriptor; + // Handle for creating file. if ((operator_flags == (O_CREAT | O_APPEND | O_WRONLY)) || (operator_flags == (O_CREAT | O_TRUNC | O_WRONLY))) { - // Handle for creating file. // Umask for issues with other_write privledges not assigning. mode_t oldval = umask(0); file_descriptor = open(file_location, operator_flags, 0777); - //S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH); umask(oldval); if (file_descriptor < 0) { err_sys("Failed to open file."); } - } else { // Handle for all else. + } else { // Handle for non-creation. file_descriptor = open(file_location, operator_flags); if (file_descriptor < 0) { err_sys("Failed to open file."); @@ -406,18 +405,18 @@ void save_line(int directory_descriptor, int binary_descriptor) { line_buffer = calloc(1, BUFFER_SIZE); char_pointer = (char*)line_buffer; - printf("SongName: %s\n", songs_array[song_index]->song_name); - printf("Size of SongName: %ld\n", (strlen(songs_array[song_index]->song_name)) * sizeof(char)); - printf("AlbumName: %s\n", songs_array[song_index]->album_name); - printf("Size of AlbumName: %ld\n", (strlen(songs_array[song_index]->album_name)) * sizeof(char)); - printf("Artist: %s\n", songs_array[song_index]->artist); - printf("Size of Artist: %ld\n", (strlen(songs_array[song_index]->artist)) * sizeof(char)); - printf("Duration: %.2f\n", *songs_array[song_index]->duration); - printf("Size of Duration: %ld\n", sizeof(float)); - printf("Hotttnesss: %f\n", *songs_array[song_index]->hotttnesss); - printf("Size of Hotttnesss: %ld\n", sizeof(double)); - printf("Year: %d\n", *songs_array[song_index]->year); - printf("Size of Year: %ld\n", sizeof(int)); + // printf("SongName: %s\n", songs_array[song_index]->song_name); + // printf("Size of SongName: %ld\n", (strlen(songs_array[song_index]->song_name)) * sizeof(char)); + // printf("AlbumName: %s\n", songs_array[song_index]->album_name); + // printf("Size of AlbumName: %ld\n", (strlen(songs_array[song_index]->album_name)) * sizeof(char)); + // printf("Artist: %s\n", songs_array[song_index]->artist); + // printf("Size of Artist: %ld\n", (strlen(songs_array[song_index]->artist)) * sizeof(char)); + // printf("Duration: %.2f\n", *songs_array[song_index]->duration); + // printf("Size of Duration: %ld\n", sizeof(float)); + // printf("Hotttnesss: %f\n", *songs_array[song_index]->hotttnesss); + // printf("Size of Hotttnesss: %ld\n", sizeof(double)); + // printf("Year: %d\n", *songs_array[song_index]->year); + // printf("Size of Year: %ld\n", sizeof(int)); // Get total song value size by adding field memory together. current_line_size += name_size; @@ -427,8 +426,8 @@ void save_line(int directory_descriptor, int binary_descriptor) { current_line_size += hotttnesss_size; current_line_size += year_size; - printf("Song Struct Size: %d\n", current_line_size); - printf("\n\n"); + // 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)); @@ -470,32 +469,23 @@ void save_line(int directory_descriptor, int binary_descriptor) { index = 0; float_pointer = (float*)char_pointer; *float_pointer = songs_array[song_index]->duration[index]; - while (index < duration_size) { - ++float_pointer; - index++; - } + ++float_pointer; // Hotttnesss. index = 0; double_pointer = (double*)float_pointer; *double_pointer = songs_array[song_index]->hotttnesss[index]; - while (index < hotttnesss_size) { - ++double_pointer; - index++; - } + ++double_pointer; // Year. index = 0; int_pointer = (int*)double_pointer; - *double_pointer = songs_array[song_index]->year[index]; - while (index < year_size) { - ++int_pointer; - index++; - } + *int_pointer = songs_array[song_index]->year[index]; + ++int_pointer; - write(1, "Buffer Value: ", 17); - write(1, line_buffer, current_line_size); - write(1, "\n\n", 3); + // write(1, "Buffer Value: ", 17); + // write(1, line_buffer, current_line_size); + // write(1, "\n\n", 3); // Write buffer to file. write_size = write(binary_descriptor, line_buffer, current_line_size); diff --git a/UseDataBase.c b/UseDataBase.c index a114c1bff7aa0c8023b26313704debdaef13cd92..f5122075dcb7e2afe91f53dc02b857ead538a342 100644 --- a/UseDataBase.c +++ b/UseDataBase.c @@ -86,7 +86,7 @@ int main(int argc, char* argv[]) { read_songs(song_binary_descriptor, song_size); } - //print_all_songs(); + print_all_songs(); free(song_size); close(song_directory_descriptor); @@ -131,7 +131,6 @@ void read_songs(int binary_descriptor, int* song_size) { populate_array(song_size, read_buffer); - //free(song_size); free(read_buffer); } @@ -151,106 +150,86 @@ void populate_array(int song_size, ssize_t read_buffer) { songs_array[song_index] = calloc(1, sizeof(songs_struct)); // Get Song Name. - //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); char_buffer[index] = *char_pointer; ++char_pointer; index++; } printf("Name: %s\n", char_buffer); + songs_array[song_index]->song_name = copy_string(char_buffer); + printf("Struct Value: %s\n", songs_array[song_index]->song_name); ++char_pointer; free(char_buffer); - // songs_array[song_index]->song_name = copy_string(field_buffer); - // field_size = ((strlen(songs_array[song_index]->song_name) * sizeof(char)) + 1); - - // printf("%s\n\n", songs_array[song_index]->song_name); // Get Album. - //printf("Getting album name...\n"); char_buffer = calloc(1, BUFFER_SIZE); index = 0; while (char_pointer[0] != '\0') { - //printf("%c\n", *char_pointer); char_buffer[index] = *char_pointer; ++char_pointer; index++; } printf("Album: %s\n", char_buffer); + songs_array[song_index]->album_name = copy_string(char_buffer); + printf("Struct Value: %s\n", songs_array[song_index]->album_name); ++char_pointer; free(char_buffer); - // songs_array[song_index]->album_name = copy_string(field_buffer); - // field_size = ((strlen(songs_array[song_index]->album_name) * sizeof(char)) + 1); - - // printf("%s\n\n", songs_array[song_index]->album_name); // Get Artist. - //printf("Getting artist name...\n"); char_buffer = calloc(1, BUFFER_SIZE); index = 0; while (char_pointer[0] != '\0') { - //printf("%c\n", *char_pointer); char_buffer[index] = *char_pointer; ++char_pointer; index++; } printf("Artist: %s\n", char_buffer); + songs_array[song_index]->artist = copy_string(char_buffer); + printf("Struct Value: %s\n", songs_array[song_index]->artist); ++char_pointer; free(char_buffer); // Get Duration. - //printf("Getting duration...\n"); - float_buffer = calloc(1, BUFFER_SIZE); + float_buffer = calloc(1, (sizeof(float) + 1)); float_pointer = (float*)char_pointer; - //printf("%.2f\n", *float_pointer); *float_buffer = *float_pointer; - index = 0; - while (index < sizeof(float)) { - ++float_pointer; - index++; - } + printf("Duraton: %.2f\n", *float_buffer); + songs_array[song_index]->duration = copy_float(float_buffer); + printf("Struct Value: %.2f\n", *songs_array[song_index]->duration); ++float_pointer; free(float_buffer); // Get Hotttnesss. - //printf("Getting hotttnesss...\n"); - double_buffer = calloc(1, BUFFER_SIZE); + double_buffer = calloc(1, (sizeof(double) + 1)); double_pointer = (double*)float_pointer; - //printf("%.2f\n", *double_pointer); *double_buffer = *double_pointer; - index = 0; - while (index < sizeof(double)) { - ++double_pointer; - index++; - } + printf("Hotttnesss: %.2f\n", *double_buffer); + songs_array[song_index]->hotttnesss = copy_double(double_buffer); + printf("Struct Value: %.2f\n", *songs_array[song_index]->hotttnesss); ++double_pointer; free(double_buffer); // Get Year. - //printf("Getting year...\n"); - int_buffer = calloc(1, BUFFER_SIZE); + int_buffer = calloc(1, (sizeof(int) + 1)); int_pointer = (int*)double_pointer; - //printf("%d\n", *int_pointer); *int_buffer = *int_pointer; - index = 0; - while (index < sizeof(int)) { - ++int_pointer; - index++; - } + printf("Year: %d\n", *int_buffer); + songs_array[song_index]->year = copy_int(int_buffer); + printf("Struct Value: %d\n", *songs_array[song_index]->year); ++int_pointer; free(int_buffer); @@ -299,11 +278,11 @@ void exit_program() { int index = 0; while (songs_array[index] != NULL) { free(songs_array[index]->album_name); - // free(songs_array[index]->artist); + free(songs_array[index]->artist); free(songs_array[index]->song_name); - // free(songs_array[index]->duration); - // free(songs_array[index]->year); - // free(songs_array[index]->hotttnesss); + free(songs_array[index]->duration); + free(songs_array[index]->year); + free(songs_array[index]->hotttnesss); free(songs_array[index]); index++; }