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