adbncfs  0.9.1
spawn.h
Go to the documentation of this file.
1 /*
2  * $Id: spawn.h 2 2015-12-03 19:46:25Z wejaeger $
3  *
4  * File: spawn.h
5  * Author: Werner Jaeger
6  *
7  * Created on November 19, 2015, 5:31 PM
8  *
9  * Copyright 2015 Werner Jaeger.
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 
25 #ifndef SPAWN_H
26 #define SPAWN_H
27 
28 #include <ext/stdio_filebuf.h> // NB: Specific to libstdc++
29 #include <iostream>
30 #include <fstream>
31 
33 class Cpipe
34 {
35 public:
36 
37  Cpipe();
38  virtual ~Cpipe();
39 
40  const inline int readFd() const;
41  const inline int writeFd() const;
42  void close();
43 
44 private:
45  int m_aiFd[2];
46 };
47 
48 //
49 // Usage:
50 // spawn s(argv)
51 // s.stdin << ...
52 // s.stdout >> ...
53 // s.send_eol()
54 // s.wait()
55 //
66 class Spawn
67 {
68 public:
69  Spawn(const char* const argv[], bool fUseStdErr = false, bool fWithPath = false, const char* const envp[] = NULL);
70  virtual ~Spawn();
71 
77  std::istream& inStream() { return(stdout); }
78 
84  std::ostream& outStream() { return(stdin); }
85  void sendEof();
86  int wait();
87 
88 private:
90  Spawn(const Spawn& orig);
91 
93  Spawn& operator=(const Spawn& orig);
94 
96  __gnu_cxx::stdio_filebuf<char> *m_pWriteBuf;
97  __gnu_cxx::stdio_filebuf<char> *m_pReadBuf;
98  std::ostream stdin;
99  std::istream stdout;
102 };
103 
104 #endif /* SPAWN_H */
105 
Cpipe m_WritePipe
Definition: spawn.h:100
Cpipe m_ReadPipe
Definition: spawn.h:101
Spawn & operator=(const Spawn &orig)
Prevent assignment.
virtual ~Spawn()
Definition: spawn.cpp:143
__gnu_cxx::stdio_filebuf< char > * m_pReadBuf
Definition: spawn.h:97
int wait()
Wait until child process has terminated.
Definition: spawn.cpp:172
std::istream & inStream()
Access the child process stdout as in stream to read from.
Definition: spawn.h:77
std::ostream stdin
Definition: spawn.h:98
const int writeFd() const
Definition: spawn.cpp:52
void sendEof()
Definition: spawn.cpp:152
std::ostream & outStream()
Access the child process stdin as out stream to write to the process.
Definition: spawn.h:84
Spawn(const char *const argv[], bool fUseStdErr=false, bool fWithPath=false, const char *const envp[]=NULL)
Spawns a child process.
Definition: spawn.cpp:87
const int readFd() const
Definition: spawn.cpp:47
int m_iChildPid
Definition: spawn.h:95
Spawn a child process.
Definition: spawn.h:66
std::istream stdout
Definition: spawn.h:99
void close()
Definition: spawn.cpp:57
Cpipe()
Definition: spawn.cpp:37
virtual ~Cpipe()
Definition: spawn.cpp:63
int m_aiFd[2]
Definition: spawn.h:45
__gnu_cxx::stdio_filebuf< char > * m_pWriteBuf
Definition: spawn.h:96
Wrapping pipe in a class makes sure they are closed when we leave scope.
Definition: spawn.h:33