00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00013
00014
00015 #include "wx/wxprec.h"
00016
00017 #ifdef __BORLANDC__
00018 #pragma hdrstop
00019 #endif
00020
00021
00022
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
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
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) {
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
00233 gnBaseSource* gnSourceFactory::AddSource( const string& sourceStr, boolean searchPaths )
00234 {
00235 string openStr = sourceStr;
00236
00237 gnBaseSource* source = HasSource( sourceStr, false );
00238 if( source != 0 )
00239 return source;
00240
00241 gnBaseSource* newSource = MatchSourceClass( sourceStr )->Clone();
00242 if(newSource == NULL)
00243 return NULL;
00244
00245
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
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
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
00289 source = HasSource( newSourceStr, false );
00290 if( source != 0 )
00291 {
00292 delete newSource;
00293 return source;
00294 }
00295
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
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
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