All Classes Files Functions Variables Enumerations Enumerator Macros
matchPattern.cpp
Go to the documentation of this file.
1 #include "parser.hpp"
7 
16 // Searches the filename trying to match a season/episode pattern
17 void Parser::matchPattern( string &t_oldFile, int *coordofSnE, int &season, int &episode, int &xepisode, string &pepisode )
18 {
19  // Loop through each character of the filename
20  for ( unsigned int i=0; i<t_oldFile.length(); i++)
21  {
22  // Check for the following pattern S##E##-##
23  if ( t_oldFile[i]=='s' && isdigit( t_oldFile[i+1] ) && isdigit( t_oldFile[i+2] ) && t_oldFile[i+3]=='e' && isdigit( t_oldFile[i+4] ) && isdigit( t_oldFile[i+5] ) && t_oldFile[i+6]=='-' && isdigit( t_oldFile[i+7] ) && isdigit( t_oldFile[i+8] ) )
24  {
25  // Set the start coordinate of the season and episode information
26  coordofSnE[0] = i-1;
27  // Set the end coordinate of the season and episode information
28  coordofSnE[1] = i+10;
29  // Store the season information
30  season = atoi(t_oldFile.substr(i+1, 2).c_str());
31  // Store the episode information
32  episode = atoi(t_oldFile.substr(i+4, 2).c_str());
33  // Store extra episode information
34  xepisode = atoi(t_oldFile.substr(i+7, 2).c_str());
35  // Exit the pattern search
36  break;
37  }
38  // Check for the following pattern S##E##X
39  else if ( t_oldFile[i]=='s' && isdigit( t_oldFile[i+1] ) && isdigit( t_oldFile[i+2] ) && t_oldFile[i+3]=='e' && isdigit( t_oldFile[i+4] ) && isdigit( t_oldFile[i+5] ) && isalpha( t_oldFile[i+6] ) )
40  {
41  // Set the start coordinate of the season and episode information
42  coordofSnE[0] = i-1;
43  // Set the end coordinate of the season and episode information
44  coordofSnE[1] = i+7;
45  // Store the season information
46  season = atoi(t_oldFile.substr(i+1, 2).c_str());
47  // Store the episode information
48  episode = atoi(t_oldFile.substr(i+4, 2).c_str());
49  // Store the partial episode information
50  pepisode = t_oldFile.substr(i+6, 1).c_str();
51  // Exit the pattern search
52  break;
53  }
54  // Check for the following pattern S##E##
55  else if ( t_oldFile[i]=='s' && isdigit( t_oldFile[i+1] ) && isdigit( t_oldFile[i+2] ) && t_oldFile[i+3]=='e' && isdigit( t_oldFile[i+4] ) && isdigit( t_oldFile[i+5] ) )
56  {
57  // Set the start coordinate of the season and episode information
58  coordofSnE[0] = i-1;
59  // Set the end coordinate of the season and episode information
60  coordofSnE[1] = i+7;
61  // Store the season information
62  season = atoi(t_oldFile.substr(i+1, 2).c_str());
63  // Store the episode information
64  episode = atoi(t_oldFile.substr(i+4, 2).c_str());
65  // Exit the pattern search
66  break;
67  }
68  // Check for the following patterns S## E##-## or S##XE##-##
69  else if ( t_oldFile[i]=='s' && isdigit( t_oldFile[i+1] ) && isdigit( t_oldFile[i+2] ) && ( t_oldFile[i+3]==' ' || t_oldFile[i+3]=='/' || t_oldFile[i+3]=='.' || t_oldFile[i+3]=='x' ) && t_oldFile[i+4]=='e' && isdigit( t_oldFile[i+5] ) && isdigit( t_oldFile[i+6] ) && t_oldFile[i+7]=='-' && isdigit( t_oldFile[i+8] ) && isdigit( t_oldFile[i+9] ) )
70  {
71  // Set the start coordinate of the season and episode information
72  coordofSnE[0] = i-1;
73  // Set the end coordinate of the season and episode information
74  coordofSnE[1] = i+11;
75  // Store the season information
76  season = atoi(t_oldFile.substr(i+1, 2).c_str());
77  // Store the episode information
78  episode = atoi(t_oldFile.substr(i+5, 2).c_str());
79  // Store extra episode information
80  xepisode = atoi(t_oldFile.substr(i+8, 2).c_str());
81  // Exit the pattern search
82  break;
83  }
84  // Check for the following patterns S## E##X or S##XE##X
85  else if ( t_oldFile[i]=='s' && isdigit( t_oldFile[i+1] ) && isdigit( t_oldFile[i+2] ) && ( t_oldFile[i+3]==' ' || t_oldFile[i+3]=='/' || t_oldFile[i+3]=='.' || t_oldFile[i+3]=='x' ) && t_oldFile[i+4]=='e' && isdigit( t_oldFile[i+5] ) && isdigit( t_oldFile[i+6] ) && isalpha( t_oldFile[i+7] ) )
86  {
87  // Set the start coordinate of the season and episode information
88  coordofSnE[0] = i-1;
89  // Set the end coordinate of the season and episode information
90  coordofSnE[1] = i+8;
91  // Store the season information
92  season = atoi(t_oldFile.substr(i+1, 2).c_str());
93  // Store the episode information
94  episode = atoi(t_oldFile.substr(i+5, 2).c_str());
95  // Store the partial episode information
96  pepisode = t_oldFile.substr(i+7, 1).c_str();
97  // Exit the pattern search
98  break;
99  }
100  // Check for the following patterns S## E## or S##XE##
101  else if ( t_oldFile[i]=='s' && isdigit( t_oldFile[i+1] ) && isdigit( t_oldFile[i+2] ) && ( t_oldFile[i+3]==' ' || t_oldFile[i+3]=='/' || t_oldFile[i+3]=='.' || t_oldFile[i+3]=='x' ) && t_oldFile[i+4]=='e' && isdigit( t_oldFile[i+5] ) && isdigit( t_oldFile[i+6] ) )
102  {
103  // Set the start coordinate of the season and episode information
104  coordofSnE[0] = i-1;
105  // Set the end coordinate of the season and episode information
106  coordofSnE[1] = i+8;
107  // Store the season information
108  season = atoi(t_oldFile.substr(i+1, 2).c_str());
109  // Store the episode information
110  episode = atoi(t_oldFile.substr(i+5, 2).c_str());
111  // Exit the pattern search
112  break;
113  }
114  // Check for the following pattern S# E##-##
115  else if ( t_oldFile[i]=='s' && isdigit( t_oldFile[i+1] ) && t_oldFile[i+2]==' ' && t_oldFile[i+3]=='e' && isdigit( t_oldFile[i+4] ) && isdigit( t_oldFile[i+5] ) && t_oldFile[i+6]=='-' && isdigit( t_oldFile[i+7] ) && isdigit( t_oldFile[i+8] ) )
116  {
117  // Set the start coordinate of the season and episode information
118  coordofSnE[0] = i-1;
119  // Set the end coordinate of the season and episode information
120  coordofSnE[1] = i+10;
121  // Store the season information
122  season = atoi(t_oldFile.substr(i+1, 1).c_str());
123  // Store the episode information
124  episode = atoi(t_oldFile.substr(i+4, 2).c_str());
125  // Store extra episode information
126  xepisode = atoi(t_oldFile.substr(i+7, 2).c_str());
127  // Exit the pattern search
128  break;
129  }
130  // Check for the following pattern S# E##X
131  else if ( t_oldFile[i]=='s' && isdigit( t_oldFile[i+1] ) && t_oldFile[i+2]==' ' && t_oldFile[i+3]=='e' && isdigit( t_oldFile[i+4] ) && isdigit( t_oldFile[i+5] ) && isalpha( t_oldFile[i+6] ) )
132  {
133  // Set the start coordinate of the season and episode information
134  coordofSnE[0] = i-1;
135  // Set the end coordinate of the season and episode information
136  coordofSnE[1] = i+7;
137  // Store the season information
138  season = atoi(t_oldFile.substr(i+1, 1).c_str());
139  // Store the episode information
140  episode = atoi(t_oldFile.substr(i+4, 2).c_str());
141  // Store the partial episode information
142  pepisode = t_oldFile.substr(i+6, 1).c_str();
143  // Exit the pattern search
144  break;
145  }
146  // Check for the following pattern S# E##
147  else if ( t_oldFile[i]=='s' && isdigit( t_oldFile[i+1] ) && t_oldFile[i+2]==' ' && t_oldFile[i+3]=='e' && isdigit( t_oldFile[i+4] ) && isdigit( t_oldFile[i+5] ) )
148  {
149  // Set the start coordinate of the season and episode information
150  coordofSnE[0] = i-1;
151  // Set the end coordinate of the season and episode information
152  coordofSnE[1] = i+7;
153  // Store the season information
154  season = atoi(t_oldFile.substr(i+1, 1).c_str());
155  // Store the episode information
156  episode = atoi(t_oldFile.substr(i+4, 2).c_str());
157  // Exit the pattern search
158  break;
159  }
160  // Check for the following pattern S#E##-##
161  else if ( t_oldFile[i]=='s' && isdigit( t_oldFile[i+1] ) && t_oldFile[i+2]=='e' && isdigit( t_oldFile[i+3] ) && isdigit( t_oldFile[i+4] ) && t_oldFile[i+5]=='-' && isdigit( t_oldFile[i+6] ) && isdigit( t_oldFile[i+7] ) )
162  {
163  // Set the start coordinate of the season and episode information
164  coordofSnE[0] = i-1;
165  // Set the end coordinate of the season and episode information
166  coordofSnE[1] = i+9;
167  // Store the season information
168  season = atoi(t_oldFile.substr(i+1, 1).c_str());
169  // Store the episode information
170  episode = atoi(t_oldFile.substr(i+3, 2).c_str());
171  // Store extra episode information
172  xepisode = atoi(t_oldFile.substr(i+6, 2).c_str());
173  // Exit the pattern search
174  break;
175  }
176  // Check for the following pattern S#E##X
177  else if ( t_oldFile[i]=='s' && isdigit( t_oldFile[i+1] ) && t_oldFile[i+2]=='e' && isdigit( t_oldFile[i+3] ) && isdigit( t_oldFile[i+4] ) && isalpha( t_oldFile[i+5] ) )
178  {
179  // Set the start coordinate of the season and episode information
180  coordofSnE[0] = i-1;
181  // Set the end coordinate of the season and episode information
182  coordofSnE[1] = i+6;
183  // Store the season information
184  season = atoi(t_oldFile.substr(i+1, 1).c_str());
185  // Store the episode information
186  episode = atoi(t_oldFile.substr(i+3, 2).c_str());
187  // Store the partial episode information
188  pepisode = t_oldFile.substr(i+5, 1).c_str();
189  // Exit the pattern search
190  break;
191  }
192  // Check for the following pattern S#E##
193  else if ( t_oldFile[i]=='s' && isdigit( t_oldFile[i+1] ) && t_oldFile[i+2]=='e' && isdigit( t_oldFile[i+3] ) && isdigit( t_oldFile[i+4] ) )
194  {
195  // Set the start coordinate of the season and episode information
196  coordofSnE[0] = i-1;
197  // Set the end coordinate of the season and episode information
198  coordofSnE[1] = i+6;
199  // Store the season information
200  season = atoi(t_oldFile.substr(i+1, 1).c_str());
201  // Store the episode information
202  episode = atoi(t_oldFile.substr(i+3, 2).c_str());
203  // Exit the pattern search
204  break;
205  }
206  // Check for the following pattern S#E#-##
207  else if ( t_oldFile[i]=='s' && isdigit( t_oldFile[i+1] ) && t_oldFile[i+2]=='e' && isdigit( t_oldFile[i+3] ) && isdigit( t_oldFile[i+4] ) && t_oldFile[i+5]=='-' && isdigit( t_oldFile[i+6] ) )
208  {
209  // Set the start coordinate of the season and episode information
210  coordofSnE[0] = i-1;
211  // Set the end coordinate of the season and episode information
212  coordofSnE[1] = i+8;
213  // Store the season information
214  season = atoi(t_oldFile.substr(i+1, 1).c_str());
215  // Store the episode information
216  episode = atoi(t_oldFile.substr(i+3, 1).c_str());
217  // Store extra episode information
218  xepisode = atoi(t_oldFile.substr(i+5, 2).c_str());
219  // Exit the pattern search
220  break;
221  }
222  // Check for the following pattern S#E#X
223  else if ( t_oldFile[i]=='s' && isdigit( t_oldFile[i+1] ) && t_oldFile[i+2]=='e' && isdigit( t_oldFile[i+3] ) && isalpha( t_oldFile[i+4] ) )
224  {
225  // Set the start coordinate of the season and episode information
226  coordofSnE[0] = i-1;
227  // Set the end coordinate of the season and episode information
228  coordofSnE[1] = i+5;
229  // Store the season information
230  season = atoi(t_oldFile.substr(i+1, 1).c_str());
231  // Store the episode information
232  episode = atoi(t_oldFile.substr(i+3, 1).c_str());
233  // Store the partial episode information
234  pepisode = t_oldFile.substr(i+4, 1).c_str();
235  // Exit the pattern search
236  break;
237  }
238  // Check for the following pattern S#E#
239  else if ( t_oldFile[i]=='s' && isdigit( t_oldFile[i+1] ) && t_oldFile[i+2]=='e' && isdigit( t_oldFile[i+3] ) )
240  {
241  // Set the start coordinate of the season and episode information
242  coordofSnE[0] = i-1;
243  // Set the end coordinate of the season and episode information
244  coordofSnE[1] = i+5;
245  // Store the season information
246  season = atoi(t_oldFile.substr(i+1, 1).c_str());
247  // Store the episode information
248  episode = atoi(t_oldFile.substr(i+3, 1).c_str());
249  // Exit the pattern search
250  break;
251  }
252  // Check for the following pattern ####-##
253  else if ( isdigit(t_oldFile[i]) && isdigit(t_oldFile[i+1]) && isdigit(t_oldFile[i+2]) && isdigit(t_oldFile[i+3]) && t_oldFile[i+4]=='-' && isdigit(t_oldFile[i+5]) && isdigit(t_oldFile[i+6]) )
254  {
255  // Set the start coordinate of the season and episode information
256  coordofSnE[0] = i-1;
257  // Set the end coordinate of the season and episode information
258  coordofSnE[1] = i+8;
259  // Store the season information
260  season = atoi(t_oldFile.substr(i, 2).c_str());
261  // Store the episode information
262  episode = atoi(t_oldFile.substr(i+2, 2).c_str());
263  // Store extra episode information
264  xepisode = atoi(t_oldFile.substr(i+5, 2).c_str());
265  // Exit the pattern search
266  break;
267  }
268  // Check for the following pattern ####X
269  else if ( isdigit(t_oldFile[i]) && isdigit(t_oldFile[i+1]) && isdigit(t_oldFile[i+2]) && isdigit(t_oldFile[i+3]) && isalpha(t_oldFile[i+4]) )
270  {
271  // Set the start coordinate of the season and episode information
272  coordofSnE[0] = i-1;
273  // Set the end coordinate of the season and episode information
274  coordofSnE[1] = i+5;
275  // Store the season information
276  season = atoi(t_oldFile.substr(i, 2).c_str());
277  // Store the episode information
278  episode = atoi(t_oldFile.substr(i+2, 2).c_str());
279  // Store the partial episode information
280  pepisode = t_oldFile.substr(i+4, 1).c_str();
281  // Exit the pattern search
282  break;
283  }
284  // Check for the following pattern ####
285  else if ( isdigit(t_oldFile[i]) && isdigit(t_oldFile[i+1]) && isdigit(t_oldFile[i+2]) && isdigit(t_oldFile[i+3]) )
286  {
287  // Set the start coordinate of the season and episode information
288  coordofSnE[0] = i-1;
289  // Set the end coordinate of the season and episode information
290  coordofSnE[1] = i+5;
291  // Store the season information
292  season = atoi(t_oldFile.substr(i, 2).c_str());
293  // Store the episode information
294  episode = atoi(t_oldFile.substr(i+2, 2).c_str());
295  // Exit the pattern search
296  break;
297  }
298  // Check for the following pattern ###-##
299  else if ( isdigit(t_oldFile[i]) && isdigit(t_oldFile[i+1]) && isdigit(t_oldFile[i+2]) && t_oldFile[i+3]=='-' && isdigit(t_oldFile[i+4]) && isdigit(t_oldFile[i+5]) )
300  {
301  // Set the start coordinate of the season and episode information
302  coordofSnE[0] = i-1;
303  // Set the end coordinate of the season and episode information
304  coordofSnE[1] = i+7;
305  // Store the season information
306  season = atoi(t_oldFile.substr(i, 1).c_str());
307  // Store the episode information
308  episode = atoi(t_oldFile.substr(i+1, 2).c_str());
309  // Store extra episode information
310  xepisode = atoi(t_oldFile.substr(i+4, 2).c_str());
311  // Exit the pattern search
312  break;
313  }
314  // Check for the following pattern ###X
315  else if ( isdigit(t_oldFile[i]) && isdigit(t_oldFile[i+1]) && isdigit(t_oldFile[i+2]) && isalpha(t_oldFile[i+3]) )
316  {
317  // Set the start coordinate of the season and episode information
318  coordofSnE[0] = i-1;
319  // Set the end coordinate of the season and episode information
320  coordofSnE[1] = i+4;
321  // Store the season information
322  season = atoi(t_oldFile.substr(i, 1).c_str());
323  // Store the episode information
324  episode = atoi(t_oldFile.substr(i+1, 2).c_str());
325  // Store the partial episode information
326  pepisode = t_oldFile.substr(i+3, 1).c_str();
327  // Exit the pattern search
328  break;
329  }
330  // Check for the following pattern ###
331  else if ( isdigit(t_oldFile[i]) && isdigit(t_oldFile[i+1]) && isdigit(t_oldFile[i+2]) )
332  {
333  // Set the start coordinate of the season and episode information
334  coordofSnE[0] = i-1;
335  // Set the end coordinate of the season and episode information
336  coordofSnE[1] = i+4;
337  // Store the season information
338  season = atoi(t_oldFile.substr(i, 1).c_str());
339  // Store the episode information
340  episode = atoi(t_oldFile.substr(i+1, 2).c_str());
341  // Exit the pattern search
342  break;
343  }
344  // Check for the following pattern ##X##-##
345  else if ( isdigit(t_oldFile[i]) && isdigit(t_oldFile[i+1]) && t_oldFile[i+2]=='x' && isdigit(t_oldFile[i+3]) && isdigit(t_oldFile[i+4]) && t_oldFile[i+5]=='-' && isdigit(t_oldFile[i+6]) && isdigit(t_oldFile[i+7]) )
346  {
347  // Set the start coordinate of the season and episode information
348  coordofSnE[0] = i-1;
349  // Set the end coordinate of the season and episode information
350  coordofSnE[1] = i+9;
351  // Store the season information
352  season = atoi(t_oldFile.substr(i, 2).c_str());
353  // Store the episode information
354  episode = atoi(t_oldFile.substr(i+3, 2).c_str());
355  // Store extra episode information
356  xepisode = atoi(t_oldFile.substr(i+6, 2).c_str());
357  // Exit the pattern search
358  break;
359  }
360  // Check for the following pattern ##X##X
361  else if ( isdigit(t_oldFile[i]) && isdigit(t_oldFile[i+1]) && t_oldFile[i+2]=='x' && isdigit(t_oldFile[i+3]) && isdigit(t_oldFile[i+4]) && isalpha(t_oldFile[i+5]) )
362  {
363  // Set the start coordinate of the season and episode information
364  coordofSnE[0] = i-1;
365  // Set the end coordinate of the season and episode information
366  coordofSnE[1] = i+6;
367  // Store the season information
368  season = atoi(t_oldFile.substr(i, 2).c_str());
369  // Store the episode information
370  episode = atoi(t_oldFile.substr(i+3, 2).c_str());
371  // Store the partial episode information
372  pepisode = t_oldFile.substr(i+5, 1).c_str();
373  // Exit the pattern search
374  break;
375  }
376  // Check for the following pattern ##X##
377  else if ( isdigit(t_oldFile[i]) && isdigit(t_oldFile[i+1]) && t_oldFile[i+2]=='x' && isdigit(t_oldFile[i+3]) && isdigit(t_oldFile[i+4]) )
378  {
379  // Set the start coordinate of the season and episode information
380  coordofSnE[0] = i-1;
381  // Set the end coordinate of the season and episode information
382  coordofSnE[1] = i+6;
383  // Store the season information
384  season = atoi(t_oldFile.substr(i, 2).c_str());
385  // Store the episode information
386  episode = atoi(t_oldFile.substr(i+3, 2).c_str());
387  // Exit the pattern search
388  break;
389  }
390  // Check for the following pattern #X##-## or #/##-##
391  else if ( isdigit(t_oldFile[i]) && ( t_oldFile[i+1]=='x' || t_oldFile[i+1]=='/' || t_oldFile[i+1]=='-' ) && isdigit(t_oldFile[i+2]) && isdigit(t_oldFile[i+3]) && t_oldFile[i+4]=='-' && isdigit(t_oldFile[i+5]) && isdigit(t_oldFile[i+6]) )
392  {
393  // Set the start coordinate of the season and episode information
394  coordofSnE[0] = i-1;
395  // Set the end coordinate of the season and episode information
396  coordofSnE[1] = i+8;
397  // Store the season information
398  season = atoi(t_oldFile.substr(i, 1).c_str());
399  // Store the episode information
400  episode = atoi(t_oldFile.substr(i+2, 2).c_str());
401  // Store extra episode information
402  xepisode = atoi(t_oldFile.substr(i+5, 2).c_str());
403  // Exit the pattern search
404  break;
405  }
406  // Check for the following patterns #X##X or #/##X
407  else if ( isdigit(t_oldFile[i]) && ( t_oldFile[i+1]=='x' || t_oldFile[i+1]=='/' || t_oldFile[i+1]=='-' ) && isdigit(t_oldFile[i+2]) && isdigit(t_oldFile[i+3]) && isalpha(t_oldFile[i+4]) )
408  {
409  // Set the start coordinate of the season and episode information
410  coordofSnE[0] = i-1;
411  // Set the end coordinate of the season and episode information
412  coordofSnE[1] = i+5;
413  // Store the season information
414  season = atoi(t_oldFile.substr(i, 1).c_str());
415  // Store the episode information
416  episode = atoi(t_oldFile.substr(i+2, 2).c_str());
417  // Store the partial episode information
418  pepisode = t_oldFile.substr(i+4, 1).c_str();
419  // Exit the pattern search
420  break;
421  }
422  // Check for the following patterns #X## or #/##
423  else if ( isdigit(t_oldFile[i]) && ( t_oldFile[i+1]=='x' || t_oldFile[i+1]=='/' || t_oldFile[i+1]=='-' ) && isdigit(t_oldFile[i+2]) && isdigit(t_oldFile[i+3]) )
424  {
425  // Set the start coordinate of the season and episode information
426  coordofSnE[0] = i-1;
427  // Set the end coordinate of the season and episode information
428  coordofSnE[1] = i+5;
429  // Store the season information
430  season = atoi(t_oldFile.substr(i, 1).c_str());
431  // Store the episode information
432  episode = atoi(t_oldFile.substr(i+2, 2).c_str());
433  // Exit the pattern search
434  break;
435  }
436  }
437 };
The Parser class creates an object for each input file and parses file and folder names...
static void matchPattern(string &, int *, int &, int &, int &, string &)
Finds a season and episode number in a given string (filename).