aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Input/aac/aacfile.cpp
Commit message (Collapse)AuthorAgeFilesLines
* aac: fixed ADTS parsertrialuser022016-08-251-0/+6
| | | | git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@6683 90c681e8-e032-0410-971d-27865f9a5e38
* improved aac plugintrialuser022016-08-021-46/+58
| | | | git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@6630 90c681e8-e032-0410-971d-27865f9a5e38
* removed trailing whitespacestrialuser022015-07-261-1/+0
| | | | git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@5272 90c681e8-e032-0410-971d-27865f9a5e38
* aac decoder: improved detection by contenttrialuser022012-11-111-1/+15
| | | | git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@2987 90c681e8-e032-0410-971d-27865f9a5e38
* fixed FSF headerstrialuser022012-07-311-1/+1
| | | | git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@2845 90c681e8-e032-0410-971d-27865f9a5e38
* fixed FSF address (trunk)trialuser022012-07-311-1/+1
| | | | git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@2844 90c681e8-e032-0410-971d-27865f9a5e38
* fixed copyrightstrialuser022012-02-291-1/+1
| | | | git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@2655 90c681e8-e032-0410-971d-27865f9a5e38
* aac decoder: fixed streams supporttrialuser022012-02-291-11/+30
| | | | git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@2652 90c681e8-e032-0410-971d-27865f9a5e38
* reapplied previous patchtrialuser022010-03-111-1/+1
| | | | git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@1620 90c681e8-e032-0410-971d-27865f9a5e38
* removed obsolete memberstrialuser022010-03-111-3/+3
| | | | git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@1619 90c681e8-e032-0410-971d-27865f9a5e38
* aac plugin fixes, updated Russian translationtrialuser022009-03-141-3/+8
| | | | git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@852 90c681e8-e032-0410-971d-27865f9a5e38
* AAC plugin: added details dialogtrialuser022008-10-311-9/+68
| | | | git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@607 90c681e8-e032-0410-971d-27865f9a5e38
* AAC plugin: fixed seeking; id3v2 tags supporttrialuser022008-10-301-0/+9
| | | | git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@606 90c681e8-e032-0410-971d-27865f9a5e38
* enabled aac plugintrialuser022008-10-301-4/+51
| | | | git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@605 90c681e8-e032-0410-971d-27865f9a5e38
* aac plugintrialuser022008-10-251-0/+120
git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@601 90c681e8-e032-0410-971d-27865f9a5e38
mpui/metadataformatter.cpp?id=264f3ddbe777c212620a2e5c66c1b9f358f833e7'>264f3ddbe
9908966b7
264f3ddbe
1e31fe896
264f3ddbe



9908966b7
264f3ddbe




90b71df13

908de607e
abaab158b
ee5abc611
abaab158b

ee5abc611
90b71df13
ee5abc611
7a1dd2cf2

c8134e1cf




ad55f2654
e14146d9a
ad55f2654










ad4866f45
e14146d9a
ad55f2654




e14146d9a

264f3ddbe


































fefbce2e2
































264f3ddbe
90b71df13
264f3ddbe









90b71df13
264f3ddbe











017c91015

264f3ddbe
90b71df13
017c91015











264f3ddbe
017c91015
264f3ddbe
017c91015






264f3ddbe
017c91015

264f3ddbe
264f3ddbe












































90b71df13
264f3ddbe
















bb1da332b











































































264f3ddbe








90b71df13
264f3ddbe











017c91015













ee5abc611
264f3ddbe







ee5abc611
264f3ddbe



ee5abc611
7e324269d
ee5abc611
264f3ddbe
ee5abc611
264f3ddbe
7e324269d

ee5abc611

7e324269d




ee5abc611
7e324269d



ee5abc611
7e324269d
017c91015
7e324269d

bb1da332b

ee5abc611
bb1da332b
264f3ddbe



ee5abc611
264f3ddbe



ee5abc611
264f3ddbe
fefbce2e2


264f3ddbe



ee5abc611
264f3ddbe






ee5abc611
264f3ddbe
9bdd208bb
264f3ddbe


ee5abc611
264f3ddbe

