Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

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

Generated on Mon Feb 3 02:34:41 2003 for libGenome by doxygen1.3-rc3