forked from ryanflannery/vitunes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlist.c
514 lines (425 loc) · 13.6 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
* Copyright (c) 2010, 2011, 2012 Ryan Flannery <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "playlist.h"
int history_size = DEFAULT_HISTORY_SIZE;
void
playlist_increase_capacity(playlist *p)
{
meta_info **new_files;
size_t nbytes;
p->capacity += PLAYLIST_CHUNK_SIZE;
nbytes = p->capacity * sizeof(meta_info*);
if ((new_files = realloc(p->files, nbytes)) == NULL)
err(1, "%s: failed to realloc(3) files", __FUNCTION__);
p->files = new_files;
}
/*
* Allocate a new playlist and return a pointer to it. The resulting
* structure must be free(2)'d using playlist_free().
*/
playlist *
playlist_new(void)
{
playlist *p;
if ((p = malloc(sizeof(playlist))) == NULL)
err(1, "playlist_new: failed to allocate playlist");
if ((p->files = calloc(PLAYLIST_CHUNK_SIZE, sizeof(meta_info*))) == NULL)
err(1, "playlist_new: failed to allocate files");
p->capacity = PLAYLIST_CHUNK_SIZE;
p->filename = NULL;
p->name = NULL;
p->nfiles = 0;
p->history = playlist_history_new();
p->hist_present = -1;
p->needs_saving = false;
return p;
}
/* Free all of the memory consumed by a given playlist. */
void
playlist_free(playlist *p)
{
if (p->filename != NULL) free(p->filename);
if (p->name != NULL) free(p->name);
if (p->files != NULL) free(p->files);
playlist_history_free(p);
free(p);
}
/*
* Duplicate an existing playlist, returning a pointer to the newly allocated
* playlist. The filename and name of the new playlist must be specified.
*/
playlist *
playlist_dup(const playlist *original, const char *filename,
const char *name)
{
playlist *newplist;
int i;
/* create new playlist and copy simple members */
newplist = playlist_new();
newplist->nfiles = original->nfiles;
newplist->capacity = original->nfiles;
if (name != NULL) {
if ((newplist->name = strdup(name)) == NULL)
err(1, "playlist_dup: strdup name failed");
}
if (filename != NULL) {
if ((newplist->filename = strdup(filename)) == NULL)
err(1, "playlist_dup: strdup filename failed");
}
/* copy all of the files */
newplist->files = calloc(original->nfiles, sizeof(meta_info*));
if (newplist->files == NULL)
err(1, "playlist_dup: failed to allocate files");
for (i = 0; i < original->nfiles; i++)
newplist->files[i] = original->files[i];
return newplist;
}
/*
* Add files to a playlist at the index specified by start. Note that if
* start is the length of the files array the files are appended to the end.
*/
void
playlist_files_add(playlist *p, meta_info **f, int start, int size, bool record)
{
int i;
if (start < 0 || start > p->nfiles)
errx(1, "playlist_file_add: index %d out of range", start);
while (p->capacity <= p->nfiles + size)
playlist_increase_capacity(p);
/* push everything after start back size places */
for (i = p->nfiles + size; i > start; i--)
p->files[i] = p->files[i - size];
/* add the files */
for (i = 0; i < size; i++)
p->files[start + i] = f[i];
p->nfiles += size;
/* update the history for this playlist */
if (record) {
playlist_changeset *changes = changeset_create(CHANGE_ADD, size,
f, start);
playlist_history_push(p, changes);
p->needs_saving = true;
}
}
/* Append a file to the end of a playlist */
void
playlist_files_append(playlist *p, meta_info **f, int size, bool record)
{
return playlist_files_add(p, f, p->nfiles, size, record);
}
/* Remove a file at a given index from a playlist. */
void
playlist_files_remove(playlist *p, int start, int size, bool record)
{
playlist_changeset *changes;
int i;
if (start < 0 || start >= p->nfiles)
errx(1, "playlist_remove_file: index %d out of range", start);
if (record) {
changes = changeset_create(CHANGE_REMOVE, size, &(p->files[start]), start);
playlist_history_push(p, changes);
p->needs_saving = true;
}
for (i = start; i < p->nfiles; i++)
p->files[i] = p->files[i + size];
p->nfiles -= size;
}
/* Replaces the file at a given index in a playlist with a new file */
void
playlist_file_replace(playlist *p, int index, meta_info *newEntry)
{
if (index < 0 || index >= p->nfiles)
errx(1, "playlist_file_replace: index %d out of range", index);
p->files[index] = newEntry;
}
/* Used with bsearch to find playlist entry by filename. */
static int cmp_fn_mi(const void *ai, const void *bi)
{
const char *a = (const char *) ai;
const meta_info **b2 = (const meta_info **) bi;
const meta_info *b = (const meta_info *) *b2;
return strcmp(a, b->filename);
}
/*
* Loads a playlist from the provided filename. The files within the playlist
* are compared against the given meta-information-database to see if they
* exist there. If they do, the corresponding entry in the playlist structure
* built is simply a pointer to the existing entry. Otherwise, that file's
* meta information is set to NULL and the file is copied (allocated) in the
* playlist structure.
*
* A newly allocated playlist is returned.
*/
playlist *
playlist_load(const char *filename, meta_info **db, int ndb)
{
meta_info *mi, **mit;
FILE *fin;
char *period;
char entry[PATH_MAX + 1];
/* open file */
if ((fin = fopen(filename, "r")) == NULL)
err(1, "playlist_load: failed to open playlist '%s'", filename);
/* create playlist and setup */
playlist *p = playlist_new();
p->filename = strdup(filename);
p->name = strdup(basename(p->filename));
if (p->filename == NULL || p->name == NULL)
err(1, "playlist_load: failed to allocate info for playlist '%s'", filename);
/* hack to remove '.playlist' from name */
period = strrchr(p->name, '.');
*period = '\0';
/* read each line from the file and copy into playlist object */
while (fgets(entry, PATH_MAX, fin) != NULL) {
/* sanitize */
entry[strcspn(entry, "\n")] = '\0';
/* check if file exists in the meta info. db */
mit = bsearch(entry, db, ndb, sizeof(meta_info *), cmp_fn_mi);
if (mit != NULL) { /* file DOES exist in DB */
mi = *mit;
playlist_files_append(p, &mi, 1, false);
} else { /* file does NOT exist in DB */
/* create empty meta-info object with just the file name */
mi = mi_new();
mi->filename = strdup(entry);
if (mi->filename == NULL)
err(1, "playlist_load: failed to strdup filename");
/* add new record to the db and link it to the playlist */
playlist_files_append(p, &mi, 1, false);
warnx("playlist \"%s\", file \"%s\" is NOT in media database (added for now)",
p->name, entry);
}
}
fclose(fin);
return p;
}
/*
* Save a playlist to file. The filename used is whatever is in the
* playlist.
*/
void
playlist_save(const playlist *p)
{
FILE *fout;
int i;
if ((fout = fopen(p->filename, "w")) == NULL)
err(1, "playlist_save: failed to open playlist \"%s\"", p->filename);
/* write each song to file */
for (i = 0; i < p->nfiles; i++) {
if (fprintf(fout, "%s\n", p->files[i]->filename) == -1)
err(1, "playlist_save: failed to record playlist \"%s\"", p->filename);
}
fclose(fout);
}
/*
* Deletes playlist from disk and destroy's the object, free()'ing all
* memory.
*/
void
playlist_delete(playlist *p)
{
/* delete file if the playlist is stored in a file */
if (p->filename != NULL && unlink(p->filename) != 0)
err(1, "playlist_delete: failed to delete playlist \"%s\"", p->filename);
/* destroy/free() all memory */
playlist_free(p);
}
/*
* Filter a playlist. After a query string is setup using the meta_info
* function "mi_query_set(..)" function, this function can be used to
* filter out all of the entried of a playlist that match/do-not-match
* that query string.
*
* The results are in the new playlist that is returned.
* If no query is set with mi_query_init(), NULL is returned.
*
* The 'm' parameter controls if records matching should be returned
* (m = true) or if records not matching should be returned (m=false)
*/
playlist *
playlist_filter(const playlist *p, bool m)
{
playlist *results;
int i;
if (!mi_query_isset())
return NULL;
results = playlist_new();
for (i = 0; i < p->nfiles; i++) {
if (mi_match(p->files[i])) {
if (m) playlist_files_append(results, &(p->files[i]), 1, false);
} else {
if (!m) playlist_files_append(results, &(p->files[i]), 1, false);
}
}
return results;
}
/*
* Builds an array of all files in the given directory with a '.playlist'
* extension, returning the number of such files found.
*
* Parameters:
* dirname C-string of directory containing playlist files
*
* filenames Pointer to an array of C-strings that will be allocated
* and built in this function. This array is where the
* filename of each playlist will be stored.
* Returns:
* The number of files with a '.playlist' extension that were found.
*
* Notes:
* All allocation of the filenames array is handled here. It is the
* responsibility of the caller to free()
* 1. each element of that array
* 2. the array itself.
*/
int
retrieve_playlist_filenames(const char *dirname, char ***fnames)
{
char *glob_pattern;
glob_t files;
int globbed;
# if defined(__linux) || defined(__FreeBSD__) || defined(__MACH__)
size_t fcount;
# else
int fcount;
# endif
/* build the search pattern */
if (asprintf(&glob_pattern, "%s/*.playlist", dirname) == -1)
errx(1, "failed in building glob pattern");
/* get the files */
globbed = glob(glob_pattern, 0, NULL, &files);
if (globbed != 0 && globbed != GLOB_NOMATCH && errno != 0)
err(1, "failed to glob playlists directory");
/* allocate & copy each of the filenames found into the filenames array */
if ((*fnames = calloc(files.gl_pathc, sizeof(char*))) == NULL)
err(1, "failed to allocate playlist filenames array");
for (fcount = 0; fcount < files.gl_pathc; fcount++) {
if (asprintf(&((*fnames)[fcount]), "%s", files.gl_pathv[fcount]) == -1)
errx(1, "failed to allocate filename for playlist");
}
/* cleanup */
globfree(&files);
free(glob_pattern);
return fcount;
}
playlist_changeset*
changeset_create(short type, size_t size, meta_info **files, int loc)
{
size_t i;
playlist_changeset *c;
if ((c = malloc(sizeof(playlist_changeset))) == NULL)
err(1, "%s: malloc(3) failed", __FUNCTION__);
if ((c->files = calloc(size, sizeof(meta_info*))) == NULL)
err(1, "%s: calloc(3) failed", __FUNCTION__);
c->type = type;
c->size = size;
c->location = loc;
for (i = 0; i < size; i++)
c->files[i] = files[i];
return c;
}
void
changeset_free(playlist_changeset *c)
{
free(c->files);
free(c);
}
playlist_changeset**
playlist_history_new(void)
{
playlist_changeset **h;
int i;
if ((h = calloc(history_size, sizeof(playlist_changeset*))) == NULL)
err(1, "%s: calloc(3) failed", __FUNCTION__);
for (i = 0; i < history_size; i++)
h[i] = NULL;
return h;
}
void
playlist_history_free_future(playlist *p)
{
int i;
for (i = p->hist_present + 1; i < history_size; i++) {
if (p->history[i] != NULL) {
changeset_free(p->history[i]);
p->history[i] = NULL;
}
}
}
void
playlist_history_free(playlist *p)
{
p->hist_present = 0;
playlist_history_free_future(p);
free(p->history);
}
void
playlist_history_push(playlist *p, playlist_changeset *c)
{
if (p->hist_present < history_size - 1)
playlist_history_free_future(p);
else {
int i;
for (i = 0; i < history_size - 1; i++)
p->history[i] = p->history[i + 1];
p->hist_present--;
}
p->hist_present++;
p->history[p->hist_present] = c;
}
/* returns 0 if successfull, 1 if there was no history to undo */
int
playlist_undo(playlist *p)
{
playlist_changeset *c;
if (p->hist_present == -1)
return 1;
c = p->history[p->hist_present];
switch (c->type) {
case CHANGE_ADD:
playlist_files_remove(p, c->location, c->size, false);
break;
case CHANGE_REMOVE:
playlist_files_add(p, c->files, c->location, c->size, false);
break;
default:
errx(1, "%s: invalid change type", __FUNCTION__);
}
p->hist_present--;
return 0;
}
/* returns 0 if successfull, 1 if there was no history to re-do */
int
playlist_redo(playlist *p)
{
playlist_changeset *c;
if (p->hist_present == history_size - 1
|| p->history[p->hist_present + 1] == NULL)
return 1;
c = p->history[p->hist_present + 1];
switch (c->type) {
case CHANGE_ADD:
playlist_files_add(p, c->files, c->location, c->size, false);
break;
case CHANGE_REMOVE:
playlist_files_remove(p, c->location, c->size, false);
break;
default:
errx(1, "%s: invalid change type", __FUNCTION__);
}
p->hist_present++;
return 0;
}