Flow123d
file_path.hh
Go to the documentation of this file.
1 /*
2  * file_name.hh
3  *
4  * Created on: May 23, 2012
5  * Author: jb
6  */
7 
8 #ifndef FILE_NAME_HH_
9 #define FILE_NAME_HH_
10 
11 #include <string>
12 
13 #include "system/exceptions.hh"
14 
15 //#include "input/json_to_storage.hh"
16 
17 using namespace std;
18 
19 
20 
21 
22 /**
23  * @brief Dedicated class for storing path to input and output files.
24  *
25  * FilePath objects are constructed from given absolute or relative path to the file and its type (input or output).
26  * Before you create any instance of the class you have to call static method @p set_io_dirs to set:
27  * - working directory of the program (when it was started)
28  * - root input directory, i.e. directory of the main input file (given by -s parameter)
29  * - input directory, used to replace ${INPUT} placeholder (given by -i parameter)
30  * - output directory, where all output files should be placed (given by -o parameter)
31  *
32  */
33 
34 class FilePath {
35 public:
36 
37 
38  TYPEDEF_ERR_INFO( EI_Path, string);
39  DECLARE_EXCEPTION( ExcAbsOutputPath, << "Can not set absolute path " << EI_Path::qval << "for an output file." );
40 
41  /// Possible types of file.
42  enum FileType {
44  output_file
45  };
46 
47  /**
48  * Default constructor, necessary when using Input::Record::opt_val() to initialize a FilePath.
49  */
50  FilePath() : abs_file_path("/__NO_FILE_NAME_GIVEN__") {}
51 
52  /**
53  * Translates the given absolute or relative path to a file @p file_path depending on the file type @p ft.
54  *
55  * For input files:
56  * - For relative path prepend absolute path of the directory of the main input file (root directory).
57  * - Replace ${INPUT} place holder with the input directory given at command line.
58  *
59  * For output files:
60  * - Forbids absolute output paths.
61  * - For relative output path prepends it by the output directory given at the command line.
62  */
63  FilePath(const string file_path, const FileType ft);
64 
65  /**
66  * Set:
67  * - working directory (used only if the output directory is relative)
68  * - root directory (of the main input file)
69  * - input directory to replace ${INPUT} place holder
70  * - output directory used as prefix to the output files (relative output dirs are relative to the working directory)
71  */
72  static void set_io_dirs(const string working_dir, const string root_input,const string input,const string output);
73 
74  /**
75  * This class is implicitly convertible to string.
76  */
77  inline operator string() const
78  {return abs_file_path;}
79 
80  /*!
81  * @brief Add new item to place holder.
82  *
83  * Placeholder is extended by adding a single new item. The item can be used in the name of the input or output file name.
84  * Currently, the only supported placeholder is ${INPUT}.
85  *
86  * @par Example usage:
87  * @code
88  * FilePath::add_placeholder_item("${SUBST_VAL}", "path/value");
89  * @endcode
90  *
91  * @param[in] key Key of new item.
92  * @param[in] value Value of new item.
93  */
94  static void add_placeholder(string key,string value);
95 
96 
97 private:
98  /**
99  * Substitutes placeholders in @p abs_file_path.
100  */
101  void substitute_value();
102 
103 
104  /// Final absolute path to the file.
106 
107  /// dictionary of placeholders
109 
110  /// Prefix path for output files.
111  static string output_dir;
112 
113  /// Prefix path for input files (directory of the main input file).
114  static string root_dir;
115 };
116 
117 #endif /* FILE_NAME_HH_ */