ug4
path_provider.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2015: G-CSC, Goethe University Frankfurt
3  * Author: Sebastian Reiter
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__UG__path_provider__
34 #define __H__UG__path_provider__
35 
36 #include <string>
37 #include <map>
38 #include <stack>
39 
40 #include "common/util/file_util.h"
41 #include "common/util/os_info.h"
42 
43 
44 namespace ug
45 {
46 
49 
53 {
54  BIN_PATH = 0,
59 
60 // always last
62 };
63 
66 
76 {
77  public:
79 
84  static inline void set_path(PathTypes pathType, const std::string& path)
85  {inst().m_map[pathType] = path;}
86 
88 
93  static inline const std::string& get_path(PathTypes pathType)
94  {return inst().m_map[pathType];}
95 
97 
102  static inline bool has_path(PathTypes pathType)
103  {return inst().m_map.find(pathType) != inst().m_map.end();}
104 
106 
112  static inline const std::string& get_current_path(PathTypes defPath = BIN_PATH)
113  {
114  if(inst().m_curPaths.empty())
115  return get_path(defPath);
116  return inst().m_curPaths.top();
117  }
118 
120  static inline bool has_current_path()
121  {return !inst().m_curPaths.empty();}
122 
124  static inline void push_current_path(const std::string& path)
125  {inst().m_curPaths.push(path);}
126 
128  static inline void pop_current_path()
129  {inst().m_curPaths.pop();}
130 
132  static inline void clear_current_path_stack()
133  {while(has_current_path()) {pop_current_path();}}
134 
140  static inline bool get_filename_relative_to_current_path(const std::string &relativeFilename, std::string &absoluteFilename)
141  {
142  const char* pathSep = GetPathSeparator();
143 
144  if(inst().has_current_path() == false) return false;
145  absoluteFilename = inst().get_current_path() + pathSep + relativeFilename;
146  return FileExists(absoluteFilename.c_str());
147  }
148 
149 
155  static inline bool get_dirname_relative_to_current_path(const std::string &relativeDirname, std::string &absoluteDirname)
156  {
157  const char* pathSep = GetPathSeparator();
158 
159  if (!inst().has_current_path())
160  return false;
161  absoluteDirname = inst().get_current_path() + pathSep + relativeDirname;
162  return DirectoryExists(absoluteDirname.c_str());
163  }
164 
170  static inline bool get_filename_relative_to_path(PathTypes pathType, const std::string &relativeFilename, std::string &absoluteFilename)
171  {
172  const char* pathSep = GetPathSeparator();
173 
174  if(inst().has_path(pathType) == false) return false;
175  absoluteFilename = inst().get_path(pathType) + pathSep + relativeFilename;
176  return FileExists(absoluteFilename.c_str());
177  }
178 
184  static inline bool get_dirname_relative_to_path(PathTypes pathType, const std::string &relativeDirname, std::string &absoluteDirname)
185  {
186  const char* pathSep = GetPathSeparator();
187 
188  if (!inst().has_path(pathType))
189  return false;
190  absoluteDirname = inst().get_path(pathType) + pathSep + relativeDirname;
191  return DirectoryExists(absoluteDirname.c_str());
192  }
193  private:
196 
197  static PathProvider& inst()
198  {
199  static PathProvider pp;
200  return pp;
201  }
202 
203  private:
204  std::map<PathTypes, std::string> m_map;
205  std::stack<std::string> m_curPaths;
206 };
207 
208 // end group ugbase_common_util
210 
211 }// end of namespace
212 
213 #endif
Singleton which stores common paths and a stack of current paths.
Definition: path_provider.h:76
static bool has_current_path()
returns true if a current path exists, false if not.
Definition: path_provider.h:120
static void push_current_path(const std::string &path)
pushes a path to the stack of current paths
Definition: path_provider.h:124
std::map< PathTypes, std::string > m_map
Definition: path_provider.h:204
std::stack< std::string > m_curPaths
Definition: path_provider.h:205
static void pop_current_path()
pops a path from the stack of current paths
Definition: path_provider.h:128
static PathProvider & inst()
Definition: path_provider.h:197
static bool get_filename_relative_to_path(PathTypes pathType, const std::string &relativeFilename, std::string &absoluteFilename)
Definition: path_provider.h:170
PathProvider(const PathProvider &)
Definition: path_provider.h:195
static void clear_current_path_stack()
clears the stack of current paths. This makes sense if an error was catched.
Definition: path_provider.h:132
static const std::string & get_current_path(PathTypes defPath=BIN_PATH)
returns the current path
Definition: path_provider.h:112
static bool get_filename_relative_to_current_path(const std::string &relativeFilename, std::string &absoluteFilename)
Definition: path_provider.h:140
static bool get_dirname_relative_to_path(PathTypes pathType, const std::string &relativeDirname, std::string &absoluteDirname)
Definition: path_provider.h:184
static void set_path(PathTypes pathType, const std::string &path)
sets the path for the given constant.
Definition: path_provider.h:84
static const std::string & get_path(PathTypes pathType)
returns the path associated with the given constant.
Definition: path_provider.h:93
static bool get_dirname_relative_to_current_path(const std::string &relativeDirname, std::string &absoluteDirname)
Definition: path_provider.h:155
PathProvider()
Definition: path_provider.h:194
static bool has_path(PathTypes pathType)
returns true, if the path associated with the given constant exists.
Definition: path_provider.h:102
File utility functions.
UG_API bool FileExists(const char *filename)
!!! Serial i/o version !!!
Definition: file_util.cpp:53
UG_API bool DirectoryExists(const char *dirname)
Checks the existence of a given directory.
Definition: file_util_posix.cpp:74
const char * GetPathSeparator()
returns a string containing the path-separator for the current os
Definition: os_info_linux.cpp:47
PathTypes
Constants used by PathProvider.
Definition: path_provider.h:53
@ SCRIPT_PATH
Definition: path_provider.h:55
@ ROOT_PATH
Definition: path_provider.h:56
@ MAX_PATH_CONSTANT
Definition: path_provider.h:61
@ APPS_PATH
path in which the application-scripts lie
Definition: path_provider.h:58
@ PLUGIN_PATH
Definition: path_provider.h:57
@ BIN_PATH
path in which the binary lies
Definition: path_provider.h:54
the ug namespace