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.cpp - C++ streambuffer wrapper
23 begin : Thu Dec 27 2001
24 copyright : (C) 2001 by Intra2net AG
25 ***************************************************************************/
35 #include "exception.hxx"
36 #include "pipestream.hxx"
38 inpipebuf::inpipebuf(const std::string& command)
43 pipe = popen (command.c_str(), "r");
45 throw EXCEPTION (pipestream_error, "can't open program or permission denied");
48 setg (&buffer, &buffer, &buffer);
51 inpipebuf::~inpipebuf()
54 int pclose_exit = pclose (pipe);
56 if (exit_status && pclose_exit != -1)
60 *exit_status = pclose_exit;
67 /** note: exit status only available after destruction */
68 void inpipebuf::store_exit_status(bool *_status_set, int *_exit_status)
70 status_set = _status_set;
71 exit_status = _exit_status;
74 inpipebuf::int_type inpipebuf::underflow()
77 return traits_type::to_int_type(*gptr());
79 buffer = fgetc (pipe);
86 setg (&buffer, &buffer, &buffer+sizeof(char));
88 return traits_type::to_int_type(*gptr());
91 /** @brief runs command and returns it's output as string
92 * @param command the full command with all parameters
93 * @param exit_status the full exit status, use WEXITSTATUS to get the "regular" return code
94 * @returns the output (stderr) of the called program
96 std::string pipe_to_string(const std::string& command, std::string *error, int *_exit_status)
103 inpipestream ips(command);
105 ips.store_exit_status(&exit_set, _exit_status);
110 ips.read(buffer, sizeof(buffer));
111 result.append(buffer, ips.gcount());
114 catch (pipestream_error &e)
128 outpipebuf::outpipebuf(const std::string& command)
133 pipe = popen (command.c_str(), "w");
135 throw EXCEPTION (pipestream_error, "can't open program or permission denied");
138 outpipebuf::~outpipebuf()
141 int pclose_exit = pclose (pipe);
143 if (exit_status && pclose_exit != -1)
147 *exit_status = pclose_exit;
154 /** note: exit status only available after destruction */
155 void outpipebuf::store_exit_status(bool *_status_set, int *_exit_status)
157 status_set = _status_set;
158 exit_status = _exit_status;
161 outpipebuf::int_type outpipebuf::overflow(int_type c)
165 if (fputc(c,pipe)==EOF)
171 std::streamsize outpipebuf::xsputn(const char* s, std::streamsize num)
173 return fwrite(s,num,1,pipe);