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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
|
/***************************************************************************
* Copyright (C) 2008-2009 by Andrzej Rybczak *
* electricityispower@gmail.com *
* *
* 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. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cstring>
#include <iomanip>
#include <sstream>
#include <stdexcept>
#include "charset.h"
#include "conv.h"
#include "error.h"
#include "song.h"
namespace
{
unsigned calc_hash(const char *p)
{
unsigned hash = 5381;
while (*p)
hash = (hash << 5) + hash + *p++;
return hash;
}
}
MPD::Song::Song(mpd_song *s, bool copy_ptr) : itsSong(s),
itsFile(0),
itsTags(0),
itsSlash(std::string::npos),
itsHash(0),
copyPtr(copy_ptr),
isLocalised(0)
{
if (itsSong)
SetHashAndSlash();
}
MPD::Song::Song(const Song &s) : itsSong(s.copyPtr ? s.itsSong : mpd_song_dup(s.itsSong)),
itsFile(s.itsFile ? strdup(s.itsFile) : 0),
itsTags(s.itsTags ? new TagMap(*s.itsTags) : 0),
itsNewName(s.itsNewName),
itsSlash(s.itsSlash),
itsHash(s.itsHash),
copyPtr(s.copyPtr),
isLocalised(s.isLocalised)
{
}
MPD::Song::~Song()
{
if (itsSong)
mpd_song_free(itsSong);
delete [] itsFile;
delete itsTags;
}
std::string MPD::Song::GetLength(unsigned pos) const
{
if (pos > 0)
return "";
unsigned len = mpd_song_get_duration(itsSong);
return !len ? "-:--" : ShowTime(len);
}
void MPD::Song::Localize()
{
# ifdef HAVE_ICONV_H
if (isLocalised)
return;
const char *tag, *conv_tag;
conv_tag = tag = mpd_song_get_uri(itsSong);
utf_to_locale(conv_tag, 0);
if (tag != conv_tag) // file has been converted
{
itsFile = conv_tag;
SetHashAndSlash();
}
for (unsigned t = MPD_TAG_ARTIST; t <= MPD_TAG_DISC; ++t)
{
unsigned pos = 0;
for (; (tag = mpd_song_get_tag(itsSong, mpd_tag_type(t), pos)); ++pos)
{
conv_tag = tag;
utf_to_locale(conv_tag, 0);
if (tag != conv_tag) // tag has been converted
{
SetTag(mpd_tag_type(t), pos, conv_tag);
delete [] conv_tag;
}
}
}
isLocalised = 1;
# endif // HAVE_ICONV_H
}
void MPD::Song::Clear()
{
if (itsSong)
mpd_song_free(itsSong);
itsSong = 0;
delete [] itsFile;
itsFile = 0;
delete itsTags;
itsTags = 0;
itsNewName.clear();
itsSlash = std::string::npos;
itsHash = 0;
isLocalised = 0;
copyPtr = 0;
}
bool MPD::Song::Empty() const
{
return !itsSong;
}
bool MPD::Song::isFromDB() const
{
return (MyFilename()[0] != '/') || itsSlash == std::string::npos;
}
bool MPD::Song::isStream() const
{
return !strncmp(MyFilename(), "http://", 7);
}
std::string MPD::Song::GetFile(unsigned pos) const
{
return pos > 0 ? "" : MyFilename();
}
std::string MPD::Song::GetName(unsigned pos) const
{
if (pos > 0)
return "";
std::string name = GetTag(MPD_TAG_NAME, 0);
if (!name.empty())
return name;
else if (itsSlash != std::string::npos)
return MyFilename()+itsSlash+1;
else
return MyFilename();
}
std::string MPD::Song::GetDirectory(unsigned pos) const
{
if (pos > 0 || isStream())
return "";
else if (itsSlash == std::string::npos)
return "/";
else
return std::string(MyFilename(), itsSlash);
}
std::string MPD::Song::GetArtist(unsigned pos) const
{
return GetTag(MPD_TAG_ARTIST, pos);
}
std::string MPD::Song::GetTitle(unsigned pos) const
{
return GetTag(MPD_TAG_TITLE, pos);
}
std::string MPD::Song::GetAlbum(unsigned pos) const
{
return GetTag(MPD_TAG_ALBUM, pos);
}
std::string MPD::Song::GetAlbumArtist(unsigned pos) const
{
return GetTag(MPD_TAG_ALBUM_ARTIST, pos);
}
std::string MPD::Song::GetTrack(unsigned pos) const
{
std::string track = GetTag(MPD_TAG_TRACK, pos);
return (track.length() == 1 && track[0] != '0') || (track.length() > 3 && track[1] == '/') ? "0"+track : track;
}
std::string MPD::Song::GetTrackNumber(unsigned pos) const
{
std::string track = GetTag(MPD_TAG_TRACK, pos);
size_t slash = track.find('/');
if (slash != std::string::npos)
track.resize(slash);
return track.length() == 1 && track[0] != '0' ? "0"+track : track;
}
std::string MPD::Song::GetDate(unsigned pos) const
{
return GetTag(MPD_TAG_DATE, pos);
}
std::string MPD::Song::GetGenre(unsigned pos) const
{
return GetTag(MPD_TAG_GENRE, pos);
}
std::string MPD::Song::GetComposer(unsigned pos) const
{
return GetTag(MPD_TAG_COMPOSER, pos);
}
std::string MPD::Song::GetPerformer(unsigned pos) const
{
return GetTag(MPD_TAG_PERFORMER, pos);
}
std::string MPD::Song::GetDisc(unsigned pos) const
{
return GetTag(MPD_TAG_DISC, pos);
}
std::string MPD::Song::GetComment(unsigned pos) const
{
return GetTag(MPD_TAG_COMMENT, pos);
}
std::string MPD::Song::GetTags(GetFunction f) const
{
unsigned pos = 0;
std::string result;
for (std::string tag; !(tag = (this->*f)(pos)).empty(); ++pos)
{
if (!result.empty())
result += ", ";
result += tag;
}
return result;
}
void MPD::Song::SetArtist(const std::string &str, unsigned pos)
{
SetTag(MPD_TAG_ARTIST, pos, str);
}
void MPD::Song::SetTitle(const std::string &str, unsigned pos)
{
SetTag(MPD_TAG_TITLE, pos, str);
}
void MPD::Song::SetAlbum(const std::string &str, unsigned pos)
{
SetTag(MPD_TAG_ALBUM, pos, str);
}
void MPD::Song::SetAlbumArtist(const std::string &str, unsigned pos)
{
SetTag(MPD_TAG_ALBUM_ARTIST, pos, str);
}
void MPD::Song::SetTrack(const std::string &str, unsigned pos)
{
SetTag(MPD_TAG_TRACK, pos, str);
}
void MPD::Song::SetTrack(unsigned track, unsigned pos)
{
SetTag(MPD_TAG_TRACK, pos, IntoStr(track));
}
void MPD::Song::SetDate(const std::string &str, unsigned pos)
{
SetTag(MPD_TAG_DATE, pos, str);
}
void MPD::Song::SetDate(unsigned year, unsigned pos)
{
SetTag(MPD_TAG_DATE, pos, IntoStr(year));
}
void MPD::Song::SetGenre(const std::string &str, unsigned pos)
{
SetTag(MPD_TAG_GENRE, pos, str);
}
void MPD::Song::SetComposer(const std::string &str, unsigned pos)
{
SetTag(MPD_TAG_COMPOSER, pos, str);
}
void MPD::Song::SetPerformer(const std::string &str, unsigned pos)
{
SetTag(MPD_TAG_PERFORMER, pos, str);
}
void MPD::Song::SetDisc(const std::string &str, unsigned pos)
{
SetTag(MPD_TAG_DISC, pos, str);
}
void MPD::Song::SetComment(const std::string &str, unsigned pos)
{
SetTag(MPD_TAG_COMMENT, pos, str);
}
void MPD::Song::SetPosition(unsigned pos)
{
mpd_song_set_pos(itsSong, pos);
}
void MPD::Song::SetTags(SetFunction f, const std::string &value)
{
unsigned pos = 0;
// tag editor can save multiple instances of performer and composer
// tag, so we need to split them and allow it to read them separately.
if (f == &Song::SetComposer || f == &Song::SetPerformer)
{
for (size_t i = 0; i != std::string::npos; i = value.find(",", i))
{
if (i)
++i;
while (value[i] == ' ')
++i;
size_t j = value.find(",", i);
(this->*f)(value.substr(i, j-i), pos++);
}
}
else
(this->*f)(value, pos++);
// there should be empty tag at the end since if we are
// reading them, original tag from mpd_song at the position
// after the last one locally set can be non-empty and in this
// case GetTags() would read it, which is undesirable.
(this->*f)("", pos);
}
std::string MPD::Song::ParseFormat(std::string::const_iterator &it, const char *escape_chars) const
{
std::string result;
bool has_some_tags = 0;
MPD::Song::GetFunction get = 0;
while (*++it != '}')
{
while (*it == '{')
{
std::string tags = ParseFormat(it, escape_chars);
if (!tags.empty())
{
has_some_tags = 1;
result += tags;
}
}
if (*it == '}')
break;
if (*it == '%')
{
switch (*++it)
{
case 'l':
get = &MPD::Song::GetLength;
break;
case 'D':
get = &MPD::Song::GetDirectory;
break;
case 'f':
get = &MPD::Song::GetName;
break;
case 'a':
get = &MPD::Song::GetArtist;
break;
case 'A':
get = &MPD::Song::GetAlbumArtist;
break;
case 'b':
get = &MPD::Song::GetAlbum;
break;
case 'y':
get = &MPD::Song::GetDate;
break;
case 'n':
get = &MPD::Song::GetTrackNumber;
break;
case 'N':
get = &MPD::Song::GetTrack;
break;
case 'g':
get = &MPD::Song::GetGenre;
break;
case 'c':
get = &MPD::Song::GetComposer;
break;
case 'p':
get = &MPD::Song::GetPerformer;
break;
case 'd':
get = &MPD::Song::GetDisc;
break;
case 'C':
get = &MPD::Song::GetComment;
break;
case 't':
get = &MPD::Song::GetTitle;
break;
case '%':
result += *it; // no break here
default:
get = 0;
break;
}
if (get)
{
std::string tag = GetTags(get);
if (escape_chars) // prepend format escape character to all given chars to escape
for (const char *ch = escape_chars; *ch; ++ch)
for (size_t i = 0; (i = tag.find(*ch, i)) != std::string::npos; i += 2)
tag.replace(i, 1, std::string(1, FormatEscapeCharacter) + *ch);
if (!tag.empty() && (get != &MPD::Song::GetLength || GetTotalLength()))
{
has_some_tags = 1;
result += tag;
}
else
break;
}
}
else
result += *it;
}
int brace_counter = 0;
if (*it != '}' || !has_some_tags)
{
for (; *it != '}' || brace_counter; ++it)
{
if (*it == '{')
++brace_counter;
else if (*it == '}')
--brace_counter;
}
if (*++it == '|')
return ParseFormat(++it, escape_chars);
else
return "";
}
else
{
if (*(it+1) == '|')
{
for (; *it != '}' || *(it+1) == '|' || brace_counter; ++it)
{
if (*it == '{')
++brace_counter;
else if (*it == '}')
--brace_counter;
}
}
++it;
return result;
}
}
std::string MPD::Song::toString(const std::string &format, const char *escape_chars) const
{
std::string::const_iterator it = format.begin();
return ParseFormat(it, escape_chars);
}
MPD::Song &MPD::Song::operator=(const MPD::Song &s)
{
if (this == &s)
return *this;
if (itsSong)
mpd_song_free(itsSong);
delete [] itsFile;
delete itsTags;
itsSong = s.copyPtr ? s.itsSong : (s.itsSong ? mpd_song_dup(s.itsSong) : 0);
itsFile = s.itsFile ? strdup(s.itsFile) : 0;
itsTags = s.itsTags ? new TagMap(*s.itsTags) : 0;
itsNewName = s.itsNewName;
itsSlash = s.itsSlash;
itsHash = s.itsHash;
copyPtr = s.copyPtr;
isLocalised = s.isLocalised;
return *this;
}
std::string MPD::Song::ShowTime(int length)
{
std::ostringstream ss;
int hours = length/3600;
length -= hours*3600;
int minutes = length/60;
length -= minutes*60;
int seconds = length;
if (hours > 0)
{
ss << hours << ":"
<< std::setw(2) << std::setfill('0') << minutes << ":"
<< std::setw(2) << std::setfill('0') << seconds;
}
else
{
ss << minutes << ":"
<< std::setw(2) << std::setfill('0') << seconds;
}
return ss.str();
}
void MPD::Song::ValidateFormat(const std::string &type, const std::string &s)
{
int braces = 0;
for (std::string::const_iterator it = s.begin(); it != s.end(); ++it)
{
if (*it == '{')
++braces;
else if (*it == '}')
--braces;
}
if (braces)
FatalError(type + ": number of opening and closing braces does not equal!");
}
void MPD::Song::SetHashAndSlash()
{
const char *filename = MyFilename();
if (!isStream())
{
const char *tmp = strrchr(filename, '/');
itsSlash = tmp ? tmp-filename : std::string::npos;
}
if (!itsFile)
itsHash = calc_hash(filename);
}
const char *MPD::Song::MyFilename() const
{
return itsFile ? itsFile : mpd_song_get_uri(itsSong);
}
void MPD::Song::SetTag(mpd_tag_type type, unsigned pos, const std::string &value)
{
if (!itsTags)
itsTags = new TagMap;
(*itsTags)[std::make_pair(type, pos)] = value;
}
std::string MPD::Song::GetTag(mpd_tag_type type, unsigned pos) const
{
if (itsTags)
{
TagMap::const_iterator it = itsTags->find(std::make_pair(type, pos));
if (it != itsTags->end())
return it->second;
}
const char *tag = mpd_song_get_tag(itsSong, type, pos);
return tag ? tag : "";
}
|