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

Library.cpp

Go to the documentation of this file.
00001 #include "OSDependent/Library.h"
00002 #include "OSDependent/OS.h"
00003 
00004 #define DLL_PREFIX "lib"
00005 #define DLL_SUFFIX ".so"
00006 
00007 Library::Library() :
00008 _handle(NULL)
00009 {
00010 }
00011 
00012 Library::~Library()
00013 {
00014   unload();
00015 }
00016 
00017 /*
00018  * Load a library.
00019  *
00020  * Return -1 on failure, 0 on success.
00021  */
00022 int 
00023 Library::load(const char *libName, int mode)
00024 {
00025   _handle = ::dlopen(libName, mode);
00026 
00027   char fullName[MAXNAMELEN];
00028   
00029   if (_handle == NULL){
00030     // Try once more adding the standard DLL prefix and suffix for this platform.
00031     getNativeLibName(libName, fullName);
00032     
00033     _handle = ::dlopen (fullName, mode);
00034     if (_handle == NULL)
00035       return -1;
00036   }
00037   return 0;
00038 }
00039 
00040 /*
00041  * Unload the previously loaded library.
00042  *
00043  * Return -1 on failure, 0 on success.
00044  */
00045 int 
00046 Library::unload()
00047 {
00048   if (_handle == NULL)
00049     return -1;
00050   if (::dlclose(_handle) == 0) {
00051     _handle = NULL;
00052     return 0;
00053   }
00054   return -1;
00055 }
00056 
00057 /*
00058  * Get a pointer to a function from this library.
00059  *
00060  * Returns function address on success (caller must
00061  * cast to correct function signature), NULL on failure.
00062  */
00063 void *
00064 Library::getFuncPtr(const char *funcName) const
00065 {
00066   if (_handle == NULL)
00067     return NULL;
00068   return ::dlsym(_handle, funcName);
00069 }
00070 
00071 /*
00072  * Add the standard dynamic library prefix and suffix for this platform.
00073  * Returns the string containing the native library name.
00074  */
00075 void
00076 Library::getNativeLibName (const char *genericName, char *nativeName)
00077 {
00078   char temp [MAXNAMELEN];
00079   
00080   strcpy(temp, genericName);
00081   // strrchr returns a pointer to the last occurrence of a char in a string.
00082   char *filename = strrchr(temp, OS::DirectorySeparator);
00083   if (filename != NULL) {
00084     // It's a compound pathname
00085     *filename = '\0';
00086     filename++;
00087     sprintf (nativeName, "%s%c%s%s%s", temp, OS::DirectorySeparator, DLL_PREFIX,
00088              filename, DLL_SUFFIX);
00089     // Note that the method we use inside this if statement does not look into
00090     // the LD_LIBRARY_PATH, it only looks at the current directory.
00091     // To do that, we'd need somthing like the complicated ACE::ldfind() in ace/ACE.cpp.
00092   } else {
00093     // It's not compound pathname, simply add prefix and suffix.
00094     sprintf (nativeName, "%s%s%s", DLL_PREFIX, genericName, DLL_SUFFIX);
00095   }
00096 }

Generated on Mon Oct 7 09:33:29 2002 for Gaia by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001