src/gnSourceFactory.cpp

Go to the documentation of this file.
00001 00002 // File: gnSourceFactory.cpp 00003 // Purpose: Source Factory for all Sources 00004 // Description: 00005 // Changes: 00006 // Version: libGenome 0.5.1 00007 // Author: Aaron Darling 00008 // Modified by: 00009 // Copyright: (c) Aaron Darling 00010 // Licenses: See COPYING file for details 00012 #ifdef GN_GUI 00013 // For compilers that support precompilation, includes "wx/wx.h". 00014 #include "wx/wxprec.h" 00015 00016 #ifdef __BORLANDC__ 00017 #pragma hdrstop 00018 #endif 00019 00020 // for all others, include the necessary headers (this file is usually all you 00021 // need because it includes almost all "standard" wxWindows headers) 00022 #ifndef WX_PRECOMP 00023 #include "wx/wx.h" 00024 #endif 00025 #include "wx/url.h" 00026 #endif //GN_GUI 00027 00028 #include "gn/gnSourceFactory.h" 00029 #include "gn/gnBaseSource.h" 00030 #include "gn/gnStringTools.h" 00031 #include "gn/gnFASSource.h" 00032 #include "gn/gnDNXSource.h" 00033 #include "gn/gnGBKSource.h" 00034 #include "gn/gnSEQSource.h" 00035 #include "gn/gnRAWSource.h" 00036 #include "gn/gnException.h" 00037 #include <unistd.h> 00038 00039 gnSourceFactory::gnSourceFactory() 00040 { 00041 m_sourceClassList.insert( map< string, gnBaseSource* >::value_type(".fas", new gnFASSource())); 00042 m_sourceClassList.insert( map< string, gnBaseSource* >::value_type(".FAS", new gnFASSource())); 00043 m_sourceClassList.insert( map< string, gnBaseSource* >::value_type(".dnx", new gnDNXSource())); 00044 m_sourceClassList.insert( map< string, gnBaseSource* >::value_type(".DNX", new gnDNXSource())); 00045 m_sourceClassList.insert( map< string, gnBaseSource* >::value_type(".seq", new gnSEQSource())); 00046 m_sourceClassList.insert( map< string, gnBaseSource* >::value_type(".SEQ", new gnSEQSource())); 00047 m_sourceClassList.insert( map< string, gnBaseSource* >::value_type(".gbk", new gnGBKSource())); 00048 m_sourceClassList.insert( map< string, gnBaseSource* >::value_type(".GBK", new gnGBKSource())); 00049 m_sourceClassList.insert( map< string, gnBaseSource* >::value_type(".raw", new gnRAWSource())); 00050 m_sourceClassList.insert( map< string, gnBaseSource* >::value_type(".RAW", new gnRAWSource())); 00051 m_pDefaultSourceClass = new gnFASSource(); 00052 } 00053 00054 gnSourceFactory::~gnSourceFactory() 00055 { 00056 vector< gnBaseSource* >::iterator iter = m_sourceList.begin(); 00057 for(; iter != m_sourceList.end(); ++iter ) 00058 { 00059 delete *iter; 00060 } 00061 00062 map< string, gnBaseSource* >::iterator cIter = m_sourceClassList.begin(); 00063 for(; cIter != m_sourceClassList.end(); ++cIter ) 00064 { 00065 delete cIter->second; 00066 } 00067 } 00068 00069 boolean gnSourceFactory::DelSourceClass( const string& ext ){ 00070 map< string, gnBaseSource* >::iterator iter = m_sourceClassList.find( ext ); 00071 if( iter != m_sourceClassList.end() ){ 00072 m_sourceClassList.erase( iter ); 00073 return true; 00074 } 00075 return false; 00076 } 00077 gnBaseSource* gnSourceFactory::GetSourceClass( const string& ext ) const{ 00078 map< string, gnBaseSource* >::const_iterator iter = m_sourceClassList.find( ext ); 00079 if( iter != m_sourceClassList.end() ){ 00080 return iter->second; 00081 } 00082 return m_pDefaultSourceClass; 00083 } 00084 gnBaseSource* gnSourceFactory::MatchSourceClass( const string& sourceStr ) const{ 00085 string::size_type dot_loc = sourceStr.rfind('.'); 00086 if(dot_loc != string::npos){ 00087 string ext = sourceStr.substr(dot_loc, sourceStr.length() - dot_loc); 00088 return GetSourceClass(ext); 00089 } 00090 return m_pDefaultSourceClass; 00091 } 00092 boolean gnSourceFactory::HasSourceClass( const string& ext ) const{ 00093 if( m_sourceClassList.find(ext) != m_sourceClassList.end() ){ 00094 return true; 00095 } 00096 return false; 00097 } 00098 boolean gnSourceFactory::SetSourceClass( const string& ext, const gnBaseSource& source ){ 00099 map< string, gnBaseSource* >::iterator iter = m_sourceClassList.find( ext ); 00100 if( iter == m_sourceClassList.end() ){ 00101 m_sourceClassList.insert( 00102 map< string, gnBaseSource* >::value_type( ext, source.Clone() ) ); 00103 }else{ 00104 iter->second = source.Clone(); 00105 } 00106 return true; 00107 } 00108 boolean gnSourceFactory::AddPath( const string& path ){ 00109 if( PathExists( path ) && !HasPath(path) ) 00110 { 00111 m_pathList.push_back( path ); 00112 return true; 00113 } 00114 return false; 00115 } 00116 boolean gnSourceFactory::DelPath( uint32 i ){ 00117 if( i < m_pathList.size() ){ 00118 vector<string>::iterator iter = m_pathList.begin() + i; 00119 m_pathList.erase( iter ); 00120 return true; 00121 } 00122 return false; 00123 } 00124 boolean gnSourceFactory::InsPath( const string& path, uint32 i ){ 00125 if( (i < m_pathList.size()) && PathExists( path ) ) 00126 { 00127 vector<string>::iterator iter = m_pathList.begin() + i; 00128 m_pathList.insert( iter, path ); 00129 return true; 00130 } 00131 return false; 00132 } 00133 string gnSourceFactory::GetPath( uint32 i ) const{ 00134 if( i < m_pathList.size() ){ 00135 return m_pathList[i]; 00136 } 00137 return ""; 00138 } 00139 boolean gnSourceFactory::HasPath( string path ) const{ 00140 standarizePathString( path ); 00141 for( uint32 i = 0; i < m_pathList.size(); ++i ){ 00142 if( m_pathList[i] == path ) 00143 return true; 00144 } 00145 return false; 00146 } 00147 00148 /*boolean gnSourceFactory::FtpGet( string& urlStr, string& localFile ){ 00149 if(sourceStr.substr(0, 6) == "ftp://") 00150 openStr = urlStr.substr(6); 00151 else 00152 return false; 00153 ofstream outfile(localFile.c_str(), ios::binary); 00154 if(!outfile.is_open()) 00155 return false; 00156 00157 string username = "ftp"; 00158 string password = "ftp@ftp.com"; 00159 string::position at_pos = openStr.rfind("@"); 00160 if(at_pos = string::npos){ 00161 username = openStr.substr(0, at_pos); 00162 openStr = openStr.substr(at_pos + 1, openStr.length()); 00163 string::position colon_pos = username.find(":"); 00164 if(colon_pos = string::npos){ 00165 password = username.substr(colon_pos + 1, username.length()); 00166 username = username.substr(0, colon_pos); 00167 }else 00168 password = ""; 00169 } 00170 string::position slash_pos = openStr.find("/"); 00171 string hostname = openStr.substr(0, slash_pos); 00172 string::position slash2_pos = openStr.rfind("/"); 00173 string pathname = openStr.substr(slash_pos, slash2_pos - slash_pos); 00174 string fname = openStr.substr(slash2_pos); 00175 wxFTP m_ftp; 00176 wxInputStream *in_stream; 00177 m_ftp.Connect(hostname.c_str()); 00178 if(pathname.length() > 0) 00179 m_ftp.ChDir(pathname.c_str()); 00180 in_stream = ftp.GetInputStream(fname.c_str()); 00181 00182 char *buf = new char[BUFFER_SIZE]; 00183 uint32 streamSize = in_stream->StreamSize(); 00184 uint32 streamPos = 0; 00185 while(streamPos < streamSize){ 00186 uint32 readSize = streamSize - streamPos > BUFFER_SIZE ? streamSize - streamPos : BUFFER_SIZE; 00187 in_stream->Read(buf, readSize); 00188 if (in_stream->LastError() != wxStream_NOERROR) { // Do something. 00189 delete buf; 00190 delete in_stream; 00191 m_ftp.Close(); 00192 return false; 00193 } 00194 outfile.write(buf, readSize); 00195 } 00196 delete buf; 00197 delete in_stream; 00198 m_ftp.Close(); 00199 return true; 00200 } 00201 */ 00202 boolean gnSourceFactory::GetURL( const string& urlStr, string& localFile ){ 00203 #ifdef GN_GUI 00204 wxURL m_url(urlStr.c_str()); 00205 if(m_url.GetError() != wxURL_NOERR ) 00206 return false; 00207 ofstream outfile(localFile.c_str(), ios::binary); 00208 if(!outfile.is_open()) 00209 return false; 00210 00211 wxInputStream *in_stream = m_url.GetInputStream(); 00212 char *buf = new char[BUFFER_SIZE]; 00213 uint32 streamSize = in_stream->StreamSize(); 00214 00215 uint32 streamPos = 0; 00216 while(streamPos < streamSize){ 00217 uint32 readSize = streamSize - streamPos > BUFFER_SIZE ? streamSize - streamPos : BUFFER_SIZE; 00218 in_stream->Read(buf, readSize); 00219 if (in_stream->LastError() != wxStream_NOERROR) { // Do something. 00220 delete buf; 00221 delete in_stream; 00222 return false; 00223 } 00224 outfile.write(buf, readSize); 00225 } 00226 delete buf; 00227 delete in_stream; 00228 return true; 00229 #else 00230 return false; 00231 #endif //GN_GUI 00232 } 00233 // Sources 00234 gnBaseSource* gnSourceFactory::AddSource( const string& sourceStr, boolean searchPaths ) 00235 { 00236 string openStr = sourceStr; 00237 // Check if Exists 00238 gnBaseSource* source = HasSource( sourceStr, false ); 00239 if( source != 0 ) 00240 return source; 00241 // Check if File Present and valid 00242 gnBaseSource* newSource = MatchSourceClass( sourceStr )->Clone(); 00243 if(newSource == NULL) 00244 return NULL; 00245 00246 // Check the URL to see if we need to cache the file locally 00247 if(sourceStr.substr(0, 7) == "http://"){ 00248 #ifdef GN_GUI 00249 wxString tmpfile = wxGetTempFileName("gnhttp"); 00250 openStr = tmpfile.c_str(); 00251 GetURL(sourceStr, openStr); 00252 #else 00253 ErrorMsg("Sorry, no HTTP support without wxWindows.\n"); 00254 return NULL; 00255 #endif 00256 }else if(sourceStr.substr(0, 6) == "ftp://"){ 00257 #ifdef GN_GUI 00258 wxString tmpfile = wxGetTempFileName("gnftp"); 00259 openStr = tmpfile.c_str(); 00260 GetURL(sourceStr, openStr); 00261 #else 00262 ErrorMsg("Sorry, no FTP support without wxWindows.\n"); 00263 return NULL; 00264 #endif 00265 }else if(sourceStr.substr(0, 8) == "file:///") 00266 openStr = sourceStr.substr(8); 00267 00268 // Now try to open the local file 00269 try{ 00270 newSource->Open( openStr ); 00271 m_sourceList.push_back( newSource ); 00272 return newSource; 00273 }catch(gnException& e){ 00274 if(e.GetCode() != FileNotOpened()){ 00275 delete newSource; 00276 e.AddCaller(__PRETTY_FUNCTION__); 00277 throw e; 00278 } 00279 } 00280 00281 if( searchPaths ) 00282 { 00283 // Check other paths for Exists or Presents/valid 00284 string file = getFileString( openStr ); 00285 vector< string >::iterator iter = m_pathList.begin(); 00286 for( ; iter != m_pathList.end(); ++iter ) 00287 { 00288 string newSourceStr = *iter + file; 00289 // Check if Exists 00290 source = HasSource( newSourceStr, false ); 00291 if( source != 0 ) 00292 { 00293 delete newSource; 00294 return source; 00295 } 00296 // Check if File Present and valid 00297 try{ 00298 newSource->Open( newSourceStr ); 00299 m_sourceList.push_back( newSource ); 00300 return newSource; 00301 }catch(gnException& e){ 00302 if(e.GetCode() != FileNotOpened()){ 00303 delete newSource; 00304 e.AddCaller(__PRETTY_FUNCTION__); 00305 throw e; 00306 } 00307 } 00308 } 00309 } 00310 // Cannot find a valid source 00311 delete newSource; 00312 Throw_gnEx(FileNotOpened()); 00313 } 00314 gnBaseSource* gnSourceFactory::GetSource( uint32 i ) const{ 00315 if( i < m_sourceList.size() ){ 00316 return *(m_sourceList.begin() + i); 00317 } 00318 return 0; 00319 } 00320 void gnSourceFactory::DelSource( uint32 i ){ 00321 if( i >= m_sourceList.size() ) 00322 Throw_gnEx(IndexOutOfBounds()); 00323 vector< gnBaseSource* >::iterator iter = m_sourceList.begin() + i; 00324 gnBaseSource* source = *iter; 00325 try{ 00326 source->Close(); 00327 m_sourceList.erase(iter); 00328 delete source; 00329 }catch(gnException& e){ 00330 e.AddCaller(__PRETTY_FUNCTION__); 00331 throw e; 00332 } 00333 } 00334 00335 boolean gnSourceFactory::DelSource( const gnBaseSource* source ){ 00336 vector< gnBaseSource* >::iterator iter = m_sourceList.begin(); 00337 for( ; iter != m_sourceList.end(); ++iter ){ 00338 if( *iter == source ){ 00339 gnBaseSource* source = *iter; 00340 try{ 00341 source->Close(); 00342 m_sourceList.erase(iter); 00343 delete source; 00344 return true; 00345 }catch(gnException& e){ 00346 e.AddCaller(__PRETTY_FUNCTION__); 00347 throw e; 00348 } 00349 } 00350 } 00351 return false; 00352 } 00353 gnBaseSource* gnSourceFactory::HasSource( string sourceStr, boolean searchPaths ) const{ 00354 standarizePathString( sourceStr ); 00355 vector< gnBaseSource* >::const_iterator iter1 = m_sourceList.begin(); 00356 for( ; iter1 != m_sourceList.end(); ++iter1 ) 00357 { 00358 if( (*iter1)->GetOpenString() == sourceStr ) 00359 return *iter1; 00360 } 00361 00362 if( searchPaths ) 00363 { 00364 string file = getFileString( sourceStr ); 00365 vector< string >::const_iterator iter2 = m_pathList.begin(); 00366 for( ; iter2 != m_pathList.end(); ++iter2 ) 00367 { 00368 iter1 = m_sourceList.begin(); 00369 for( ; iter1 != m_sourceList.end(); ++iter1 ) 00370 { 00371 if( (*iter1)->GetOpenString() == (*iter2 + file) ) 00372 return *iter1; 00373 } 00374 } 00375 } 00376 return 0; 00377 } 00378 00379 // private: 00380 boolean gnSourceFactory::PathExists( string path ) const{ 00381 standarizePathString( path ); 00382 char folder[FILENAME_MAX]; 00383 getcwd( folder, FILENAME_MAX ); 00384 00385 if( chdir( path.c_str() ) ){ 00386 return false; 00387 } 00388 chdir( folder ); 00389 return true; 00390 } 00391

Generated on Mon Feb 14 19:28:20 2005 for libGenome by doxygen 1.3.8