Skip to content
Snippets Groups Projects
Commit 539dbc89 authored by Brandon Rodriguez's avatar Brandon Rodriguez
Browse files

Implement basic array sorting

parent 0c0d6e11
Branches
No related merge requests found
......@@ -57,7 +57,9 @@ void tokenize_line(); // Separates lines into fields.
void populate_array(); // Populates array with fields.
void resize_array(); // Reallocates memory for array.
char* remove_quotes(); // Removes quotes from string value.
void print_all_songs(); // Prints info for all songs in array.
void print_song_info(); // Prints all info for provided song.
void sort_array(); // Sorts array by song name.
void exit_program(); // Frees memory and closes program.
......@@ -70,17 +72,10 @@ int main(int argc, char* argv[]) {
int file_descriptor = open_file();
read_file(file_descriptor);
write(1, "\n\n\n\n\n", 10);
print_song_info(songs_array[1]);
print_song_info(songs_array[2]);
print_song_info(songs_array[3]);
print_song_info(songs_array[4]);
print_song_info(songs_array[5]);
print_song_info(songs_array[6]);
print_song_info(songs_array[7]);
print_song_info(songs_array[8]);
print_song_info(songs_array[9]);
print_song_info(songs_array[10]);
print_all_songs();
write(1, "\nSorting...\n\n\n", 15);
sort_array();
print_all_songs();
exit_program();
}
......@@ -123,7 +118,6 @@ void read_file(int file_descriptor) {
index = BUFFER_SIZE - 1;
offset_amount = 0;
*temp_buffer = *read_buffer;
printf("Looping through buffer...\n");
while (index > 0) {
if (temp_buffer[index] == '\n') {
// Found first null terminator. Save location and exit loop.
......@@ -141,7 +135,6 @@ void read_file(int file_descriptor) {
err_sys("Failed to update pointer on read in.");
}
printf("Buffer: %d, Offset: %d, ReadSize: %ld\n\n", BUFFER_SIZE, offset_amount, read_size);
read_line(read_buffer, read_size);
free(read_buffer);
}
......@@ -157,7 +150,6 @@ void read_line(char *read_buffer, size_t read_size) {
line_buffer = calloc(BUFFER_SIZE, sizeof(char));
printf("Looping through for individual lines...\n");
// Loop through entire chunk.
while ((chunk_index < read_size) && (strcmp(&read_buffer[chunk_index], "\0") != 0)) {
// Loop through individual line and copy values.
......@@ -178,9 +170,6 @@ void read_line(char *read_buffer, size_t read_size) {
chunk_index++;
}
}
// Last field so tokenize line one last time.
line_buffer[line_index] = '\0';
tokenize_line(line_buffer);
free(line_buffer);
}
......@@ -303,6 +292,44 @@ char* remove_quotes(char* a_string) {
}
/**
* Sorts array by song name.
*/
void sort_array() {
int sorted_bool = 0; // Holds if array has been sorted during this call.
int index = 1; // Current index.
songs_struct* temp_song;
// Loop through all array elements and sort if necessary.
// Starts at 1 due to comparing two elements at once.
while (songs_array[index] != NULL) {
if (strcmp(songs_array[index - 1]->song_name, songs_array[index]->song_name) > 0) {
temp_song = songs_array[index - 1];
songs_array[index - 1] = songs_array[index];
songs_array[index] = temp_song;
sorted_bool = 1;
}
index++;
}
if (sorted_bool == 1) {
sort_array();
}
}
/**
* 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.
*/
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment