rename pipe_to_string() to capture_exec() and improve it's interface
[libi2ncommon] / src / pipestream.hxx
1  /*
2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
4
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
7
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
13
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
16
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
19 */
20 /***************************************************************************
21               inpipestream.hxx  -  C++ streambuffer wrapper 
22                              -------------------
23     begin                : Thu Dec 27 2001
24     copyright            : (C) 2001 by Intra2net AG
25  ***************************************************************************/
26
27 #ifndef _PIPESTREAM
28 #define _PIPESTREAM
29
30 #include <stdio.h>
31
32 #include <string>
33 #include <streambuf>
34
35 struct ExecResult
36 {
37     /** if the program exited normally and returned a return code */
38     bool normal_exit;
39
40     /** the real return code of the program, only set when normal_exit true */
41     char return_code;
42
43     /** if the program was terminated by a signal */
44     bool terminated_by_signal;
45
46     /** number of the signal that terminated the program, only valid when terminated_by_signal true */
47     int signal;
48
49     /** errormessage if we have one */
50     std::string error_message;
51 };
52 typedef struct ExecResult ExecResult;
53
54 std::string capture_exec(const std::string& command, ExecResult &rescode);
55
56 std::string capture_exec(const std::string& command)
57 {
58     ExecResult r;
59     return capture_exec(command,r);
60 }
61
62 /** @brief runs command and provides buffered input for it through pipe
63  *
64  * opens pipe to command using popen; exit status available after destruction
65  * (use WEXITSTATUS to get the "regular" return code (lowest byte))
66  *
67  * ATTENTION: A lot of mysterious STL bugs occured
68  *            with a "real" buffer (buffer larger than 1 byte and up to 100 bytes)
69  *            -> Keep it slow and working!
70  */
71 class inpipebuf : public std::streambuf
72 {
73 protected:
74     char buffer;
75     FILE *pipe;
76
77     // "callback" variables for destructor to store exit status
78     bool *status_set;
79     int *exit_status;
80
81 public:
82     inpipebuf(const std::string& command);
83
84     ~inpipebuf();
85
86     void store_exit_status(bool *_status_set, int *_exit_status);
87
88 protected:
89     virtual int_type underflow();
90 };
91
92 /** @brief stream around inpipebuf -- see comment there */
93 class inpipestream : public std::istream
94 {
95 protected:
96     inpipebuf buf;
97
98 public:
99     inpipestream(const std::string& command)
100             : std::istream(&buf), buf(command)
101     {}
102
103     void store_exit_status(bool *_status_set, int *_exit_status)
104     { buf.store_exit_status(_status_set, _exit_status); }
105 };
106
107 /** @brief runs command and provides buffered ouptput from it through pipe
108  *
109  * opens pipe to command using popen; exit status available after destruction
110  * (use WEXITSTATUS to get the "regular" return code (lowest byte))
111  */
112 class outpipebuf : public std::streambuf
113 {
114 protected:
115     FILE *pipe;
116
117     // "callback" variables for destructor to store exit status
118     bool *status_set;
119     int *exit_status;
120
121 public:
122     outpipebuf(const std::string& command);
123
124     ~outpipebuf();
125
126     /** note: exit status only available after destruction */
127     void store_exit_status(bool *_status_set, int *_exit_status);
128
129 protected:
130     virtual int_type overflow(int_type c);
131
132     virtual std::streamsize xsputn(const char* s, std::streamsize num);
133 };
134
135
136 /** @brief stream around outpipebuf -- see comment there */
137 class outpipestream : public std::ostream
138 {
139 protected:
140     outpipebuf buf;
141 public:
142     outpipestream(const std::string& command)
143             : std::ostream(&buf), buf(command)
144     {}
145
146     void store_exit_status(bool *_status_set, int *_exit_status)
147     { buf.store_exit_status(_status_set, _exit_status); }
148 };
149
150 #endif