LCOV - code coverage report
Current view: top level - ugbase/common/util - path_provider.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 25.0 % 36 9
Test Date: 2025-09-21 23:31:46 Functions: 44.4 % 9 4

            Line data    Source code
       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              : 
      47              : /// \addtogroup ugbase_common_util
      48              : /// \{
      49              : 
      50              : ////////////////////////////////////////////////////////////////////////
      51              : ///     Constants used by PathProvider
      52              : enum PathTypes
      53              : {
      54              :         BIN_PATH = 0,   ///< path in which the binary lies
      55              :         SCRIPT_PATH,
      56              :         ROOT_PATH,
      57              :         PLUGIN_PATH,
      58              :         APPS_PATH,              ///< path in which the application-scripts lie
      59              : 
      60              : //      always last
      61              :         MAX_PATH_CONSTANT
      62              : };
      63              : 
      64              : ////////////////////////////////////////////////////////////////////////
      65              : ///     Singleton which stores common paths and a stack of current paths.
      66              : /**     All paths are initially set to "".
      67              :  *
      68              :  * Note that all public methods of PathProvider are static. That means
      69              :  * you have to call them through the :: operator. E.g.
      70              :  *
      71              :  * \code
      72              :  * std::string appPath = PathProvider::get_path(BIN_PATH);
      73              :  * \endcode
      74              :  */
      75              : class PathProvider
      76              : {
      77              :         public:
      78              :         ///     sets the path for the given constant.
      79              :         /**
      80              :          * \param pathType      should be one of the constants enumerated in PathTypes
      81              :          *                                      or a used defined constant starting from
      82              :          *                                      MAX_PATH_CONSTANT + 1.
      83              :          */
      84            5 :                 static inline void set_path(PathTypes pathType, const std::string& path)
      85            5 :                 {inst().m_map[pathType] = path;}
      86              : 
      87              :         ///     returns the path associated with the given constant.
      88              :         /**
      89              :          * \param pathType      should be one of the constants enumerated in PathTypes
      90              :          *                                      or a used defined constant starting from
      91              :          *                                      MAX_PATH_CONSTANT + 1.
      92              :          */
      93              :                 static inline const std::string& get_path(PathTypes pathType)
      94           20 :                 {return inst().m_map[pathType];}
      95              : 
      96              :         ///     returns true, if the path associated with the given constant exists.
      97              :         /**
      98              :          * \param pathType      should be one of the constants enumerated in PathTypes
      99              :          *                                      or a used defined constant starting from
     100              :          *                                      MAX_PATH_CONSTANT + 1.
     101              :          */
     102            5 :                 static inline bool has_path(PathTypes pathType)
     103            5 :                 {return inst().m_map.find(pathType) != inst().m_map.end();}
     104              : 
     105              :         ///     returns the current path
     106              :         /**     current paths are stored in a stack. The top of the stack is considered
     107              :          * to be the most current path and is returned by this method.
     108              :          *
     109              :          * \param defPath       (optional) If the stack is empty, the path associated with
     110              :          *                                      defPath is returned. By default defPath is set to BIN_PATH.
     111              :          */
     112            0 :                 static inline const std::string& get_current_path(PathTypes defPath = BIN_PATH)
     113              :                 {
     114            0 :                         if(inst().m_curPaths.empty())
     115            0 :                                 return get_path(defPath);
     116            0 :                         return inst().m_curPaths.top();
     117              :                 }
     118              : 
     119              :         ///     returns true if a current path exists, false if not.
     120              :                 static inline bool has_current_path()
     121            0 :                 {return !inst().m_curPaths.empty();}
     122              : 
     123              :         ///     pushes a path to the stack of current paths
     124              :                 static inline void push_current_path(const std::string& path)
     125            0 :                 {inst().m_curPaths.push(path);}
     126              : 
     127              :         ///     pops a path from the stack of current paths
     128              :                 static inline void pop_current_path()
     129            0 :                 {inst().m_curPaths.pop();}
     130              : 
     131              :         ///     clears the stack of current paths. This makes sense if an error was catched.
     132              :                 static inline void clear_current_path_stack()
     133              :                 {while(has_current_path()) {pop_current_path();}}
     134              : 
     135              :         /**
     136              :          * @param relativeFilename (in) relative filename
     137              :          * @param absoluteFilename (out) absolute filename
     138              :          * @return true if file exists relative to current path
     139              :          */
     140            0 :                 static inline bool get_filename_relative_to_current_path(const std::string &relativeFilename, std::string &absoluteFilename)
     141              :                 {
     142            0 :                         const char* pathSep = GetPathSeparator();
     143              : 
     144            0 :                         if(inst().has_current_path() == false) return false;
     145            0 :                         absoluteFilename = inst().get_current_path() + pathSep + relativeFilename;
     146            0 :                         return FileExists(absoluteFilename.c_str());
     147              :                 }
     148              : 
     149              : 
     150              :         /**
     151              :          * @param relativeDirname (in) relative directory name
     152              :          * @param absoluteDirname (out) absolute directory name
     153              :          * @return true if directory exists relative to current path
     154              :          */
     155            0 :                 static inline bool get_dirname_relative_to_current_path(const std::string &relativeDirname, std::string &absoluteDirname)
     156              :                 {
     157            0 :                         const char* pathSep = GetPathSeparator();
     158              : 
     159            0 :                         if (!inst().has_current_path())
     160              :                                 return false;
     161            0 :                         absoluteDirname = inst().get_current_path() + pathSep + relativeDirname;
     162            0 :                         return DirectoryExists(absoluteDirname.c_str());
     163              :                 }
     164              : 
     165              :         /**
     166              :          * @param relativeFilename (in) relative filename
     167              :          * @param absoluteFilename (out) absolute filename
     168              :          * @return true if file exists relative to current path
     169              :          */
     170            0 :                 static inline bool get_filename_relative_to_path(PathTypes pathType, const std::string &relativeFilename, std::string &absoluteFilename)
     171              :                 {
     172            0 :                         const char* pathSep = GetPathSeparator();
     173              : 
     174            0 :                         if(inst().has_path(pathType) == false) return false;
     175            0 :                         absoluteFilename = inst().get_path(pathType) + pathSep + relativeFilename;
     176            0 :                         return FileExists(absoluteFilename.c_str());
     177              :                 }
     178              : 
     179              :         /**
     180              :          * @param relativeDirname (in) relative directory name
     181              :          * @param absoluteDirname (out) absolute directory name
     182              :          * @return true if directory exists relative to current path
     183              :          */
     184            0 :                 static inline bool get_dirname_relative_to_path(PathTypes pathType, const std::string &relativeDirname, std::string &absoluteDirname)
     185              :                 {
     186            0 :                         const char* pathSep = GetPathSeparator();
     187              : 
     188            0 :                         if (!inst().has_path(pathType))
     189              :                                 return false;
     190            0 :                         absoluteDirname = inst().get_path(pathType) + pathSep + relativeDirname;
     191            0 :                         return DirectoryExists(absoluteDirname.c_str());
     192              :                 }
     193              :         private:
     194            2 :                 PathProvider()  {}
     195              :                 PathProvider(const PathProvider&)   {}
     196              : 
     197           35 :                 static PathProvider& inst()
     198              :                 {
     199           35 :                         static PathProvider pp;
     200           35 :                         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
     209              : /// \}
     210              : 
     211              : }//     end of namespace
     212              : 
     213              : #endif
        

Generated by: LCOV version 2.0-1