2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
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.
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.
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.
20 /***************************************************************************
21 inpipestream.hxx - C++ streambuffer wrapper
23 begin : Thu Dec 27 2001
24 copyright : (C) 2001 by Intra2net AG
25 ***************************************************************************/
38 #include "exception.hxx"
40 /** @brief runs command and provides buffered input for it through pipe
42 * opens pipe to command using popen; exit status available after destruction
43 * (use WEXITSTATUS to get the "regular" return code (lowest byte))
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!
49 class inpipebuf : public std::streambuf
55 // "callback" variables for destructor to store exit status
60 inpipebuf(const std::string& command)
65 pipe = popen (command.c_str(), "r");
67 throw EXCEPTION (pipestream_error, "can't open program or permission denied");
70 setg (&buffer, &buffer, &buffer);
76 int pclose_exit = pclose (pipe);
78 if (exit_status && pclose_exit != -1) {
80 *exit_status = pclose_exit;
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; }
92 virtual int_type underflow()
95 return traits_type::to_int_type(*gptr());
97 buffer = fgetc (pipe);
104 setg (&buffer, &buffer, &buffer+sizeof(char));
106 return traits_type::to_int_type(*gptr());
110 /** @brief stream around inpipebuf -- see comment there */
111 class inpipestream : public std::istream
117 inpipestream(const std::string& command)
118 : std::istream(&buf), buf(command)
121 void store_exit_status(bool *_status_set, int *_exit_status)
122 { buf.store_exit_status(_status_set, _exit_status); }
125 /** @brief runs command and returns it's output as string
126 * @param command the full command with all parameters
127 * @param exit_status the full exit status, use WEXITSTATUS to get the "regular" return code
128 * @returns the output (stderr) of the called program
130 std::string pipe_to_string(const std::string& command, std::string *error=NULL, int *_exit_status=NULL)
137 inpipestream ips(command);
139 ips.store_exit_status(&exit_set, _exit_status);
144 ips.read(buffer, sizeof(buffer));
145 result.append(buffer, ips.gcount());
148 catch (pipestream_error &e)
162 /** @brief runs command and provides buffered ouptput from it through pipe
164 * opens pipe to command using popen; exit status available after destruction
165 * (use WEXITSTATUS to get the "regular" return code (lowest byte))
167 class outpipebuf : public std::streambuf
172 // "callback" variables for destructor to store exit status
177 outpipebuf(const std::string& command)
182 pipe = popen (command.c_str(), "w");
184 throw EXCEPTION (pipestream_error, "can't open program or permission denied");
190 int pclose_exit = pclose (pipe);
192 if (exit_status && pclose_exit != -1) {
194 *exit_status = pclose_exit;
201 /** note: exit status only available after destruction */
202 void store_exit_status(bool *_status_set, int *_exit_status)
203 { status_set = _status_set; exit_status = _exit_status; }
206 virtual int_type overflow(int_type c)
210 if (fputc(c,pipe)==EOF)
216 virtual std::streamsize xsputn(const char* s, std::streamsize num)
218 return fwrite(s,num,1,pipe);
223 /** @brief stream around outpipebuf -- see comment there */
224 class outpipestream : public std::ostream
229 outpipestream(const std::string& command)
230 : std::ostream(&buf), buf(command)
233 void store_exit_status(bool *_status_set, int *_exit_status)
234 { buf.store_exit_status(_status_set, _exit_status); }