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