forked from lipnitsk/mocp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlist_file.c
468 lines (386 loc) · 10 KB
/
playlist_file.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
/*
* MOC - music on console
* Copyright (C) 2004 - 2006 Damian Pietras <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <unistd.h>
#include <fcntl.h>
#include <sys/file.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#define DEBUG
#include "common.h"
#include "playlist.h"
#include "playlist_file.h"
#include "log.h"
#include "files.h"
#include "options.h"
#include "interface.h"
#include "decoder.h"
int is_plist_file (const char *name)
{
const char *ext = ext_pos (name);
if (ext && (!strcasecmp(ext, "m3u") || !strcasecmp(ext, "pls")))
return 1;
return 0;
}
static void make_path (char *buf, const int buf_size,
const char *cwd, char *path)
{
if (file_type(path) == F_URL) {
strncpy (buf, path, buf_size);
buf[buf_size-1] = 0;
return;
}
if (path[0] != '/')
strcpy (buf, cwd);
else
strcpy (buf, "/");
resolve_path (buf, buf_size, path);
}
/* Strip white chars from the end of a string. */
static void strip_string (char *str)
{
char *c = str;
char *last_non_white = str;
while (*c) {
if (!isblank(*c))
last_non_white = c;
c++;
}
if (c > last_non_white)
*(last_non_white + 1) = 0;
}
/* Load M3U file into plist. Return the number of items read. */
static int plist_load_m3u (struct plist *plist, const char *fname,
const char *cwd, const int load_serial)
{
FILE *file;
char *line = NULL;
int last_added = -1;
int after_extinf = 0;
int added = 0;
struct flock read_lock = {.l_type = F_RDLCK, .l_whence = SEEK_SET};
file = fopen (fname, "r");
if (!file) {
error_errno ("Can't open playlist file", errno);
return 0;
}
/* Lock gets released by fclose(). */
if (fcntl (fileno (file), F_SETLKW, &read_lock) == -1)
log_errno ("Can't lock the playlist file", errno);
while ((line = read_line (file))) {
if (!strncmp (line, "#EXTINF:", sizeof("#EXTINF:") - 1)) {
char *comma, *num_err;
char time_text[10] = "";
int time_sec;
if (after_extinf) {
error ("Broken M3U file: double #EXTINF!");
plist_delete (plist, last_added);
goto err;
}
/* Find the comma */
comma = strchr (line + (sizeof("#EXTINF:") - 1), ',');
if (!comma) {
error ("Broken M3U file: no comma in #EXTINF!");
goto err;
}
/* Get the time string */
time_text[sizeof(time_text) - 1] = 0;
strncpy (time_text, line + sizeof("#EXTINF:") - 1,
MIN(comma - line - (sizeof("#EXTINF:") - 1),
sizeof(time_text)));
if (time_text[sizeof(time_text) - 1]) {
error ("Broken M3U file: wrong time!");
goto err;
}
/* Extract the time. */
time_sec = strtol (time_text, &num_err, 10);
if (*num_err) {
error ("Broken M3U file: time is not a number!");
goto err;
}
after_extinf = 1;
last_added = plist_add (plist, NULL);
plist_set_title_tags (plist, last_added, comma + 1);
if (*time_text)
plist_set_item_time (plist, last_added, time_sec);
}
else if (line[0] != '#') {
char path[2 * PATH_MAX];
strip_string (line);
if (strlen (line) <= PATH_MAX) {
make_path (path, sizeof(path), cwd, line);
if (plist_find_fname (plist, path) == -1) {
if (after_extinf)
plist_set_file (plist, last_added, path);
else
plist_add (plist, path);
added += 1;
}
else if (after_extinf)
plist_delete (plist, last_added);
}
else if (after_extinf)
plist_delete (plist, last_added);
after_extinf = 0;
}
else if (load_serial &&
!strncmp (line, "#MOCSERIAL: ", sizeof("#MOCSERIAL: ") - 1)) {
char *serial_str = line + sizeof("#MOCSERIAL: ") - 1;
if (serial_str[0]) {
char *err;
long serial;
serial = strtol (serial_str, &err, 0);
if (!*err) {
plist_set_serial (plist, serial);
logit ("Got MOCSERIAL tag with serial %ld", serial);
}
}
}
free (line);
}
err:
free (line);
fclose (file);
return added;
}
/* Return 1 if the line contains only blank characters, 0 otherwise. */
static int is_blank_line (const char *l)
{
while (*l && isblank(*l))
l++;
if (*l)
return 0;
return 1;
}
/* Read a value from the given section from .INI file. File should be opened
* and seeking will be performed on it. Return the malloc()ed value or NULL
* if not present or error occurred. */
static char *read_ini_value (FILE *file, const char *section, const char *key)
{
char *line = NULL;
int in_section = 0;
char *value = NULL;
int key_len;
if (fseek(file, 0, SEEK_SET)) {
error_errno ("File fseek() error", errno);
return NULL;
}
key_len = strlen (key);
while ((line = read_line(file))) {
if (line[0] == '[') {
if (in_section) {
/* we are outside of the interesting section */
free (line);
break;
}
else {
char *close = strchr (line, ']');
if (!close) {
error ("Parse error in the INI file");
free (line);
break;
}
if (!strncasecmp(line + 1, section,
close - line - 1))
in_section = 1;
}
}
else if (in_section && line[0] != '#' && !is_blank_line(line)) {
char *t, *t2;
t2 = t = strchr (line, '=');
if (!t) {
error ("Parse error in the INI file");
free (line);
break;
}
/* go back to the last char in the name */
while (t2 >= t && (isblank(*t2) || *t2 == '='))
t2--;
if (t2 == t) {
error ("Parse error in the INI file");
free (line);
break;
}
if (!strncasecmp(line, key,
MAX(t2 - line + 1, key_len))) {
value = t + 1;
while (isblank(value[0]))
value++;
if (value[0] == '"') {
char *q = strchr (value + 1, '"');
if (!q) {
error ("Parse error in the INI file");
free (line);
break;
}
*q = 0;
}
value = xstrdup (value);
free (line);
break;
}
}
free (line);
}
return value;
}
/* Load PLS file into plist. Return the number of items read. */
static int plist_load_pls (struct plist *plist, const char *fname,
const char *cwd)
{
FILE *file;
char *e, *line = NULL;
long i, nitems, added = 0;
file = fopen (fname, "r");
if (!file) {
error_errno ("Can't open playlist file", errno);
return 0;
}
line = read_ini_value (file, "playlist", "NumberOfEntries");
if (!line) {
/* Assume that it is a pls file version 1 - plist_load_m3u()
* should handle it like an m3u file without the m3u extensions. */
fclose (file);
return plist_load_m3u (plist, fname, cwd, 0);
}
nitems = strtol (line, &e, 10);
if (*e) {
error ("Broken PLS file");
goto err;
}
for (i = 1; i <= nitems; i++) {
int time, last_added;
char *pls_file, *pls_title, *pls_length;
char key[16], path[2 * PATH_MAX];
sprintf (key, "File%ld", i);
pls_file = read_ini_value (file, "playlist", key);
if (!pls_file) {
error ("Broken PLS file");
goto err;
}
sprintf (key, "Title%ld", i);
pls_title = read_ini_value (file, "playlist", key);
sprintf (key, "Length%ld", i);
pls_length = read_ini_value (file, "playlist", key);
if (pls_length) {
time = strtol (pls_length, &e, 10);
if (*e)
time = -1;
}
else
time = -1;
if (strlen (pls_file) <= PATH_MAX) {
make_path (path, sizeof(path), cwd, pls_file);
if (plist_find_fname (plist, path) == -1) {
last_added = plist_add (plist, path);
if (pls_title && pls_title[0])
plist_set_title_tags (plist, last_added, pls_title);
if (time > 0) {
plist->items[last_added].tags = tags_new ();
plist->items[last_added].tags->time = time;
plist->items[last_added].tags->filled |= TAGS_TIME;
}
}
}
free (pls_file);
if (pls_title)
free (pls_title);
if (pls_length)
free (pls_length);
added += 1;
}
err:
free (line);
fclose (file);
return added;
}
/* Load a playlist into plist. Return the number of items on the list. */
/* The playlist may have deleted items. */
int plist_load (struct plist *plist, const char *fname, const char *cwd,
const int load_serial)
{
int num, read_tags;
const char *ext;
read_tags = options_get_bool ("ReadTags");
ext = ext_pos (fname);
if (ext && !strcasecmp(ext, "pls"))
num = plist_load_pls (plist, fname, cwd);
else
num = plist_load_m3u (plist, fname, cwd, load_serial);
if (read_tags)
switch_titles_tags (plist);
else
switch_titles_file (plist);
return num;
}
/* Save the playlist into the file in m3u format. If save_serial is not 0,
* the playlist serial is saved in a comment. */
int plist_save (struct plist *plist, const char *fname, const int save_serial)
{
FILE *file = NULL;
int i, ret, result = 0;
struct flock write_lock = {.l_type = F_WRLCK, .l_whence = SEEK_SET};
debug ("Saving playlist to '%s'", fname);
file = fopen (fname, "w");
if (!file) {
error_errno ("Can't save playlist", errno);
return 0;
}
/* Lock gets released by fclose(). */
if (fcntl (fileno (file), F_SETLKW, &write_lock) == -1)
log_errno ("Can't lock the playlist file", errno);
if (fprintf (file, "#EXTM3U\r\n") < 0) {
error_errno ("Error writing playlist", errno);
goto err;
}
if (save_serial && fprintf (file, "#MOCSERIAL: %d\r\n",
plist_get_serial (plist)) < 0) {
error_errno ("Error writing playlist", errno);
goto err;
}
for (i = 0; i < plist->num; i++) {
if (!plist_deleted (plist, i)) {
/* EXTM3U */
if (plist->items[i].tags)
ret = fprintf (file, "#EXTINF:%d,%s\r\n",
plist->items[i].tags->time,
plist->items[i].title_tags ?
plist->items[i].title_tags
: plist->items[i].title_file);
else
ret = fprintf (file, "#EXTINF:%d,%s\r\n", 0,
plist->items[i].title_file);
/* file */
if (ret >= 0)
ret = fprintf (file, "%s\r\n", plist->items[i].file);
if (ret < 0) {
error_errno ("Error writing playlist", errno);
goto err;
}
}
}
ret = fclose (file);
file = NULL;
if (ret)
error_errno ("Error writing playlist", errno);
else
result = 1;
err:
if (file)
fclose (file);
return result;
}