-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.c
234 lines (216 loc) · 5.6 KB
/
library.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "library.h"
struct library *make_library()
{
struct library *library = calloc(1, sizeof(struct library));
library->album = NULL;
library->next = NULL;
return library;
}
struct library *add_to_library(struct library *data, char *filename)
{
struct tags *new_mp3 = make_tag();
new_mp3 = read_file_info(new_mp3, filename);
struct library *head = data;
if (data->album == NULL)
{
struct album *new_album = make_album();
strcpy(new_album->name, new_mp3->album);
new_album->data = new_mp3;
data->album = new_album;
}
else
{
while (data != NULL)
{
if (!strcasecmp(data->album->name, new_mp3->album))
{
add_to_album(data->album, new_mp3);
return head;
}
data = data->next;
}
struct album *new_album = make_album();
strcpy(new_album->name, new_mp3->album);
new_album->data = new_mp3;
data = make_library();
data->album = new_album;
struct library *temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = data;
}
return head;
}
struct library *remove_from_library(struct library *data, char *name)
{
struct library *head = data;
while (data)
{
struct album *tempalb = data->album;
while (data->album)
{
if (!strcasecmp(data->album->data->title, name))
{
data->album = data->album->next;
if (data->album == NULL)
{
data->album = data->next->album;
}
return head;
}
data->album = data->album->next;
}
data->album = tempalb;
data = data->next;
}
return head;
}
struct library *save_library(struct library *data)
{
struct library *copy = make_library();
int i = open("library.data", O_CREAT | O_TRUNC | O_WRONLY, 0644);
while (data)
{
while (data->album)
{
copy = add_to_library(copy, data->album->data->path);
write(i, data->album->data->path, sizeof(data->album->data->path));
data->album = data->album->next;
}
data = data->next;
}
close(i);
return copy;
}
struct library *read_library()
{
int i = open("library.data", O_RDONLY);
struct library *new_library = make_library();
char path[100];
while (read(i, path, sizeof(path)))
{
new_library = add_to_library(new_library, path);
}
close(i);
return new_library;
}
int get_library_len(struct library *data)
{
int i = 0;
while (data)
{
++i;
data = data->next;
}
return i;
}
struct library *get_nth_album(struct library *data, int n)
{
int i = 0;
while (data)
{
if (i == n)
{
return data;
}
++i;
data = data->next;
}
}
struct library *sort(struct library *data, int direction)
{
if (direction == 0)
{
struct library *current = data;
struct library *sorted = NULL;
while (current != NULL)
{
struct library *next = current->next;
sorted = sortedInsertAsc(sorted, current);
current = next;
}
return sorted;
}
else
{
struct library *current = data;
struct library *sorted = NULL;
while (current != NULL)
{
struct library *next = current->next;
sorted = sortedInsertDes(sorted, current);
current = next;
}
return sorted;
}
}
struct library *sortedInsertAsc(struct library *head, struct library *node)
{
if (head == NULL || strcasecmp(head->album->name, node->album->name) > 0)
{
node->next = head;
return node;
}
else
{
struct library *current = head;
while (current->next != NULL && strcasecmp(current->next->album->name, node->album->name) < 0)
{
current = current->next;
}
node->next = current->next;
current->next = node;
}
return head;
}
struct library *sortedInsertDes(struct library *head, struct library *node)
{
if (head == NULL || strcasecmp(head->album->name, node->album->name) < 0)
{
node->next = head;
return node;
}
else
{
struct library *current = head;
while (current->next != NULL && strcasecmp(current->next->album->name, node->album->name) > 0)
{
current = current->next;
}
node->next = current->next;
current->next = node;
}
return head;
}
struct library *sort_album(struct library *data, int direction, int type)
{
struct album *current = data->album;
struct album *sorted = NULL;
while (current != NULL)
{
struct album *next = current->next;
if (direction == 0)
{
if (type == 0)
sorted = sorted_insert_name_asc(sorted, current);
if (type == 1)
sorted = sorted_insert_artist_asc(sorted, current);
if (type == 2)
sorted = sorted_insert_duration_asc(sorted, current);
}
else
{
if (type == 0)
sorted = sorted_insert_name_des(sorted, current);
if (type == 1)
sorted = sorted_insert_artist_des(sorted, current);
if (type == 2)
sorted = sorted_insert_duration_des(sorted, current);
}
current = next;
}
data->album = sorted;
return data;
}