diff --git a/UseDataBase.c b/UseDataBase.c index 1d98322b6631c7e5d108bc8dc74f171dc1690ec6..0220d89f91232e252687d89c212f60809c3201db 100644 --- a/UseDataBase.c +++ b/UseDataBase.c @@ -9,15 +9,21 @@ /** * Description: * - * Reads in song data from two song-database files. + * Prompts user for input. * - * Then allows user to search and print out song information. + * Upon needing to access songs, program reads from two files: + * Directory: Stores the byte size of each song, in order, as they vary. + * Binary: Stores the actual data for each song. + * + * Data read from files is temporary, and only one song is read in at a time. */ /** * Known Issues: * + * Since the songs are only sorted by song name, searching by album is slow. + * Album search automatically occurs if provided "song name" is not found. */ @@ -46,7 +52,7 @@ typedef struct { double* hotttnesss; } songs_struct; // Songs struct. -int test_file_bool = 1; // Check if using smaller test csv or not. +int test_file_bool = 0; // Check if using smaller test csv or not. int total_song_count; // Total number of songs in database. songs_struct* song_holder_struct; // The holder of current song data. @@ -193,10 +199,6 @@ void read_song_of_index(int desired_index) { err_sys("Failed to read line."); } - // printf("Index: %d ", current_index); - // printf("Read value: %ld ", read_value); - // printf("Song size: %d\n", *song_size); - if (current_index < desired_index) { // Read binary file. // I don't understand why but this read seems necessary @@ -228,10 +230,6 @@ void read_song_of_index(int desired_index) { err_sys("Failed to read line."); } - // printf("Read value: %ld\n", read_value); - // printf("Buffer value: %s\n\n", read_buffer); - - // Populate song struct. populate_song(read_buffer); @@ -325,31 +323,38 @@ void populate_song(ssize_t song_buffer) { void get_song_count() { - int index = 0; - char* previous_name = calloc(1, BUFFER_SIZE); - char* previous_album = calloc(1, BUFFER_SIZE); + int song_directory_descriptor; // File descriptor for directory data. + int* dummy_holder; + ssize_t read_value; + off_t offset_size; - previous_name[index] = '\n'; - previous_album[index] = '\n'; + total_song_count = 0; + dummy_holder = calloc(1, sizeof(int)); - // Initialize to first song for start of while loop. - read_song_of_index(index); + // Check test csv bool. If true, read smaller csv. + if (test_file_bool) { + song_directory_descriptor = open_file("Data/SongDirectory_Small", O_RDONLY); + } else { // Else use full data set. + song_directory_descriptor = open_file("Data/SongDirectory", O_RDONLY); + } + + // Read directory file until no more values and count index. + while ((read_value = read(song_directory_descriptor, dummy_holder, sizeof(int))) != 0) { + if (read_value < 0) { + err_sys("Failed to read line."); + } - // Count current number of songs in array. - while ((strcmp(previous_name, song_holder_struct->song_name) != 0) && - (strcmp(previous_album, song_holder_struct->album_name) != 0)) { - free(previous_name); - free(previous_album); - previous_name = copy_string(song_holder_struct->song_name); - previous_album = copy_string(song_holder_struct->album_name); - - index++; - total_song_count++; - read_song_of_index(index); - printf("Total songs: %d\n", total_song_count); + // Offset directory file. + offset_size = lseek(song_directory_descriptor, sizeof(int), SEEK_CUR); + if (offset_size < 0) { + err_sys("Failed to update pointer on read in."); + } + total_song_count++; } - free(previous_name); - free(previous_album); + printf("Total songs: %d\n\n", total_song_count); + + free(dummy_holder); + close(song_directory_descriptor); } @@ -419,10 +424,6 @@ void find_song(char* user_input_string) { songs_struct* song; songs_struct* temp_song = calloc(1, sizeof(songs_struct*)); - if (total_song_count < 1) { - get_song_count(); - } - // // Search for user's input, first by song_name, then by album name. song = search_array_by_song(temp_song, 0, total_song_count, user_input_string); @@ -431,6 +432,8 @@ void find_song(char* user_input_string) { print_song_info(); free(temp_song); } else { // Not found. Search again by album instead of song_name. + printf("Could not find song with name. Searching by album.\n"); + printf("(This may take a bit. Album search is slower.)\n"); song = search_array_by_album(0, total_song_count, user_input_string); if (song != NULL) { print_song_info();