summaryrefslogtreecommitdiffstats
path: root/src/rip/k3bpatternparser.cpp
blob: 0ab14d8770a25c70b827bdaee2459aa50f59cd4c (plain)
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
/*
 *
 * $Id: k3bpatternparser.cpp 619556 2007-01-03 17:38:12Z trueg $
 * Copyright (C) 2003 Sebastian Trueg <trueg@k3b.org>
 * Copyright (C) 2004-2005 Jakob Petsovits <jpetso@gmx.at>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.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.
 * See the file "COPYING" for the exact licensing terms.
 */


#include "k3bpatternparser.h"

#include <tqregexp.h>
#include <tqdatetime.h>
#include <tqvaluestack.h>

#include <kglobal.h>
#include <klocale.h>


TQString K3bPatternParser::parsePattern( const K3bCddbResultEntry& entry,
                                        unsigned int trackNumber,
                                        const TQString& pattern,
                                        bool replace,
                                        const TQString& replaceString )
{
  if( entry.titles.count() < trackNumber )
    return "";

  TQString dir, s;
  char c = ' ';     // contains the character representation of a special string
  unsigned int len; // length of the current special string


  for( unsigned int i = 0; i < pattern.length(); ++i ) {

    if( pattern[i] == '%' ) {

      if( i + 1 < pattern.length() ) {
        len = 2;

        if( pattern[i+1] != '{' ) {  // strings like %a
          c = pattern[i+1];
        }
        else if( i + 3 >= pattern.length() ) {  // too short to contain a %{*} string
          c = ' ';
        }
        else {  // long enough to contain %{*}

          if( pattern[i+3] == '}' ) {  // strings like %{a}
            c = pattern[i+2];
            len = 4;
          }
          else {  // strings like %{artist}, or anything like %{*

            while( i + len - 1 < pattern.length() ) {
              ++len;

              if( pattern[i + len - 1] == '%' ) {  // don't touch other special strings
                c = ' ';
                --len;
                break;
              }
              else if( pattern[i + len - 1] == '}' ) {
                s = pattern.mid( i + 2, len - 3 );

                if( s == "title" ) {
                  c = TITLE;
                }
                else if( s == "artist" ) {
                  c = ARTIST;
                }
                else if( s == "number" ) {
                  c = NUMBER;
                }
                else if( s == "comment" ) {
                  c = COMMENT;
                }
                else if( s == "year" ) {
                  c = YEAR;
                }
                else if( s == "genre" ) {
                  c = GENRE;
                }
                else if( s == "albumtitle" ) {
                  c = ALBUMTITLE;
                }
                else if( s == "albumartist" ) {
                  c = ALBUMARTIST;
                }
                else if( s == "albumcomment" ) {
                  c = ALBUMCOMMENT;
                }
                else if( s == "date" ) {
                  c = DATE;
                }
                else {  // no valid pattern in here, don't replace anything
                  c = ' ';
                }
                break; // finished parsing %{* string
              }
            } // end of while(...)

          } // end of %{* strings

        } // end of if( long enough to contain %{*} )

        switch( c ) {
          case ARTIST:
            s = entry.artists[trackNumber-1];
            s.replace( '/', '_' );
            s.replace( '*', '_' );
            s.replace( '}', '*' );  // for conditional inclusion
            dir.append( s.isEmpty()
                ? i18n("unknown") + TQString(" %1").arg(trackNumber)
                : s );
            break;
          case TITLE:
            s = entry.titles[trackNumber-1];
            s.replace( '/', '_' );
            s.replace( '*', '_' );
            s.replace( '}', '*' );
            dir.append( s.isEmpty()
                ? i18n("Track %1").arg(trackNumber)
                : s );
            break;
          case NUMBER:
            dir.append( TQString::number(trackNumber).rightJustify( 2, '0' ) );
            break;
          case YEAR:
            dir.append( TQString::number( entry.year ) );
            break;
          case COMMENT:
            s = entry.extInfos[trackNumber-1];
            s.replace( '/', '_' );
            s.replace( '*', '_' );
            s.replace( '}', '*' );
            dir.append( s );
            break;
          case GENRE:
            s = ( entry.genre.isEmpty() ? entry.category : entry.genre );
            s.replace( '/', '_' );
            s.replace( '*', '_' );
            s.replace( '}', '*' );
            dir.append( s );
            break;
          case ALBUMARTIST:
            dir.append( entry.cdArtist.isEmpty()
                ? i18n("unknown") : entry.cdArtist );
            break;
          case ALBUMTITLE:
            s = entry.cdTitle;
            s.replace( '/', '_' );
            s.replace( '*', '_' );
            s.replace( '}', '*' );
            dir.append( s.isEmpty()
                ? i18n("unknown") : s );
            break;
          case ALBUMCOMMENT:
            s = entry.cdExtInfo;
            s.replace( '/', '_' );
            s.replace( '*', '_' );
            s.replace( '}', '*' );
            dir.append( s ); // I think it makes more sense to allow empty comments
            break;
          case DATE:
            dir.append( KGlobal::locale()->formatDate( TQDate::currentDate() ) );
            break;
          default:
            dir.append( pattern.mid(i, len) );
            break;
        }
        i += len - 1;
      }
      else {  // end of pattern
        dir.append( "%" );
      }
    }
    else {
      dir.append( pattern[i] );
    }
  }



  // /* delete line comment to comment out
  // the following part: Conditional Inclusion

  TQValueStack<int> offsetStack;
  TQString inclusion;
  bool isIncluded;

  static TQRegExp conditionrx( "^[@|!][atyegrmx](?:='.*')?\\{" );
  conditionrx.setMinimal( TRUE );

  for( unsigned int i = 0; i < dir.length(); ++i ) {

    offsetStack.push(
      conditionrx.search(dir, i, TQRegExp::CaretAtOffset) );

    if( offsetStack.top() == -1 ) {
      offsetStack.pop();
    }
    else {
      i += conditionrx.matchedLength() - 1;
      continue;
    }

    if( dir[i] == '}' && !offsetStack.isEmpty() ) {

      int offset = offsetStack.pop();
      int length = i - offset + 1;

      switch( (TQChar) dir[offset+1] ) {
      case ARTIST:
        s = entry.artists[trackNumber-1];
        break;
      case TITLE:
        s = entry.titles[trackNumber-1];
        break;
      case NUMBER:
        s = TQString::number( trackNumber );
        break;
      case YEAR:
        s = TQString::number( entry.year );
        break;
      case COMMENT:
        s = entry.extInfos[trackNumber-1];
        break;
      case GENRE:
        s = ( entry.genre.isEmpty() ? entry.category : entry.genre );
        break;
      case ALBUMARTIST:
        s = entry.cdArtist;
        break;
      case ALBUMTITLE:
        s = entry.cdTitle;
        break;
      case ALBUMCOMMENT:
        s = entry.cdExtInfo;
        break;
      case DATE:
        s = KGlobal::locale()->formatDate( TQDate::currentDate() );
        break;
      default: // we must never get here,
        break; // all choices should be covered
      }

      if( dir[offset+2] == '{' ) { // no string matching, e.g. ?y{text}
        switch( (TQChar) dir[offset+1] ) {
        case YEAR:
          isIncluded = (s != "0");
          break;
        default:
          isIncluded = !s.isEmpty();
          break;
        }
        inclusion = dir.mid( offset + 3, length - 4 );
      }
      else { // with string matching, e.g. ?y='2004'{text}

	// Be aware that there might be ' in the condition text
        int endOfCondition = dir.find( '{', offset+4 )-1;
        TQString condition = dir.mid( offset+4,
				     endOfCondition - (offset+4) );

        isIncluded = (s == condition);
        inclusion = dir.mid( endOfCondition+2,
                             i - (endOfCondition+2) );
      }

      if( dir[offset] == '!' )
          isIncluded = !isIncluded;
      // Leave it when it's '@'.

      dir.replace( offset, length, ( isIncluded ? inclusion : TQString("") ) );

      if( isIncluded == TRUE )
        i -= length - inclusion.length();
      else
        i = offset - 1; // start next loop at offset

      continue;

    } // end of replace (at closing bracket '}')

  } // end of conditional inclusion for(...)

  // end of Conditional Inclusion */


  dir.replace( '*', '}' );  // bring the brackets back, if there were any

  if( replace )
    dir.replace( TQRegExp( "\\s" ), replaceString );

  return dir;
}