OpenWalnut  1.5.0dev
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 <algorithm>
26 #include <cstdlib>
27 #include <memory>
28 #include <string>
29 #include <vector>
30 
31 #include <boost/tokenizer.hpp>
32 
33 #include "WPathHelper.h"
34 
35 #ifndef W_LIB_DIR_RELATIVE
36  #define W_LIB_DIR_RELATIVE "lib"
37 #endif
38 
39 // path helper instance as singleton
40 std::shared_ptr< WPathHelper > WPathHelper::m_instance = std::shared_ptr< WPathHelper >();
41 
43 {
44  // initialize members
45 }
46 
48 {
49  // cleanup
50 }
51 
52 std::shared_ptr< WPathHelper > WPathHelper::getPathHelper()
53 {
54  if( !m_instance )
55  {
56  m_instance = std::shared_ptr< WPathHelper >( new WPathHelper() );
57  }
58 
59  return m_instance;
60 }
61 
62 void WPathHelper::setBasePaths( boost::filesystem::path appPath, boost::filesystem::path homePath )
63 {
64  m_appPath = appPath;
65  m_homePath = homePath;
66  m_sharePath = m_appPath / "../share/openwalnut";
67  m_docPath = m_appPath / "../share/doc";
68  m_configPath = m_appPath / "../share/openwalnut";
69  m_libPath = m_appPath / ".." / W_LIB_DIR_RELATIVE; // NOTE: this variable is set by CMake.
70  // This is the default search path. Use getAllModulePaths for finding all modules, including those whose
71  // directories where defined by the user.
72  m_modulePath = m_libPath / "openwalnut"; // This is the default search path.
73 
74  // this is the relative path for module resources. It is relative to the path of the lib containing the module.
75  // Our build system places modules in lib/openwalnut/packagename/. The relative path needs to go out of the lib directory to a share
76  // directory.
77  m_moduleResourcePathRelative = boost::filesystem::path( "../../../share/openwalnut/modules" );
78 }
79 
80 void WPathHelper::setBasePathsOSXBundle( boost::filesystem::path appPath, boost::filesystem::path homePath )
81 {
82  setBasePaths( appPath, homePath ); // set all to ordinary paths, but the following are different:
83 
84  m_sharePath = m_appPath / "../Resources/openwalnut";
85  m_docPath = m_appPath / "../Resources/doc";
86  m_configPath = m_appPath / "../Resources/openwalnut";
87  m_modulePath = m_appPath / "../Resources/modules";
88 
89  // The MacOSX bundle stores the modules in Resources/modules. We want the additional resources to be stored in the module's directory.
90  m_moduleResourcePathRelative = boost::filesystem::path( "." );
91 }
92 
93 boost::filesystem::path WPathHelper::getAppPath()
94 {
95  return getPathHelper()->m_appPath;
96 }
97 
98 boost::filesystem::path WPathHelper::getFontPath()
99 {
100  return getPathHelper()->m_sharePath / "fonts";
101 }
102 
103 boost::filesystem::path WPathHelper::getShaderPath()
104 {
105  return getPathHelper()->m_sharePath / "shaders";
106 }
107 
109 {
110  Fonts fonts;
111  fonts.Regular = getFontPath() / "Regular.ttf";
112  fonts.Bold = getFontPath() / "Bold.ttf";
113  fonts.Italic = getFontPath() / "Italic.ttf";
114  fonts.Default = fonts.Bold;
115 
116  return fonts;
117 }
118 
119 boost::filesystem::path WPathHelper::getModulePath()
120 {
121  return getPathHelper()->m_modulePath;
122 }
123 
124 boost::filesystem::path WPathHelper::getHomePath()
125 {
126  return getPathHelper()->m_homePath;
127 }
128 
129 boost::filesystem::path WPathHelper::getLibPath()
130 {
131  return getPathHelper()->m_libPath;
132 }
133 
134 boost::filesystem::path WPathHelper::getSharePath()
135 {
136  return getPathHelper()->m_sharePath;
137 }
138 
139 boost::filesystem::path WPathHelper::getDocPath()
140 {
141  return getPathHelper()->m_docPath;
142 }
143 
144 boost::filesystem::path WPathHelper::getConfigPath()
145 {
146  return getPathHelper()->m_configPath;
147 }
148 
149 boost::filesystem::path WPathHelper::getModuleResourcePath( boost::filesystem::path moduleLibPath, std::string packageName )
150 {
151  // relative path to the resources
152  boost::filesystem::path resRel = getPathHelper()->m_moduleResourcePathRelative / packageName;
153 
154  // This is related to issue #378, the multiarch support. For modules build with multiarch support, the lib dir is one step
155  // deeper nested -> hence we need to go up one more step. The resource path is always created, even if empty.
156  //
157  // If you encounter any problems, please report them to the openwalnut-dev@lists.informatik.uni-leipzig.de.
158  if( boost::filesystem::exists( moduleLibPath / resRel ) )
159  {
160  return moduleLibPath / resRel;
161  }
162  else if( boost::filesystem::exists( moduleLibPath / ".." / resRel ) )
163  {
164  return moduleLibPath / ".." / resRel;
165  }
166  else
167  {
168  // Lets hope this works ... as both paths are not found, there probably is none.
169  return moduleLibPath / resRel;
170  }
171 }
172 
173 std::vector< boost::filesystem::path > WPathHelper::getAllModulePaths()
174 {
175  // the list of paths
176  std::vector< boost::filesystem::path > paths;
177  // the first element always is the global search path
178  paths.push_back( getModulePath() );
179  paths.push_back( getHomePath() / "modules" );
180 
181  // the environment variable stores the additional paths
182  std::string additionalPaths( getenv( "OW_MODULE_PATH" ) ? getenv( "OW_MODULE_PATH" ) : "" );
183 
184  // separate list of additional paths:
185  typedef boost::tokenizer< boost::char_separator< char > > tokenizer;
186  boost::char_separator< char > sep( ";" );
187  tokenizer tok( additionalPaths, sep );
188  for( tokenizer::iterator it = tok.begin(); it != tok.end(); ++it )
189  {
190  paths.push_back( boost::filesystem::path( *it ) );
191  }
192 
193  // add the additional paths
194  for( std::vector< boost::filesystem::path >::const_iterator it = getPathHelper()->m_additionalModulePaths.begin();
195  it != getPathHelper()->m_additionalModulePaths.end();
196  ++it )
197  {
198  if( !std::count( paths.begin(), paths.end(), *it ) )
199  {
200  paths.push_back( *it );
201  }
202  }
203 
204  return paths;
205 }
206 
207 void WPathHelper::addAdditionalModulePath( const boost::filesystem::path& path )
208 {
209  if( !std::count( m_additionalModulePaths.begin(), m_additionalModulePaths.end(), path ) )
210  {
211  m_additionalModulePaths.push_back( path );
212  }
213 }
214 
215 const std::vector< boost::filesystem::path >& WPathHelper::getAdditionalModulePaths() const
216 {
218 }
static boost::filesystem::path getFontPath()
The path where font files reside in.
Definition: WPathHelper.cpp:98
static std::shared_ptr< WPathHelper > m_instance
Singleton instance of WPathHelper.
Definition: WPathHelper.h:264
const std::vector< boost::filesystem::path > & getAdditionalModulePaths() const
Returns the list of paths added using addAdditionalModulePath.
static boost::filesystem::path getSharePath()
The path where shared files reside in.
virtual ~WPathHelper()
Destructor.
Definition: WPathHelper.cpp:47
boost::filesystem::path m_modulePath
The path to the globally installed modules.
Definition: WPathHelper.h:238
static boost::filesystem::path getConfigPath()
The path where the config files reside in.
WPathHelper()
Constructors are protected because this is a Singleton.
Definition: WPathHelper.cpp:42
boost::filesystem::path m_appPath
Application path.
Definition: WPathHelper.h:218
static boost::filesystem::path getShaderPath()
The path to the global shaders.
static boost::filesystem::path getModulePath()
The path to the globally installed modules.
std::vector< boost::filesystem::path > m_additionalModulePaths
A list of additional paths to search for modules.
Definition: WPathHelper.h:259
void setBasePaths(boost::filesystem::path appPath, boost::filesystem::path homePath)
Set the current application path.
Definition: WPathHelper.cpp:62
boost::filesystem::path m_moduleResourcePathRelative
The path to module resources, relative to the module libraries path.
Definition: WPathHelper.h:253
static boost::filesystem::path getLibPath()
The path to the OW libs.
boost::filesystem::path m_libPath
The path to the OW libs.
Definition: WPathHelper.h:243
static Fonts getAllFonts()
The paths to all fonts supported.
static boost::filesystem::path getHomePath()
The path to the OW dir in the user's home.
static std::shared_ptr< WPathHelper > getPathHelper()
Returns instance of the path helper.
Definition: WPathHelper.cpp:52
static boost::filesystem::path getModuleResourcePath(boost::filesystem::path moduleLibPath, std::string packageName)
The path to a given module's resources.
boost::filesystem::path m_configPath
The path where all the config files reside in.
Definition: WPathHelper.h:233
void addAdditionalModulePath(const boost::filesystem::path &path)
This method adds the given path to the list of module paths.
void setBasePathsOSXBundle(boost::filesystem::path appPath, boost::filesystem::path homePath)
Set the current application path.
Definition: WPathHelper.cpp:80
static std::vector< boost::filesystem::path > getAllModulePaths()
This returns a list of search paths for modules.
boost::filesystem::path m_docPath
The path where all the documentation files reside in.
Definition: WPathHelper.h:228
boost::filesystem::path m_homePath
The path of a user specific OW directory.
Definition: WPathHelper.h:248
static boost::filesystem::path getDocPath()
The path where the doc files reside in.
boost::filesystem::path m_sharePath
The path where all the shared files reside in.
Definition: WPathHelper.h:223
static boost::filesystem::path getAppPath()
The path where the binary file resides in.
Definition: WPathHelper.cpp:93
Paths to all known fonts.
Definition: WPathHelper.h:89
boost::filesystem::path Regular
The Regular font (not bold, not italic)
Definition: WPathHelper.h:98
boost::filesystem::path Bold
Bold font.
Definition: WPathHelper.h:108
boost::filesystem::path Default
The default font to use in most cases.
Definition: WPathHelper.h:93
boost::filesystem::path Italic
Italic font.
Definition: WPathHelper.h:103