M3SMetaInfo.cpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: M3SMetaInfo.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 * Author: Klaus Schoeffmann * 00038 * REVISION HISTORY: * 00039 * * 00040 * * 00041 * * 00042 ***********************************************************************/ 00043 00044 #include "M3SMetaInfo.hpp" 00045 #include "global.hpp" 00046 00047 #include <stdio.h> 00048 00049 00050 /***********************************************************************/ 00051 M3SMetaInfo::M3SMetaInfo() { 00052 initialize(); 00053 } 00054 00055 /***********************************************************************/ 00056 M3SMetaInfo::~M3SMetaInfo() { 00057 if (videoFilepath != NULL) delete videoFilepath; 00058 if (audioFilepath != NULL) delete audioFilepath; 00059 if (http != NULL) delete http; 00060 } 00061 00062 /***********************************************************************/ 00063 void M3SMetaInfo::initialize() { 00064 length = 0; 00065 00066 videoEnabled = false; 00067 videoFilepath = NULL; 00068 fps = 0; 00069 videoKbps = 0; 00070 width = 0; 00071 height = 0; 00072 00073 audioEnabled = false; 00074 audioFilepath = NULL; 00075 audioKbps = 0; 00076 numAudioChannels = 0; 00077 sampleRate = 0; 00078 http = NULL; 00079 } 00080 00081 /***********************************************************************/ 00082 char *M3SMetaInfo::findParam(char *line, char *name, int *len) { 00083 char *pos = strstr(line, name); 00084 *len = 0; 00085 if (pos != NULL) { 00086 while (*pos != '=') pos++; 00087 while (*pos == ' ' || *pos == '=') pos++; 00088 char *tmppos = pos; 00089 while (*tmppos != '\r' && *tmppos != '\n') { 00090 tmppos++; 00091 (*len)++; 00092 } 00093 } 00094 return pos; 00095 } 00096 00097 /***********************************************************************/ 00098 bool M3SMetaInfo::loadFromURL(const char *url) { 00099 dprintf_full("M3SMetaInfo::loadFromURL() starts...\n"); 00100 00101 http = new HttpRequest(url); 00102 00103 if (http->makeHttpRequest()) { 00104 00105 int contentLength = http->getContentLength(); 00106 00107 char *file = new char[contentLength]; //buffer for file-download 00108 u32 offset = 0; 00109 int bytesReceived = 0; 00110 00111 while ((bytesReceived = http->continueReceive(file+offset, contentLength-offset)) != -1) { 00112 offset += bytesReceived; 00113 } 00114 00115 http->closeSock(); 00116 00117 //parse content 00118 parseFileContent(file, contentLength); 00119 delete [] file; 00120 00121 return true; 00122 } 00123 return false; 00124 } 00125 00126 00127 /***********************************************************************/ 00128 int M3SMetaInfo::readLine(char **line, char *content, u32 &offset, u32 contentLength) 00129 { 00130 int linelen = 0; 00131 *line = new char[LINE_SIZE]; 00132 00133 while (offset < contentLength && content[offset] != '\r' && content[offset] != '\n') { 00134 (*line)[linelen++] = content[offset++]; 00135 } 00136 if (offset < contentLength && content[offset] == '\r') (*line)[linelen++] = content[offset++]; 00137 if (offset < contentLength && content[offset] == '\n') (*line)[linelen++] = content[offset++]; 00138 00139 if (linelen == 0) delete [] (*line); 00140 00141 return linelen; 00142 } 00143 00144 /***********************************************************************/ 00145 void M3SMetaInfo::parseFileContent(char *content, u32 contentLength) 00146 { 00147 int n = 0; 00148 u32 offset = 0; 00149 char *line = NULL; 00150 00151 char *buffer; 00152 int paramlen; 00153 SECTION sect = M3SMetaInfo::none; 00154 00155 //read line by line until end of file 00156 while ((n = readLine(&line, content, offset, contentLength)) > 0) 00157 { 00158 00159 if (line[0] == '#') 00160 { 00161 //ignore comments 00162 } 00163 else if (strncmp(line, "[General]",strlen("[General]")) == 0) { 00164 sect = M3SMetaInfo::general; 00165 } 00166 else if (strncmp(line, "[Video]",strlen("[Video]")) == 0) { 00167 sect = M3SMetaInfo::video; 00168 } 00169 else if (strncmp(line, "[Audio]",strlen("[Audio]")) == 0) { 00170 sect = M3SMetaInfo::audio; 00171 } 00172 00173 else if (sect == M3SMetaInfo::general && (buffer = findParam(line, "length", &paramlen)) != NULL) { 00174 length = (int)ceil(atof(buffer)); 00175 dprintf_full("length=%d\n", length); 00176 } 00177 00178 00179 else if (sect == M3SMetaInfo::video && (buffer = findParam(line, "enable", &paramlen)) != NULL) { 00180 if (strncmp(buffer, "yes", 3) == 0) videoEnabled = true; 00181 dprintf_full("videoEnabled=%s\n", videoEnabled==true?"yes":"no"); 00182 } 00183 else if (sect == M3SMetaInfo::video && (buffer = findParam(line, "filepath", &paramlen)) != NULL) { 00184 videoFilepath = new char [paramlen + 2]; 00185 strncpy(videoFilepath, buffer, paramlen); 00186 videoFilepath[paramlen] = '\0'; 00187 dprintf_full("videoFilepath=%s\n", videoFilepath); 00188 } 00189 else if (sect == M3SMetaInfo::video && (buffer = findParam(line, "fps", &paramlen)) != NULL) { 00190 fps = atoi(buffer); 00191 dprintf_full("fps=%d\n", fps); 00192 } 00193 else if (sect == M3SMetaInfo::video && (buffer = findParam(line, "kbps", &paramlen)) != NULL) { 00194 videoKbps = atoi(buffer); 00195 dprintf_full("videokbps=%d\n", videoKbps); 00196 } 00197 else if (sect == M3SMetaInfo::video && (buffer = findParam(line, "width", &paramlen)) != NULL) { 00198 width = atoi(buffer); 00199 dprintf_full("width=%d\n", width); 00200 } 00201 else if (sect == M3SMetaInfo::video && (buffer = findParam(line, "height", &paramlen)) != NULL) { 00202 height = atoi(buffer); 00203 dprintf_full("height=%d\n", height); 00204 } 00205 00206 00207 else if (sect == M3SMetaInfo::audio && (buffer = findParam(line, "enable", &paramlen)) != NULL) { 00208 if (strncmp(buffer, "yes", 3) == 0) audioEnabled = true; 00209 dprintf_full("audioEnabled=%s\n", audioEnabled==true?"yes":"no"); 00210 } 00211 else if (sect == M3SMetaInfo::audio && (buffer = findParam(line, "filepath", &paramlen)) != NULL) { 00212 audioFilepath = new char [paramlen + 2]; 00213 strncpy(audioFilepath, buffer, paramlen); 00214 audioFilepath[paramlen] = '\0'; 00215 dprintf_full("audioFilepath=%s\n", audioFilepath); 00216 } 00217 else if (sect == M3SMetaInfo::audio && (buffer = findParam(line, "kbps", &paramlen)) != NULL) { 00218 audioKbps = atoi(buffer); 00219 dprintf_full("audiokbps=%d\n", audioKbps); 00220 } 00221 else if (sect == M3SMetaInfo::audio && (buffer = findParam(line, "numchannels", &paramlen)) != NULL) { 00222 numAudioChannels = atoi(buffer); 00223 dprintf_full("numchannels=%d\n", numAudioChannels); 00224 } 00225 else if (sect == M3SMetaInfo::audio && (buffer = findParam(line, "samplerate", &paramlen)) != NULL) { 00226 sampleRate = atoi(buffer); 00227 dprintf_full("samplerate=%d\n", sampleRate); 00228 } 00229 00230 else 00231 { 00232 //Unrecognized parameter ignored 00233 } 00234 00235 } //while 00236 00237 } 00238 00239 /***********************************************************************/ 00240 bool M3SMetaInfo::loadFromFile(const char *filename) 00241 { 00242 00243 dprintf_full("M3SMetaInfo::loadFromFile() starts..\n"); 00244 00245 //open file of users 00246 FILE *file = fopen(filename, "r"); 00247 if (!file) 00248 { 00249 dprintf_err_fatal("M3SMetaInfo::loadFromFile() error opening file %s\n", filename); 00250 return false; 00251 } 00252 00253 //get file size 00254 fseek(file, 0, SEEK_END); 00255 u32 len = ftell(file); 00256 fseek(file, 0, SEEK_SET); 00257 00258 //read content of file 00259 char *fileContent = new char[len]; 00260 fread(fileContent, sizeof(char), len, file); 00261 fclose(file); 00262 00263 //parse content 00264 parseFileContent(fileContent, len); 00265 00266 delete [] fileContent; 00267 00268 dprintf_full("M3SMetaInfo::loadFromFile() succeeded!\n"); 00269 return true; 00270 } 00271 00272 00273 /***********************************************************************/ 00274 void M3SMetaInfo::setVideoFilepath(char *videoFilepath) 00275 { 00276 if (this->videoFilepath != NULL) delete [] this->videoFilepath; 00277 00278 this->videoFilepath = new char[strlen(videoFilepath) + 1]; 00279 strcpy(this->videoFilepath, videoFilepath); 00280 } 00281 00282 /***********************************************************************/ 00283 void M3SMetaInfo::setAudioFilepath(char *audioFilepath) 00284 { 00285 if (this->audioFilepath != NULL) delete [] this->audioFilepath; 00286 00287 this->audioFilepath = new char[strlen(audioFilepath) + 1]; 00288 strcpy(this->audioFilepath, audioFilepath); 00289 } 00290 00291 /***********************************************************************/ 00292 void M3SMetaInfo::appendVideoFilepath(int paramSize, char *prefix, int len) 00293 { 00294 00295 if ((signed)(len + strlen(getVideoFilepath())) >= paramSize) { 00296 dprintf_err_fatal("M3SMetaInfo::appendVideoFilepath() cannot change video-filepath - too long! (maximum length=%d)\n",paramSize); 00297 } 00298 00299 char *tmp = new char[paramSize]; 00300 strncpy(tmp, prefix, len); 00301 tmp[len] = '\0'; 00302 strcat(tmp, getVideoFilepath()); 00303 setVideoFilepath(tmp); 00304 delete [] tmp; 00305 dprintf_full("NEW VIDEO Filepath = %s\n", getVideoFilepath()); 00306 00307 } 00308 00309 /***********************************************************************/ 00310 void M3SMetaInfo::appendAudioFilepath(int paramSize, char *prefix, int len) 00311 { 00312 00313 if ((signed)(len + strlen(getAudioFilepath())) >= paramSize) { 00314 dprintf_err_fatal("M3SMetaInfo::appendAudioFilepath() cannot change audio-filepath - too long! (maximum length=%d)\n",paramSize); 00315 } 00316 00317 char *tmp = new char[paramSize]; 00318 strncpy(tmp, prefix, len); 00319 tmp[len] = '\0'; 00320 strcat(tmp, getAudioFilepath()); 00321 setAudioFilepath(tmp); 00322 delete [] tmp; 00323 dprintf_full("NEW AUDIO Filepath = %s\n", getAudioFilepath()); 00324 00325 }