All Classes Files Functions Variables Enumerations Enumerator Macros
parseFilter.cpp
Go to the documentation of this file.
1 #include "parser.hpp"
20 
30 // Matches filename from showlist
31 bool Parser::findShow( string &p_showName, ConfigOpts *confOpt )
32 {
33  // For each show in the showlist
34  for( unsigned int i = 0; i < confOpt->v_showList.size(); i++ )
35  {
36  // Find the position of the first delimeter in the showlists search terms
37  unsigned int pos = confOpt->v_showList[i][1].find( confOpt->getOptions( confOpt->ShowListDelimiter_opt )[1] );
38  // Initialize the old position to 0
39  int oPos = 0;
40  // While the show name index hasn't been set, and the position is within the search term string
41  while( pos < confOpt->v_showList[i][1].length() && showNameIndex == -1 )
42  {
43  // Set the new position to the last found delimeter
44  int nPos = pos;
45  // Save a term from between delimeters
46  string cmpStr = confOpt->v_showList[i][1].substr( oPos, nPos-oPos );
47  // Check if the term is found inside the files name
48  if ( p_showName.find(cmpStr) < p_showName.length() )
49  {
50  // check if an override directory is availible
51  if( confOpt->v_showList[i].size() == 3 )
52  {
53  // Set the override directory flag to 1
54  overrideDir = 1;
55  }
56  // Set the ShowNameIndex to the showlist matched line
57  showNameIndex = i;
58  // Return with a match found
59  return 1;
60  }
61  // Set the old position to the new position +1
62  oPos = nPos+1;
63  // Find the position of the next delimeter in the showlists search terms
64  pos = confOpt->v_showList[i][1].find( confOpt->getOptions( confOpt->ShowListDelimiter_opt )[1], pos+1 );
65  }
66  }
67 
68  // Return with no match found
69  return 0;
70 };
71 
76 // Removes filler and extra white space
77 void Parser::cleanUp( string &fillStr, ConfigOpts *confOpt )
78 {
79  // Initialize input text counter
80  unsigned int i = 0;
81  // Initialize filler character counter
82  unsigned int k = 0;
83 
84  // Create a double space character array
85  char sp[] = " ";
86 
87  // Check each character of the passed input text
88  for( ; i < fillStr.length(); i++ )
89  {
90  // Check each character against filler characters
91  for( ; k < confOpt->getOptions( confOpt->Filler_opt ).length(); k++ )
92  {
93  // If the input chacter matches a filler character
94  if ( fillStr[i] == confOpt->getOptions( confOpt->Filler_opt )[k] )
95  {
96  // Replace the input character with a single space
97  fillStr[i] = sp[0];
98  }
99  }
100  // Reset the filler counter
101  k = 0;
102  }
103 
104  // Find double whitespaces
105  unsigned int found = fillStr.find( sp );
106  // While whitespaces are found
107  while( found < fillStr.length() )
108  {
109  // Erase the first whitespace charcter
110  fillStr.erase( found, 1 );
111  // find the next double whitespace
112  found = fillStr.find( sp );
113  }
114 
115  // Check if whitespace exists at the begining of the string
116  if ( fillStr[0] == sp[0] )
117  {
118  // Remove the first character
119  fillStr = fillStr.substr( 1 );
120  }
121 
122  // Get the new length of the string
123  int l = fillStr.length();
124 
125  // Check if whitespace exists at the end of the string
126  if ( fillStr[l-1] == sp[0] )
127  {
128  // Remove the last character
129  fillStr = fillStr.substr( 0, l-1 );
130  }
131 };
132 
141 // Removes release group tags
142 void Parser::removeTag( string &tagStr, ConfigOpts *confOpt )
143 {
144  // For each tag in the blacklist
145  for( unsigned int i = 0; i < confOpt->v_blackList.size(); i++ )
146  {
147  // Search the input text for the blacklisted tag
148  unsigned int pos = tagStr.find( confOpt->v_blackList[i] );
149  // Check if the tag exists within the input text
150  if( pos < tagStr.length() )
151  {
152  // Remove the tag
153  tagStr.erase( pos, confOpt->v_blackList[i].length() );
154  }
155  }
156 };
157 
162 // Corrects capitalization
163 void Parser::titleCase( string &capStr, ConfigOpts *confOpt )
164 {
165  // Create a an array for characters that can occur after a space but before a letter
166  string sp = "\"'[({";
167 
168  // Capitalize the first character
169  capStr[0] = toupper(capStr[0]);
170 
171  // For each chacter in the input text
172  for ( unsigned int i=1; i < capStr.length(); i++ )
173  {
174  // Check for a space
175  if ( capStr[i-1] == ' ' )
176  {
177  // Check if inbetween characters follow the space
178  if ( capStr[i] == sp[0] || capStr[i] == sp[1] || capStr[i] == sp[2] || capStr[i] == sp[3] || capStr[i] == sp[4] )
179  {
180  // Capitalize the next character
181  capStr[i+1] = toupper( capStr[i+1] );
182  }
183  // Capitalize the character
184  capStr[i] = toupper( capStr[i] );
185  }
186  // Check for a hyphen
187  if ( capStr[i] == '-' )
188  {
189  // Capitalize the next character
190  capStr[i+1] = toupper( capStr[i+1] );
191  }
192  }
193 
194  // Find the first delimeter in the no capitalization word list
195  unsigned int pos = confOpt->getOptions( confOpt->LowerCaseWords_opt ).find( confOpt->getOptions( confOpt->DelimiterWords_opt )[0] );
196  // Set the old position to 0
197  int oPos = 0;
198 
199  // While the delimeter is within the no capitalization list
200  while( pos < confOpt->getOptions( confOpt->LowerCaseWords_opt ).length() )
201  {
202  // Set the new position to the last found position
203  int nPos = pos;
204  // Save the no capitalization word
205  string cmpStr = confOpt->getOptions( confOpt->LowerCaseWords_opt ).substr( oPos, nPos-oPos );
206  // Set the old position to the new position +1
207  oPos = nPos+1;
208  // Find the next delimeter in the no capitalization word list
209  pos = confOpt->getOptions( confOpt->LowerCaseWords_opt ).find( confOpt->getOptions( confOpt->DelimiterWords_opt )[0], pos+1 );
210 
211  // Find the no capitalization word in the input text
212  unsigned int textPos = capStr.find( cmpStr.c_str() );
213  // While the no capitalization word exists within the input text
214  while( textPos < capStr.length() )
215  {
216  // Replace the uppercase character with a lowercased one
217  capStr[textPos+1] = tolower( cmpStr[1] );
218  // Find the no capitalization word again in the input text
219  textPos = capStr.find( cmpStr.c_str(), textPos+1 );
220  }
221  }
222 };
223 
227 // Creates new filename
228 void Parser::newName( ConfigOpts *confOpt )
229 {
230  // Create a temporary input/output string stream
231  stringstream tempStr;
232 
233  // Save show name and space:
234  show = confOpt->v_showList[showNameIndex][0] + " ";
235  // Add an S for season info to the show name
236  show += "S";
237  // Check if the season number is less than 10
238  if( season < 10 )
239  {
240  // Add a 0 to the season info for the show name
241  show += "0";
242  }
243  // Copy the season integer to the temporary input/output string stream
244  tempStr << season;
245  // Add the season number to the show name
246  show += tempStr.str();
247  // Clear the temporary input/output string stream
248  tempStr.str( "" );
249  // Add an E for episode info to the show name
250  show += "E";
251  // Check if the episode number is less than 10
252  if( episode < 10 )
253  {
254  // Add a 0 to the episode info for the show name
255  show += "0";
256  }
257  // Copy the episode integer to the temporary input/output string stream
258  tempStr << episode;
259  // Add the episode number to the show name
260  show += tempStr.str();
261  // Clear the temporary input/output string stream
262  tempStr.str( "" );
263  // Check if extra episodes exist
264  if( xepisode > 0 )
265  {
266  // Add a hyphon separator between episode info
267  show += "-";
268  // Check if the extra episode number is less than 10
269  if( xepisode < 10 )
270  {
271  // Add a 0 to the extra episode info for the show name
272  show += "0";
273  }
274  // Copy the extra episode integer to the temporary input/output string stream
275  tempStr << xepisode;
276  // Add the extra episode number to the show name
277  show += tempStr.str();
278  // Clear the temporary input/output string stream
279  tempStr.str( "" );
280  }
281  // Check if partial episodes exist
282  if( pepisode != "" )
283  {
284  show += toupper( pepisode[0] );
285  }
286  // Check if there is a title
287  if( title.length() > 0 )
288  {
289  // Add the title and separator to the show name
290  show += confOpt->getOptions( confOpt->TitleFormat_opt ) + title;
291  }
292  // Add a period and an extension to the show name
293  show += "." + extension;
294 };
295 
302 // Creates new directory
303 void Parser::newDir( ConfigOpts *confOpt )
304 {
305  // Create a temporary input/output string stream
306  stringstream tempStr;
307  // Check if double digit flag is set
308  if( confOpt->getFlags( confOpt->SeasonDigit_flag ) )
309  {
310  // Check if season is less than 10
311  if( season < 10 )
312  {
313  // Add a zero to make the number double digit
314  tempStr << 0;
315  }
316  }
317  // Copy the season integer to the temporary input/output string stream
318  tempStr << season;
319  // Set the element to the show name element
320  int i = 0;
321  // Check if the over ride directory flag is set
322  if( overrideDir )
323  {
324  // Set the element to the over ride directory element
325  i = 2;
326  }
327  // Save the directory as the output directory / the show name or the over ride directory / season info
328  dir = confOpt->getOptions( confOpt->OutputSourceArgument_opt ) + string(SLASH) + confOpt->v_showList[ showNameIndex ][ i ] + string(SLASH) + "Season " + tempStr.str() + string(SLASH);
329 };
season is double digit
Definition: options.hpp:113
static bool getFlags(flagValue)
Retrieves the user configuration flags from an array.
Definition: mainPublic.cpp:280
void newDir(ConfigOpts *)
Generates the new directory in which the file will be located.
bool overrideDir
Override directory flag - (WTF?)
Definition: parser.hpp:74
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
string show
Holds the show's new filename.
Definition: parser.hpp:66
string pepisode
Holds the show's episode part.
Definition: parser.hpp:71
int episode
Holds the show's episode number.
Definition: parser.hpp:69
static vector< vector< string > > v_showList
A vector of vectors the will store a list of shows and a list of their episodes.
Definition: options.hpp:140
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
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
output directory from args
Definition: options.hpp:80
The Parser class creates an object for each input file and parses file and folder names...
the delimiters used to parse showlist lines
Definition: options.hpp:72
string title
Holds the show's new title.
Definition: parser.hpp:72
the delimters used to parse the no uppercased words
Definition: options.hpp:77
static void titleCase(string &, ConfigOpts *)
Standardizes capitalization of titles.
holds the season/episode and title separator
Definition: options.hpp:78
words that should not be uppercased
Definition: options.hpp:76
int season
Holds the show's season number.
Definition: parser.hpp:68
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