Commit b58defbc authored by Dominik Widhalm's avatar Dominik Widhalm
Browse files

Improved example solution of task 6.06 (read only used data from file)

parent 2ac39ba7
......@@ -8,8 +8,8 @@
* Date: 2017-08-15 *
* *
* Info: *
* -) The name of the directory can be entered as "songs", "./songs", *
* "songs/" or "./songs/" *
* -) The name of the directory can be entered as: *
* "songs", "./songs", "songs/" or "./songs/" *
* -) Shuffle an array, see: *
* http://www.geeksforgeeks.org/shuffle-a-given-array/ *
* -) Read MP3 IDE3v1 Tag, see: *
......@@ -32,7 +32,7 @@
// Maximum number of songs to read in
#define SONGS_MAX 20
// (Optional) print the list before shuffling
#define DEBUG 0
#define DEBUG 1
/***** TYPEDEFS ***************************************************************/
......@@ -77,69 +77,79 @@ song_t *songs_new (char *file) {
/* File could not be opened --- return NULL */
return NULL;
}
/* Allocate memory for the new entry */
song_t* new = (song_t*)malloc(sizeof(song_t));
/* Check if memory allocation was successful */
if (new == NULL) {
/* Return NULL to caller */
return NULL;
}
/* Temporary variable to store ID3v1 information (128 byte for raw ID3 data) */
char id3[128];
/* Set the handle to the supposed start of the TAG section */
if (fseek(fp,-128,SEEK_END)) {
/* Close the file handle */
fclose(fp);
/* Free the respective memory */
free(new);
/* Section could not be accessed --- return NULL */
return NULL;
}
/* Try to read the last 128 bytes of MP3 file (where the TAG should be located) */
if ((fseek(fp,-128,SEEK_END)) || (fread(id3,128,1,fp) != 1)) {
/* Read in the first 3 byte (char) */
char tag[3];
if(fread(tag,1,3,fp) != 3) {
/* Close the file handle */
fclose(fp);
/* Section could not be read --- return NULL */
/* Free the respective memory */
free(new);
/* Section could not be accessed --- return NULL */
return NULL;
}
/* All required information read, close the file handle */
fclose(fp);
/* Check if file contains ID3v1 (identifier 'TAG' at the begin) */
if (!strncmp(id3,"TAG",3)) {
/* Pointer to scan through the TAG content (starting after 'TAG') */
char *s = id3+3;
/* Temporary variable for the year string */
char year[5];
/* Check if there is the required "TAG" */
if (!strncmp(tag,"TAG",3)) {
/* First 30 characters are the title */
strncpy(new->title,s,30);
if(fread(new->title,1,30,fp) != 30) {
/* Title could not be read, use default one */
strcpy(new->title,"Unknown Title");
}
/* Terminate string */
new->title[30] = '\0';
/* Increment pointer with 30 character */
s += 30;
/* Next 30 characters are the artist */
strncpy(new->artist,s,30);
if(fread(new->artist,1,30,fp) != 30) {
/* Artist could not be read, use default one */
strcpy(new->artist,"Unknown Artist");
}
/* Terminate string */
new->artist[30] = '\0';
/* Increment pointer with 30 character */
s += 30;
/* The next 30 characters would be the album name --- skip */
s += 30;
if (fseek(fp,30,SEEK_CUR)) {
/* Close the file handle */
fclose(fp);
/* Free the respective memory */
free(new);
/* Section could not be accessed --- return NULL */
return NULL;
}
/* Temporary variable for the year string */
char year[5];
/* Following 4 characters represent the year */
strncpy(year,s,4);
if(fread(year,1,4,fp) != 4) {
/* Year could not be read, use default one */
new->year = 0;
}
/* Terminate string */
year[4] = '\0';
/* Convert string to integer number */
/* Try to convert string to integer number */
if (sscanf(year,"%d",&new->year) == 0) {
/* No number found, use default one */
new->year = 0;
}
/* Increment pointer with 4 character */
s += 4;
/* The next 30 characters would be the comment --- skip */
s += 30;
/* Finally the TAG contains the genre (number 0..147) --- also skip */
s += 1;
/* If now ID3v1 was found, use default information */
/* If no ID3v1 was found, use default information */
} else {
strcpy(new->artist,"Unknown Artist");
strcpy(new->title,"Unknown Title");
......@@ -147,6 +157,10 @@ song_t *songs_new (char *file) {
}
/* Initially set next pointer to NULL */
new->next = NULL;
/* All required information read, close the file handle */
fclose(fp);
/* Return the address of the new element */
return new;
}
......@@ -262,17 +276,17 @@ void songs_print (song_t *head) {
/* Counter for number of songs in list */
int count = 0;
/* Print header */
printf("\n # | Artist | Title | Year\n");
printf("-------------------------------------------------------------------------------------------------\n");
printf(" # | Artist | Title | Year \n");
printf("-----------------------------------------------------------------------------\n");
/* Go through the entire list starting with head */
while (curr != NULL) {
printf(" %2d | %-40s | %-40s | %4d\n",(count+1),curr->artist,curr->title,curr->year);
printf(" %2d | %-30s | %-30s | %4d\n",(count+1),curr->artist,curr->title,curr->year);
/* Set curr to next element */
curr = curr->next;
/* Increase count */
count++;
}
printf("\n");
printf("-----------------------------------------------------------------------------\n");
/* A void function has nothing to return */
return;
}
......@@ -358,7 +372,7 @@ void swap (song_t **a, song_t **b) {
/***** MAIN ROUTINE ***********************************************************/
int main (void) {
/*** Local Variables ***/
static song_t *head = NULL;
song_t *head = NULL;
song_t *tmp;
char path[STRING_MAX];
......@@ -378,7 +392,7 @@ int main (void) {
} else {
/* (Optional) print list before shuffling */
if (DEBUG) {
printf("Before shuffling:\n");
printf("\nBefore shuffling:\n");
songs_print(head);
}
......@@ -386,7 +400,7 @@ int main (void) {
songs_shuffle(&head);
/* Finally print the shuffled list */
if (DEBUG) printf("After shuffling:\n");
if (DEBUG) printf("\nAfter shuffling:\n");
songs_print(head);
/* Release the allocated memory */
......@@ -401,7 +415,7 @@ int main (void) {
}
/* Notify the user about the termination of the program */
printf("The program will now be terminated...\n");
printf("\nThe program will now be terminated...\n");
return 0; /* Return with Success (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