ee5abc611
264f3ddbe

8e257213e

ee5abc611
8e257213e
264f3ddbe

ee5abc611
264f3ddbe
9bdd208bb

ee5abc611
9bdd208bb
264f3ddbe

ee5abc611
264f3ddbe


ee5abc611
264f3ddbe


ee5abc611
264f3ddbe
33317ad58

ee5abc611
33317ad58
264f3ddbe


fefbce2e2




908de607e
264f3ddbe






7e324269d



264f3ddbe
1e31fe896
264f3ddbe


fefbce2e2

264f3ddbe

bb1da332b

264f3ddbe


1e31fe896
264f3ddbe























bb1da332b





264f3ddbe





fefbce2e2





264f3ddbe






7e324269d













017c91015





264f3ddbe




90b71df13
7e324269d























264f3ddbe
90b71df13
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
                                                                            
                                                                            
                                                                            













                                                                            
                                                                            




                                                                             





                           
                 







                 
                 






                                 

                                                          





                              

                    
                                                            
 









                                                 
                                          


                                                      
                                                 






                                                                






                                                          


                            


                        
                 
                                                                  
      
                               
                 
                                               
                                          



                                                
      




                                                

 
                                                                  
 
                                                       

 
                                                                              
 
                                                

 




                                                                              
                                                                                      
 










                                                                              
                                                                                                                   
        




                                                                         

 


































                                                                                                               
































                                                                                                                  
                                                                                                                               
 









                                                      
 











                             

                         
                      
     











                                             
         
                                   
             






                                         
             

                                        
         












































                                                            
     
















                                                                                       











































































                                                                                                                                








                                                                                                                                 
     











                                              













                                                                                                                                   
                                                                                                         







                                            
                                                         



                                                 
                                                                         
                                             
                                                                          
                
                                                                          
         

                                                   

                                                                         




                                                  
                                                                         



                                
                                                                             
                                   
                                    

             

                                                   
                                                                                                          
         



               
                                                                                                               



                      
                                                      
              


                                             



                       
                                                       






                     
                                                                                             
 
                                                         


                                
                                                     

                                                                   
                                                     

                                                           

                                                                       
                                     
 

                         
                                                   
     

                                 
                            
     

                                            
                                                                 


                                     
                                                


                                      
                                            
     

                                        
                                               
     


                     




                                                                                
                                                                       






                                             



                                               
               
                                               


                                                            

                                                               

                                                          

                                                              


                                       
                                                     























                                                                              





                                                     





                                                       





                                                          






                                                    













                                              





                                                     




                                                   
     























                                                          
                 
 
/***************************************************************************
 *   Copyright (C) 2015-2019 by Ilya Kotov                                 *
 *   forkotov02@ya.ru                                                      *
 *                                                                         *
 *   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.         *
 ***************************************************************************/


/*
Syntax:
%p - artist,
%a - album,
%aa - album artist,
%t - title,
%n - track number,
%NN - 2-digit track number,
%I - track index,
%g - genre,
%c - comment,
%C - composer,
%D - disc number,
%f - file name,
%F - full path,
%y - year,
%l - duration,
%I - track index,
%{bitrate} - bitrate,
%{samplerate} - sample rate,
%{channels} - number of channels,
%{samplesize} - bits per sample,
%{format} - format name,
%{decoder} - decoder name,
%{filesize} - file size,
%if(A,B,C) or %if(A&B&C,D,E) - condition,
%dir(n) - Name of the directory located on n levels above.
*/

#include <QStringList>
#include <QUrl>
#include "metadataformatter.h"

//#define DUMP_NODES

