OpenWalnut  1.4.0
WPathHelper.cpp
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include <string>
00026 #include <vector>
00027 #include <cstdlib>
00028 #include <algorithm>
00029 
00030 #include <boost/tokenizer.hpp>
00031 
00032 #include "WPathHelper.h"
00033 
00034 // path helper instance as singleton
00035 boost::shared_ptr< WPathHelper > WPathHelper::m_instance = boost::shared_ptr< WPathHelper >();
00036 
00037 WPathHelper::WPathHelper()
00038 {
00039     // initialize members
00040 }
00041 
00042 WPathHelper::~WPathHelper()
00043 {
00044     // cleanup
00045 }
00046 
00047 boost::shared_ptr< WPathHelper > WPathHelper::getPathHelper()
00048 {
00049     if( !m_instance )
00050     {
00051         m_instance = boost::shared_ptr< WPathHelper >( new WPathHelper() );
00052     }
00053 
00054     return m_instance;
00055 }
00056 
00057 void WPathHelper::setBasePaths( boost::filesystem::path appPath, boost::filesystem::path homePath )
00058 {
00059     m_appPath    = appPath;
00060     m_homePath   = homePath;
00061     m_sharePath  = m_appPath / "../share/openwalnut";
00062     m_docPath    = m_appPath / "../share/doc";
00063     m_configPath = m_appPath / "../share/openwalnut";
00064     m_libPath    = m_appPath / "../lib";
00065     m_modulePath = m_libPath / "openwalnut";
00066 
00067     // this is the relative path for module resources. It is relative to the path of the lib containing the module.
00068     // Our build system places modules in lib/openwalnut/packagename/. The relative path needs to go out of the lib directory to a share
00069     // directory.
00070     m_moduleResourcePathRelative = boost::filesystem::path( "../../../share/openwalnut/modules" );
00071 }
00072 
00073 void WPathHelper::setBasePathsOSXBundle( boost::filesystem::path appPath, boost::filesystem::path homePath )
00074 {
00075     //W_ASSERT( appPath.substr( size()-sizeof( "MacOS" ), sizeof( "MacOS" ) ) == "MacOS" );
00076     m_appPath    = appPath;
00077     m_homePath   = homePath;
00078     m_sharePath  = m_appPath / "../Resources/openwalnut";
00079     m_docPath    = m_appPath / "../Resources/doc";
00080     m_configPath = m_appPath / "../Resources/openwalnut";
00081     m_libPath    = m_appPath / "../lib"; // TODO(mario): what is this for?
00082     m_modulePath = m_appPath / "../Resources/modules";
00083 
00084     // this is the relative path for module resources. It is relative to the path of the lib containing the module.
00085     // The MacOSX bundle stores the modules in Resources/modules. We want the additional resources to be stored in the module's directory.
00086     m_moduleResourcePathRelative = boost::filesystem::path( "." );
00087 }
00088 
00089 boost::filesystem::path WPathHelper::getAppPath()
00090 {
00091     return getPathHelper()->m_appPath;
00092 }
00093 
00094 boost::filesystem::path WPathHelper::getFontPath()
00095 {
00096     return getPathHelper()->m_sharePath / "fonts";
00097 }
00098 
00099 boost::filesystem::path WPathHelper::getShaderPath()
00100 {
00101     return getPathHelper()->m_sharePath / "shaders";
00102 }
00103 
00104 WPathHelper::Fonts WPathHelper::getAllFonts()
00105 {
00106     Fonts fonts;
00107     fonts.Regular   = getFontPath() / "Regular.ttf";
00108     fonts.Bold      = getFontPath() / "Bold.ttf";
00109     fonts.Italic    = getFontPath() / "Italic.ttf";
00110     fonts.Default   = fonts.Bold;
00111 
00112     return fonts;
00113 }
00114 
00115 boost::filesystem::path WPathHelper::getModulePath()
00116 {
00117     return getPathHelper()->m_modulePath;
00118 }
00119 
00120 boost::filesystem::path WPathHelper::getHomePath()
00121 {
00122     return getPathHelper()->m_homePath;
00123 }
00124 
00125 boost::filesystem::path WPathHelper::getLibPath()
00126 {
00127     return getPathHelper()->m_libPath;
00128 }
00129 
00130 boost::filesystem::path WPathHelper::getSharePath()
00131 {
00132     return getPathHelper()->m_sharePath;
00133 }
00134 
00135 boost::filesystem::path WPathHelper::getDocPath()
00136 {
00137     return getPathHelper()->m_docPath;
00138 }
00139 
00140 boost::filesystem::path WPathHelper::getConfigPath()
00141 {
00142     return getPathHelper()->m_configPath;
00143 }
00144 
00145 boost::filesystem::path WPathHelper::getModuleResourcePath( boost::filesystem::path moduleLibPath, std::string packageName )
00146 {
00147     // relative path to the resources
00148     boost::filesystem::path resRel = getPathHelper()->m_moduleResourcePathRelative / packageName;
00149 
00150     // return absolute paths
00151     return moduleLibPath / resRel;
00152 }
00153 
00154 std::vector< boost::filesystem::path > WPathHelper::getAllModulePaths()
00155 {
00156     // the list of paths
00157     std::vector< boost::filesystem::path > paths;
00158     // the first element always is the global search path
00159     paths.push_back( getModulePath() );
00160     paths.push_back( getHomePath() / "modules" );
00161 
00162     // the environment variable stores the additional paths
00163     std::string additionalPaths( getenv( "OW_MODULE_PATH" ) ? getenv( "OW_MODULE_PATH" ) : "" );
00164 
00165     // separate list of additional paths:
00166     typedef boost::tokenizer< boost::char_separator< char > > tokenizer;
00167     boost::char_separator< char > sep( ";" );
00168     tokenizer tok( additionalPaths, sep );
00169     for( tokenizer::iterator it = tok.begin(); it != tok.end(); ++it )
00170     {
00171         paths.push_back( boost::filesystem::path( *it ) );
00172     }
00173 
00174     // add the additional paths
00175     for( std::vector< boost::filesystem::path >::const_iterator it = getPathHelper()->m_additionalModulePaths.begin();
00176                                                                 it != getPathHelper()->m_additionalModulePaths.end();
00177                                                                 ++it )
00178     {
00179         if( !std::count( paths.begin(), paths.end(), *it ) )
00180         {
00181             paths.push_back( *it );
00182         }
00183     }
00184 
00185     return paths;
00186 }
00187 
00188 void WPathHelper::addAdditionalModulePath( const boost::filesystem::path& path )
00189 {
00190     if( !std::count( m_additionalModulePaths.begin(), m_additionalModulePaths.end(), path ) )
00191     {
00192         m_additionalModulePaths.push_back( path );
00193     }
00194 }
00195 
00196 const std::vector< boost::filesystem::path >& WPathHelper::getAdditionalModulePaths() const
00197 {
00198     return m_additionalModulePaths;
00199 }