All Classes Files Functions Variables Enumerations Enumerator Macros
showParser.cpp
Go to the documentation of this file.
1 #include "parser.hpp"
29 
31 // Default constructor: Sets to default values
33 {
34  oldDir = "";
35  oldFile = "";
36  dir = "";
37  show = "";
38  showNameIndex = -1;
39  season = -1;
40  episode = -1;
41  xepisode = -1;
42  pepisode = "";
43  title = "";
44  extension = "";
45  overrideDir = 0;
46  error = 0;
48  weight = 0;
49 };
50 
52 // Default destructor
54 {};
55 
65 // Parses the filename
66 void Parser::parseShow( string fileName, ConfigOpts *confOpt )
67 {
68  // Find where the file name begins
69  unsigned int pos = fileName.find_last_of( SLASH );
70  // Capture directory info - everything before and including the trailing slash
71  oldDir = fileName.substr(0, pos+1);
72  // Capture file info - everything after and excluding the trailing slash
73  oldFile = fileName.substr(pos+1);
74 
75  // Create temporary vars for manipulation
76  string t_oldDir = oldDir;
77  string t_oldFile = oldFile;
78 
79  // For each character in the file name
80  for( unsigned int i = 0; i < t_oldFile.length(); i++ )
81  {
82  // Convert to lowercase
83  t_oldFile[i] = tolower( t_oldFile[i] );
84  }
85 
86  // For each character in the directory name
87  for( unsigned int i = 0; i < t_oldDir.length(); i++ )
88  {
89  // Convert to lowercase
90  t_oldDir[i] = tolower( t_oldDir[i] );
91  }
92 
93  // Find where the file extension begins
94  pos = t_oldFile.find_last_of(".");
95  // Capture extension info after the period
96  extension = t_oldFile.substr(pos+1);
97  // Capture file name excluding the extension name and period
98  t_oldFile = t_oldFile.substr(0, pos);
99 
100  // Everything before coordofSnE[0] should be the show name
101  // coordofSnE[0] = start coordinate of s/e info
102  // coordofSnE[1] = end coordinate of s/e info
103  // Everything after coordofSnE[1] should be the show title
104  int coordofSnE[2] = {0,0};
105 
106  // Initalize a counter
107  unsigned int i;
108 
109  // Search the filename to match a season/episode pattern
110  matchPattern( t_oldFile, coordofSnE, season, episode, xepisode, pepisode );
111 
112  // If no season information matched
113  if( season == -1 )
114  {
115  // Season information search
116 
117  // Set the current position to the last position
118  unsigned int pos_oF = -1;
119  // Locate the string in the file name
120  pos_oF = t_oldFile.find( "season" );
121  // Set the current position to the last position
122  unsigned int pos_oD = -1;
123  // Locate the string in the directory name
124  pos_oD = t_oldDir.find( "season" );
125 
126  // Check if the file name contained the string
127  if( pos_oF < t_oldFile.length() - 7 && t_oldFile.length() > 7 )
128  {
129  // Check if two numbers follow the string 'season'
130  if( isdigit( t_oldFile.substr( pos_oF + 7, 2 ).c_str()[1] ) )
131  {
132  // Convert the two numbers found as season information
133  season = atoi( t_oldFile.substr( pos_oF + 7, 2 ).c_str() );
134  }
135  // Check if one number follows the string 'season'
136  else if( isdigit( t_oldFile.substr( pos_oF + 7, 1 ).c_str()[0] ) )
137  {
138  // Convert the one number found as season information
139  season = atoi( t_oldFile.substr( pos_oF + 7, 1 ).c_str() );
140  }
141  // Set the start coordinate of the season and episode information
142  coordofSnE[0] = pos_oF-1;
143  }
144  // Check if the directory name contained the string
145  else if( pos_oD < t_oldDir.length() - 7 && t_oldFile.length() > 7 )
146  {
147  // Check if two numbers follow the string 'season'
148  if( isdigit( t_oldDir.substr( pos_oD + 7, 2 ).c_str()[1] ) )
149  {
150  // Convert the two numbers found as season information
151  season = atoi( t_oldDir.substr( pos_oD + 7, 2 ).c_str() );
152  }
153  // Check if one number follows the string 'season'
154  else if( isdigit( t_oldDir.substr( pos_oD + 7, 1 ).c_str()[0] ) )
155  {
156  // Convert the one number found as season information
157  season = atoi( t_oldDir.substr( pos_oD + 7, 1 ).c_str() );
158  }
159  // Set the start coordinate of the season and episode information
160  coordofSnE[0] = 0;
161  }
162  // Check if a season pattern exists
163  else
164  {
165  // Loop through each character of the filename
166  for (i=0; i<t_oldFile.length(); i++)
167  {
168  // Check for the following pattern S##
169  if ( t_oldFile[i]=='s' && isdigit(t_oldFile[i+1]) && isdigit(t_oldFile[i+2]) )
170  {
171  // Set the start coordinate of the season and episode information
172  coordofSnE[0] = i-1;
173  // Convert the two numbers found as season information
174  season = atoi(t_oldFile.substr(i+1, 2).c_str());
175  // Exit the pattern search
176  break;
177  }
178  // Check for the following pattern S#
179  else if ( t_oldFile[i]=='s' && isdigit(t_oldFile[i+1]) )
180  {
181  // Set the start coordinate of the season and episode information
182  coordofSnE[0] = i-1;
183  // Convert the one number found as season information
184  season = atoi(t_oldFile.substr(i+1, 1).c_str());
185  // Exit the pattern search
186  break;
187  }
188  }
189  }
190 
191  // Episode information search
192 
193  // Initialize a counter
194  i = 0;
195  // Set the position to the last place
196  pos = -1;
197  // Locate the string in the file name
198  pos = t_oldFile.find( "episode" );
199  // Check if the file name contained the string
200  if( pos < t_oldFile.length() - 8 && t_oldFile.length() > 8 )
201  {
202  // Check if two numbers follow the string 'episode'
203  if( isdigit( t_oldFile.substr( pos + 8, 2 ).c_str()[1] ) )
204  {
205  // Convert the two numbers found as episode information
206  episode = atoi( t_oldFile.substr( pos + 8, 2 ).c_str() );
207  // Set the end coordinate of the season and episode information
208  coordofSnE[1] = pos+11;
209  }
210  // Check if one number follows the string 'episode'
211  else if( isdigit( t_oldFile.substr( pos + 8, 1 ).c_str()[0] ) )
212  {
213  // Convert the one number found as episode information
214  episode = atoi( t_oldFile.substr( pos + 8, 1 ).c_str() );
215  // Set the end coordinate of the season and episode information
216  coordofSnE[1] = pos+10;
217  }
218  }
219  // Check if a episode pattern exists
220  else
221  {
222  // Loop through each character of the filename
223  for (i=0; i<t_oldFile.length(); i++)
224  {
225  // Check for the following pattern ##
226  if ( isdigit(t_oldFile[i]) && isdigit(t_oldFile[i+1]) )
227  {
228  // Set the end coordinate of the season and episode information
229  coordofSnE[1] = i+3;
230  // Convert the two numbers found as episode information
231  episode = atoi(t_oldFile.substr(i, 2).c_str());
232  // Exit the pattern search
233  break;
234  }
235  // Check for the following pattern #
236  else if ( isdigit(t_oldFile[i]) )
237  {
238  // Set the end coordinate of the season and episode information
239  coordofSnE[1] = i+2;
240  // Convert the one number found as episode information
241  episode = atoi(t_oldFile.substr(i, 1).c_str());
242  // Exit the pattern search
243  break;
244  }
245  }
246  }
247 
248  }
249 
250  // If no season/episode information still matched
251  if( season == -1 || episode == -1 )
252  {
253  // Set the error flag
254  error = 1;
255  // Set the error data
257  }
258 
259  // If no season information still matched
260  if( season == -1 )
261  {
262  // Set season to a whole number value
263  season = 0;
264  // Set the end coordinate of the season and episode information
265  coordofSnE[0] = 0;
266  }
267 
268  // If no episode information still matched
269  if( episode == -1 )
270  {
271  // Set episode to a whole number value
272  episode = 0;
273  }
274 
275  // Check for errors
276  if( !error )
277  {
278  // Initialize a name holder var
279  string name;
280  // Check if s/e is found at the begining
281  if( coordofSnE[0] <= 0 )
282  {
283  // Use the directory as name
284  name = t_oldDir;
285  }
286  // If no s/e is found at the begining
287  else
288  // Capture the name, stopping where the s/e info was detected
289  name = t_oldFile.substr(0, coordofSnE[0]);
290 
291  // remove all filler
292  cleanUp( name, confOpt );
293 
294  // Check if the name matches a showname from the showlist file
295  if( !findShow( name, confOpt ) )
296  {
297  // Set the error flag
298  error = 1;
299  // Set the error data
301  }
302  }
303 
304  // Check for error
305  if( !error )
306  {
307  // Set the end coordinate of the season and episode information
308  if ( coordofSnE[1] < int( t_oldFile.length() ) )
309  {
310  // Capture the title, starting after the detected s/e info
311  title = t_oldFile.substr( coordofSnE[1] );
312  // Check if there's any data in title and if there is a black list
313  if (title.length() > 0 && !confOpt->v_blackList.empty() )
314  // Remove release group tags
315  removeTag( title, confOpt );
316  // Check if there's any data in title after tag removal
317  if (title.length() > 0)
318  // Remove all filler
319  cleanUp( title, confOpt );
320  // Check if there's any data in title after cleaning
321  if (title.length() > 0)
322  // Recapitalize
323  titleCase( title, confOpt );
324  }
325  // store the new file name
326  newName( confOpt );
327 
328  // Check if output argument is not empty
329  if( confOpt->getOptions( confOpt->OutputSourceArgument_opt ) != "" )
330  // store the new directory
331  newDir( confOpt );
332  }
333 }
334 
336 // Fetches the original directory
337 string Parser::getOldDir() const
338 {
339  return oldDir;
340 };
341 
343 // Fetches the original filename
344 string Parser::getOldFile() const
345 {
346  return oldFile;
347 };
348 
350 // Fetches the new directory
351 string Parser::getDir() const
352 {
353  return dir;
354 };
355 
357 // Fetches the new filename
358 string Parser::getShow() const
359 {
360  return show;
361 };
362 
364 // Fetches the index of showname
366 {
367  return showNameIndex;
368 };
369 
371 // Fetches the integer for season
372 int Parser::getSeason() const
373 {
374  return season;
375 };
376 
378 // Fetches the integer for episode
380 {
381  return episode;
382 };
383 
385 // Fetches the integer for extra episode info
387 {
388  return xepisode;
389 };
390 
392 // Fetches the string with episode part info
393 string Parser::getPEpisode() const
394 {
395  return pepisode;
396 };
397 
399 // Fetches the new title
400 string Parser::getTitle() const
401 {
402  return title;
403 };
404 
406 // Fetches the extension
407 string Parser::getExtension() const
408 {
409  return extension;
410 };
411 
413 // Fetches the over ride directory flag
415 {
416  return overrideDir;
417 };
418 
420 // Fetches the error flag
421 bool Parser::getError() const
422 {
423  return error;
424 };
425 
427 // Fetches the error information
429 {
430  return errorData;
431 };
void parseShow(string, ConfigOpts *)
Parses the filename.
Definition: showParser.cpp:66
string oldDir
Holds the show's original directory.
Definition: parser.hpp:63
A Show name match failure occured.
Definition: options.hpp:124
string getOldFile() const
Definition: showParser.cpp:344
void newDir(ConfigOpts *)
Generates the new directory in which the file will be located.
bool overrideDir
Override directory flag - (WTF?)
Definition: parser.hpp:74
string oldFile
Holds the show's original filename.
Definition: parser.hpp:64
void newName(ConfigOpts *)
Generates the new filename from parsed data complete with extension.
bool findShow(string &, ConfigOpts *)
Determines if the given filename contains a show name.
Definition: parseFilter.cpp:31
static void cleanUp(string &, ConfigOpts *)
Removes filler characters and extra white spaces from a string.
Definition: parseFilter.cpp:77
bool getError() const
Definition: showParser.cpp:421
string show
Holds the show's new filename.
Definition: parser.hpp:66
string getPEpisode() const
Definition: showParser.cpp:393
string pepisode
Holds the show's episode part.
Definition: parser.hpp:71
int episode
Holds the show's episode number.
Definition: parser.hpp:69
string getShow() const
Definition: showParser.cpp:358
int weight
Holds the value used for duplicate selection - (WTF?)
Definition: parser.hpp:89
ConfigOpts::errorType getErrorData() const
Definition: showParser.cpp:428
int xepisode
Holds the show's multi-episode number.
Definition: parser.hpp:70
static void removeTag(string &, ConfigOpts *)
Removes release group tags as defined in the blacklist file.
int showNameIndex
Holds the index of the showname.
Definition: parser.hpp:67
int getSeason() const
Definition: showParser.cpp:372
string extension
Holds the show's new extension.
Definition: parser.hpp:73
string dir
Holds the show's new directory.
Definition: parser.hpp:65
#define SLASH
Definition: options.hpp:32
static string getOptions(optionValue)
Retrieves the user configuration options from an array.
Definition: mainPublic.cpp:129
int getEpisode() const
Definition: showParser.cpp:379
output directory from args
Definition: options.hpp:80
~Parser()
Default destructor.
Definition: showParser.cpp:53
int getShowNameIndex() const
Definition: showParser.cpp:365
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).
string getDir() const
Definition: showParser.cpp:351
string getOldDir() const
Definition: showParser.cpp:337
ConfigOpts::errorType errorData
Stores the error type code.
Definition: parser.hpp:76
string title
Holds the show's new title.
Definition: parser.hpp:72
static void titleCase(string &, ConfigOpts *)
Standardizes capitalization of titles.
Parser()
Default constructor, yields default values.
Definition: showParser.cpp:32
bool getOverRideDir() const
Definition: showParser.cpp:414
string getTitle() const
Definition: showParser.cpp:400
A Season or Episode pattern match failure occured.
Definition: options.hpp:123
int season
Holds the show's season number.
Definition: parser.hpp:68
string getExtension() const
Definition: showParser.cpp:407
Stores all config options, settings, and flags for clerk.
Definition: options.hpp:60
static vector< string > v_blackList
A vector that holds a list of tags to be removed from each file's name.
Definition: options.hpp:141
No errors occured.
Definition: options.hpp:127
int getXEpisode() const
Definition: showParser.cpp:386
bool error
Error flag - True if an error occured.
Definition: parser.hpp:75