diff --git a/BuildDataBase.c b/BuildDataBase.c index 8d75e78fb854125bc090516fcadb6870ec883730..f2d665b067a46259e1acecd7848f49dd0d5a35e5 100644 --- a/BuildDataBase.c +++ b/BuildDataBase.c @@ -375,10 +375,10 @@ void save_array() { 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); @@ -389,6 +389,12 @@ void save_array() { * Takes a single song from the songs arary and creates buffer to save. */ void save_line(int directory_descriptor, int binary_descriptor) { + void* line_buffer; + char* char_pointer; + float* float_pointer; + double* double_pointer; + int* int_pointer; + int index; int current_line_size = 0; int name_size = ((strlen(songs_array[song_index]->song_name) * sizeof(char)) + 1); int album_size = ((strlen(songs_array[song_index]->album_name) * sizeof(char)) + 1); @@ -398,6 +404,9 @@ void save_line(int directory_descriptor, int binary_descriptor) { int year_size = (sizeof(int)); int* current_line_size_ptr = ¤t_line_size; + 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); @@ -435,17 +444,71 @@ void save_line(int directory_descriptor, int binary_descriptor) { // Create song buffer to save values to file. // Song Name. - write_song_field(binary_descriptor, songs_array[song_index]->song_name, name_size); + index = 0; + while (index < name_size) { + *char_pointer = songs_array[song_index]->song_name[index]; + ++char_pointer; + index++; + } + // Album Name. - write_song_field(binary_descriptor, songs_array[song_index]->album_name, album_size); + index = 0; + while (index < album_size) { + *char_pointer = songs_array[song_index]->album_name[index]; + ++char_pointer; + index++; + } + // Artist Name. - write_song_field(binary_descriptor, songs_array[song_index]->artist, artist_size); + index = 0; + while (index < artist_size) { + *char_pointer = songs_array[song_index]->artist[index]; + ++char_pointer; + index++; + } + // Duration. - write_song_field(binary_descriptor, songs_array[song_index]->duration, duration_size); + index = 0; + float_pointer = (float*)char_pointer; + *float_pointer = songs_array[song_index]->duration[index]; + while (index < duration_size) { + ++float_pointer; + index++; + } + // Hotttnesss. - write_song_field(binary_descriptor, songs_array[song_index]->hotttnesss, hotttnesss_size); + index = 0; + double_pointer = (double*)float_pointer; + *double_pointer = songs_array[song_index]->hotttnesss[index]; + while (index < hotttnesss_size) { + ++double_pointer; + index++; + } + // Year. - write_song_field(binary_descriptor, songs_array[song_index]->year, year_size); + index = 0; + int_pointer = (int*)double_pointer; + *double_pointer = songs_array[song_index]->year[index]; + while (index < year_size) { + ++int_pointer; + index++; + } + + 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); + if (write_size < current_line_size) { + err_sys("Failed to write to song_binary file."); + } + + offset_size = lseek(binary_descriptor, current_line_size, SEEK_CUR); + if (offset_size < 0) { + err_sys("Failed to update song_binary write pointer."); + } + + free(line_buffer); } @@ -455,12 +518,12 @@ void save_line(int directory_descriptor, int binary_descriptor) { void write_song_field(int binary_descriptor, void* field, int field_size) { ssize_t write_size = write(binary_descriptor, field, field_size); if (write_size < field_size) { - err_sys("Failed to write to song_directory file."); + err_sys("Failed to write to song_binary file."); } off_t offset_size = lseek(binary_descriptor, field_size, SEEK_CUR); if (offset_size < 0) { - err_sys("Failed to update song_directory write pointer."); + err_sys("Failed to update song_binary write pointer."); } } diff --git a/UseDataBase.c b/UseDataBase.c index f0266709775b18df3e8f73a087f0e62faeebdb69..780ee52c2de3368a7b8706ac9b82519dfe5e46dc 100644 --- a/UseDataBase.c +++ b/UseDataBase.c @@ -22,21 +22,43 @@ // Import headers. +#include <fcntl.h> #include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include <ctype.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> #include "apue.h" +#include "HelperHeader.h" // Define Vars. -#define BUFFERSIZE 4096; +#define BUFFER_SIZE 4096 +#define ARRAY_SIZE 500 // Variables. +typedef struct { + char* artist; + char* song_name; + char* album_name; + float* duration; + int* year; + double* hotttnesss; +} songs_struct; // Songs struct. + +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. // Method Declaration. +int open_file(); // Initially opens file to read. +void read_songs(); // Reads in songs from database files. +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 exit_program(); // Frees memory and closes program. /** @@ -44,5 +66,233 @@ * Initializes and runs 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_binary_descriptor = open_file("Data/BinarySongData_Small", O_RDONLY); + read_songs(song_directory_descriptor, song_binary_descriptor); + + //print_all_songs(); + + close(song_directory_descriptor); + close(song_binary_descriptor); + + exit_program(); +} + + +/** + * Opens file. + */ +int open_file(char* file_location, int operator_flags) { + int file_descriptor; + file_descriptor = open(file_location, operator_flags); + if (file_descriptor < 0) { + err_sys("Failed to open file."); + } + return file_descriptor; +} + + + +/** + * Reads in songs from database files. + */ +void read_songs(int directory_descriptor, 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)); + if (read_value < 0) { + err_sys("Failed to read line."); + } + + off_t offset_size = lseek(directory_descriptor, (sizeof(int) + 1), 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(read_buffer); +} + + +void populate_array(int song_size, void* read_buffer) { + int index; + int field_size; + int* int_buffer; + int* int_pointer; + char* char_buffer; + char* char_pointer; + float* float_buffer; + float* float_pointer; + double* double_buffer; + double* double_pointer; + + + 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); + ++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); + ++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); + ++char_pointer; + free(char_buffer); + + + // Get Duration. + printf("Getting duration...\n"); + float_buffer = calloc(1, BUFFER_SIZE); + 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); + ++float_pointer; + free(float_buffer); + + + // Get Hotttnesss. + printf("Getting hotttnesss...\n"); + double_buffer = calloc(1, BUFFER_SIZE); + 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); + ++double_pointer; + free(double_buffer); + + + // Get Year. + printf("Getting year...\n"); + int_buffer = calloc(1, BUFFER_SIZE); + 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); + ++int_pointer; + free(int_buffer); + + printf("\n\n"); + +} + + + +/** + * Prints all songs. + */ +void print_all_songs() { + int index = 0; + while (songs_array[index] != NULL) { + print_song_info(songs_array[index]); + index++; + } +} + + +/** + * Attempts to find song with provided name. Prints result. + */ +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("\n"); +} + + +/** + * Frees remaining memory and so program can close cleanly. + */ +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]->song_name); + // free(songs_array[index]->duration); + // free(songs_array[index]->year); + // free(songs_array[index]->hotttnesss); + free(songs_array[index]); + index++; + } + free(songs_array); } diff --git a/makefile b/makefile index a8602f869df5d7bafa1d8b653e676d31337f7542..9cf216c559fd501c0ce214833fb4e3bd3f35e490 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,6 @@ all: gcc -Wall -Wpedantic -std=c99 -g BuildDataBase.c error.c CopyFunctions.c -o BuildDataBase - gcc -Wall -Wpedantic -std=c99 -g UseDataBase.c -o UseDataBase + gcc -Wall -Wpedantic -std=c99 -g UseDataBase.c error.c CopyFunctions.c -o UseDataBase build: ./BuildDataBase use: