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
00019
00020
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
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
00042
00043
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
00059
00060
00061
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
00073
00074
00075 void
00076 Library::getNativeLibName (const char *genericName, char *nativeName)
00077 {
00078 char temp [MAXNAMELEN];
00079
00080 strcpy(temp, genericName);
00081
00082 char *filename = strrchr(temp, OS::DirectorySeparator);
00083 if (filename != NULL) {
00084
00085 *filename = '\0';
00086 filename++;
00087 sprintf (nativeName, "%s%c%s%s%s", temp, OS::DirectorySeparator, DLL_PREFIX,
00088 filename, DLL_SUFFIX);
00089
00090
00091
00092 } else {
00093
00094 sprintf (nativeName, "%s%s%s", DLL_PREFIX, genericName, DLL_SUFFIX);
00095 }
00096 }