Changeset 210


Ignore:
Timestamp:
11/10/09 19:32:07 (2 years ago)
Author:
mickem
Message:

removed file find (replaced with boost)

Location:
trunk/service
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/service/NSClient++.cpp

    r209 r210  
    2525//#ifdef DEBUG 
    2626#include <crtdbg.h> 
     27#include "boost/filesystem.hpp"  
    2728//#endif 
    2829#endif 
     
    200201 
    201202 
     203bool is_module( std::wstring file )  
     204{ 
     205  return boost::ends_with(file, _T(".dll")); 
     206} 
    202207/** 
    203208 * Application startup point 
     
    370375      LOG_MESSAGE_STD(_T("Looking at plugins in: ") + pluginPath); 
    371376 
    372       WIN32_FIND_DATA wfd; 
    373       HANDLE hFind = FindFirstFile((pluginPath + _T("*.dll")).c_str(), &wfd); 
    374       if (hFind != INVALID_HANDLE_VALUE) { 
    375         do { 
    376           std::wstring file = wfd.cFileName; 
    377           NSCPlugin *plugin = new NSCPlugin(pluginPath + _T("\\") + file); 
    378           std::wstring name = _T("<unknown>"); 
    379           std::wstring description = _T("<unknown>"); 
    380           try { 
    381             plugin->load_dll(); 
    382             name = plugin->getName(); 
    383             description = plugin->getDescription(); 
    384           } catch(const NSPluginException& e) { 
    385             LOG_ERROR_STD(_T("Exception raised: ") + e.error_ + _T(" in module: ") + e.file_); 
    386           } catch (std::exception e) { 
    387             LOG_ERROR_STD(_T("exception loading plugin: ") + strEx::string_to_wstring(e.what())); 
    388           } catch (...) { 
    389             LOG_ERROR_STD(_T("Unknown exception loading plugin")); 
     377      boost::filesystem::wdirectory_iterator end_itr; // default construction yields past-the-end 
     378      for ( boost::filesystem::wdirectory_iterator itr( pluginPath ); itr != end_itr; ++itr ) { 
     379        if ( !is_directory(itr->status()) ) { 
     380          std::wstring file= itr->leaf(); 
     381          if (is_module(pluginPath + _T("\\") + file)) { 
     382            NSCPlugin *plugin = new NSCPlugin(pluginPath + _T("\\") + file); 
     383            std::wstring name = _T("<unknown>"); 
     384            std::wstring description = _T("<unknown>"); 
     385            try { 
     386              plugin->load_dll(); 
     387              name = plugin->getName(); 
     388              description = plugin->getDescription(); 
     389            } catch(const NSPluginException& e) { 
     390              LOG_ERROR_STD(_T("Exception raised: ") + e.error_ + _T(" in module: ") + e.file_); 
     391            } catch (std::exception e) { 
     392              LOG_ERROR_STD(_T("exception loading plugin: ") + strEx::string_to_wstring(e.what())); 
     393            } catch (...) { 
     394              LOG_ERROR_STD(_T("Unknown exception loading plugin")); 
     395            } 
     396            LOG_MESSAGE_STD(_T("* ") + name + _T(" (") + file + _T(")")); 
     397            std::list<std::wstring> list = strEx::splitEx(description, _T("\n")); 
     398            for (std::list<std::wstring>::const_iterator cit = list.begin(); cit != list.end(); ++cit) { 
     399              LOG_MESSAGE_STD(_T("    ") + *cit); 
     400            } 
    390401          } 
    391           LOG_MESSAGE_STD(_T("* ") + name + _T(" (") + file + _T(")")); 
    392           std::list<std::wstring> list = strEx::splitEx(description, _T("\n")); 
    393           for (std::list<std::wstring>::const_iterator cit = list.begin(); cit != list.end(); ++cit) { 
    394             LOG_MESSAGE_STD(_T("    ") + *cit); 
    395           } 
    396         } while (FindNextFile(hFind, &wfd)); 
    397       } else { 
    398         LOG_CRITICAL(_T("No plugin was found!")); 
    399       } 
    400       FindClose(hFind); 
     402        } 
     403      } 
     404      return false; 
    401405    } else if ( _wcsicmp( _T("version"), argv[1]+1 ) == 0 ) { 
    402406      g_bConsoleLog = true; 
     
    541545NSClientT::plugin_info_list NSClientT::get_all_plugins() { 
    542546  plugin_info_list ret; 
    543   std::wstring modPath = getBasePath() + _T("modules\\"); 
    544  
    545   WIN32_FIND_DATA wfd; 
    546   HANDLE hFind = FindFirstFile((modPath + _T("*.dll")).c_str(), &wfd); 
    547   if (hFind != INVALID_HANDLE_VALUE) { 
    548     do { 
    549       plugin_info_type info; 
    550       info.dll = wfd.cFileName; 
    551       try { 
    552         LOG_DEBUG_STD(_T("Attempting to fake load: ") + wfd.cFileName); 
    553         NSCPlugin plugin(modPath + wfd.cFileName); 
    554         plugin.load_dll(); 
    555         plugin.load_plugin(NSCAPI::dontStart); 
    556         info.name = plugin.getName(); 
    557         info.description = plugin.getDescription(); 
    558         plugin.unload(); 
    559       } catch (NSPluginException e) { 
    560         LOG_CRITICAL_STD(_T("Error loading: ") + e.file_ + _T(" root cause: ") + e.error_); 
    561       } catch (...) { 
    562         LOG_CRITICAL_STD(_T("Unknown Error loading: ") + wfd.cFileName); 
    563       } 
    564       ret.push_back(info); 
    565     } while (FindNextFile(hFind, &wfd)); 
    566   } 
    567   FindClose(hFind); 
     547  std::wstring pluginPath = getBasePath() + _T("modules\\"); 
     548  boost::filesystem::wdirectory_iterator end_itr; // default construction yields past-the-end 
     549  for ( boost::filesystem::wdirectory_iterator itr( pluginPath ); itr != end_itr; ++itr ) { 
     550    if ( !is_directory(itr->status()) ) { 
     551      std::wstring file= itr->leaf(); 
     552      if (is_module(pluginPath + _T("\\") + file)) { 
     553        plugin_info_type info; 
     554        info.dll = itr->leaf(); 
     555        try { 
     556          LOG_DEBUG_STD(_T("Attempting to fake load: ") + file); 
     557          NSCPlugin plugin(pluginPath + _T("\\") + file); 
     558          plugin.load_dll(); 
     559          plugin.load_plugin(NSCAPI::dontStart); 
     560          info.name = plugin.getName(); 
     561          info.description = plugin.getDescription(); 
     562          plugin.unload(); 
     563        } catch (NSPluginException e) { 
     564          LOG_CRITICAL_STD(_T("Error loading: ") + e.file_ + _T(" root cause: ") + e.error_); 
     565        } catch (...) { 
     566          LOG_CRITICAL_STD(_T("Unknown Error loading: ") + file); 
     567        } 
     568        ret.push_back(info); 
     569      } 
     570    } 
     571  } 
    568572  return ret; 
    569573} 
     
    573577  std::wstring modPath = getBasePath() + _T("modules\\"); 
    574578 
    575   WIN32_FIND_DATA wfd; 
    576   HANDLE hFind = FindFirstFile((modPath + _T("*.dll")).c_str(), &wfd); 
    577   if (hFind != INVALID_HANDLE_VALUE) { 
    578     do { 
    579       if (settings_manager::get_settings()->has_key(MAIN_MODULES_SECTION, wfd.cFileName)) { 
    580         if (settings_manager::get_settings()->get_string(MAIN_MODULES_SECTION, wfd.cFileName) == _T("disabled")) { 
     579  boost::filesystem::wdirectory_iterator end_itr; // default construction yields past-the-end 
     580  for ( boost::filesystem::wdirectory_iterator itr( modPath ); itr != end_itr; ++itr ) { 
     581    if ( !is_directory(itr->status()) ) { 
     582      std::wstring file= itr->leaf(); 
     583      if (is_module(modPath + _T("\\") + file)) { 
     584        if (settings_manager::get_settings()->has_key(MAIN_MODULES_SECTION, file)) { 
     585          if (settings_manager::get_settings()->get_string(MAIN_MODULES_SECTION, file) == _T("disabled")) { 
     586            try { 
     587              LOG_DEBUG_STD(_T("Attempting to fake load: ") + file); 
     588              NSCPlugin plugin(modPath + file); 
     589              plugin.load_dll(); 
     590              plugin.load_plugin(mode); 
     591              plugin.unload(); 
     592            } catch (NSPluginException e) { 
     593              LOG_CRITICAL_STD(_T("Error loading: ") + e.file_ + _T(" root cause: ") + e.error_); 
     594            } catch (...) { 
     595              LOG_CRITICAL_STD(_T("Unknown Error loading: ") + file); 
     596            } 
     597          } 
     598        } else { 
     599          std::wstring desc; 
    581600          try { 
    582             LOG_DEBUG_STD(_T("Attempting to fake load: ") + wfd.cFileName); 
    583             NSCPlugin plugin(modPath + wfd.cFileName); 
     601            NSCPlugin plugin(modPath + file); 
    584602            plugin.load_dll(); 
    585603            plugin.load_plugin(mode); 
     604            desc = plugin.getName() + _T(" - "); 
     605            desc += plugin.getDescription(); 
    586606            plugin.unload(); 
    587607          } catch (NSPluginException e) { 
     608            desc += _T("unknown module"); 
    588609            LOG_CRITICAL_STD(_T("Error loading: ") + e.file_ + _T(" root cause: ") + e.error_); 
    589610          } catch (...) { 
    590             LOG_CRITICAL_STD(_T("Unknown Error loading: ") + wfd.cFileName); 
     611            desc += _T("unknown module"); 
     612            LOG_CRITICAL_STD(_T("Unknown Error loading: ") + file); 
    591613          } 
    592         } 
    593       } else { 
    594         std::wstring desc; 
    595         try { 
    596           NSCPlugin plugin(modPath + wfd.cFileName); 
    597           plugin.load_dll(); 
    598           plugin.load_plugin(mode); 
    599           desc = plugin.getName() + _T(" - "); 
    600           desc += plugin.getDescription(); 
    601           plugin.unload(); 
    602         } catch (NSPluginException e) { 
    603           desc += _T("unknown module"); 
    604           LOG_CRITICAL_STD(_T("Error loading: ") + e.file_ + _T(" root cause: ") + e.error_); 
    605         } catch (...) { 
    606           desc += _T("unknown module"); 
    607           LOG_CRITICAL_STD(_T("Unknown Error loading: ") + wfd.cFileName); 
    608         } 
    609         settings_manager::get_core()->register_key(MAIN_MODULES_SECTION, wfd.cFileName, Settings::SettingsCore::key_string, desc, desc, _T("disabled"), false); 
    610       } 
    611     } while (FindNextFile(hFind, &wfd)); 
    612   } 
    613   FindClose(hFind); 
     614          settings_manager::get_core()->register_key(MAIN_MODULES_SECTION, file, Settings::SettingsCore::key_string, desc, desc, _T("disabled"), false); 
     615        } 
     616      } 
     617    }  
     618  } 
    614619} 
    615620 
  • trunk/service/StdAfx.h

    r207 r210  
    4747#include <memory> 
    4848 
     49#include <boost/algorithm/string.hpp> 
    4950#ifdef MEMCHECK 
    5051#include <vld.h> 
Note: See TracChangeset for help on using the changeset viewer.