-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlist.c
120 lines (106 loc) · 2.76 KB
/
playlist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "playlist.h"
#include "DLList.h"
Playlist * createPlaylist(char name[30], int size){
Playlist * playlist = (Playlist *) malloc(sizeof(Playlist));
strncpy (playlist->name, name, 28);
playlist->size = size;
if(!size){
size = 0;
}
playlist->album = createDLList();
playlist->sort = 0;
return playlist;
}
Song * findSong(char name[30], Playlist * playlist){
int index = searchDLList(playlist->album, name);
Song * song = NULL;
song = getDLList(playlist->album, index);
return song;
}
Playlist * freePlaylist(Playlist * playlist){
playlist->size = 0;
freeDLList(playlist->album);
playlist->album = NULL;
free(playlist);
return NULL;
}
void insertSong(Song * s, Playlist * playlist){
int index;
DNode * node = createDNode(s);
playlist->size += 1;
index = findIndexDLList(playlist->album, node, playlist->sort);
insertDLList(playlist->album, index, node);
}
void listenPlaylist(Playlist * playlist){
if (playlist->size == 0){
printf("Insira musicas para poder ouvi-las!");
return;
}
int i;
for (i = 0; i < playlist->size; i++){
printf("Playlist \"%s\" ", playlist->name);
if (playlist->size == 1) printf("com 1 musica disponivel\n\n");
else printf("com %d musicas disponiveis\n\n", playlist->size);
playSong(getDLList(playlist->album, i));
sleep(5);
system("clear");
}
printf("Obrigado por ouvir a playlist \"%s\"!\n", playlist->name);
}
void printSort(Playlist * playlist){
switch (playlist->sort) {
case 0:
printf("Data de adicao");
break;
case 1:
printf("Data de lancamento");
break;
case 2:
printf("Alfabetica - Nome (Crescente)");
break;
case 3:
printf("Alfabetica - Nome (Decrescente)");
break;
case 4:
printf("Alfabetica - Artista/Banda");
break;
case 5:
printf("Alfabetica - Genero");
break;
default:
return;
}
return;
}
Playlist * readPlaylist(FILE * f){
int i;
Playlist * playlist = (Playlist *) malloc(sizeof(Playlist));
fread(&(playlist->name), sizeof(char), 30, f);
fread(&(playlist->size), sizeof(int), 1, f);
fread(&(playlist->sort), sizeof(char), 1, f);
playlist->album = readDLList(f);
return playlist;
}
void removeSong(int index, Playlist * playlist){
if (index >= playlist->size || index < 0) return;
playlist->size -= 1;
removeDLList(playlist->album, index);
}
void sortPlaylist(Playlist * playlist, char sort){
DLinkedList * list;
playlist->sort = sort;
list = sortDLList(playlist->album, sort);
if(list != NULL){
playlist->album = list;
}
}
void updateName(char * name, Playlist * playlist){
strcpy(playlist->name, name);
}
void writePlaylist(FILE * f, Playlist * playlist){
int i;
fwrite(&(playlist->name), sizeof(char), 30, f);
fwrite(&(playlist->size), sizeof(int), 1, f);
fwrite(&(playlist->sort), sizeof(char), 1, f);
writeDLList(f, playlist->album);
}