00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifdef GN_GUI
00013
00014 #include "wx/wxprec.h"
00015
00016 #ifdef __BORLANDC__
00017 #pragma hdrstop
00018 #endif
00019
00020
00021
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_sourceClassList.insert( map< string, gnBaseSource* >::value_type(".txt", new gnRAWSource()));
00052 m_sourceClassList.insert( map< string, gnBaseSource* >::value_type(".TXT", new gnRAWSource()));
00053 m_pDefaultSourceClass = new gnFASSource();
00054 }
00055
00056 gnSourceFactory::~gnSourceFactory()
00057 {
00058 vector< gnBaseSource* >::iterator iter = m_sourceList.begin();
00059 for(; iter != m_sourceList.end(); ++iter )
00060 {
00061 delete *iter;
00062 }
00063
00064 map< string, gnBaseSource* >::iterator cIter = m_sourceClassList.begin();
00065 for(; cIter != m_sourceClassList.end(); ++cIter )
00066 {
00067 delete cIter->second;
00068 }
00069 }
00070
00071 boolean gnSourceFactory::DelSourceClass( const string& ext ){
00072 map< string, gnBaseSource* >::iterator iter = m_sourceClassList.find( ext );
00073 if( iter != m_sourceClassList.end() ){
00074 m_sourceClassList.erase( iter );
00075 return true;
00076 }
00077 return false;
00078 }
00079 gnBaseSource* gnSourceFactory::GetSourceClass( const string& ext ) const{
00080 map< string, gnBaseSource* >::const_iterator iter = m_sourceClassList.find( ext );
00081 if( iter != m_sourceClassList.end() ){
00082 return iter->second;
00083 }
00084 return m_pDefaultSourceClass;
00085 }
00086 gnBaseSource* gnSourceFactory::MatchSourceClass( const string& sourceStr ) const{
00087 string::size_type dot_loc = sourceStr.rfind('.');
00088 if(dot_loc != string::npos){
00089 string ext = sourceStr.substr(dot_loc, sourceStr.length() - dot_loc);
00090 return GetSourceClass(ext);
00091 }
00092 return m_pDefaultSourceClass;
00093 }
00094 boolean gnSourceFactory::HasSourceClass( const string& ext ) const{
00095 if( m_sourceClassList.find(ext) != m_sourceClassList.end() ){
00096 return true;
00097 }
00098 return false;
00099 }
00100 boolean gnSourceFactory::SetSourceClass( const string& ext, const gnBaseSource& source ){
00101 map< string, gnBaseSource* >::iterator iter = m_sourceClassList.find( ext );
00102 if( iter == m_sourceClassList.end() ){
00103 m_sourceClassList.insert(
00104 map< string, gnBaseSource* >::value_type( ext, source.Clone() ) );
00105 }else{
00106 iter->second = source.Clone();
00107 }
00108 return true;
00109 }
00110 boolean gnSourceFactory::AddPath( const string& path ){
00111 if( PathExists( path ) && !HasPath(path) )
00112 {
00113 m_pathList.push_back( path );
00114 return true;
00115 }
00116 return false;
00117 }
00118 boolean gnSourceFactory::DelPath( uint32 i ){
00119 if( i < m_pathList.size() ){
00120 vector<string>::iterator iter = m_pathList.begin() + i;
00121 m_pathList.erase( iter );
00122 return true;
00123 }
00124 return false;
00125 }
00126 boolean gnSourceFactory::InsPath( const string& path, uint32 i ){
00127 if( (i < m_pathList.size()) && PathExists( path ) )
00128 {
00129 vector<string>::iterator iter = m_pathList.begin() + i;
00130 m_pathList.insert( iter, path );
00131 return true;
00132 }
00133 return false;
00134 }
00135 string gnSourceFactory::GetPath( uint32 i ) const{
00136 if( i < m_pathList.size() ){
00137 return m_pathList[i];
00138 }
00139 return "";
00140 }
00141 boolean gnSourceFactory::HasPath( string path ) const{
00142 standarizePathString( path );
00143 for( uint32 i = 0; i < m_pathList.size(); ++i ){
00144 if( m_pathList[i] == path )
00145 return true;
00146 }
00147 return false;
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
00202
00203
00204 boolean gnSourceFactory::GetURL( const string& urlStr, string& localFile ){
00205 #ifdef GN_GUI
00206 wxURL m_url(urlStr.c_str());
00207 if(m_url.GetError() != wxURL_NOERR )
00208 return false;
00209 ofstream outfile(localFile.c_str(), ios::binary);
00210 if(!outfile.is_open())
00211 return false;
00212
00213 wxInputStream *in_stream = m_url.GetInputStream();
00214 char *buf = new char[BUFFER_SIZE];
00215 uint32 streamSize = in_stream->StreamSize();
00216
00217 uint32 streamPos = 0;
00218 while(streamPos < streamSize){
00219 uint32 readSize = streamSize - streamPos > BUFFER_SIZE ? streamSize - streamPos : BUFFER_SIZE;
00220 in_stream->Read(buf, readSize);
00221 if (in_stream->LastError() != wxStream_NOERROR) {
00222 delete buf;
00223 delete in_stream;
00224 return false;
00225 }
00226 outfile.write(buf, readSize);
00227 }
00228 delete buf;
00229 delete in_stream;
00230 return true;
00231 #else
00232 return false;
00233 #endif //GN_GUI
00234 }
00235
00236 gnBaseSource* gnSourceFactory::AddSource( const string& sourceStr, boolean searchPaths )
00237 {
00238 string openStr = sourceStr;
00239
00240 gnBaseSource* source = HasSource( sourceStr, false );
00241 if( source != 0 )
00242 return source;
00243
00244 gnBaseSource* newSource = MatchSourceClass( sourceStr )->Clone();
00245 if(newSource == NULL)
00246 return NULL;
00247
00248
00249 if(sourceStr.substr(0, 7) == "http://"){
00250 #ifdef GN_GUI
00251 wxString tmpfile = wxGetTempFileName("gnhttp");
00252 openStr = tmpfile.c_str();
00253 GetURL(sourceStr, openStr);
00254 #else
00255 ErrorMsg("Sorry, no HTTP support without wxWindows.\n");
00256 return NULL;
00257 #endif
00258 }else if(sourceStr.substr(0, 6) == "ftp://"){
00259 #ifdef GN_GUI
00260 wxString tmpfile = wxGetTempFileName("gnftp");
00261 openStr = tmpfile.c_str();
00262 GetURL(sourceStr, openStr);
00263 #else
00264 ErrorMsg("Sorry, no FTP support without wxWindows.\n");
00265 return NULL;
00266 #endif
00267 }else if(sourceStr.substr(0, 8) == "file:///")
00268 openStr = sourceStr.substr(8);
00269
00270
00271 try{
00272 newSource->Open( openStr );
00273 m_sourceList.push_back( newSource );
00274 return newSource;
00275 }catch(gnException& e){
00276 if(e.GetCode() != FileNotOpened()){
00277 delete newSource;
00278 e.AddCaller(__PRETTY_FUNCTION__);
00279 throw e;
00280 }
00281 }
00282
00283 if( searchPaths )
00284 {
00285
00286 string file = getFileString( openStr );
00287 vector< string >::iterator iter = m_pathList.begin();
00288 for( ; iter != m_pathList.end(); ++iter )
00289 {
00290 string newSourceStr = *iter + file;
00291
00292 source = HasSource( newSourceStr, false );
00293 if( source != 0 )
00294 {
00295 delete newSource;
00296 return source;
00297 }
00298
00299 try{
00300 newSource->Open( newSourceStr );
00301 m_sourceList.push_back( newSource );
00302 return newSource;
00303 }catch(gnException& e){
00304 if(e.GetCode() != FileNotOpened()){
00305 delete newSource;
00306 e.AddCaller(__PRETTY_FUNCTION__);
00307 throw e;
00308 }
00309 }
00310 }
00311 }
00312
00313 delete newSource;
00314 Throw_gnEx(FileNotOpened());
00315 }
00316 gnBaseSource* gnSourceFactory::GetSource( uint32 i ) const{
00317 if( i < m_sourceList.size() ){
00318 return *(m_sourceList.begin() + i);
00319 }
00320 return 0;
00321 }
00322 void gnSourceFactory::DelSource( uint32 i ){
00323 if( i >= m_sourceList.size() )
00324 Throw_gnEx(IndexOutOfBounds());
00325 vector< gnBaseSource* >::iterator iter = m_sourceList.begin() + i;
00326 gnBaseSource* source = *iter;
00327 try{
00328 source->Close();
00329 m_sourceList.erase(iter);
00330 delete source;
00331 }catch(gnException& e){
00332 e.AddCaller(__PRETTY_FUNCTION__);
00333 throw e;
00334 }
00335 }
00336
00337 boolean gnSourceFactory::DelSource( const gnBaseSource* source ){
00338 vector< gnBaseSource* >::iterator iter = m_sourceList.begin();
00339 for( ; iter != m_sourceList.end(); ++iter ){
00340 if( *iter == source ){
00341 gnBaseSource* source = *iter;
00342 try{
00343 source->Close();
00344 m_sourceList.erase(iter);
00345 delete source;
00346 return true;
00347 }catch(gnException& e){
00348 e.AddCaller(__PRETTY_FUNCTION__);
00349 throw e;
00350 }
00351 }
00352 }
00353 return false;
00354 }
00355 gnBaseSource* gnSourceFactory::HasSource( string sourceStr, boolean searchPaths ) const{
00356 standarizePathString( sourceStr );
00357 vector< gnBaseSource* >::const_iterator iter1 = m_sourceList.begin();
00358 for( ; iter1 != m_sourceList.end(); ++iter1 )
00359 {
00360 if( (*iter1)->GetOpenString() == sourceStr )
00361 return *iter1;
00362 }
00363
00364 if( searchPaths )
00365 {
00366 string file = getFileString( sourceStr );
00367 vector< string >::const_iterator iter2 = m_pathList.begin();
00368 for( ; iter2 != m_pathList.end(); ++iter2 )
00369 {
00370 iter1 = m_sourceList.begin();
00371 for( ; iter1 != m_sourceList.end(); ++iter1 )
00372 {
00373 if( (*iter1)->GetOpenString() == (*iter2 + file) )
00374 return *iter1;
00375 }
00376 }
00377 }
00378 return 0;
00379 }
00380
00381
00382 boolean gnSourceFactory::PathExists( string path ) const{
00383 standarizePathString( path );
00384 char folder[FILENAME_MAX];
00385 getcwd( folder, FILENAME_MAX );
00386
00387 if( chdir( path.c_str() ) ){
00388 return false;
00389 }
00390 chdir( folder );
00391 return true;
00392 }
00393