MetaDataFormatter::MetaDataFormatter(const QString &pattern)
{
    m_fieldNames.insert("t", Qmmp::TITLE);
    m_fieldNames.insert("p", Qmmp::ARTIST);
    m_fieldNames.insert("aa", Qmmp::ALBUMARTIST);
    m_fieldNames.insert("a", Qmmp::ALBUM);
    m_fieldNames.insert("c", Qmmp::COMMENT);
    m_fieldNames.insert("g", Qmmp::GENRE);
    m_fieldNames.insert("C", Qmmp::COMPOSER);
    m_fieldNames.insert("y", Qmmp::YEAR);
    m_fieldNames.insert("n", Qmmp::TRACK);
    m_fieldNames.insert("D", Qmmp::DISCNUMBER);
    m_fieldNames.insert("F", Param::PATH);
    m_fieldNames.insert("NN", Param::TWO_DIGIT_TRACK);
    m_fieldNames.insert("l", Param::DURATION);
    m_fieldNames.insert("f", Param::FILE_NAME);
    m_fieldNames.insert("I", Param::TRACK_INDEX);
    m_propertyNames.insert("bitrate", Qmmp::BITRATE);
    m_propertyNames.insert("samplerate", Qmmp::SAMPLERATE);
    m_propertyNames.insert("channels", Qmmp::CHANNELS);
    m_propertyNames.insert("samplesize", Qmmp::BITS_PER_SAMPLE);
    m_propertyNames.insert("format", Qmmp::FORMAT_NAME);
    m_propertyNames.insert("decoder", Qmmp::DECODER);
    m_propertyNames.insert("filesize", Qmmp::FILE_SIZE);

    if(!pattern.isEmpty())
        setPattern(pattern);
}

void MetaDataFormatter::setPattern(const QString &pattern)
{
    if(pattern == m_pattern)
        return;

    m_pattern = pattern;
    m_nodes.clear();

#ifdef DUMP_NODES
    qDebug("MetaDataFormatter: pattern: %s", qPrintable(pattern));
#endif
    m_nodes = compile(pattern);
#ifdef DUMP_NODES
    qDebug("MetaDataFormatter: dump of nodes");
    for(const Node &n : qAsConst(m_nodes))
    {
        qDebug("=>%s", qPrintable(dumpNode(n)));
    }
    qDebug("MetaDataFormatter: end of dump");
#endif
}

const QString MetaDataFormatter::pattern() const
{
    return m_pattern;
}

QString MetaDataFormatter::format(const PlayListTrack *item) const
{
    return evalute(&m_nodes, item, item->trackIndex());
}

QString MetaDataFormatter::format(const TrackInfo &info, int trackIndex) const
{
    return evalute(&m_nodes, &info, trackIndex);
}

QString MetaDataFormatter::format(const TrackInfo *info, int trackIndex) const
{
    return format(*info, trackIndex);
}

QString MetaDataFormatter::formatDuration(qint64 duration, bool hideZero, bool showMs)
{
    if(duration <= 0)
    {
        if(hideZero)
            return QString();
        else
            return showMs ? QLatin1String("0:00.000") : QLatin1String("0:00");
    }

    QString out;
    qint64 durationInSeconds = duration / 1000;
    if(durationInSeconds >= 3600)
        out = QString("%1:%2").arg(durationInSeconds / 3600).arg(durationInSeconds % 3600 / 60, 2, 10, QChar('0'));
    else
        out = QString("%1").arg(durationInSeconds % 3600 / 60);
    out += QString(":%1").arg(durationInSeconds % 60, 2, 10, QChar('0'));
    if(showMs)
        out += QString(".%1").arg(duration % 1000, 3, 10, QChar('0'));
    return out;
}

bool MetaDataFormatter::parseField(QList<Node> *nodes, QString::const_iterator *i, QString::const_iterator end)
{
    QString fieldName;
    int field = Qmmp::UNKNOWN;

    //try to find field with 2 symbols
    if((*i) + 1 != end)
    {
        fieldName.append((**i));
        fieldName.append(*((*i)+1));
        field = m_fieldNames.value(fieldName, Qmmp::UNKNOWN);
    }
    //try to find field with 1 symbol
    if(field == Qmmp::UNKNOWN)
    {
        fieldName.clear();
        fieldName.append((**i));
        field = m_fieldNames.value(fieldName, Qmmp::UNKNOWN);
    }

    if(field != Qmmp::UNKNOWN)
    {
        Node node;
        node.command = Node::PRINT_TEXT;
        Param param;
        param.type = Param::FIELD;
        param.field = field;
        node.params.append(param);
        nodes->append(node);
        (*i) += fieldName.size() - 1;
        return true;
    }
    return false;
}

