diff --git a/BuildDataBase.c b/BuildDataBase.c index afb9222e42f48d2900707bbe37e5ed58d49e35a6..6b0a35961d7c3a35660f4c6270b6e0c84f2e43b4 100644 --- a/BuildDataBase.c +++ b/BuildDataBase.c @@ -16,6 +16,8 @@ /** * Known Issues: * + * Due to linear sort, valgrind may "freeze" up for a bit. + * Should finish in no more than a minute or two. */ diff --git a/UseDataBase.c b/UseDataBase.c index 4c0812c3385eb475500502d1e4aa159f0ab7e023..c76ad61db8918f308bb948fdef10c803e39a9a95 100644 --- a/UseDataBase.c +++ b/UseDataBase.c @@ -59,6 +59,7 @@ void populate_array(); // Populates array with fields. void resize_array(); // Increase buffer size of song array. void print_all_songs(); // Prints info for all songs in array. void print_song_info(); // Prints all info for provided song. +void print_song_of_index(); // Finds and prints song of given index. void print_help_text(); // Prints helper text for user. void find_song(); // Find song with given name. songs_struct* search_array_by_song(); // Binary search for name. @@ -74,8 +75,12 @@ int main(int argc, char* argv[]) { int run_program_bool; // Bool to keep program running. int song_directory_descriptor; // File descriptor for directory data. int song_binary_descriptor; // File descriptor for binary song data. + int index; // Buffer index; + int atoi_index; // Used if user provides song index. int* song_size; // "Directory" size of current song. - char* user_input_string; // String of user's input. + char* user_input_buffer; // Buffer of user input. + char* temp_buffer; // Temp buffer. + char* user_input_string; // Current string of user's input. char* input_to_lower; // User input as all lower. off_t read_value; // Return value of read attempt. off_t offset_size; // Return value of lseek attempt. @@ -112,41 +117,62 @@ int main(int argc, char* argv[]) { // Actually run program for user input. while (run_program_bool) { - user_input_string = calloc(1, BUFFER_SIZE); + user_input_buffer = calloc(1, BUFFER_SIZE); // Read input from console. + // Puts into buffer incase user inputs multiple lines at once, + // such as from a test file. write(1, "Enter input: ", 14); - read_value = read(0, user_input_string, BUFFER_SIZE); + read_value = read(0, user_input_buffer, BUFFER_SIZE); if (read_value < 0) { err_sys("Failed to read line."); } write(1, "\n", 2); + temp_buffer = user_input_buffer; - user_input_string = remove_newline(user_input_string); - // Read user input and handle accordingly. - if ((user_input_string != NULL)) { // && (strcmp(user_input_string, "") != 0)) { - input_to_lower = to_lower_case(user_input_string); + while (*temp_buffer != '\0') { + index = 0; + user_input_string = calloc(1, BUFFER_SIZE); - // Check if user needs reprinting of help text. - if (strcmp(input_to_lower, "help") == 0) { - print_help_text(); + // Get current value from buffer. + while (*temp_buffer != '\n') { + user_input_string[index] = *temp_buffer; + ++temp_buffer; + index++; } - // Check if user has requested to exit program. - else if (strcmp(input_to_lower, "zzz") == 0) { - run_program_bool = 0; - printf("Exiting Program...\n"); + ++temp_buffer; + //user_input_string = remove_newline(user_input_string); + + // Read user input and handle accordingly. + if ((user_input_string != NULL) && (strcmp(user_input_string, "") != 0)) { + input_to_lower = to_lower_case(user_input_string); + + // Check if user needs reprinting of help text. + if (strcmp(input_to_lower, "help") == 0) { + print_help_text(); + } + // Check if user has requested to exit program. + else if (strcmp(input_to_lower, "zzz") == 0) { + run_program_bool = 0; + printf("Exiting Program...\n"); + } + // Check if user has requested to print all songs. + else if (strcmp(input_to_lower, "all") == 0) { + print_all_songs(); + } + // Check if song index was provided + else if ((atoi_index = atoi(input_to_lower)) != 0) { + print_song_of_index(atoi_index); + } + else { // Attempt to find song with given name. + find_song(input_to_lower); + } + free(input_to_lower); } - // Check if user has requested to print all songs. - else if (strcmp(input_to_lower, "all") == 0) { - print_all_songs(); - } - else { // Attempt to find song with given name. - find_song(input_to_lower); - } - free(input_to_lower); + free(user_input_string); } - free(user_input_string); + free(user_input_buffer); } // Free memory and close program. @@ -298,6 +324,7 @@ void resize_array() { void print_all_songs() { int index = 0; while (songs_array[index] != NULL) { + printf("Song #: %d\n", index); print_song_info(songs_array[index]); index++; } @@ -305,23 +332,38 @@ void print_all_songs() { /** - * Attempts to find song with provided name. Prints result. + * Prints info of provided song. */ void print_song_info(songs_struct* song) { - float duration = *song->duration; - int year = *song->year; - double hotttnesss = *song->hotttnesss; - printf("Song Name: %s\n", song->song_name); printf("Album Name: %s\n", song->album_name); printf("Artist Name: %s\n", song->artist); - printf("Duration: %.2f\n", duration); - printf("Year: %d\n", year); - printf("Hotttnesss: %f\n", hotttnesss); + printf("Duration: %.2f\n", *song->duration); + printf("Year: %d\n", *song->year); + printf("Hotttnesss: %f\n", *song->hotttnesss); printf("\n"); } +/** + * Finds and prints infor for song given index. + */ +void print_song_of_index(int index) { + if (songs_array[index] != NULL) { + printf("Song #: %d\n", index); + printf("Song Name: %s\n", songs_array[index]->song_name); + printf("Album Name: %s\n", songs_array[index]->album_name); + printf("Artist Name: %s\n", songs_array[index]->artist); + printf("Duration: %.2f\n", *songs_array[index]->duration); + printf("Year: %d\n", *songs_array[index]->year); + printf("Hotttnesss: %f\n", *songs_array[index]->hotttnesss); + printf("\n"); + } else { // No song at provided index. + printf("No song at index %d\n", index); + } +} + + /** * Prints helper text. * Called on program start and again if user types "Help". @@ -368,7 +410,6 @@ void find_song(char* user_input_string) { free(temp_string); } } - //free(song); } diff --git a/test-input.txt b/test-input.txt new file mode 100644 index 0000000000000000000000000000000000000000..e5967bf63abd902a23e97bf0c80d04de93dbdb90 --- /dev/null +++ b/test-input.txt @@ -0,0 +1,14 @@ +We're Not Gonna Bow +Deep Sea Creature +Stand By Me +Ralph's Rhapsody +Don't Mess With the IRS +Dr. Elmo's Twisted Christmas + +Too Many Choices +These Days +Armageddon's Raid +Single Ladies (Put A Ring On It) +Get On Top (Album Version) +Rudeboy +ZZZ