Flow123d  jenkins-Flow123d-windows-release-multijob-285
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  */
49  : abs_file_path("/__NO_FILE_NAME_GIVEN__"),
50  file_type_(output_file)
51  {}
52 
53  /**
54  * Translates the given absolute or relative path to a file @p file_path depending on the file type @p ft.
55  *
56  * For input files:
57  * - For relative path prepend absolute path of the directory of the main input file (root directory).
58  * - Replace ${INPUT} place holder with the input directory given at command line.
59  *
60  * For output files:
61  * - Forbids absolute output paths.
62  * - For relative output path prepends it by the output directory given at the command line.
63  */
64  FilePath(string file_path, const FileType ft);
65 
66  /**
67  * Set:
68  * - working directory (used only if the output directory is relative)
69  * - root directory (of the main input file)
70  * - input directory to replace ${INPUT} place holder
71  * - output directory used as prefix to the output files (relative output dirs are relative to the working directory)
72  */
73  static void set_io_dirs(const string working_dir, const string root_input,const string input,const string output);
74 
75  /**
76  * This class is implicitly convertible to string.
77  */
78  inline operator string() const
79  {return abs_file_path;}
80 
81  /*!
82  * @brief Add new item to place holder.
83  *
84  * Placeholder is extended by adding a single new item. The item can be used in the name of the input or output file name.
85  * Currently, the only supported placeholder is ${INPUT}.
86  *
87  * @par Example usage:
88  * @code
89  * FilePath::add_placeholder_item("${SUBST_VAL}", "path/value");
90  * @endcode
91  *
92  * @param[in] key Key of new item.
93  * @param[in] value Value of new item.
94  */
95  static void add_placeholder(string key,string value);
96 
97  /**
98  * Return absolute path of actual working directory.
99  */
100  static const string get_absolute_working_dir();
101 
102  /// Equality comparison operators for regions.
103  inline bool operator ==(const FilePath &other) const
104  {return abs_file_path == string(other); }
105 
106 
107  /**
108  * For an output filepath, the directory part (up to last separator) is
109  * extracted and all subdirectories are created if doesn't exist yet.
110  */
111  void create_output_dir();
112 
113 private:
114  /**
115  * Substitutes placeholders in @p abs_file_path.
116  */
117  void substitute_value();
118 
119 
120  /**
121  * Test if get path is absolute for used operating system.
122  */
123  static bool is_absolute_path(const string path);
124 
125 
126  /**
127  * Check if directory stored in output_dir doesn't exist and create its
128  */
129  static void create_dir(string dir);
130 
131 
132  /**
133  * Create canonical path of output directory given by relative path.
134  */
135  static void create_canonical_path(const string working_dir, const string output);
136 
137 
138  /// Final absolute path to the file.
140 
141  /// File type
143 
144  /// dictionary of placeholders
146 
147  /// Prefix path for output files.
148  static string output_dir;
149 
150  /// Prefix path for input files (directory of the main input file).
151  static string root_dir;
152 };
153 
154 #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:139
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:151
#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:145
static string output_dir
Prefix path for output files.
Definition: file_path.hh:148
FileType file_type_
File type.
Definition: file_path.hh:142