added comments to pipestream, including WEXITSTATUS
[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 #include <istream>
35 #include <ostream>
36 #include <cstdio>
37
38 #include "exception.hxx"
39
40 /** @brief runs command and provides buffered input for it through pipe
41  *
42  * opens pipe to command using popen; exit status available after destruction
43  * (use WEXITSTATUS to get the "regular" return code (lowest byte))
44  *
45  * ATTENTION: A lot of mysterious STL bugs occured
46  *            with a "real" buffer (buffer larger than 1 byte and up to 100 bytes)
47  *            -> Keep it slow and working!
48  */
49 class inpipebuf : public std::streambuf
50 {
51 protected:
52     char buffer;
53     FILE *pipe;
54
55     // "callback" variables for destructor to store exit status
56     bool *status_set;
57     int *exit_status;
58
59 public:
60     inpipebuf(const std::string& command)
61     {
62         status_set = NULL;
63         exit_status = NULL;
64
65         pipe = popen (command.c_str(), "r");
66         if (pipe == NULL)
67             throw EXCEPTION (pipestream_error, "can't open program or permission denied");
68
69         // force underflow
70         setg (&buffer, &buffer, &buffer);
71     }
72
73     ~inpipebuf()
74     {
75         if (pipe != NULL) {
76             int pclose_exit = pclose (pipe);
77
78             if (exit_status && pclose_exit != -1) {
79                 *status_set = true;
80                 *exit_status = pclose_exit;
81             }
82
83             pipe = NULL;
84         }
85     }
86
87     /** note: exit status only available after destruction */
88     void store_exit_status(bool *_status_set, int *_exit_status)
89     { status_set = _status_set; exit_status = _exit_status; }
90
91 protected:
92     virtual int_type underflow()
93     {
94         if (gptr() < egptr())
95             return traits_type::to_int_type(*gptr());
96
97         buffer = fgetc (pipe);
98         if (feof (pipe))
99         {
100             // ERROR or EOF
101             return EOF;
102         }
103
104         setg (&buffer, &buffer, &buffer+sizeof(char));
105
106         return traits_type::to_int_type(*gptr());
107     }
108 };
109
110 /** @brief stream around inpipebuf -- see comment there */
111 class inpipestream : public std::istream
112 {
113 protected:
114     inpipebuf buf;
115
116 public:
117     inpipestream(const std::string& command)
118             : std::istream(&buf), buf(command)
119     {}
120
121     void store_exit_status(bool *_status_set, int *_exit_status)
122     { buf.store_exit_status(_status_set, _exit_status); }
123 };
124
125
126 /** @brief runs command and provides buffered ouptput from it through pipe
127  *
128  * opens pipe to command using popen; exit status available after destruction
129  * (use WEXITSTATUS to get the "regular" return code (lowest byte))
130  */
131 class outpipebuf : public std::streambuf
132 {
133 protected:
134     FILE *pipe;
135
136     // "callback" variables for destructor to store exit status
137     bool *status_set;
138     int *exit_status;
139
140 public:
141     outpipebuf(const std::string& command)
142     {
143         status_set = NULL;
144         exit_status = NULL;
145
146         pipe = popen (command.c_str(), "w");
147         if (pipe == NULL)
148             throw EXCEPTION (pipestream_error, "can't open program or permission denied");
149     }
150
151     ~outpipebuf()
152     {
153         if (pipe != NULL) {
154             int pclose_exit = pclose (pipe);
155
156             if (exit_status && pclose_exit != -1) {
157                 *status_set = true;
158                 *exit_status = pclose_exit;
159             }
160
161             pipe = NULL;
162         }
163     }
164
165     /** note: exit status only available after destruction */
166     void store_exit_status(bool *_status_set, int *_exit_status)
167     { status_set = _status_set; exit_status = _exit_status; }
168
169 protected:
170     virtual int_type overflow(int_type c)
171     {
172         if (c != EOF)
173         {
174             if (fputc(c,pipe)==EOF)
175                 return EOF;
176         }
177         return c;
178     }
179
180     virtual std::streamsize xsputn(const char* s, std::streamsize num)
181     {
182         return fwrite(s,num,1,pipe);
183     }
184 };
185
186
187 /** @brief stream around outpipebuf -- see comment there */
188 class outpipestream : public std::ostream
189 {
190 protected:
191     outpipebuf buf;
192 public:
193     outpipestream(const std::string& command)
194             : std::ostream(&buf), buf(command)
195     {}
196
197     void store_exit_status(bool *_status_set, int *_exit_status)
198     { buf.store_exit_status(_status_set, _exit_status); }
199 };
200
201 #endif