Flow123d  release_1.8.2-1603-g0109a2b
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 
29 
30 /**
31  * @brief Dedicated class for storing path to input and output files.
32  *
33  * FilePath objects are constructed from given absolute or relative path to the file and its type (input or output).
34  * Before you create any instance of the class you have to call static method @p set_io_dirs to set:
35  * - working directory of the program (when it was started)
36  * - root input directory, i.e. directory of the main input file (given by -s parameter)
37  * - input directory, used to replace ${INPUT} placeholder (given by -i parameter)
38  * - output directory, where all output files should be placed (given by -o parameter)
39  *
40  */
41 
42 class FilePath {
43 public:
44 
45 
46  TYPEDEF_ERR_INFO( EI_Path, string);
47  DECLARE_EXCEPTION( ExcAbsOutputPath, << "Can not set absolute path " << EI_Path::qval << " for an output file." );
48 
49  /// Possible types of file.
50  enum FileType {
52  output_file
53  };
54 
55  /**
56  * Default constructor, necessary when using Input::Record::opt_val() to initialize a FilePath.
57  */
59  : abs_file_path_("/__NO_FILE_NAME_GIVEN__"),
60  file_type_(output_file)
61  {}
62 
63  /**
64  * Translates the given absolute or relative path to a file @p file_path depending on the file type @p ft.
65  *
66  * For input files:
67  * - For relative path prepend absolute path of the directory of the main input file (root directory).
68  * - Replace ${INPUT} place holder with the input directory given at command line.
69  *
70  * For output files:
71  * - Forbids absolute output paths.
72  * - For relative output path prepends it by the output directory given at the command line.
73  */
74  FilePath(string file_path, const FileType ft);
75 
76  /**
77  * Set:
78  * - working directory (used only if the output directory is relative)
79  * - root directory (of the main input file)
80  * - input directory to replace ${INPUT} place holder
81  * - output directory used as prefix to the output files (relative output dirs are relative to the working directory)
82  */
83  static void set_io_dirs(const string working_dir, const string root_input,const string input,const string output);
84 
85  /**
86  * This class is implicitly convertible to string.
87  */
88  inline operator string() const
89  {return abs_file_path_;}
90 
91  /*!
92  * @brief Add new item to place holder.
93  *
94  * Placeholder is extended by adding a single new item. The item can be used in the name of the input or output file name.
95  * Currently, the only supported placeholder is ${INPUT}.
96  *
97  * @par Example usage:
98  * @code
99  * FilePath::add_placeholder_item("${SUBST_VAL}", "path/value");
100  * @endcode
101  *
102  * @param[in] key Key of new item.
103  * @param[in] value Value of new item.
104  */
105  static void add_placeholder(string key,string value);
106 
107  /**
108  * Return absolute path of actual working directory.
109  */
110  static const string get_absolute_working_dir();
111 
112  /// Equality comparison operators for regions.
113  inline bool operator ==(const FilePath &other) const
114  {return abs_file_path_ == string(other); }
115 
116 
117  /**
118  * For an output filepath, the directory part (up to last separator) is
119  * extracted and all subdirectories are created if doesn't exist yet.
120  */
121  void create_output_dir();
122 
123 private:
124  /**
125  * Substitutes placeholders in @p abs_file_path_.
126  */
127  void substitute_value();
128 
129 
130  /**
131  * Test if get path is absolute for used operating system.
132  */
133  static bool is_absolute_path(const string path);
134 
135 
136  /**
137  * Check if directory stored in output_dir doesn't exist and create its
138  */
139  static void create_dir(string dir);
140 
141 
142  /**
143  * Create canonical path of output directory given by relative path.
144  */
145  static void create_canonical_path(const string working_dir, const string output);
146 
147 
148  /// Final absolute path to the file.
150 
151  /// File type
153 
154  /// dictionary of placeholders
156 
157  /// Prefix path for output files.
158  static string output_dir;
159 
160  /// Prefix path for input files (directory of the main input file).
161  static string root_dir;
162 };
163 
164 #endif /* FILE_NAME_HH_ */
string abs_file_path_
Final absolute path to the file.
Definition: file_path.hh:149
FilePath()
Definition: file_path.hh:58
FileType
Possible types of file.
Definition: file_path.hh:50
#define DECLARE_EXCEPTION(ExcName, Format)
Macro for simple definition of exceptions.
Definition: exceptions.hh:144
bool operator==(const Null &, const Null &)
static string root_dir
Prefix path for input files (directory of the main input file).
Definition: file_path.hh:161
Dedicated class for storing path to input and output files.
Definition: file_path.hh:42
static std::map< string, string > placeholder
dictionary of placeholders
Definition: file_path.hh:155
#define TYPEDEF_ERR_INFO(EI_Type, Type)
Macro to simplify declaration of error_info types.
Definition: exceptions.hh:180
static string output_dir
Prefix path for output files.
Definition: file_path.hh:158
FileType file_type_
File type.
Definition: file_path.hh:152