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
|
/*
* Copyright 2003-2018 The Music Player Daemon Project
* http://www.musicpd.org
*
* 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "SongFilter.hxx"
#include "db/LightSong.hxx"
#include "tag/ParseName.hxx"
#include "tag/Tag.hxx"
#include "util/CharUtil.hxx"
#include "util/ChronoUtil.hxx"
#include "util/ConstBuffer.hxx"
#include "util/RuntimeError.hxx"
#include "util/StringAPI.hxx"
#include "util/StringCompare.hxx"
#include "util/StringStrip.hxx"
#include "util/StringView.hxx"
#include "util/ASCII.hxx"
#include "util/TimeISO8601.hxx"
#include "util/UriUtil.hxx"
#include "lib/icu/CaseFold.hxx"
#include <exception>
#include <assert.h>
#include <stdlib.h>
#define LOCATE_TAG_FILE_KEY "file"
#define LOCATE_TAG_FILE_KEY_OLD "filename"
#define LOCATE_TAG_ANY_KEY "any"
/**
* Limit the search to files within the given directory.
*/
#define LOCATE_TAG_BASE_TYPE (TAG_NUM_OF_ITEM_TYPES + 1)
#define LOCATE_TAG_MODIFIED_SINCE (TAG_NUM_OF_ITEM_TYPES + 2)
#define LOCATE_TAG_FILE_TYPE TAG_NUM_OF_ITEM_TYPES+10
#define LOCATE_TAG_ANY_TYPE TAG_NUM_OF_ITEM_TYPES+20
/**
* @return #TAG_NUM_OF_ITEM_TYPES on error
*/
gcc_pure
static unsigned
locate_parse_type(const char *str) noexcept
{
if (StringEqualsCaseASCII(str, LOCATE_TAG_FILE_KEY) ||
StringEqualsCaseASCII(str, LOCATE_TAG_FILE_KEY_OLD))
return LOCATE_TAG_FILE_TYPE;
if (StringEqualsCaseASCII(str, LOCATE_TAG_ANY_KEY))
return LOCATE_TAG_ANY_TYPE;
if (strcmp(str, "base") == 0)
return LOCATE_TAG_BASE_TYPE;
if (strcmp(str, "modified-since") == 0)
return LOCATE_TAG_MODIFIED_SINCE;
return tag_name_parse_i(str);
}
bool
StringFilter::Match(const char *s) const noexcept
{
#if !CLANG_CHECK_VERSION(3,6)
/* disabled on clang due to -Wtautological-pointer-compare */
assert(s != nullptr);
#endif
if (fold_case) {
return fold_case.IsIn(s);
} else {
return StringIsEqual(s, value.c_str());
}
}
std::string
UriSongFilter::ToExpression() const noexcept
{
return std::string("(" LOCATE_TAG_FILE_KEY " ") + (negated ? "!=" : "==") + " \"" + filter.GetValue() + "\")";
}
bool
UriSongFilter::Match(const LightSong &song) const noexcept
{
return filter.Match(song.GetURI().c_str()) != negated;
}
std::string
BaseSongFilter::ToExpression() const noexcept
{
return "(base \"" + value + "\")";
}
bool
BaseSongFilter::Match(const LightSong &song) const noexcept
{
return uri_is_child_or_same(value.c_str(), song.GetURI().c_str());
}
std::string
TagSongFilter::ToExpression() const noexcept
{
const char *name = type == TAG_NUM_OF_ITEM_TYPES
? LOCATE_TAG_ANY_KEY
: tag_item_names[type];
return std::string("(") + name + " " + (negated ? "!=" : "==") + " \"" + filter.GetValue() + "\")";
}
bool
TagSongFilter::MatchNN(const TagItem &item) const noexcept
{
return (type == TAG_NUM_OF_ITEM_TYPES || item.type == type) &&
filter.Match(item.value);
}
bool
TagSongFilter::MatchNN(const Tag &tag) const noexcept
{
bool visited_types[TAG_NUM_OF_ITEM_TYPES];
std::fill_n(visited_types, size_t(TAG_NUM_OF_ITEM_TYPES), false);
for (const auto &i : tag) {
visited_types[i.type] = true;
if (MatchNN(i))
return true;
}
if (type < TAG_NUM_OF_ITEM_TYPES && !visited_types[type]) {
/* If the search critieron was not visited during the
sweep through the song's tag, it means this field
is absent from the tag or empty. Thus, if the
searched string is also empty
then it's a match as well and we should return
true. */
if (filter.empty())
return true;
if (type == TAG_ALBUM_ARTIST && visited_types[TAG_ARTIST]) {
/* if we're looking for "album artist", but
only "artist" exists, use that */
for (const auto &item : tag)
if (item.type == TAG_ARTIST &&
filter.Match(item.value))
return true;
}
}
return false;
}
bool
TagSongFilter::Match(const LightSong &song) const noexcept
{
return MatchNN(song.tag) != negated;
}
std::string
ModifiedSinceSongFilter::ToExpression() const noexcept
{
return std::string("(modified-since \"") + FormatISO8601(value).c_str() + "\")";
}
bool
ModifiedSinceSongFilter::Match(const LightSong &song) const noexcept
{
return song.mtime >= value;
}
ISongFilterPtr
AndSongFilter::Clone() const noexcept
{
auto result = std::make_unique<AndSongFilter>();
for (const auto &i : items)
result->items.emplace_back(i->Clone());
return result;
}
std::string
AndSongFilter::ToExpression() const noexcept
{
auto i = items.begin();
const auto end = items.end();
if (std::next(i) == end)
return (*i)->ToExpression();
std::string e("(");
e += (*i)->ToExpression();
for (++i; i != end; ++i) {
e += " AND ";
e += (*i)->ToExpression();
}
e.push_back(')');
return e;
}
bool
AndSongFilter::Match(const LightSong &song) const noexcept
{
for (const auto &i : items)
if (!i->Match(song))
return false;
return true;
}
SongFilter::SongFilter(TagType tag, const char *value, bool fold_case)
{
and_filter.AddItem(std::make_unique<TagSongFilter>(tag, value,
fold_case, false));
}
SongFilter::~SongFilter()
{
/* this destructor exists here just so it won't get inlined */
}
std::string
SongFilter::ToExpression() const noexcept
{
return and_filter.ToExpression();
}
static std::chrono::system_clock::time_point
ParseTimeStamp(const char *s)
{
assert(s != nullptr);
char *endptr;
unsigned long long value = strtoull(s, &endptr, 10);
if (*endptr == 0 && endptr > s)
/* it's an integral UNIX time stamp */
return std::chrono::system_clock::from_time_t((time_t)value);
/* try ISO 8601 */
return ParseISO8601(s);
}
static constexpr bool
IsTagNameChar(char ch) noexcept
{
return IsAlphaASCII(ch) || ch == '_' || ch == '-';
}
static const char *
FirstNonTagNameChar(const char *s) noexcept
{
while (IsTagNameChar(*s))
++s;
return s;
}
static auto
ExpectWord(const char *&s)
{
const char *begin = s;
const char *end = FirstNonTagNameChar(s);
if (end == s)
throw std::runtime_error("Word expected");
s = StripLeft(end);
return std::string(begin, end);
}
static auto
ExpectFilterType(const char *&s)
{
const auto name = ExpectWord(s);
const auto type = locate_parse_type(name.c_str());
if (type == TAG_NUM_OF_ITEM_TYPES)
throw FormatRuntimeError("Unknown filter type: %s",
name.c_str());
return type;
}
static constexpr bool
IsQuote(char ch) noexcept
{
return ch == '"' || ch == '\'';
}
static std::string
ExpectQuoted(const char *&s)
{
const char quote = *s++;
if (!IsQuote(quote))
throw std::runtime_error("Quoted string expected");
const char *begin = s;
const char *end = strchr(s, quote);
if (end == nullptr)
throw std::runtime_error("Closing quote not found");
s = StripLeft(end + 1);
return {begin, end};
}
ISongFilterPtr
SongFilter::ParseExpression(const char *&s, bool fold_case)
{
assert(*s == '(');
s = StripLeft(s + 1);
if (*s == '(') {
auto first = ParseExpression(s, fold_case);
if (*s == ')') {
++s;
return first;
}
if (ExpectWord(s) != "AND")
throw std::runtime_error("'AND' expected");
auto and_filter = std::make_unique<AndSongFilter>();
and_filter->AddItem(std::move(first));
while (true) {
and_filter->AddItem(ParseExpression(s, fold_case));
if (*s == ')') {
++s;
return and_filter;
}
if (ExpectWord(s) != "AND")
throw std::runtime_error("'AND' expected");
}
}
auto type = ExpectFilterType(s);
if (type == LOCATE_TAG_MODIFIED_SINCE) {
const auto value_s = ExpectQuoted(s);
if (*s != ')')
throw std::runtime_error("')' expected");
s = StripLeft(s + 1);
return std::make_unique<ModifiedSinceSongFilter>(ParseTimeStamp(value_s.c_str()));
} else if (type == LOCATE_TAG_BASE_TYPE) {
auto value = ExpectQuoted(s);
if (*s != ')')
throw std::runtime_error("')' expected");
s = StripLeft(s + 1);
return std::make_unique<BaseSongFilter>(std::move(value));
} else {
bool negated = false;
if (s[0] == '!' && s[1] == '=')
negated = true;
else if (s[0] != '=' || s[1] != '=')
throw std::runtime_error("'==' or '!=' expected");
s = StripLeft(s + 2);
auto value = ExpectQuoted(s);
if (*s != ')')
throw std::runtime_error("')' expected");
s = StripLeft(s + 1);
if (type == LOCATE_TAG_ANY_TYPE)
type = TAG_NUM_OF_ITEM_TYPES;
if (type == LOCATE_TAG_FILE_TYPE)
return std::make_unique<UriSongFilter>(std::move(value),
fold_case,
negated);
return std::make_unique<TagSongFilter>(TagType(type),
std::move(value),
fold_case, negated);
}
}
void
SongFilter::Parse(const char *tag_string, const char *value, bool fold_case)
{
unsigned tag = locate_parse_type(tag_string);
switch (tag) {
case TAG_NUM_OF_ITEM_TYPES:
throw std::runtime_error("Unknown filter type");
case LOCATE_TAG_BASE_TYPE:
if (!uri_safe_local(value))
throw std::runtime_error("Bad URI");
and_filter.AddItem(std::make_unique<BaseSongFilter>(value));
break;
case LOCATE_TAG_MODIFIED_SINCE:
and_filter.AddItem(std::make_unique<ModifiedSinceSongFilter>(ParseTimeStamp(value)));
break;
case LOCATE_TAG_FILE_TYPE:
and_filter.AddItem(std::make_unique<UriSongFilter>(value,
fold_case,
false));
break;
default:
if (tag == LOCATE_TAG_ANY_TYPE)
tag = TAG_NUM_OF_ITEM_TYPES;
and_filter.AddItem(std::make_unique<TagSongFilter>(TagType(tag),
value,
fold_case,
false));
break;
}
}
void
SongFilter::Parse(ConstBuffer<const char *> args, bool fold_case)
{
if (args.empty())
throw std::runtime_error("Incorrect number of filter arguments");
do {
if (*args.front() == '(') {
const char *s = args.shift();
const char *end = s;
auto f = ParseExpression(end, fold_case);
if (*end != 0)
throw std::runtime_error("Unparsed garbage after expression");
and_filter.AddItem(std::move(f));
continue;
}
if (args.size < 2)
throw std::runtime_error("Incorrect number of filter arguments");
const char *tag = args.shift();
const char *value = args.shift();
Parse(tag, value, fold_case);
} while (!args.empty());
}
bool
SongFilter::Match(const LightSong &song) const noexcept
{
return and_filter.Match(song);
}
bool
SongFilter::HasFoldCase() const noexcept
{
for (const auto &i : and_filter.GetItems()) {
if (auto t = dynamic_cast<const TagSongFilter *>(i.get())) {
if (t->GetFoldCase())
return true;
} else if (auto u = dynamic_cast<const UriSongFilter *>(i.get())) {
if (u->GetFoldCase())
return true;
}
}
return false;
}
bool
SongFilter::HasOtherThanBase() const noexcept
{
for (const auto &i : and_filter.GetItems()) {
const auto *f = dynamic_cast<const BaseSongFilter *>(i.get());
if (f == nullptr)
return true;
}
return false;
}
const char *
SongFilter::GetBase() const noexcept
{
for (const auto &i : and_filter.GetItems()) {
const auto *f = dynamic_cast<const BaseSongFilter *>(i.get());
if (f != nullptr)
return f->GetValue();
}
return nullptr;
}
SongFilter
SongFilter::WithoutBasePrefix(const char *_prefix) const noexcept
{
const StringView prefix(_prefix);
SongFilter result;
for (const auto &i : and_filter.GetItems()) {
const auto *f = dynamic_cast<const BaseSongFilter *>(i.get());
if (f != nullptr) {
const char *s = StringAfterPrefix(f->GetValue(), prefix);
if (s != nullptr) {
if (*s == 0)
continue;
if (*s == '/') {
++s;
if (*s != 0)
result.and_filter.AddItem(std::make_unique<BaseSongFilter>(s));
continue;
}
}
}
result.and_filter.AddItem(i->Clone());
}
return result;
}
|