Documentation for OpenFLUID 2.2.1
Process.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2021-2026, INRAE
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  @author Armel THÖNI <armel.thoni@inrae.fr>
38  */
39 
40 
41 #ifndef __OPENFLUID_UTILS_PROCESS_HPP__
42 #define __OPENFLUID_UTILS_PROCESS_HPP__
43 
44 
45 #include <string>
46 #include <vector>
47 #include <map>
48 #include <iostream>
49 
50 #include <openfluid/dllexport.hpp>
51 
52 
53 namespace openfluid { namespace utils {
54 
55 /**
56  Management of processes to launch external programs
57 
58  <I>Example using the run() method with program and arguments</I>
59  @snippet misc/process.cpp usage_run_progargs
60 
61  <I>Example using the run() method with fine tuning of command configuration</I>
62  @snippet misc/process.cpp usage_run_cmd
63 
64  <I>Example using the execute() method</I>
65  @snippet misc/process.cpp usage_execute
66 */
68 {
69  public:
70 
71  /**
72  Structure to hold execution environment for processes
73  */
75  {
76  /**
77  Custom environment variables defined for a process.
78  If a variable already exists in parent environment when inherited, the parent variable is overidden.
79  */
80  std::map<std::string,std::string> Vars;
81 
82  /**
83  Enable/disable inheritance of parent environment, enabled by default.
84  */
85  bool Inherits;
86 
87  Environment() : Inherits(true)
88  { }
89  };
90 
91  /**
92  Structure to hold command configuration for processes
93  */
95  {
96  /**
97  Path of the program to execute.
98  */
99  std::string Program;
100 
101  /**
102  Program arguments
103  */
104  std::vector<std::string> Args;
105 
106  /**
107  Path to the file for redirection of standard output stream (stdout) during program execution.
108  No redirection is performed if empty.
109  */
110  std::string OutFile;
111 
112  /**
113  Path to the file for redirection of standard error stream (stderr) during program execution.
114  No redirection is performed if empty.
115  */
116  std::string ErrFile;
117 
118  /**
119  Path to work directory where the process is launched.
120  No workdir is set if empty.
121  */
122  std::string WorkDir;
123 
124  /**
125  Joins program and arguments as a single string
126  @param[in] Sep The separator to use for joining
127  @return The string after joining
128  */
129  std::string joined(const std::string& Sep = " ") const;
130 
131  /**
132  Sets the redirection file for output using normalized naming
133  @param[in] DirPath The directory path for output file
134  @param[in] NameBase The base to use for naming the output file
135  */
136  void setOutFile(const std::string& DirPath, const std::string& NameBase);
137 
138  /**
139  Sets the redirection file for error using normalized naming
140  @param[in] DirPath The directory path for error file
141  @param[in] NameBase The base to use for naming the error file
142  */
143  void setErrFile(const std::string& DirPath, const std::string& NameBase);
144 
145  /**
146  Sets the redirection files for output and error using normalized naming
147  @param[in] DirPath The directory path for files
148  @param[in] NameBase The base to use for naming the files
149  */
150  void setOutErrFiles(const std::string& DirPath, const std::string& NameBase);
151  };
152 
153 
154  private:
155 
156  const Command m_Cmd;
157 
158  const Environment m_Env;
159 
160  int m_ExitCode = -1;
161 
162  std::string m_ErrorMsg;
163 
164  std::vector<std::string> m_OutLines;
165 
166  std::vector<std::string> m_ErrLines;
167 
168  void reset();
169 
170 
171  public:
172 
173  /**
174  Constructor using program and arguments to configure the process
175  @param[in] Program Path of the program to execute.
176  @param[in] Args Program arguments (empty by default)
177  @param[in] Env Execution environment (parent environment by default)
178  */
179  Process(const std::string& Program, const std::vector<std::string>& Args = {},
180  const Environment& Env = Environment());
181 
182  /**
183  Constructor using command structure to configure the process
184  @param[in] Cmd Command configuration
185  @param[in] Env Execution environment (parent environment by default)
186  */
187  Process(const Command& Cmd, const Environment& Env = {});
188 
189  /**
190  Runs the process
191  @return true if the process has been run, false otherwise. Do not confuse with exit code
192  */
193  bool run();
194 
195  /**
196  Gets the exit code returned by the executed program when finished
197  @return The exit code
198  */
199  int getExitCode() const
200  {
201  return m_ExitCode;
202  }
203 
204  /**
205  Gets the error message in case of execution problem
206  @return The error message. Do not confuse with exit code
207  */
208  std::string getErrorMessage() const
209  {
210  return m_ErrorMsg;
211  }
212 
213  /**
214  Accesses to the lines of the standard output stream once the process have run.
215  These lines set is empty if the redirection of the standard output stream has been enabled
216  @return The output stream lines
217  */
218  const std::vector<std::string>& stdOutLines() const
219  {
220  return m_OutLines;
221  }
222 
223  /**
224  Accesses to the lines of the standard error stream once the process have run.
225  These lines set is empty if the redirection of the standard error stream has been enabled
226  @return The error stream lines
227  */
228  const std::vector<std::string>& stdErrLines() const
229  {
230  return m_ErrLines;
231  }
232 
233  /**
234  Accesses the execution environment used for the process
235  @return The execution environment
236  */
237  const Environment& environment() const
238  {
239  return m_Env;
240  }
241 
242  /**
243  Accesses the command configuration used for the process
244  @return The command configuration
245  */
246  const Command& command() const
247  {
248  return m_Cmd;
249  }
250 
251  /**
252  Executes a process using the given program, arguments and execution environment
253  @param[in] Program Path of the program to execute.
254  @param[in] Args Program arguments (empty by default)
255  @param[in] Env Execution environment (parent environment by default)
256  */
257  static int execute(const std::string& Program, const std::vector<std::string>& Args = {},
258  const Environment& Env = Environment());
259 
260  /**
261  Executes a process using the given command configuration and execution environment
262  @param[in] Cmd Command configuration
263  @param[in] Env Execution environment (parent environment by default)
264  */
265  static int execute(const Command& Cmd, const Environment& Env = Environment());
266 
267  /**
268  Executes a process using the given program, arguments and execution environment
269  with default standard i/o (stdout,stderr,stdin). It works as std::system with more options.
270  @param[in] Program Path of the program to execute.
271  @param[in] Args Program arguments (empty by default)
272  @param[in] Env Execution environment (parent environment by default)
273  */
274  static int system(const std::string& Program, const std::vector<std::string>& Args = {},
275  const Environment& Env = Environment());
276 
277  /**
278  Executes a process using the given command configuration and execution environment
279  with default standard i/o (stdout,stderr,stdin). It works as std::system with more options.
280  @param[in] Cmd Command configuration
281  @param[in] Env Execution environment (parent environment by default)
282  */
283  static int system(const Command& Cmd, const Environment& Env = Environment());
284 
285 };
286 
287 
288 } } // namespaces
289 
290 #endif /* __OPENFLUID_UTILS_PROCESS_HPP__ */
Definition: Process.hpp:68
std::string getErrorMessage() const
Definition: Process.hpp:208
Process(const Command &Cmd, const Environment &Env={})
const std::vector< std::string > & stdErrLines() const
Definition: Process.hpp:228
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:246
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:237
static int system(const Command &Cmd, const Environment &Env=Environment())
int getExitCode() const
Definition: Process.hpp:199
const std::vector< std::string > & stdOutLines() const
Definition: Process.hpp:218
#define OPENFLUID_API
Definition: dllexport.hpp:86
Definition: ApplicationException.hpp:47
Definition: Process.hpp:95
std::string Program
Definition: Process.hpp:99
std::string ErrFile
Definition: Process.hpp:116
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:110
std::string WorkDir
Definition: Process.hpp:122
std::vector< std::string > Args
Definition: Process.hpp:104
void setOutFile(const std::string &DirPath, const std::string &NameBase)
Definition: Process.hpp:75
Environment()
Definition: Process.hpp:87
bool Inherits
Definition: Process.hpp:85
std::map< std::string, std::string > Vars
Definition: Process.hpp:80