b2api
B2000++ API Reference Manual, VERSION 4.6
 
Loading...
Searching...
No Matches
b2subprocess.H
1//------------------------------------------------------------------------
2// b2subprocess.H --
3//
4// written by doreille
5//
6// Copyright (c) 2009 SMR Engineering & Development SA
7// 2502 Bienne, Switzerland
8//
9// All Rights Reserved. Proprietary source code. The contents of
10// this file may not be disclosed to third parties, copied or
11// duplicated in any form, in whole or in part, without the prior
12// written permission of SMR.
13//------------------------------------------------------------------------
14
15#ifndef B2SUBPROCESS_H_
16#define B2SUBPROCESS_H_
17
18#include <sys/wait.h>
19
20#include <sstream>
21
22#include "utils/b2exception.H"
23
24namespace b2000 {
25
26class Subprocess : public std::ostringstream {
27public:
28 int call(
29 const std::string& out = "cout", const std::string& err = "cerr",
30 bool raise_exception_on_error = false) {
31 std::string s = str();
32 if (out != "cout") {
33 s += " >";
34 s += out;
35 }
36 if (err != "cerr") {
37 if (err == "cout") {
38 s += " 2>&1";
39 } else {
40 s += " 2>";
41 s += err;
42 }
43 }
44
45 returncode = ::system(s.c_str());
46 if (WIFSIGNALED(returncode) || (WEXITSTATUS(returncode) && raise_exception_on_error)) {
47 Exception() << get_error() << THROW;
48 }
49 return WEXITSTATUS(returncode);
50 }
51
52 std::string get_error() const {
53 std::ostringstream res;
54 if (WIFSIGNALED(returncode)) {
55 res << "Command " << str() << " killed by signal " << WTERMSIG(returncode);
56 } else if (WEXITSTATUS(returncode) > 0) {
57 res << "Command " << str() << " terminates with nonzero exit status "
58 << WEXITSTATUS(returncode);
59 }
60 return res.str();
61 }
62
63private:
64 ::pid_t returncode;
65};
66
67} // namespace b2000
68
69#endif /* B2SUBPROCESS_H_ */
#define THROW
Definition b2exception.H:198
Contains the base classes for implementing Finite Elements.
Definition b2boundary_condition.H:32