27 #include "../Defines.h" 
   34 #include <boost/format.hpp> 
   36 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS 
   39 #elif (TE_PLATFORM == TE_PLATFORMCODE_LINUX) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE) 
   43   #error "Platform not supported! Please, contact the TerraLib team (terralib-team@dpi.inpe.br) for helping support this platform!" 
   46 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS 
   74 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS 
   77 #elif (TE_PLATFORM == TE_PLATFORMCODE_LINUX) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE) 
   81       #error "Platform not supported! Please, contact the TerraLib team (terralib-team@dpi.inpe.br) for helping support this platform!" 
   85 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS 
   87 #elif (TE_PLATFORM == TE_PLATFORMCODE_LINUX) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE) 
   90       #error "Platform not supported! Please, contact the TerraLib team (terralib-team@dpi.inpe.br) for helping support this platform!" 
   99 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS 
  100       BOOL result = FreeLibrary((HMODULE)
m_module);
 
  105 #elif (TE_PLATFORM == TE_PLATFORMCODE_LINUX) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE) 
  106       if(dlclose(m_module))
 
  109       #error "Platform not supported! Please, contact the TerraLib team (terralib-team@dpi.inpe.br) for helping support this platform!" 
  127 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS 
  128       void* f = GetProcAddress((HMODULE)
m_module, symbol.c_str());
 
  129 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX || TE_PLATFORM == TE_PLATFORMCODE_APPLE 
  130       void* f = dlsym(m_module, symbol.c_str());
 
  132       #error "Platform not supported! Please, contact the TerraLib team for helping support this platform!" 
  136 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS 
  138 #elif (TE_PLATFORM == TE_PLATFORMCODE_LINUX) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE) 
  141       #error "Platform not supported! Please, contact terralib-team@dpi.inpe.br for helping support this platform!" 
  156   m_pImpl = 
new Impl(fileName, delayLoad);
 
  176   return m_pImpl->isLoaded();
 
  181   return m_pImpl->getFileName();
 
  186   return m_pImpl->getAddress(symbol);
 
  191 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS 
  192   std::string nativeName = name + 
".dll";
 
  194 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX 
  195   std::string nativeName = 
"lib" + name + 
".so";
 
  197 #elif TE_PLATFORM == TE_PLATFORMCODE_APPLE 
  198   std::string nativeName = 
"lib" + name + 
".dylib";
 
  201   #error "Platform not supported! Please, contact the TerraLib team (terralib-team@dpi.inpe.br) for helping support this platform!" 
  209 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS 
  210   if(d.length() > (MAX_PATH - 2))
 
  211     throw Exception((boost::format(
TE_TR(
"The DLL lookup path is too long: %1%.")) % d).str());
 
  214   BOOL retval = SetDllDirectory(d.c_str());
 
  221 #elif (TE_PLATFORM == TE_PLATFORMCODE_LINUX) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE) 
  223   const char* ldLibraryPath = getenv(
"LD_LIBRARY_PATH");
 
  225   if(ldLibraryPath == 0)
 
  228     int result = setenv(
"LD_LIBRARY_PATH", d.c_str(), 1);
 
  231       throw Exception((boost::format(
TE_TR(
"Could not create LD_LIBRARY_PATH for the application. It is not pointing to the informed dir \"%1%\".")) % d).str());
 
  236     std::string newLdLibraryPath(ldLibraryPath);
 
  237     newLdLibraryPath += 
";";
 
  238     newLdLibraryPath += d;
 
  240     int result = setenv(
"LD_LIBRARY_PATH", newLdLibraryPath.c_str(), 1);
 
  243       throw Exception((boost::format(
TE_TR(
"Couldn't add the informed dir \"%1%\" to the application's environment variable LD_LIBRARY_PATH.")) % d).str());
 
  247   throw Exception(
TE_TR(
"method: void te::common::Library::addSearchDir(const std::string& d) throw(Exception) not implemented for this platform! Contact terralib-team@dpi.inpe.br"));
 
  253 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS 
  255   BOOL retval = SetDllDirectory(
"");
 
  258     throw Exception((boost::format(
TE_TR(
"Couldn't come back with default Windows DLL lookup path due to the following error: %1%.")) % 
te::common::win::GetLastError()).str());
 
  263   throw Exception(
TE_TR(
"method: void te::common::Library::resetSearchPath() throw(Exception) not implemented for this platform! Contact terralib-team@dpi.inpe.br"));
 
  269 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS 
  270   const DWORD buffSize = 32768;
 
  274   DWORD length = GetDllDirectory(buffSize, buff);
 
  279   if(length <= buffSize)
 
  280     return std::string(buff, length);
 
  282   throw Exception(
TE_TR(
"Windows DLL lookup path too long!"));
 
  284 #elif (TE_PLATFORM == TE_PLATFORMCODE_LINUX) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE) 
  286   const char* ldLibraryPath = getenv(
"LD_LIBRARY_PATH");
 
  288   if(ldLibraryPath == 0)
 
  289     return std::string(
"");
 
  291     return std::string(ldLibraryPath);
 
  294   #warning "Verify the method: std::string te::common::Library::getSearchPath() throw(std::exception) for this platform!" 
static bool sg_addedSearchPath(false)
This flag is very important for Windows because some routines in its API returns 0 with two meanings:...
 
Impl(const std::string &fileName, bool delayLoad)
 
static void resetSearchPath()
It comes back the application lookupo path to the original state, before any addPath has been called...
 
A class for handling shared libraries. 
 
bool isLoaded() const 
It returns true if the shared library is loaded otherwise return false. 
 
void * getAddress(const std::string &symbol) const 
 
void unload()
It forces the unload of the shared library from memory. 
 
Impl * m_pImpl
A pointer to the real implementation. 
 
#define TE_TR(message)
It marks a string in order to get translated. 
 
static std::string getSearchPath()
It returns the system lookup path. 
 
static std::string getNativeName(const std::string &name)
Given a library name without file extensions, prefixes and nor suffixes it will construct a library n...
 
~Library()
The destructor will automatically unload the library from memory. 
 
void load()
It loads the shared library to memory. 
 
TECOMMONEXPORT const std::string GetLastError()
It obtains the last thread's error message in a Windows System. 
 
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
 
This class is designed for dealing with multi-language text translation in TerraLib. 
 
void * getAddress(const std::string &symbol) const 
It returns the address where the given symbol is loaded into memory. 
 
const std::string GetDlError()
It returns a human readable string describing the most recent error that occurred from dlopen()...
 
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
 
const std::string & getFileName() const 
It returns the library file name as informed in the constructor. 
 
void * m_module
The handle for DLLs, SO or a DyLib. 
 
static void addSearchDir(const std::string &d)
It adds the informed dir to the path used by the operational system to lookup for shared libraries...
 
Library(const std::string &fileName, bool delayLoad=false)
Loads a new library specified by the file name. 
 
const std::string & getFileName() const 
 
std::string m_fileName
The library file name with full path.