All Classes Files Functions Variables Enumerations Enumerator Macros
library.cpp
Go to the documentation of this file.
1 
13 #include "library.hpp"
14 
17 {};
18 
21 {};
22 
37 // Add an episode, show, and season if required
38 bool Library::addEpisode( int show, int seas, int ep, int xep, string pep, string title,
39  string newfile, string newdir, string oldfile, string olddir,
40  vector<ErrorHandler> &fail, ConfigOpts *confObj )
41 {
42  // Create a temporary existance flag and set false
43  bool existsFlag = 0;
44  // Create a temporary show location holder
45  int showMarker = 0;
46  // Search through the shows vector
47  for( unsigned int i = 0; i < shows.size() && existsFlag == 0; i++ )
48  {
49  // Check if the show exists
50  if( shows[i].getIndex() == show )
51  {
52  // Set the flag to true
53  existsFlag = 1;
54  // Save the shows current location
55  showMarker = i;
56  }
57  }
58 
59  // If the show doesn't exist
60  if( !existsFlag )
61  {
62  // Add the show
63  addShow( show, confObj, fail );
64  // Search through the shows vector
65  for( unsigned int i = 0; i < shows.size() && existsFlag == 0; i++ )
66  {
67  // Find the shows location
68  if( shows[i].getIndex() == show )
69  {
70  // Set the flag to true
71  existsFlag = 1;
72  // Save the shows current location
73  showMarker = i;
74  }
75  }
76  }
77 
78  // Reset the flag
79  existsFlag = 0;
80  // Create a temporary season location holder
81  int seasonMarker = 0;
82  // Search through the seasons vector
83  for( unsigned int i = 0; i < shows[showMarker].seasons.size() && existsFlag == 0; i++ )
84  {
85  // Check if the season exists
86  if( shows[showMarker].seasons[i].getNumber() == seas )
87  {
88  // Set the flag to true
89  existsFlag = 1;
90  // Save the seasons current location
91  seasonMarker = i;
92  }
93  }
94 
95  // If the season doesn't exist
96  if( !existsFlag )
97  {
98  // Add the season
99  shows[showMarker].addSeason( seas, fail );
100  // Search through the seasons vector
101  for( unsigned int i = 0; i < shows[showMarker].seasons.size() && existsFlag == 0; i++ )
102  {
103  // Find the seasons location
104  if( shows[showMarker].seasons[i].getNumber() == seas )
105  {
106  // Set the flag to true
107  existsFlag = 1;
108  // Save the seasons current location
109  seasonMarker = i;
110  }
111  }
112  }
113 
114  // Reset the flag
115  existsFlag = 0;
116  // Search through the episodes vector
117  for( unsigned int i = 0; i < shows[showMarker].seasons[seasonMarker].episodes.size() && existsFlag == 0; i++ )
118  {
119  // Check if the episode exists
120  if( shows[showMarker].seasons[seasonMarker].episodes[i].getNumber() == ep &&
121  shows[showMarker].seasons[seasonMarker].episodes[i].getExtra() == xep &&
122  shows[showMarker].seasons[seasonMarker].episodes[i].getPart() == pep &&
123  shows[showMarker].seasons[seasonMarker].getNumber() == seas &&
124  shows[showMarker].getIndex() == show )
125  {
126  // Set the flag to true
127  existsFlag = 1;
128  // Output info about duplicate file
129  string fileError = "old - " + shows[showMarker].seasons[seasonMarker].episodes[i].getOldDir();
130  fileError += shows[showMarker].seasons[seasonMarker].episodes[i].getOldFile() + "\r\n";
131  fileError += " new - " + olddir + oldfile;
132  // Save the error to the error handler vector
133  fail.push_back ( ErrorHandler( fileError, ConfigOpts::DuplicateEpisodeError ) );
134  }
135  }
136 
137  // If the episode doesn't exist
138  if( !existsFlag )
139  {
140  // Add the episode
141  shows[showMarker].seasons[seasonMarker].addEpisode( ep, xep, pep, title, newfile, newdir, oldfile, olddir );
142  // Sort the episodes in ascending order
143  if( !shows[showMarker].seasons[seasonMarker].sortAscending() )
144  {
145  // Output info about files that do not sort
146  string fileError = "Showname : " + shows[showMarker].getShowname() + " : Season : ";
147  fileError += shows[showMarker].seasons[seasonMarker].getNumber() + " : Episode : " + ep;
148  // Save the error to the error handler vector
149  fail.push_back (ErrorHandler( fileError, ConfigOpts::SortFailureError ));
150  }
151  }
152 
153  // Return 1 = Success
154  return 1;
155 };
156 
162 bool Library::addShow( int showNameIndex, ConfigOpts *confObj, vector<ErrorHandler> &fail )
163 {
164  // Increase the show vector size
165  shows.resize( shows.size()+1 );
166  // Add the index to the last element of the show vector
167  shows[shows.size()-1].setIndex( showNameIndex );
168  // Add show name to the last element of the show vector
169  shows[shows.size()-1].setShowname( confObj->v_showList[showNameIndex][0] );
170 
171  // Sort the seasons in ascending order
172  if( !sortAscending() )
173  {
174  // Output info about files that do not sort
175  string fileError = "Showname : " + shows[shows.size()-1].getShowname();
176  // Save the error to the error handler vector
177  fail.push_back ( ErrorHandler( fileError, ConfigOpts::SortFailureError ) );
178  }
179 
180  // Return 1 = Success
181  return 1;
182 };
183 
186 // Sort shows in ascending order
188 {
189  // Create a swap flag
190  bool swapped = true;
191  // Create the initial search range
192  int rangeInt = shows.size() - 1;
193 
194  // Check if there are less than 2 shows
195  if( shows.size() < 2 )
196  {
197  // Return 1 = Success
198  return 1;
199  }
200 
201  // Bubble sort: stop when no swaps, shorten range each time
202  while( swapped )
203  {
204  // Reset condition
205  swapped = false;
206  // Make the search range smaller
207  rangeInt--;
208  // For each show
209  for( int i = 0; i < rangeInt; i++ )
210  {
211  // Check if previous season number is greater than then next season number
212  if( shows[i].getIndex() > shows[i+1].getIndex() )
213  {
214  // Create a temporary season object
215  Show tempShow;
216 
217  // Store current season info in the temp object
218  tempShow = shows[i];
219  // Store the next season as the current season
220  shows[i] = shows[i+1];
221  // Store the temp object as the next season
222  shows[i+1] = tempShow;
223 
224  // Restart the sort
225  swapped = true;
226  }
227  }
228  }
229 
230  // Return 1 = Success
231  return 1;
232 };
bool addShow(int, ConfigOpts *, vector< ErrorHandler > &)
Insert a show into the show vector.
Definition: library.cpp:162
bool addEpisode(int, int, int, int, string, string, string, string, string, string, vector< ErrorHandler > &, ConfigOpts *)
Saves the info required to describe a particular episode.
Definition: library.cpp:38
Stores all info required to describe an entire TV show.
Definition: show.hpp:31
The Library class stores all info required to describe all shows.
vector< Show > shows
A vector that holds each Show.
Definition: library.hpp:41
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
A sort failure occured.
Definition: options.hpp:126
~Library()
Default destructor.
Definition: library.cpp:20
Library()
Default constructor.
Definition: library.cpp:16
A duplicate Episode failure occured.
Definition: options.hpp:125
bool sortAscending()
Sorts the shows in ascending order.
Definition: library.cpp:187
Stores all config options, settings, and flags for clerk.
Definition: options.hpp:60
The ErrorHandler class stores an error as it occurs.
Definition: handler.hpp:17