CARMA C++
CoProc.h
Go to the documentation of this file.
1 // $Id: CoProc.h,v 1.2 2013/08/20 21:56:51 eml Exp $
2 
3 #ifndef SZA_UTIL_COPROC_H
4 #define SZA_UTIL_COPROC_H
5 
16 #include <iostream>
17 #include <vector>
18 
19 namespace sza {
20  namespace util {
21 
22  class CoProc;
23 
24  // Define a class for managing a spawned process whose stdout and
25  // stderr are captured by the parent
26 
27  class CoProc {
28  public:
29 
30  class Pipe {
31 
32  public:
33 
34  ~Pipe();
35 
36  FILE* readFp();
37  FILE* writeFp();
38 
39  int readFd();
40  int writeFd();
41 
42  void closeForWrite();
43  void closeForRead();
44 
45  void close();
46 
47  private:
48 
49  friend class CoProc;
50 
51  int fd_[2]; // pair of file descriptors
52 
53  Pipe();
54  };
55 
59  CoProc(std::string exe);
60 
61  CoProc(char *exe);
62 
63  CoProc(char *exe, FILE** stdInFp, FILE** stdOutFp, FILE** stdErrFp);
64 
65  CoProc(char *exe, char** argv,
66  FILE** stdInFp, FILE** stdOutFp, FILE** stdErrFp);
67 
68  void fork(char *exe, char** argv,
69  FILE** stdInFp, FILE** stdOutFp, FILE** stdErrFp);
70 
71  CoProc::Pipe* getPipe();
72 
73  CoProc::Pipe* stdIn();
74  CoProc::Pipe* stdOut();
75  CoProc::Pipe* stdErr();
76 
80  virtual ~CoProc();
81 
82  // Split a command string into separate tokens
83 
84  static std::vector<std::string> split(std::string command);
85 
86  int readFd();
87  int writeFd();
88 
89  private:
90 
91  int pid_;
92  CoProc::Pipe* stdIn_;
93  CoProc::Pipe* stdOut_;
94  CoProc::Pipe* stdErr_;
95 
96  }; // End class CoProc
97 
98  } // End namespace util
99 } // End namespace sza
100 
101 
102 
103 #endif // End #ifndef SZA_UTIL_COPROC_H