Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OSGSharedLibrary.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *           Copyright (C) 2000-2002,2002 by the OpenSG Forum                *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038 
00039 
00040 #include <stdlib.h>
00041 #include <stdio.h>
00042 
00043 #include "OSGConfig.h"
00044 
00045 #ifndef OSG_DISABLE_DEPRECATED
00046 
00047 #include "OSGSharedLibrary.h"
00048 #include "OSGBaseFunctions.h"
00049 
00050 #ifndef WIN32
00051 #include <dlfcn.h>
00052 #endif
00053 
00054 #define OSG_DLOPEN_LAZY
00055 
00056 OSG_USING_NAMESPACE
00057 
00058 Char8 SharedLibrary::_szApplicationName[] = "Application";
00059 
00060 
00061 /*--------------------------- Constructors --------------------------------*/
00062 
00063 SharedLibrary::SharedLibrary(void) :
00064     _szName (NULL   ),
00065     _pHandle(NULL   ),
00066     _type   (Invalid)
00067 {
00068 }
00069 
00070 SharedLibrary::SharedLibrary(const Char8 *szName) :
00071     _szName (NULL   ),
00072     _pHandle(NULL   ),
00073     _type   (Invalid)
00074 {
00075     if(szName == NULL)
00076     {
00077         stringDup(_szApplicationName, _szName);
00078 
00079         _type = Application;
00080     }
00081     else
00082     {
00083         stringDup(szName, _szName);
00084 
00085         _type = SharedLib;
00086     }    
00087 
00088     this->open();
00089 }
00090 
00091 /*---------------------------- Destructor ---------------------------------*/
00092 
00093 SharedLibrary::~SharedLibrary(void)
00094 {
00095     close();
00096 }
00097 
00098 /*---------------------------- Class Specific -----------------------------*/
00099 
00100 bool SharedLibrary::open(const Char8 *szName)
00101 {
00102     if(isOpen() == true)
00103     {
00104         close();
00105     }
00106 
00107     if(szName == NULL)
00108     {
00109         stringDup(_szApplicationName, _szName);
00110 
00111         _type = Application;
00112     }
00113     else
00114     {
00115         stringDup(szName, _szName);
00116 
00117         _type = SharedLib;
00118     }    
00119 
00120     return this->open();
00121 }
00122 
00123 bool SharedLibrary::close(void)
00124 {
00125     bool returnValue = false;
00126 
00127     if(_pHandle != NULL)
00128     {
00129 #ifndef WIN32
00130         returnValue = (dlclose(_pHandle) == 0);
00131 #else
00132         returnValue = (FreeLibrary(_pHandle)) != FALSE;
00133 #endif
00134         _pHandle    = NULL;
00135     }
00136 
00137     return returnValue;
00138 }
00139 
00140 AnonSymbolHandle SharedLibrary::getSymbol(const Char8 *szSymbolName)
00141 {
00142     AnonSymbolHandle returnValue = NULL;
00143 
00144     if(isOpen() == false)
00145     {
00146         if(open() == false)
00147         {
00148             return returnValue;
00149         }
00150     }
00151 
00152     if(_pHandle != NULL && szSymbolName != NULL)
00153     {
00154 #ifndef WIN32
00155         returnValue = dlsym(_pHandle, szSymbolName);
00156 #else
00157         returnValue = (void*)GetProcAddress(_pHandle, szSymbolName);
00158 #endif
00159     }
00160 
00161 #ifndef WIN32
00162     if(returnValue == NULL)
00163         fprintf(stderr, "%s\n", dlerror());
00164 #else
00165 #endif
00166 
00167     fprintf(stderr, "Got Symbol : %s %p %p\n", 
00168             szSymbolName, 
00169             _pHandle, 
00170             returnValue);
00171 
00172     return returnValue;
00173 }
00174 
00175 bool SharedLibrary::isOpen(void)
00176 {
00177     return _pHandle != NULL;
00178 }
00179 
00180 const Char8 *SharedLibrary::getName(void)
00181 {
00182     return _szName;
00183 }
00184 
00185 /*------------------------------- Helper ----------------------------------*/
00186 
00187 bool SharedLibrary::open(void)
00188 {
00189     Char8 *libName = NULL;
00190 
00191     if(_pHandle != NULL)
00192     {
00193         return true;
00194     }
00195 
00196     if(_type == SharedLib)
00197     {
00198         libName = _szName;
00199     }
00200 
00201 #ifndef WIN32
00202 #ifdef OSG_DLOPEN_LAZY
00203     _pHandle = dlopen(libName, RTLD_LAZY | RTLD_GLOBAL);
00204 #else
00205     _pHandle = dlopen(libName, RTLD_NOW  | RTLD_GLOBAL);
00206 #endif
00207 
00208     if(_pHandle == NULL)
00209     {
00210         fprintf(stderr, "%s\n", dlerror());
00211     }
00212 #else
00213     _pHandle = LoadLibrary(libName);
00214 #endif
00215 
00216     return (_pHandle != NULL);
00217 }
00218 
00219 /*-------------------------------------------------------------------------*/
00220 /*                              cvs id's                                   */
00221 
00222 #ifdef __sgi
00223 #pragma set woff 1174
00224 #endif
00225 
00226 #ifdef OSG_LINUX_ICC
00227 #pragma warning( disable : 177 )
00228 #endif
00229 
00230 namespace
00231 {
00232     static Char8 cvsid_cpp[] = "@(#)$Id: $";
00233     static Char8 cvsid_hpp[] = OSGSHAREDLIBRARY_HEADER_CVSID;
00234 }
00235 
00236 #endif

Generated on Thu Aug 25 04:09:52 2005 for OpenSG by  doxygen 1.4.3