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

OSGBaseFunctions.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-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 //  Includes
00041 //---------------------------------------------------------------------------
00042 
00043 #include "OSGConfig.h"
00044 
00045 #include <stdlib.h>
00046 #include <stdio.h>
00047 #include <math.h>
00048 #include <locale.h>
00049 
00050 #include <string.h>
00051 
00052 
00053 #include <vector>
00054 
00055 #include "OSGBaseFunctions.h"
00056 #include "OSGThreadManager.h"
00057 #include "OSGSharedObjectHandler.h"
00058 #include "OSGFileSystem.h"
00059 
00060 #ifdef OSG_GV_BETA
00061 #include <OSGFactoryController.h>
00062 #endif
00063 
00064 OSG_BEGIN_NAMESPACE
00065 
00066 static std::vector<InitFuncF>    *osgInitFunctions       = NULL;
00067 static std::vector<ExitFuncF>    *osgSystemExitFunctions = NULL;
00068 
00069 static std::vector<InitFuncF>    *osgPreMPInitFunctions  = NULL;
00070 static std::vector<ExitFuncF>    *osgPostMPExitFunctions = NULL;
00071 
00072 static std::vector<std::string>  *osgPreloadSharedObject = NULL;
00073 
00077 OSG_BASE_DLLMAPPING 
00078 SystemState GlobalSystemState = Startup;
00079 
00080 /*---------------------------------------------------------------------*/
00090 void addInitFunction(InitFuncF initFunc)
00091 {
00092     if(osgInitFunctions == NULL)
00093     {
00094         osgInitFunctions = new std::vector<InitFuncF>(0);
00095     }
00096 
00097     osgInitFunctions->push_back(initFunc);
00098 }
00099 
00105 void addPreMPInitFunction(InitFuncF initFunc)
00106 {
00107     if(osgPreMPInitFunctions == NULL)
00108     {
00109         osgPreMPInitFunctions = new std::vector<InitFuncF>;
00110     }
00111 
00112     osgPreMPInitFunctions->push_back(initFunc);
00113 }
00114 
00116 /*---------------------------------------------------------------------*/
00125 void addSystemExitFunction(ExitFuncF exitFunc)
00126 {
00127     if(osgSystemExitFunctions == NULL)
00128     {
00129         osgSystemExitFunctions = new std::vector<ExitFuncF>(0);
00130     }
00131     
00132     osgSystemExitFunctions->push_back(exitFunc);
00133 }
00134 
00140 void addPostMPExitFunction(ExitFuncF exitFunc)
00141 {
00142     if(osgPostMPExitFunctions == NULL)
00143     {
00144         osgPostMPExitFunctions = new std::vector<ExitFuncF>;
00145     }
00146     
00147     osgPostMPExitFunctions->push_back(exitFunc);
00148 }
00149 
00151 /*---------------------------------------------------------------------*/
00158 void preloadSharedObject(const Char8 *szName)
00159 {
00160     if(osgPreloadSharedObject == NULL)
00161     {
00162         osgPreloadSharedObject = new std::vector<std::string>;
00163     }
00164 
00165     std::string tmpString(szName);
00166 
00167     osgPreloadSharedObject->push_back(tmpString);
00168 }
00169 
00173 static void osgExitWrapper(void)
00174 {
00175     osgExit();
00176 }
00177 
00181 bool osgInit(Int32, Char8 **)
00182 {
00183     // without it floats could be written as "1,456" ...
00184     setlocale(LC_NUMERIC, "English");
00185     
00186     UInt32 i;
00187     bool   returnValue = true;
00188 
00189     returnValue = SharedObjectHandler::the()->initialize();
00190 
00191     if(osgPreloadSharedObject != NULL)
00192     {
00193         for(UInt32 i = 0; i < osgPreloadSharedObject->size(); ++i)
00194         {
00195             SharedObjectHandler::the()->getOSGSharedObject(
00196                 (*osgPreloadSharedObject)[i].c_str());
00197         }
00198     }
00199 
00200     char *szEnvLibs = getenv("OSG_LOAD_LIBS");
00201 
00202     if(szEnvLibs != NULL)
00203     {
00204         std::string tmpString(szEnvLibs);
00205         string_token_iterator libIt(tmpString, ":");
00206 
00207         string_token_iterator libEnd;
00208 
00209         while(libIt != libEnd)
00210         {
00211             SharedObjectHandler::the()->getOSGSharedObject(
00212                 (*libIt).c_str());
00213 
00214             ++libIt;
00215         }
00216     }
00217 
00218     const char *szEnvPlugins       = getenv("OSG_PLUGIN_PATH");
00219     const char *szEnvPluginPattern = getenv("OSG_PLUGIN_PATTERN");
00220 
00221     if(szEnvPlugins != NULL)
00222     {
00223         if(szEnvPluginPattern == NULL)
00224         {
00225             szEnvPluginPattern = PluginPattern;
00226         }
00227 
00228         FINFO(("Get Plugins %s from %s\n", 
00229                szEnvPluginPattern,
00230                szEnvPlugins));
00231 
00232         std::vector<Char8 *> *pPlugins = 
00233             Directory::getEntries(szEnvPlugins, szEnvPluginPattern);
00234 
00235         if(pPlugins != NULL)
00236         {
00237             std::string szPluginName;
00238 
00239             for(UInt32 i = 0; i < pPlugins->size(); ++i)
00240             {
00241                 if((*pPlugins)[i][0] == '.')
00242                 {
00243                     if((*pPlugins)[i][1] == '\0')
00244                     {
00245                         continue;
00246                     }
00247                     else if((*pPlugins)[i][1] == '.' &&
00248                             (*pPlugins)[i][2] == '\0')
00249                     {
00250                         continue;
00251                     }
00252                 }
00253 
00254                 szPluginName.assign(szEnvPlugins);
00255                 szPluginName += '/';
00256                 szPluginName += (*pPlugins)[i];
00257 
00258                 SharedObjectHandler::the()->getSharedObject(
00259                     szPluginName.c_str());
00260             }
00261         }
00262 
00263         delete pPlugins;
00264     }
00265 
00266     SharedObjectHandler::the()->dump();
00267 
00268     if(returnValue == false)
00269         return returnValue;
00270 
00271     if(osgPreMPInitFunctions != NULL)
00272     {
00273         for(i = 0; i < osgPreMPInitFunctions->size(); i++)
00274         {
00275             returnValue &= (*osgPreMPInitFunctions)[i]();
00276             
00277             if(returnValue == false)
00278                 break;         
00279         }
00280     
00281         osgPreMPInitFunctions->clear();
00282     }
00283         
00284     if(returnValue == false)
00285         return returnValue;
00286 
00287     returnValue &= ThreadManager::initialize();
00288 
00289     if(returnValue == false)
00290         return returnValue;
00291 
00292     if(osgInitFunctions != NULL)
00293     {
00294         for(i = 0; i < osgInitFunctions->size(); i++)
00295         {
00296             returnValue &= (*osgInitFunctions)[i]();
00297             
00298             if(returnValue == false)
00299                 break;         
00300         }
00301 
00302         osgInitFunctions->clear();
00303     }
00304     
00305     if(returnValue == false)
00306         return returnValue;
00307 
00308 #ifdef OSG_GV_BETA
00309     returnValue &= FactoryController::the()->initialize();
00310 #endif
00311 
00312     GlobalSystemState = Running;
00313 
00314     atexit(osgExitWrapper);
00315     
00316     return returnValue;
00317 }
00318 
00322 bool osgExit(void)
00323 {
00324     bool returnValue = true;
00325 
00326     if(GlobalSystemState != Running)
00327         return true;
00328         
00329     GlobalSystemState = Shutdown;
00330 
00331 #ifdef OSG_GV_BETA
00332     returnValue &= FactoryController::the()->terminate();
00333 #endif
00334 
00335     if(osgSystemExitFunctions != NULL)
00336     {
00337         for(Int32 i = osgSystemExitFunctions->size() - 1; i >= 0; i--)
00338         {
00339             returnValue &= (*osgSystemExitFunctions)[i]();
00340             
00341             if(returnValue == false)
00342                 break;         
00343         }
00344     }
00345 
00346     returnValue &= ThreadManager::terminate();
00347 
00348     if(osgPostMPExitFunctions != NULL)
00349     {
00350         for(Int32 i = osgPostMPExitFunctions->size() - 1; i >= 0; i--)
00351         {
00352             returnValue &= (*osgPostMPExitFunctions)[i]();
00353             
00354             if(returnValue == false)
00355                 break;         
00356         }
00357     }
00358 
00359     return returnValue;
00360 }
00361 
00364 OSG_END_NAMESPACE
00365 
00366 
00367 OSG_USING_NAMESPACE
00368 
00369 
00370 InitFuncWrapper::InitFuncWrapper(const InitFuncF func)
00371 {
00372     addInitFunction(func);
00373 }
00374 
00375 
00376  
00377 StaticInitFuncWrapper::StaticInitFuncWrapper(const InitFuncF func)
00378 {
00379     func();
00380 }
00381 
00382 
00394 string_token_iterator::string_token_iterator() : _str(0), _start(0), _end(0) 
00395 {
00396 }
00397   
00398 string_token_iterator::string_token_iterator(const std::string &str, 
00399                                              const char        *separator) :
00400         _separator(separator),
00401         _str(&str),
00402         _end(0)
00403 {
00404     find_next();
00405 }
00406   
00407 string_token_iterator::string_token_iterator(const string_token_iterator & rhs) :
00408         _separator(rhs._separator),
00409         _str(rhs._str),
00410         _start(rhs._start),
00411         _end(rhs._end)
00412 {
00413 }
00414 
00415 string_token_iterator & string_token_iterator::operator++()
00416 {
00417     find_next();
00418     return *this;
00419 }
00420 
00421 string_token_iterator string_token_iterator::operator++(int)
00422 {
00423     string_token_iterator temp(*this);
00424     ++(*this);
00425     return temp;
00426 }
00427 
00428 std::string string_token_iterator::operator*() const
00429 {
00430     return std::string(*_str, _start, _end - _start);
00431 }
00432 
00433 bool string_token_iterator::operator==(const string_token_iterator & rhs) const
00434 {
00435     return (rhs._str == _str && rhs._start == _start && rhs._end == _end);
00436 }
00437 
00438 bool string_token_iterator::operator!=(const string_token_iterator & rhs) const
00439 {
00440     return !(rhs == *this);
00441 }
00442 
00443 void string_token_iterator::find_next(void)
00444 {
00445     _start = _str->find_first_not_of(_separator, _end);
00446     // Apparently some STL implementations don't do npos !?!
00447     if(_start == std::string::npos || _start >= _str->length())
00448     {
00449         _start = _end = 0;
00450         _str = 0;
00451         return;
00452     }
00453 
00454     _end = _str->find_first_of(_separator, _start);
00455 }
00456 
00457 /*-------------------------------------------------------------------------*/
00458 /*                              cvs id's                                   */
00459 
00460 #ifdef __sgi
00461 #pragma set woff 1174
00462 #endif
00463 
00464 #ifdef OSG_LINUX_ICC
00465 #pragma warning( disable : 177 )
00466 #endif
00467 
00468 namespace
00469 {
00470     static Char8 cvsid_cpp[] = "@(#)$Id: $";
00471     static Char8 cvsid_hpp[] = OSGBASEFUNCTIONS_HEADER_CVSID;
00472     static Char8 cvsid_inl[] = OSGBASEFUNCTIONS_INLINE_CVSID;
00473 }

Generated on Thu Aug 25 04:01:10 2005 for OpenSG by  doxygen 1.4.3