bool MetaDataFormatter::parseProperty(QList<Node> *nodes, QString::const_iterator *i, QString::const_iterator end)
{
    if((*i) + 1 == end || (*i) + 2 == end)
        return false;

    if((**i) != QChar('{'))
        return false;

    (*i)++; //skip '{'

    QString propertyName;

    while((*i) != end && (**i) != QChar('}'))
    {
        propertyName.append((**i));
        (*i)++;
    }

    int field = m_propertyNames.value(propertyName, Qmmp::UNKNOWN);
    if(field != Qmmp::UNKNOWN)
    {
        Node node;
        node.command = Node::PRINT_TEXT;
        Param param;
        param.type = Param::PROPERTY;
        param.field = field;
        node.params.append(param);
        nodes->append(node);
        return true;
    }
    return false;
}

bool MetaDataFormatter::parseIf(QList<MetaDataFormatter::Node> *nodes, QString::const_iterator *i, QString::const_iterator end)
{
    if((*i) + 1 == end || (*i) + 2 == end)
        return false;

    if((**i) != QChar('i') || *((*i)+1) != QChar('f'))
        return false;

    (*i)+=2;

    Node node;
    node.command = Node::IF_KEYWORD;

    int brackets_tracker = 0;
    QString var1, var2, var3;

    enum {
        STARTING = 0,
        READING_VAR1,
        READING_VAR2,
        READING_VAR3,
        FINISHED,

    } state = STARTING;

    bool escaped = false;

    while((*i) != end)
    {
        if((**i) == QChar('\\'))
        {
            (*i)++;
            escaped = true;
            continue;
        }

        if(escaped) //ignore escaped brackets
        {
            escaped = false;
        }
        else
        {
            if((**i) == QChar('('))
            {
                brackets_tracker++;
                if(state == STARTING)
                {
                    state = READING_VAR1;
                    (*i)++;
                    continue;
                }
            }
            else if((**i) == QChar(')'))
                brackets_tracker--;
        }

        switch (state)
        {
        case STARTING:
        {
            break;
        }
        case READING_VAR1:
        {
            if((**i) == QChar(',') && brackets_tracker == 1)
            {
                state = READING_VAR2;
                break;
            }
            var1.append((**i));
            break;
        }
        case READING_VAR2:
        {
            if((**i) == QChar(',') && brackets_tracker == 1)
            {
                state = READING_VAR3;
                break;
            }
            var2.append((**i));
            break;
        }
        case READING_VAR3:
        {
            if((**i) == QChar(')') && brackets_tracker == 0)
            {
                state = FINISHED;
                break;
            }
            var3.append((**i));
            break;
        }
        default:
            break;
        }

        if(state == FINISHED)
            break;

        (*i)++;
    }

    if(state != FINISHED)
    {
        qWarning("MetaDataFormatter: syntax error");
        return false;
    }

    Param param1, param2, param3;
    param1.type = Param::NODES, param2.type = Param::NODES, param3.type = Param::NODES;
    param1.children = compile(var1);
    param2.children = compile(var2);
    param3.children = compile(var3);
    node.params << param1 << param2 << param3;
    nodes->append(node);
    return true;
}

bool MetaDataFormatter::parseDir(QList<MetaDataFormatter::Node> *nodes, QString::const_iterator *i, QString::const_iterator end)
{
    if((*i) + 1 == end || (*i) + 2 == end || (*i) + 3 == end)
        return false;

    if((**i) != QChar('d') || *((*i)+1) != QChar('i') || *((*i)+2) != QChar('r'))
        return false;

    (*i)+=3;

    if((**i) != QChar('('))
        return false;

    Node node;
    node.command = Node::DIR_FUNCTION;
    QString var;

    enum {
        STARTING = 0,
        READING_VAR,
        FINISHED,

    } state = STARTING;

    while((*i) != end)
    {
        if((**i) == QChar('(') && state == STARTING)
        {
            state = READING_VAR;
            (*i)++;
            continue;
        }

        switch (state)
        {
        case STARTING:
        {
            break;
        }
        case READING_VAR:
        {
            if((**i) == QChar(')'))
            {
                state = FINISHED;
                break;
            }
            var.append((**i));
            break;
        }
        default:
            break;
        }

        if(state == FINISHED)
            break;

        (*i)++;
    }

    if(state != FINISHED)
    {
        qWarning("MetaDataFormatter: syntax error");
        return false;
    }

    Param param;
    param.type = Param::NUMERIC;
    bool ok = false;
    param.number = var.toInt(&ok);
    if(!ok)
        param.number = 0;
    node.params << param;
    nodes->append(node);
    return true;
}

