-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
171 lines (138 loc) · 3.72 KB
/
main.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <stdio.h>
#include "playlist.h"
#include <stdlib.h>
typedef struct mysongT {
song tune;
int played;
} mysong;
struct playlistType{
int first, last;
mysong * Q;
int size, next, mode;
};
int initSize = 10;
/* EFFECTS: allocates and initialises an empty playlist which will play in normal mode.*/
playlist initialisePlaylist() {
playlist p = malloc(sizeof(playlist));
p->first = -1;
p->last = -1;
p->Q = malloc(sizeof(mysong) * initSize);
p->size = initSize;
p->next = 0;
p->mode = 0;
return p;
}
//EFFECTS: copies array aux into array Q
void copy(mysong * aux, mysong * Q, int size) {
int i;
for (i=0; i<size; i++) {
Q[i] = aux[i];
}
}
// EFFECTS: checks whether arrray p->Q needs expanding
void check(playlist p) {
if (p->last == p->size - 1) {
mysong * aux = p->Q;
p->size = p->size * 2;
p->Q = malloc(sizeof(mysong) * p->size);
copy(aux, p->Q, p->size);
//free(aux);
}
}
/* EFFECTS: returns position of next song to be played */
int findnext(playlist p) {
if (p->next == p->last)
p->next = 0;
else
p->next++;
return p->next;
}
/* EFFECTS: adds s as the last element of p.*/
void addSong(playlist p, song s){
check (p);
mysong newsong;
newsong.tune = s;
newsong.played = 0;
p->last++;
p->Q[p->last] = newsong;
}
/* EFFECTS: returns the ID of the next song of p to be played.*/
int playSong(playlist p) {
int aux;
aux = p->next;
if (p->mode == 1) {
if (p->Q[p->next].played == 0) { //unplayed
p->Q[p->next].played = 1;
p->next = findnext(p);
return p->Q[aux].tune->id;
}
else { // already played
int r;
int i;
p->next = findnext(p);
while (i <= p->last) {
if (p->Q[p->next].played == 0) {
r = p->Q[p->next].tune->id;
p->Q[p->next].played = 1;
p->next = findnext(p);
break;
}
else {
r = -1;
p->next = findnext(p);
}
i++;
}
return r;
}
}
else { // p->mode == 0
p->Q[p->next].played = 1;
p->next = findnext(p);
return p->Q[aux].tune->id;
}
}
/* EFFECTS: set p to play from the ith song. I.e., after the call the ith song will be the next song to be played.*/
void playFrom(playlist p, int i){
if (i-1 <= p->last)
p->next = i - 1;
}
/* EFFECTS: if p is not empty, skips the next song which will not be played.*/
void skipSong(playlist p) {
if (p)
p->next = findnext(p);
}
/* EFFECTS: if mode is 1 (skip Mode) then set the playlist p to play only songs that were not played before.
* If mode is 0 (normal mode) resets p and sets it to play all songs.*/
void skipAllPlayedSongs(playlist p, int mode){
if (mode == 1) {
p->mode = 1;
}
else {
p->mode = 0;
int i = 0;
p->next = 0;
for (i=0; i<=p->last; i++)
p->Q[i].played = 0;
}
}
/* EFFECTS: resets p making the first song in the sequence to be played the next to be played.*/
void restart(playlist p){
p->next = 0;
}
/* EFFECTS: if i and j are smaller than or equal to the number of songs in p,
* the function swaps the ith song and the jth song in p.*/
void swapSongs(playlist p, int i, int j){
int a = i-1;
int b = j-1;
if (a <= p->last && b <= p->last) {
mysong aux;
aux = p->Q[a];
p->Q[a] = p->Q[b];
p->Q[b] = aux;
}
}
/* EFFECTS: deallocate all the memory required by p.*/
void freePlaylist(playlist p){
free(p);
}