Stride Reference Manual  - generated for commit 9643b11
FileSys.h
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2017, Kuylen E, Willem L, Broeckhove J
14  */
15 
21 #pragma once
22 
23 #include <boost/property_tree/ptree_fwd.hpp>
24 #include <functional>
25 #include <string>
26 
27 #ifdef BOOST_FOUND
28 #include <boost/filesystem.hpp>
29 #include <boost/filesystem/path.hpp>
30 namespace filesys = boost::filesystem;
31 #else
32 #include <filesystem>
33 namespace filesys = std::filesystem;
34 #endif
35 
36 namespace stride {
37 namespace util {
38 
42 class FileSys
43 {
44 public:
47  static bool IsDirectoryString(const std::string& s);
48 
50  static bool CreateDirectory(std::string s);
51 
53  static filesys::path BuildPath(const std::string& output_prefix, const std::string& filename);
54 
56  static boost::property_tree::ptree ReadPtreeFile(const filesys::path& f_p);
57 
59  static boost::property_tree::ptree ReadPtreeFile(const std::string& f_n);
60 
62  static void WritePtreeFile(const filesys::path& f_p, const boost::property_tree::ptree& pt);
63 
65  static void WritePtreeFile(const std::string& f_n, const boost::property_tree::ptree& pt);
66 
67 public:
69  static filesys::path GetCurrentDir() { return Get().m_current_dir; }
70 
72  static filesys::path GetExecPath() { return Get().m_exec_path; }
73 
74 public:
78  static bool CheckInstallEnv(
79  std::function<void(const std::string&)> logger = std::function<void(const std::string&)>());
80 
82  static filesys::path GetBinDir() { return Get().m_bin_dir; }
83 
85  static filesys::path GetConfigDir() { return Get().m_config_dir; }
86 
88  static filesys::path GetDataDir() { return Get().m_data_dir; }
89 
91  static filesys::path GetRootDir() { return Get().m_root_dir; }
92 
94  static filesys::path GetTestsDir() { return Get().m_tests_dir; }
95 
96 private:
98  struct Dirs
99  {
103 
104  filesys::path m_current_dir;
105  filesys::path m_exec_path;
106 
107  // only relevant when use_install_dirs mode is active
108  filesys::path m_bin_dir;
109  filesys::path m_config_dir;
110  filesys::path m_data_dir;
111  filesys::path m_root_dir;
112  filesys::path m_tests_dir;
113  };
114 
115 private:
117  static Dirs Initialize();
118 
120  static Dirs& Get();
121 };
122 
123 } // namespace util
124 } // namespace stride
static Dirs Initialize()
Initialize all paths.
Definition: FileSys.cpp:102
static filesys::path GetDataDir()
/// Return data dir (only relevant when use_install_dirs mode is active)
Definition: FileSys.h:88
filesys::path m_config_dir
Definition: FileSys.h:109
static filesys::path GetCurrentDir()
Get path to the current directory.
Definition: FileSys.h:69
filesys::path m_bin_dir
Definition: FileSys.h:108
static filesys::path GetExecPath()
Get path of the executable.
Definition: FileSys.h:72
filesys::path m_data_dir
Definition: FileSys.h:110
static filesys::path GetTestsDir()
Return tests dir (only relevant when use_install_dirs mode is active)
Definition: FileSys.h:94
Utilities for interaction with filesystem.
Definition: FileSys.h:42
static bool CreateDirectory(std::string s)
Create a directory relative to the current directory with the given path, returns if it was succesful...
Definition: FileSys.cpp:199
filesys::path m_root_dir
Definition: FileSys.h:111
static boost::property_tree::ptree ReadPtreeFile(const filesys::path &f_p)
Read ptree from file at path.
Definition: FileSys.cpp:208
static Dirs & Get()
Return paths.
Definition: FileSys.cpp:96
static filesys::path GetConfigDir()
Return config dir (only relevant when use_install_dirs mode is active)
Definition: FileSys.h:85
static filesys::path GetRootDir()
Return install root dir (only relevant when use_install_dirs mode is active)
Definition: FileSys.h:91
filesys::path m_exec_path
Definition: FileSys.h:105
static bool CheckInstallEnv(std::function< void(const std::string &)> logger=std::function< void(const std::string &)>())
Verify that current dir is root dir and all install dirs are present.
Definition: FileSys.cpp:65
static filesys::path BuildPath(const std::string &output_prefix, const std::string &filename)
Interpret prefix (directory or filename prefix) and return appropriate path.
Definition: FileSys.cpp:52
filesys::path m_tests_dir
Definition: FileSys.h:112
static void WritePtreeFile(const filesys::path &f_p, const boost::property_tree::ptree &pt)
Write ptree to file at path.
Definition: FileSys.cpp:229
static bool IsDirectoryString(const std::string &s)
String represents a directory path (relative or absolute) iff it contains at least one / (may be a tr...
Definition: FileSys.cpp:193
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
filesys::path m_current_dir
Definition: FileSys.h:102
Using this to avoid global variables & their initialization.
Definition: FileSys.h:98
static filesys::path GetBinDir()
Return bin dir (only relevant when use_install_dirs mode is active)
Definition: FileSys.h:82