freControllerCollector.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   F.R.E.E. - flexible registration evaluation engine
00004   Version:   v.1.0.0
00005   Date:      $Date: 2006/09/01 12:00:00 $
00006   Module:    $RCSfile: freControllerCollector.cxx,v $
00007   Language:  C++
00008 
00009 
00010 
00011   Copyright (c) 2007 Ralf o Floca (Department of Medical Informatics,
00012   Institute for Medical Biometry and Informatics, University of Heidelberg,
00013   Germany). All rights reserved.
00014   See FREECopyright.txt or http://www.mi.med.uni-hd.de/free/copyright.htm
00015   for details.
00016 
00017      This software is distributed WITHOUT ANY WARRANTY; without even 
00018      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00019      PURPOSE.  See the above copyright notices for more information.
00020 
00021 =========================================================================*/
00022 #if defined(_MSC_VER)
00023 #pragma warning ( disable : 4786 )
00024 #endif
00025 
00026 #include "freControllerCollector.h"
00027 #include "freControllerCentral.h"
00028 #include "freFileDispatch.h"
00029 
00030 #include "itkDirectory.h"
00031 #include "itkVersion.h"
00032 #include "itksys/SystemTools.hxx"
00033 
00034 namespace FREE
00035 {
00036 
00037 
00038 ControllerCollectorInfo::ControllerCollectorInfo()
00039 {
00040   m_ControllerID = "";
00041   m_ControllerStatus = ControllerCollectorInfo::CSActive;
00042   m_LibraryFile = "";
00043 };
00044 
00048 void
00049 ControllerCollector::
00050 LoadDynamicControllers()
00051 {
00055 #ifdef _WIN32
00056   char cPathSeparator = ';';
00057 #else
00058   char cPathSeparator = ':';
00059 #endif
00060   
00061   std::string sLoadPath = GetGeneralFREEPath();
00062   std::string sAutoLoadPath;
00063   
00064 #ifndef _DEBUG
00065   //Auto load path should not be added in the debug version of f.r.e.e. to avoid
00066   //conflicts with any installations of release versions
00067         if (itksys::SystemTools::GetEnv(csFREEAutoLoadPath,sAutoLoadPath)) sLoadPath += cPathSeparator+sAutoLoadPath;
00068 #endif
00069 
00070   std::string sCurrentDirectory = itksys::SystemTools::GetCurrentWorkingDirectory();
00071 
00072   if (!sCurrentDirectory.empty())
00073         {
00074                 sLoadPath += cPathSeparator+sCurrentDirectory;
00075         }
00076 
00077   if(sLoadPath.size() == 0)
00078   {
00079     return;
00080   }
00081   
00082         std::string::size_type endSeparatorPosition = 0;
00083   std::string::size_type startSeparatorPosition = 0;
00084 
00085   while ( startSeparatorPosition != std::string::npos )
00086   {
00087     startSeparatorPosition = endSeparatorPosition;
00091     endSeparatorPosition = sLoadPath.find(cPathSeparator, startSeparatorPosition);
00092 
00093     if(endSeparatorPosition == std::string::npos)
00094     {
00095       endSeparatorPosition = sLoadPath.size();
00096     }
00097 
00098     std::string CurrentPath = sLoadPath.substr(startSeparatorPosition, endSeparatorPosition-startSeparatorPosition);
00099     ControllerCollector::LoadLibrariesInPath(CurrentPath.c_str());
00100     
00104     if(endSeparatorPosition == sLoadPath.size())
00105     {
00106       startSeparatorPosition = std::string::npos;
00107     }
00108     else
00109     {
00110       endSeparatorPosition++;
00111     }
00112   }
00113 }
00114 
00115 
00122 inline bool
00123 NameIsSharedLibrary(const char* name)
00124 {
00125   std::string sname = name;
00126   if ( sname.find(itk::DynamicLoader::LibExtension()) != std::string::npos )
00127     {
00128     return true;
00129     }
00130   return false;
00131 }
00132 
00133 
00134 void
00135 ControllerCollector::
00136 LoadLibrariesInPath(const char* path)
00137 {
00138         itk::Directory::Pointer dir = itk::Directory::New();
00139         if ( !dir->Load(path) )
00140         {
00141                 return;
00142         }
00143 
00147         for ( unsigned int i = 0; i < dir->GetNumberOfFiles(); i++ )
00148         {
00149                 const char* file = dir->GetFile(i);
00154                 if ( NameIsSharedLibrary(file) )
00155                 {
00156       std::string fullpath = FileDispatch::CreateFullPath(path, file);
00157                         itk::LibHandle lib = itk::DynamicLoader::OpenLibrary(fullpath.c_str());
00158 
00159                         if ( lib )
00160                         {
00162                                 FREE_GET_CONTROLLER_LIST_FUNCTION pListFunction = (FREE_GET_CONTROLLER_LIST_FUNCTION)itk::DynamicLoader::GetSymbolAddress(lib, "FREEGetControllerList");
00163 
00164         if (pListFunction)
00165                                 { 
00166           int iCtrlCount = 0;
00167           char** pControllerIDs = 0;
00168                                         
00169           TriggerControlEvent(2,"load components ("+fullpath+")...");
00170 
00171           (*pListFunction)(iCtrlCount,pControllerIDs);
00172                                         
00173           for (int index=0; index<iCtrlCount; index++)
00174                                         {
00175             char* pControllerID = pControllerIDs[index];
00176             ControllerCollectorInfo::Pointer smpInfo = ControllerCollectorInfo::New();
00177             
00178             smpInfo->SetControllerID(std::string(pControllerID));
00179             smpInfo->SetLibraryFile(fullpath);
00180             if (this->m_Controllers.find(std::string(pControllerID)) != this->m_Controllers.end())
00181                                                 { //already loaded so ignore this controller
00182               smpInfo->SetControllerStatus(ControllerCollectorInfo::CSIgnored);
00183             }
00184             
00185             typedef std::pair<std::string,ControllerCollectorInfo::Pointer> ListPair;
00186             this->m_Controllers.insert(ListPair(smpInfo->GetControllerID(),smpInfo));
00187 
00188           };
00189 
00190                                         if (pControllerIDs) delete [] pControllerIDs;
00191                                 }
00192                         };
00193                 };
00194         };
00195 };
00196 
00197 
00201 ControllerCollector::
00202 ControllerCollector()
00203 {
00204   this->m_fnOnControl = NULL;
00205   this->m_Controllers.clear();
00206 }
00207 
00208 void
00209 ControllerCollector::
00210 SetOnControlEvent(ProgressEventBase* pOnControl)
00211 {
00212         m_fnOnControl = pOnControl;
00213 };
00214 
00215 bool
00216 ControllerCollector::
00217 TriggerControlEvent(const int iStatusID, const std::string& sComment, void* pSender, long threadID)
00218 {
00219   if (this->m_fnOnControl.IsNotNull())
00220   {
00221     m_fnOnControl->Execute(iStatusID, sComment, pSender, threadID);
00222     return true;
00223   }
00224   return false;
00225 };
00226 
00227 void
00228 ControllerCollector::
00229 Initialize()
00230 {
00231   try
00232   {
00233     this->m_Controllers.clear();
00234     this->LoadDynamicControllers();
00235   }
00236   catchAllNPassStaticMacro("Unknown Exception, while initializing free::ControllerCollector.");
00237 };
00238 
00239 } //end of namspace free

Generated at Sat Oct 13 15:39:55 2007 for f.r.e.e. - Flexible Registration and Evaluation Engine by doxygen 1.5.3 written by Dimitri van Heesch, © 1997-2000