OpenWalnut  1.4.0
WPathHelper.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <string>
26 #include <vector>
27 #include <cstdlib>
28 #include <algorithm>
29 
30 #include <boost/tokenizer.hpp>
31 
32 #include "WPathHelper.h"
33 
34 // path helper instance as singleton
35 boost::shared_ptr< WPathHelper > WPathHelper::m_instance = boost::shared_ptr< WPathHelper >();
36 
38 {
39  // initialize members
40 }
41 
43 {
44  // cleanup
45 }
46 
47 boost::shared_ptr< WPathHelper > WPathHelper::getPathHelper()
48 {
49  if( !m_instance )
50  {
51  m_instance = boost::shared_ptr< WPathHelper >( new WPathHelper() );
52  }
53 
54  return m_instance;
55 }
56 
57 void WPathHelper::setBasePaths( boost::filesystem::path appPath, boost::filesystem::path homePath )
58 {
59  m_appPath = appPath;
60  m_homePath = homePath;
61  m_sharePath = m_appPath / "../share/openwalnut";
62  m_docPath = m_appPath / "../share/doc";
63  m_configPath = m_appPath / "../share/openwalnut";
64  m_libPath = m_appPath / "../lib";
65  m_modulePath = m_libPath / "openwalnut";
66 
67  // this is the relative path for module resources. It is relative to the path of the lib containing the module.
68  // Our build system places modules in lib/openwalnut/packagename/. The relative path needs to go out of the lib directory to a share
69  // directory.
70  m_moduleResourcePathRelative = boost::filesystem::path( "../../../share/openwalnut/modules" );
71 }
72 
73 void WPathHelper::setBasePathsOSXBundle( boost::filesystem::path appPath, boost::filesystem::path homePath )
74 {
75  //W_ASSERT( appPath.substr( size()-sizeof( "MacOS" ), sizeof( "MacOS" ) ) == "MacOS" );
76  m_appPath = appPath;
77  m_homePath = homePath;
78  m_sharePath = m_appPath / "../Resources/openwalnut";
79  m_docPath = m_appPath / "../Resources/doc";
80  m_configPath = m_appPath / "../Resources/openwalnut";
81  m_libPath = m_appPath / "../lib"; // TODO(mario): what is this for?
82  m_modulePath = m_appPath / "../Resources/modules";
83 
84  // this is the relative path for module resources. It is relative to the path of the lib containing the module.
85  // The MacOSX bundle stores the modules in Resources/modules. We want the additional resources to be stored in the module's directory.
86  m_moduleResourcePathRelative = boost::filesystem::path( "." );
87 }
88 
89 boost::filesystem::path WPathHelper::getAppPath()
90 {
91  return getPathHelper()->m_appPath;
92 }
93 
94 boost::filesystem::path WPathHelper::getFontPath()
95 {
96  return getPathHelper()->m_sharePath / "fonts";
97 }
98 
99 boost::filesystem::path WPathHelper::getShaderPath()
100 {
101  return getPathHelper()->m_sharePath / "shaders";
102 }
103 
105 {
106  Fonts fonts;
107  fonts.Regular = getFontPath() / "Regular.ttf";
108  fonts.Bold = getFontPath() / "Bold.ttf";
109  fonts.Italic = getFontPath() / "Italic.ttf";
110  fonts.Default = fonts.Bold;
111 
112  return fonts;
113 }
114 
115 boost::filesystem::path WPathHelper::getModulePath()
116 {
117  return getPathHelper()->m_modulePath;
118 }
119 
120 boost::filesystem::path WPathHelper::getHomePath()
121 {
122  return getPathHelper()->m_homePath;
123 }
124 
125 boost::filesystem::path WPathHelper::getLibPath()
126 {
127  return getPathHelper()->m_libPath;
128 }
129 
130 boost::filesystem::path WPathHelper::getSharePath()
131 {
132  return getPathHelper()->m_sharePath;
133 }
134 
135 boost::filesystem::path WPathHelper::getDocPath()
136 {
137  return getPathHelper()->m_docPath;
138 }
139 
140 boost::filesystem::path WPathHelper::getConfigPath()
141 {
142  return getPathHelper()->m_configPath;
143 }
144 
145 boost::filesystem::path WPathHelper::getModuleResourcePath( boost::filesystem::path moduleLibPath, std::string packageName )
146 {
147  // relative path to the resources
148  boost::filesystem::path resRel = getPathHelper()->m_moduleResourcePathRelative / packageName;
149 
150  // return absolute paths
151  return moduleLibPath / resRel;
152 }
153 
154 std::vector< boost::filesystem::path > WPathHelper::getAllModulePaths()
155 {
156  // the list of paths
157  std::vector< boost::filesystem::path > paths;
158  // the first element always is the global search path
159  paths.push_back( getModulePath() );
160  paths.push_back( getHomePath() / "modules" );
161 
162  // the environment variable stores the additional paths
163  std::string additionalPaths( getenv( "OW_MODULE_PATH" ) ? getenv( "OW_MODULE_PATH" ) : "" );
164 
165  // separate list of additional paths:
166  typedef boost::tokenizer< boost::char_separator< char > > tokenizer;
167  boost::char_separator< char > sep( ";" );
168  tokenizer tok( additionalPaths, sep );
169  for( tokenizer::iterator it = tok.begin(); it != tok.end(); ++it )
170  {
171  paths.push_back( boost::filesystem::path( *it ) );
172  }
173 
174  // add the additional paths
175  for( std::vector< boost::filesystem::path >::const_iterator it = getPathHelper()->m_additionalModulePaths.begin();
176  it != getPathHelper()->m_additionalModulePaths.end();
177  ++it )
178  {
179  if( !std::count( paths.begin(), paths.end(), *it ) )
180  {
181  paths.push_back( *it );
182  }
183  }
184 
185  return paths;
186 }
187 
188 void WPathHelper::addAdditionalModulePath( const boost::filesystem::path& path )
189 {
190  if( !std::count( m_additionalModulePaths.begin(), m_additionalModulePaths.end(), path ) )
191  {
192  m_additionalModulePaths.push_back( path );
193  }
194 }
195 
196 const std::vector< boost::filesystem::path >& WPathHelper::getAdditionalModulePaths() const
197 {
199 }