Documentation for OpenFLUID 2.2.0
Process.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 
33 /**
34  @file Process.hpp
35 
36  @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
37  */
38 
39 
40 #ifndef __OPENFLUID_UTILS_PROCESS_HPP__
41 #define __OPENFLUID_UTILS_PROCESS_HPP__
42 
43 
44 #include <string>
45 #include <vector>
46 #include <map>
47 
48 #include <openfluid/dllexport.hpp>
49 
50 
51 namespace openfluid { namespace utils {
52 
53 /**
54  Management of processes to launch external programs
55 
56  <I>Example using the run() method with program and arguments</I>
57  @snippet misc/process.cpp usage_run_progargs
58 
59  <I>Example using the run() method with fine tuning of command configuration</I>
60  @snippet misc/process.cpp usage_run_cmd
61 
62  <I>Example using the execute() method</I>
63  @snippet misc/process.cpp usage_execute
64 */
66 {
67  public:
68 
69  /**
70  Structure to hold execution environment for processes
71  */
73  {
74  /**
75  Custom environment variables defined for a process.
76  If a variable already exists in parent environment when inherited, the parent variable is overidden.
77  */
78  std::map<std::string,std::string> Vars;
79 
80  /**
81  Enable/disable inheritance of parent environment, enabled by default.
82  */
83  bool Inherits;
84 
85  Environment() : Inherits(true)
86  { }
87  };
88 
89  /**
90  Structure to hold command configuration for processes
91  */
93  {
94  /**
95  Path of the program to execute.
96  */
97  std::string Program;
98 
99  /**
100  Program arguments
101  */
102  std::vector<std::string> Args;
103 
104  /**
105  Path to the file for redirection of standard output stream (stdout) during program execution.
106  No redirection is performed if empty.
107  */
108  std::string OutFile;
109 
110  /**
111  Path to the file for redirection of standard error stream (stderr) during program execution.
112  No redirection is performed if empty.
113  */
114  std::string ErrFile;
115 
116  /**
117  Path to work directory where the process is launched.
118  No workdir is set if empty.
119  */
120  std::string WorkDir;
121 
122  /**
123  Joins program and arguments as a single string
124  @param[in] Sep The separator to use for joining
125  @return The string after joining
126  */
127  std::string joined(const std::string& Sep = " ") const;
128 
129  /**
130  Sets the redirection file for output using normalized naming
131  @param[in] DirPath The directory path for output file
132  @param[in] NameBase The base to use for naming the output file
133  */
134  void setOutFile(const std::string& DirPath, const std::string& NameBase);
135 
136  /**
137  Sets the redirection file for error using normalized naming
138  @param[in] DirPath The directory path for error file
139  @param[in] NameBase The base to use for naming the error file
140  */
141  void setErrFile(const std::string& DirPath, const std::string& NameBase);
142 
143  /**
144  Sets the redirection files for output and error using normalized naming
145  @param[in] DirPath The directory path for files
146  @param[in] NameBase The base to use for naming the files
147  */
148  void setOutErrFiles(const std::string& DirPath, const std::string& NameBase);
149  };
150 
151 
152  private:
153 
154  const Command m_Cmd;
155 
156  const Environment m_Env;
157 
158  int m_ExitCode = -1;
159 
160  std::string m_ErrorMsg;
161 
162  std::vector<std::string> m_OutLines;
163 
164  std::vector<std::string> m_ErrLines;
165 
166  void reset();
167 
168 
169  public:
170 
171  /**
172  Constructor using program and arguments to configure the process
173  @param[in] Program Path of the program to execute.
174  @param[in] Args Program arguments (empty by default)
175  @param[in] Env Execution environment (parent environment by default)
176  */
177  Process(const std::string& Program, const std::vector<std::string>& Args = {},
178  const Environment& Env = Environment());
179 
180  /**
181  Constructor using command structure to configure the process
182  @param[in] Cmd Command configuration
183  @param[in] Env Execution environment (parent environment by default)
184  */
185  Process(const Command& Cmd, const Environment& Env = {});
186 
187  /**
188  Runs the process
189  @return true if the process has been run, false otherwise. Do not confuse with exit code
190  */
191  bool run();
192 
193  /**
194  Gets the exit code returned by the executed program when finished
195  @return The exit code
196  */
197  int getExitCode() const
198  {
199  return m_ExitCode;
200  }
201 
202  /**
203  Gets the error message in case of execution problem
204  @return The error message. Do not confuse with exit code
205  */
206  std::string getErrorMessage() const
207  {
208  return m_ErrorMsg;
209  }
210 
211  /**
212  Accesses to the lines of the standard output stream once the process have run.
213  These lines set is empty if the redirection of the standard output stream has been enabled
214  @return The output stream lines
215  */
216  const std::vector<std::string>& stdOutLines() const
217  {
218  return m_OutLines;
219  }
220 
221  /**
222  Accesses to the lines of the standard error stream once the process have run.
223  These lines set is empty if the redirection of the standard error stream has been enabled
224  @return The error stream lines
225  */
226  const std::vector<std::string>& stdErrLines() const
227  {
228  return m_ErrLines;
229  }
230 
231  /**
232  Accesses the execution environment used for the process
233  @return The execution environment
234  */
235  const Environment& environment() const
236  {
237  return m_Env;
238  }
239 
240  /**
241  Accesses the command configuration used for the process
242  @return The command configuration
243  */
244  const Command& command() const
245  {
246  return m_Cmd;
247  }
248 
249  /**
250  Executes a process using the given program, arguments and execution environment
251  @param[in] Program Path of the program to execute.
252  @param[in] Args Program arguments (empty by default)
253  @param[in] Env Execution environment (parent environment by default)
254  */
255  static int execute(const std::string& Program, const std::vector<std::string>& Args = {},
256  const Environment& Env = Environment());
257 
258  /**
259  Executes a process using the given command configuration and execution environment
260  @param[in] Cmd Command configuration
261  @param[in] Env Execution environment (parent environment by default)
262  */
263  static int execute(const Command& Cmd, const Environment& Env = Environment());
264 
265  /**
266  Executes a process using the given program, arguments and execution environment
267  with default standard i/o (stdout,stderr,stdin). It works as std::system with more options.
268  @param[in] Program Path of the program to execute.
269  @param[in] Args Program arguments (empty by default)
270  @param[in] Env Execution environment (parent environment by default)
271  */
272  static int system(const std::string& Program, const std::vector<std::string>& Args = {},
273  const Environment& Env = Environment());
274 
275  /**
276  Executes a process using the given command configuration and execution environment
277  with default standard i/o (stdout,stderr,stdin). It works as std::system with more options.
278  @param[in] Cmd Command configuration
279  @param[in] Env Execution environment (parent environment by default)
280  */
281  static int system(const Command& Cmd, const Environment& Env = Environment());
282 
283 };
284 
285 
286 } } // namespaces
287 
288 #endif /* __OPENFLUID_UTILS_PROCESS_HPP__ */
Definition: Process.hpp:66
std::string getErrorMessage() const
Definition: Process.hpp:206
Process(const Command &Cmd, const Environment &Env={})
const std::vector< std::string > & stdErrLines() const
Definition: Process.hpp:226
static int system(const std::string &Program, const std::vector< std::string > &Args={}, const Environment &Env=Environment())
static int execute(const Command &Cmd, const Environment &Env=Environment())
const Command & command() const
Definition: Process.hpp:244
static int execute(const std::string &Program, const std::vector< std::string > &Args={}, const Environment &Env=Environment())
Process(const std::string &Program, const std::vector< std::string > &Args={}, const Environment &Env=Environment())
const Environment & environment() const
Definition: Process.hpp:235
static int system(const Command &Cmd, const Environment &Env=Environment())
int getExitCode() const
Definition: Process.hpp:197
const std::vector< std::string > & stdOutLines() const
Definition: Process.hpp:216
#define OPENFLUID_API
Definition: dllexport.hpp:86
Definition: ApplicationException.hpp:47
Definition: Process.hpp:93
std::string Program
Definition: Process.hpp:97
std::string ErrFile
Definition: Process.hpp:114
std::string joined(const std::string &Sep=" ") const
void setOutErrFiles(const std::string &DirPath, const std::string &NameBase)
void setErrFile(const std::string &DirPath, const std::string &NameBase)
std::string OutFile
Definition: Process.hpp:108
std::string WorkDir
Definition: Process.hpp:120
std::vector< std::string > Args
Definition: Process.hpp:102
void setOutFile(const std::string &DirPath, const std::string &NameBase)
Definition: Process.hpp:73
Environment()
Definition: Process.hpp:85
bool Inherits
Definition: Process.hpp:83
std::map< std::string, std::string > Vars
Definition: Process.hpp:78