Flow123d  last_with_con_2.0.0-4-g42e6930
file_path.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file file_path.hh
15  * @brief
16  */
17 
18 #ifndef FILE_NAME_HH_
19 #define FILE_NAME_HH_
20 
21 #include <string>
22 
23 #include "system/exceptions.hh"
24 
25 using namespace std;
26 
27 
28 namespace boost {
29  namespace filesystem {
30  class path;
31 }}
32 
33 
34 
35 
36 /**
37  * @brief Dedicated class for storing path to input and output files.
38  *
39  * FilePath objects are constructed from given absolute or relative path to the file and its type (input or output).
40  * Before you create any instance of the class you have to call static method @p set_io_dirs to set:
41  * - working directory of the program (when it was started)
42  * - root input directory, i.e. directory of the main input file (given by -s parameter)
43  * - input directory, used to replace ${INPUT} placeholder (given by -i parameter)
44  * - output directory, where all output files should be placed (given by -o parameter)
45  *
46  */
47 
48 class FilePath {
49 public:
50 
51  /**
52  * Reporting failure when openning a file.
53  */
54  TYPEDEF_ERR_INFO( EI_Path, string);
55  DECLARE_EXCEPTION( ExcFileOpen, << "Can not open file: " << EI_Path::qval );
56  DECLARE_EXCEPTION( ExcAbsOutputPath, << "Can not set absolute path " << EI_Path::qval << " for an output file." );
57  DECLARE_EXCEPTION( ExcMkdirFail, << "Can not create directory: " << EI_Path::qval );
58 
59  /// Possible types of file.
60  enum FileType {
62  output_file
63  };
64 
65  /**
66  * Default constructor, necessary when using Input::Record::opt_val() to initialize a FilePath.
67  */
68  FilePath();
69 
70  /**
71  * Translates the given absolute or relative path to a file @p file_path depending on the file type @p ft.
72  *
73  * For input files:
74  * - For relative path prepend absolute path of the directory of the main input file (root directory).
75  * - Replace ${INPUT} place holder with the input directory given at command line.
76  *
77  * For output files:
78  * - Forbids absolute output paths.
79  * - For relative output path prepends it by the output directory given at the command line.
80  */
81  FilePath(string file_path, const FileType ft);
82 
83  /// Same as previous, but create path from vector of strings.
84  FilePath(vector<string> sub_paths, const FileType ft);
85 
86  /// Same as previous but implicitly use FileType::output_file
87  FilePath(string file_path);
88 
89  /**
90  * @brief Obsolete method for set input and output directories.
91  *
92  * Ensures consistency of unit tests.
93  *
94  * Set:
95  * - working directory (used only if the output directory is relative)
96  * - root directory (of the main input file)
97  * - input directory to replace ${INPUT} place holder
98  * - output directory used as prefix to the output files (relative output dirs are relative to the working directory)
99  */
100  static void set_io_dirs(const string working_dir, const string root, const string input, const string output);
101 
102  /**
103  * @brief Method for set input and output directories.
104  *
105  * Set:
106  * - root directory (of the main input file)
107  * - input directory to replace ${INPUT} place holder
108  * - output directory used as prefix to the output files (relative output dirs are relative to the working directory)
109  */
110  static void set_dirs(const string root, const string input, const string output);
111 
112  /**
113  * @brief Method for set input and output directories.
114  *
115  * Same as previous, but in first argument accepts full path of yaml file and returns filename of this yaml file.
116  *
117  * Set:
118  * - root directory (of the main yaml input file)
119  * - input directory to replace ${INPUT} place holder
120  * - output directory used as prefix to the output files (relative output dirs are relative to the working directory)
121  */
122  static string set_dirs_from_input(const string main_yaml, const string input, const string output);
123 
124  /**
125  * This class is implicitly convertible to string.
126  */
127  operator string() const;
128 
129  /*!
130  * @brief Add new item to place holder.
131  *
132  * Placeholder is extended by adding a single new item. The item can be used in the name of the input or output file name.
133  * Currently, the only supported placeholder is ${INPUT}.
134  *
135  * @par Example usage:
136  * @code
137  * FilePath::add_placeholder_item("${SUBST_VAL}", "path/value");
138  * @endcode
139  *
140  * @param[in] key Key of new item.
141  * @param[in] value Value of new item.
142  */
143  static void add_placeholder(string key,string value);
144 
145  /**
146  * Return absolute path of actual working directory.
147  */
148  static const string get_absolute_working_dir();
149 
150  /// Equality comparison operators for FilePaths.
151  bool operator ==(const FilePath &other) const;
152 
153 
154  /**
155  * For an output filepath, the directory part (up to last separator) is
156  * extracted and all subdirectories are created if doesn't exist yet.
157  */
158  void create_output_dir();
159 
160  /**
161  * Return path to file.
162  */
163  string parent_path() const;
164 
165  /**
166  * Return name of file with extension.
167  */
168  string filename() const;
169 
170  /**
171  * Return name of file without extension.
172  */
173  string stem() const;
174 
175  /**
176  * Return extension of file.
177  */
178  string extension() const;
179 
180  /**
181  * Return path to file with filename without extension.
182  */
183  string cut_extension() const;
184 
185 
186  /**
187  * Open stream for this FilePath.
188  * Open mode is determined from the FilePath type.
189  */
190  template <class Stream>
191  void open_stream(Stream &stream) const;
192 
193  /**
194  * Return true if the FilePath is a file.
195  */
196  bool exists() const;
197 
198 private:
199  /**
200  * Create a directory, and check for exceptions.
201  */
202  static void create_dir(const boost::filesystem::path &dir);
203 
204  /**
205  * Substitutes placeholders in @p path.
206  */
207  void substitute_value(string &path);
208 
209  /**
210  * @brief Prepare path string for check absolute path.
211  *
212  * Check first char of path string. If it is slash '/', add second slash char. Two slashes
213  * at begin is necessary for correct output of boost::filesystem::path.is_absolute() method
214  * for detection absolute path in unix format ("/home/x/y/z") under cygwin.
215  */
216  static string convert_for_check_absolute(string path);
217 
218 
219  /// Final absolute path to the file.
220  std::shared_ptr<boost::filesystem::path> abs_file_path_;
221 
222  /// File type
224 
225  /// dictionary of placeholders
227 
228  /// Prefix path for output files.
229  static std::shared_ptr<boost::filesystem::path> output_dir;
230 
231  /// Prefix path for input files (directory of the main input file).
232  static std::shared_ptr<boost::filesystem::path> root_dir;
233 };
234 
235 /**
236  * @brief Allow redirect FilePath to stream.
237  */
238 std::ostream& operator<<(std::ostream& stream, const FilePath& fp);
239 
240 
241 #endif /* FILE_NAME_HH_ */
FileType
Possible types of file.
Definition: file_path.hh:60
std::ostream & operator<<(std::ostream &stream, const FilePath &fp)
Allow redirect FilePath to stream.
Definition: file_path.cc:270
static std::shared_ptr< boost::filesystem::path > root_dir
Prefix path for input files (directory of the main input file).
Definition: file_path.hh:232
#define DECLARE_EXCEPTION(ExcName, Format)
Macro for simple definition of exceptions.
Definition: exceptions.hh:150
bool operator==(const Null &, const Null &)
std::shared_ptr< boost::filesystem::path > abs_file_path_
Final absolute path to the file.
Definition: file_path.hh:220
Dedicated class for storing path to input and output files.
Definition: file_path.hh:48
static std::map< string, string > placeholder
dictionary of placeholders
Definition: file_path.hh:226
static std::shared_ptr< boost::filesystem::path > output_dir
Prefix path for output files.
Definition: file_path.hh:229
#define TYPEDEF_ERR_INFO(EI_Type, Type)
Macro to simplify declaration of error_info types.
Definition: exceptions.hh:186
FileType file_type_
File type.
Definition: file_path.hh:223