MovieList.cpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: MovieList.cpp * 00006 * * 00007 * * 00008 * * 00009 * ITEC institute of the University of Klagenfurt (Austria) * 00010 * http://www.itec.uni-klu.ac.at * 00011 * * 00012 * * 00013 * For more information visit the ViTooKi homepage: * 00014 * http://ViTooKi.sourceforge.net * 00015 * vitooki-user@lists.sourceforge.net * 00016 * vitooki-devel@lists.sourceforge.net * 00017 * * 00018 * This file is part of ViTooKi, a free video toolkit. * 00019 * ViTooKi is free software; you can redistribute it and/or * 00020 * modify it under the terms of the GNU General Public License * 00021 * as published by the Free Software Foundation; either version 2 * 00022 * of the License, or (at your option) any later version. * 00023 * * 00024 * This program is distributed in the hope that it will be useful, * 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00027 * GNU General Public License for more details. * 00028 * * 00029 * You should have received a copy of the GNU General Public License * 00030 * along with this program; if not, write to the Free Software * 00031 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * 00032 * MA 02111-1307, USA. * 00033 * * 00034 ***********************************************************************/ 00035 00036 /*********************************************************************** 00037 * * 00038 * REVISION HISTORY: * 00039 * * 00040 * * 00041 * * 00042 ***********************************************************************/ 00043 00044 /*********************************************************************** 00045 * Video Session Migration System, 2004 * 00046 * Author: Klaus Schoeffmann * 00047 ************************************************************************/ 00048 00049 00050 #include "MovieList.hpp" 00051 00052 00053 using namespace std; 00054 00055 vector<Movie*> MovieList::list; 00056 00057 00058 /*******************************************************/ 00059 MovieList::MovieList(Profile *profile) 00060 { 00061 this->profile = profile; 00062 } 00063 00064 /*******************************************************/ 00065 MovieList::~MovieList() 00066 { 00067 list.clear(); 00068 } 00069 00070 /*******************************************************/ 00071 void MovieList::addMovie(Movie* m) 00072 { 00073 list.push_back(m); 00074 } 00075 00076 /*******************************************************/ 00077 Movie* MovieList::getMovieById(string movieId) 00078 { 00079 vector<Movie*>::const_iterator iter = list.begin(); 00080 for (;iter != list.end(); iter++) 00081 { 00082 Movie *movie = *iter; 00083 if (movieId == movie->getId()) return movie; 00084 } 00085 //not found 00086 return NULL; 00087 } 00088 00089 /*******************************************************/ 00090 string MovieList::getDescription() 00091 { 00092 return desc; 00093 } 00094 00095 00096 /*******************************************************/ 00097 string MovieList::getHTMLRepresentation(char *imgPath, CGIParser *cgiparser, bool fakeRtspServer) 00098 { 00099 string data; 00100 uint userId = 0; 00101 uint linktype = 0; 00102 char *sphost = NULL; 00103 char *spport = NULL; 00104 if (cgiparser != NULL) 00105 { 00106 userId = atoi(cgiparser->getFieldValue("userid").c_str()); 00107 linktype = atoi(cgiparser->getFieldValue("linktype").c_str()); 00108 sphost = semifunc::newStrCpy((char*)cgiparser->getFieldValue("sphost").c_str()); 00109 spport = semifunc::newStrCpy((char*)cgiparser->getFieldValue("spport").c_str()); 00110 } 00111 00112 //create html file of all movies 00113 vector<Movie*>::const_iterator iter = list.begin(); 00114 for (;iter != list.end(); iter++) 00115 { 00116 Movie *movie = *iter; 00117 if (cgiparser != NULL) 00118 { 00119 data += movie->getHTMLRepresentation(imgPath, 00120 userId, profile->getProfileId(), 00121 linktype, profile->getWidth(), profile->getHeight(), 00122 fakeRtspServer, sphost, spport); 00123 } 00124 else 00125 { 00126 data += movie->getHTMLRepresentation(imgPath, 0, 0, 1, 0, 0, fakeRtspServer); 00127 } 00128 } 00129 00130 return data; 00131 } 00132 00133 /*******************************************************/ 00134 void MovieList::readMoviesFromFile(const char *filename) 00135 { 00136 ifstream fp (filename, ios::in); 00137 if (! fp.is_open()) 00138 { 00139 cerr << "Error opening file '" << filename << "'"; 00140 exit (1); 00141 } 00142 00143 //read file into vector, tag by tag 00144 vector<string> vec; 00145 char buffer[4095]; 00146 00147 uint pos = 0; 00148 char c; 00149 while(true) 00150 { 00151 if (fp.get(c)) 00152 { 00153 //look if a tag starts 00154 if (c == '<' && pos != 0) 00155 { 00156 //push back old buffer 00157 buffer[pos] = '\0'; 00158 pos = 0; 00159 vec.push_back(string(buffer)); 00160 buffer[pos++] = c; 00161 } 00162 else if (c == '>') 00163 { 00164 buffer[pos++] = c; 00165 buffer[pos] = '\0'; 00166 pos = 0; 00167 vec.push_back(string(buffer)); 00168 } 00169 else 00170 { 00171 buffer[pos++] = c; 00172 } 00173 } 00174 else 00175 { 00176 break; 00177 } 00178 } 00179 fp.close(); 00180 00181 00182 //parse vector 00183 string name, description, img, url; 00184 string lastId, lastCondId; 00185 Movie *movie; 00186 uint i, statement=0; 00187 for (i=0; i<vec.size(); i++) 00188 { 00189 00190 //on selection-tags, store ID 00191 if (vec[i].find("<Selection") != string::npos) 00192 { 00193 unsigned int loc1 = vec[i].find("select_id=\"") + 11; 00194 unsigned int loc2 = vec[i].find("\"", loc1); 00195 lastId = vec[i].substr(loc1, loc2-loc1); 00196 } 00197 else if (vec[i].find("<Statement") != string::npos) 00198 { 00199 //read whole content until end of statement-tag 00200 string stmt = readStatement(i, vec); 00201 //cout << "\nread statement: " << stmt; 00202 00203 //assumption: 1st statement = name of movie, 2nd = desc, and so on... 00204 if (statement++ == 0) 00205 { 00206 //cout << "\nassigning to desc = " << stmt; 00207 this->desc.assign(stmt); 00208 } 00209 else if ((statement-1) % 2 == 1) 00210 { 00211 //cout << "\nassigning to name = " << stmt; 00212 //name of movie 00213 name.assign(stmt); 00214 } 00215 else if ((statement-1) % 2 == 0) 00216 { 00217 //cout << "\nassigning to description = " << stmt; 00218 //description of movie 00219 description.assign(stmt); 00220 } 00221 } 00222 else if (vec[i].find("<Resource mimeType=\"image/jpeg\"") != string::npos) 00223 { 00224 //image of movie 00225 unsigned int loc1 = vec[i].find("ref=\"") + 5; 00226 unsigned int loc2 = vec[i].find("\"", loc1); 00227 img = vec[i].substr(loc1, loc2-loc1); 00228 00229 //create movie and store in list 00230 movie = new Movie(name, lastId, description, img, "url"); 00231 list.push_back(movie); 00232 } 00233 //on conditions: store id for later use on resource-tag 00234 else if (vec[i].find("<Condition") != string::npos) 00235 { 00236 unsigned int loc1 = vec[i].find("require=\"") + 9; 00237 unsigned int loc2 = vec[i].find("\"", loc1); 00238 lastCondId = vec[i].substr(loc1, loc2-loc1); 00239 } 00240 //on resource: add this url to corresponding movie 00241 else if (vec[i].find("<Resource") != string::npos) 00242 { 00243 if (vec[i].find("mimeType=\"video/mpeg\"") != string::npos) 00244 { 00245 unsigned int loc1 = vec[i].find("ref=\"") + 5; 00246 unsigned int loc2 = vec[i].find("\"", loc1); 00247 url = vec[i].substr(loc1, loc2-loc1); 00248 uint j; 00249 for (j=0; j<list.size(); j++) 00250 { 00251 Movie *m = list[j]; 00252 if (m->getId().compare(lastCondId) == 0) 00253 { 00254 m->setUrl(url); 00255 } 00256 } 00257 } 00258 } 00259 } 00260 } 00261 00262 /*******************************************************/ 00263 string MovieList::readStatement(uint& i, vector<string> vec) 00264 { 00265 if (vec[i].find("/>") == string::npos) 00266 { 00267 00268 //read content of statement (everything until "</Statement>" 00269 string str; 00270 i++; 00271 while (vec[i].find("</Statement>") == string::npos) 00272 { 00273 str.append(vec[i++]); 00274 } 00275 i--; 00276 return str; 00277 00278 } 00279 else 00280 { 00281 00282 return vec[i]; //return "<Statement .... />" 00283 00284 } 00285 }