33 #include <boost/format.hpp>
35 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
38 #elif (TE_PLATFORM == TE_PLATFORMCODE_LINUX) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE)
42 #error "Platform not supported! Please, contact the TerraLib team (terralib-team@dpi.inpe.br) for helping support this platform!"
45 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
46 static bool sg_addedSearchPath(
false);
73 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
76 #elif (TE_PLATFORM == TE_PLATFORMCODE_LINUX) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE)
80 #error "Platform not supported! Please, contact the TerraLib team (terralib-team@dpi.inpe.br) for helping support this platform!"
84 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
86 #elif (TE_PLATFORM == TE_PLATFORMCODE_LINUX) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE)
89 #error "Platform not supported! Please, contact the TerraLib team (terralib-team@dpi.inpe.br) for helping support this platform!"
98 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
99 BOOL result = FreeLibrary((HMODULE)
m_module);
104 #elif (TE_PLATFORM == TE_PLATFORMCODE_LINUX) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE)
105 if(dlclose(m_module))
106 throw Exception((boost::format(
TR_COMMON(
"Could not unload library: %1%, due to following error: %2%.")) %
m_fileName % te::common::lx::GetDlError()).str());
108 #error "Platform not supported! Please, contact the TerraLib team (terralib-team@dpi.inpe.br) for helping support this platform!"
126 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
127 void* f = GetProcAddress((HMODULE)
m_module, symbol.c_str());
128 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX || TE_PLATFORM == TE_PLATFORMCODE_APPLE
129 void* f = dlsym(m_module, symbol.c_str());
131 #error "Platform not supported! Please, contact the TerraLib team for helping support this platform!"
135 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
136 throw Exception((boost::format(
TR_COMMON(
"Could not find symbol: %1%, in the library %2%, due to the following error: %3%.")) % symbol %
m_fileName % te::common::win::GetLastError()).str());
137 #elif (TE_PLATFORM == TE_PLATFORMCODE_LINUX) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE)
138 throw Exception((boost::format(
TR_COMMON(
"Could not find symbol: %1%, in the library %2%, due to the following error: %3%.")) % symbol %
m_fileName % te::common::lx::GetDlError()).str());
140 #error "Platform not supported! Please, contact terralib-team@dpi.inpe.br for helping support this platform!"
155 m_pImpl =
new Impl(fileName, delayLoad);
175 return m_pImpl->isLoaded();
180 return m_pImpl->getFileName();
185 return m_pImpl->getAddress(symbol);
190 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
191 std::string nativeName = name +
".dll";
193 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX
194 std::string nativeName =
"lib" + name +
".so";
196 #elif TE_PLATFORM == TE_PLATFORMCODE_APPLE
197 std::string nativeName =
"lib" + name +
".dylib";
200 #error "Platform not supported! Please, contact the TerraLib team (terralib-team@dpi.inpe.br) for helping support this platform!"
208 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
209 if(d.length() > (MAX_PATH - 2))
210 throw Exception((boost::format(
TR_COMMON(
"The DLL lookup path is too long: %1%.")) % d).str());
213 BOOL retval = SetDllDirectory(d.c_str());
216 throw Exception((boost::format(
TR_COMMON(
"The informed dir \"%1%\" couldn't be added to the application dll lookup path due to the following error: %2%.")) % d % te::common::win::GetLastError()).str());
218 sg_addedSearchPath =
true;
220 #elif (TE_PLATFORM == TE_PLATFORMCODE_LINUX) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE)
222 const char* ldLibraryPath = getenv(
"LD_LIBRARY_PATH");
224 if(ldLibraryPath == 0)
227 int result = setenv(
"LD_LIBRARY_PATH", d.c_str(), 1);
230 throw Exception((boost::format(
TR_COMMON(
"Could not create LD_LIBRARY_PATH for the application. It is not pointing to the informed dir \"%1%\".")) % d).str());
235 std::string newLdLibraryPath(ldLibraryPath);
236 newLdLibraryPath +=
";";
237 newLdLibraryPath += d;
239 int result = setenv(
"LD_LIBRARY_PATH", newLdLibraryPath.c_str(), 1);
242 throw Exception((boost::format(
TR_COMMON(
"Couldn't add the informed dir \"%1%\" to the application's environment variable LD_LIBRARY_PATH.")) % d).str());
246 throw Exception(
TR_COMMON(
"method: void te::common::Library::addSearchDir(const std::string& d) throw(Exception) not implemented for this platform! Contact terralib-team@dpi.inpe.br"));
252 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
254 BOOL retval = SetDllDirectory(
"");
257 throw Exception((boost::format(
TR_COMMON(
"Couldn't come back with default Windows DLL lookup path due to the following error: %1%.")) % te::common::win::GetLastError()).str());
259 sg_addedSearchPath =
false;
262 throw Exception(
TR_COMMON(
"method: void te::common::Library::resetSearchPath() throw(Exception) not implemented for this platform! Contact terralib-team@dpi.inpe.br"));
268 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
269 const DWORD buffSize = 32768;
273 DWORD length = GetDllDirectory(buffSize, buff);
275 if(length == 0 && sg_addedSearchPath)
276 throw Exception((boost::format(
TR_COMMON(
"Couldn't get Windows DLL lookup path due to the following error: %1%!")) % te::common::win::GetLastError()).str());
278 if(length <= buffSize)
279 return std::string(buff, length);
281 throw Exception(
TR_COMMON(
"Windows DLL lookup path too long!"));
283 #elif (TE_PLATFORM == TE_PLATFORMCODE_LINUX) || (TE_PLATFORM == TE_PLATFORMCODE_APPLE)
285 const char* ldLibraryPath = getenv(
"LD_LIBRARY_PATH");
287 if(ldLibraryPath == 0)
288 return std::string(
"");
290 return std::string(ldLibraryPath);
293 #warning "Verify the method: std::string te::common::Library::getSearchPath() throw(std::exception) for this platform!"
const std::string & getFileName() const
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.
A class for handling shared libraries.
static std::string getSearchPath()
It returns the system lookup path.
This file is a wrapper around platform specific include files.
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...
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
std::string m_fileName
The library file name with full path.
Library(const std::string &fileName, bool delayLoad=false)
Loads a new library specified by the file name.
static void resetSearchPath()
It comes back the application lookupo path to the original state, before any addPath has been called...
Impl * m_pImpl
A pointer to the real implementation.
void unload()
It forces the unload of the shared library from memory.
#define TR_COMMON(message)
It marks a string in order to get translated. This is the mark used in the Common module of TerraLib...
~Library()
The destructor will automatically unload the library from memory.
void load()
It loads the shared library to memory.
void * m_module
The handle for DLLs, SO or a DyLib.
void * getAddress(const std::string &symbol) const
It returns the address where the given symbol is loaded into memory.
void * getAddress(const std::string &symbol) const
bool isLoaded() const
It returns true if the shared library is loaded otherwise return false.
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...
This class is designed for dealing with multi-language text translation in TerraLib.
Impl(const std::string &fileName, bool delayLoad)