void MetaDataFormatter::parseText(QList<MetaDataFormatter::Node> *nodes, QString::const_iterator *i, QString::const_iterator end)
{
    Node node;
    node.command = Node::PRINT_TEXT;
    Param param;
    param.type = Param::TEXT;
    node.params.append(param);

    forever
    {
        if((*i) == end || (**i) == QChar('%'))
        {
            (*i)--;
            break;
        }
        node.params[0].text.append(**i);
        (*i)++;
    }
    if(!node.params[0].text.isEmpty())
        nodes->append(node);
}

void MetaDataFormatter::parseEscape(QList<MetaDataFormatter::Node> *nodes, QString::const_iterator *i, QString::const_iterator end)
{
    if((*i) == end)
        return;

    Node node;
    node.command = Node::PRINT_TEXT;
    Param param;
    param.type = Param::TEXT;
    node.params.append(param);
    node.params[0].text.append(**i);
    nodes->append(node);
}

QString MetaDataFormatter::evalute(const QList<Node> *nodes, const TrackInfo *info, int trackIndex) const
{
    QString out;
    for(int i = 0; i < nodes->count(); ++i)
    {
        Node node = nodes->at(i);
        if(node.command == Node::PRINT_TEXT)
        {
            Param p = node.params.first();
            out.append(printParam(&p, info, trackIndex));

        }
        else if(node.command == Node::IF_KEYWORD)
        {
            QString var1 = printParam(&node.params[0], info, trackIndex);
            if(var1.isEmpty() || var1 == "0")
                out.append(printParam(&node.params[2], info, trackIndex));
            else
                out.append(printParam(&node.params[1], info, trackIndex));
        }
        else if(node.command == Node::AND_OPERATOR)
        {
            QString var1 = printParam(&node.params[0], info, trackIndex);
            QString var2 = printParam(&node.params[1], info, trackIndex);
            if(!var1.isEmpty() && !var2.isEmpty())
                out.append("1");
        }
        else if(node.command == Node::OR_OPERATOR)
        {
            QString var1 = printParam(&node.params[0], info, trackIndex);
            if(!var1.isEmpty())
                out.append("1");
            else
            {
                QString var2 = printParam(&node.params[1], info, trackIndex);
                if(!var2.isEmpty())
                    out.append("1");
            }
        }
        else if(node.command == Node::DIR_FUNCTION)
        {
            out.append(info->path().section('/', -node.params[0].number - 2, -node.params[0].number - 2));
        }
    }
    return out;
}

QString MetaDataFormatter::printParam(MetaDataFormatter::Param *p, const TrackInfo *info, int trackIndex) const
{
    switch (p->type)
    {
    case Param::FIELD:
        return printField(p->field, info, trackIndex);
        break;
    case Param::PROPERTY:
        return printProperty(p->field, info);
        break;
    case Param::TEXT:
        return p->text;
        break;
    case Param::NODES:
        return evalute(&p->children, info, trackIndex);
        break;
    default:
        break;
    }
    return QString();
}

