Commit 2ac39ba7 authored by Dominik Widhalm's avatar Dominik Widhalm
Browse files

Improved example solution for task 6.06

parent 6d9e9a36
...@@ -51,10 +51,10 @@ typedef struct song { ...@@ -51,10 +51,10 @@ typedef struct song {
/***** FUNCTION PROTOTYPES ****************************************************/ /***** FUNCTION PROTOTYPES ****************************************************/
song_t *songs_new (char *file); song_t *songs_new (char *file);
int songs_scan (song_t **head, char *dir); int songs_scan (song_t **head, const char *dir);
void songs_print (song_t *head); void songs_print (song_t *head);
void songs_shuffle (song_t **head); void songs_shuffle (song_t **head);
void swap (long int *a, long int *b) ; void swap (song_t **a, song_t **b) ;
/***** LOCAL FUNCTIONS ********************************************************/ /***** LOCAL FUNCTIONS ********************************************************/
...@@ -165,7 +165,7 @@ song_t *songs_new (char *file) { ...@@ -165,7 +165,7 @@ song_t *songs_new (char *file) {
* @retval 1 Directory can be accessed * * @retval 1 Directory can be accessed *
* @retval 0 Directory cannot be accessed * * @retval 0 Directory cannot be accessed *
******************************************************************************/ ******************************************************************************/
int songs_scan (song_t **head, char *dir) { int songs_scan (song_t **head, const char *dir) {
/* Temporary song pointer for newly created element */ /* Temporary song pointer for newly created element */
song_t *new = NULL; song_t *new = NULL;
/* Temporary song pointer for traversing the list */ /* Temporary song pointer for traversing the list */
...@@ -186,7 +186,20 @@ int songs_scan (song_t **head, char *dir) { ...@@ -186,7 +186,20 @@ int songs_scan (song_t **head, char *dir) {
return 0; return 0;
} }
/* Scan the given directory */ /* Scan the given directory */
while ((file = readdir(fd)) && (read_songs < SONGS_MAX)) { while (fd && (read_songs < SONGS_MAX)) {
/* Local variable for error handling */
int errno = 0;
/* Check if there are files in the directory */
if ((file = readdir(fd)) == NULL) {
/* Close the directory handle */
closedir(fd);
/* Check if readdir resulted in an error */
if (errno != 0) {
return 0;
}
/* Return to calling function */
return 1;
}
/* Ignore the current and parent directories */ /* Ignore the current and parent directories */
if ((!strcmp(file->d_name, ".")) || (!strcmp(file->d_name, ".."))) { if ((!strcmp(file->d_name, ".")) || (!strcmp(file->d_name, ".."))) {
/* Continue with next directory entry */ /* Continue with next directory entry */
...@@ -276,7 +289,7 @@ void songs_print (song_t *head) { ...@@ -276,7 +289,7 @@ void songs_print (song_t *head) {
******************************************************************************/ ******************************************************************************/
void songs_shuffle (song_t **head) { void songs_shuffle (song_t **head) {
/* Array to store the entry's addresses */ /* Array to store the entry's addresses */
long int addresses[SONGS_MAX] = {-1}; song_t *addresses[SONGS_MAX] = {NULL};
/* Temporary variable for the number of entries */ /* Temporary variable for the number of entries */
int entries = 0; int entries = 0;
/* Temporary pointer to traverse the list */ /* Temporary pointer to traverse the list */
...@@ -284,7 +297,7 @@ void songs_shuffle (song_t **head) { ...@@ -284,7 +297,7 @@ void songs_shuffle (song_t **head) {
/* Go through the list and store the addresses */ /* Go through the list and store the addresses */
while (curr != NULL) { while (curr != NULL) {
/* Store the current address */ /* Store the current address */
addresses[entries] = (long int)curr; addresses[entries] = curr;
/* Increment number of entries */ /* Increment number of entries */
entries++; entries++;
/* Go to next element */ /* Go to next element */
...@@ -302,7 +315,7 @@ void songs_shuffle (song_t **head) { ...@@ -302,7 +315,7 @@ void songs_shuffle (song_t **head) {
/* Check if it's currently the first element */ /* Check if it's currently the first element */
if (i==0) { if (i==0) {
/* Set head to the new first element */ /* Set head to the new first element */
*head = (song_t*)addresses[i]; *head = addresses[i];
/* Set next initially to NULL */ /* Set next initially to NULL */
(*head)->next = NULL; (*head)->next = NULL;
/* Save head as new current element */ /* Save head as new current element */
...@@ -332,9 +345,9 @@ void songs_shuffle (song_t **head) { ...@@ -332,9 +345,9 @@ void songs_shuffle (song_t **head) {
* @param a Pointer to the first long int variable * * @param a Pointer to the first long int variable *
* @param b Pointer to the second long int variable * * @param b Pointer to the second long int variable *
******************************************************************************/ ******************************************************************************/
void swap (long int *a, long int *b) { void swap (song_t **a, song_t **b) {
/* Do a triangular exchange */ /* Do a triangular exchange */
long int tmp = *a; song_t *tmp = *a;
*a = *b; *a = *b;
*b = tmp; *b = tmp;
/* A void function has nothing to return */ /* A void function has nothing to return */
...@@ -345,7 +358,7 @@ void swap (long int *a, long int *b) { ...@@ -345,7 +358,7 @@ void swap (long int *a, long int *b) {
/***** MAIN ROUTINE ***********************************************************/ /***** MAIN ROUTINE ***********************************************************/
int main (void) { int main (void) {
/*** Local Variables ***/ /*** Local Variables ***/
song_t *head = NULL; static song_t *head = NULL;
song_t *tmp; song_t *tmp;
char path[STRING_MAX]; char path[STRING_MAX];
...@@ -356,6 +369,7 @@ int main (void) { ...@@ -356,6 +369,7 @@ int main (void) {
printf("Please enter the name of the directory to scan: "); printf("Please enter the name of the directory to scan: ");
/* Read in the user input */ /* Read in the user input */
fgets(path,STRING_MAX,stdin); fgets(path,STRING_MAX,stdin);
path[strlen(path)-1] = '\0';
/* Scan directory and read in the songs */ /* Scan directory and read in the songs */
if (songs_scan(&head,path) == 0) { if (songs_scan(&head,path) == 0) {
......
Markdown is supported
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