QString MetaDataFormatter::printField(int field, const TrackInfo *info, int trackIndex) const
{
    if(field >= Qmmp::TITLE && field <= Qmmp::DISCNUMBER)
    {
        if(field == Qmmp::TITLE)
        {
            QString title = info->value(Qmmp::TITLE);
            if(title.isEmpty()) //using file name if title is empty
            {
                title = info->path().section('/',-1);
                title = title.left(title.lastIndexOf('.'));
            }

            if(title.isEmpty()) //using full path if file name is empty
                title = info->path();

            return title;
        }
        return info->value((Qmmp::MetaData) field);
    }
    else if(field == Param::PATH)
    {
        return info->path();
    }
    else if(field == Param::TWO_DIGIT_TRACK)
    {
        return QString("%1").arg(info->value(Qmmp::TRACK),2,'0');
    }
    else if(field == Param::DURATION)
    {
        return formatDuration(info->duration());
    }
    else if(field == Param::FILE_NAME)
    {
        return info->path().section('/',-1);
    }
    else if(field == Param::TRACK_INDEX)
    {
        return QString::number(trackIndex + 1);
    }
    return QString();
}

QString MetaDataFormatter::printProperty(int field, const TrackInfo *info) const
{
    return info->value((Qmmp::TrackProperty) field);
}

QString MetaDataFormatter::dumpNode(MetaDataFormatter::Node node) const
{
    QString str;
    QStringList params;
    if(node.command == Node::PRINT_TEXT)
        str += "PRINT_TEXT";
    else if(node.command == Node::IF_KEYWORD)
        str += "IF_KEYWORD";
    else if(node.command == Node::AND_OPERATOR)
        str += "AND_OPERATOR";
    else if(node.command == Node::OR_OPERATOR)
        str += "OR_OPERATOR";
    str += "(";
    for(const Param &p : qAsConst(node.params))
    {
        if(p.type == Param::FIELD)
            params.append(QString("FIELD:%1").arg(p.field));
        else if(p.type == Param::PROPERTY)
            params.append(QString("PROPERTY:%1").arg(p.field));
        else if(p.type == Param::TEXT)
            params.append(QString("TEXT:%1").arg(p.text));
        else if(p.type == Param::NUMERIC)
            params.append(QString("NUMBER:%1").arg(p.number));
        else if(p.type == Param::NODES)
        {
            QStringList nodeStrList;
            for(const Node &n : qAsConst(p.children))
            {
                nodeStrList.append(dumpNode(n));
            }
            params.append(QString("NODES:%1").arg(nodeStrList.join(",")));
        }
    }
    str.append(params.join(","));
    str.append(")");
    return str;
}

QList<MetaDataFormatter::Node> MetaDataFormatter::compile(const QString &expr)
{
    QList <Node> nodes;
    QString::const_iterator i = expr.constBegin();

    while (i != expr.constEnd())
    {
        if((*i) == QChar('%'))
        {
            i++;
            if(i == expr.constEnd())
                continue;

            if(parseDir(&nodes, &i, expr.constEnd()))
            {
                i++;
                continue;
            }

            if(parseField(&nodes, &i, expr.constEnd()))
            {
                i++;
                continue;
            }

            if(parseProperty(&nodes, &i, expr.constEnd()))
            {
                i++;
                continue;
            }

            if(parseIf(&nodes, &i, expr.constEnd()))
            {
                i++;
                continue;
            }
            continue;
        }
        else if((*i) == QChar('&'))
        {
            i++;
            Node node;
            node.command = Node::AND_OPERATOR;
            nodes.append(node);
        }
        else if((*i) == QChar('|'))
        {
            i++;
            Node node;
            node.command = Node::OR_OPERATOR;
            nodes.append(node);
        }
        else if((*i) == QChar('\\'))
        {
            i++;
            parseEscape(&nodes, &i, expr.constEnd());
            i++;
        }
        else
        {
            parseText(&nodes, &i, expr.constEnd());
            i++;
        }
    }

    //wrap operators
    for(int j = 0; j < nodes.count(); ++j)
    {
        if(nodes[j].command == Node::AND_OPERATOR ||
                nodes[j].command == Node::OR_OPERATOR)
        {
            if(j == 0 || j == nodes.count() - 1)
            {
                nodes.clear();
                qDebug("MetaDataFormatter: syntax error");
            }

            Param p1;
            p1.type = Param::NODES;
            p1.children << nodes.takeAt(j+1);
            Param p2;
            p2.type = Param::NODES;
            p2.children << nodes.takeAt(j-1);
            nodes[j-1].params.append(p1);
            nodes[j-1].params.append(p2);
            j--;
        }
    }
    return nodes;
}