From 7e606af56a55ced18d4bad0f535cc183ab6f3398 Mon Sep 17 00:00:00 2001 From: Gerd von Egidy Date: Thu, 17 Dec 2015 10:26:07 +0100 Subject: [PATCH] split pipestream.hxx into .hxx and .cpp --- src/CMakeLists.txt | 1 + src/pipestream.cpp | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/pipestream.hxx | 136 ++++-------------------------------------- 3 files changed, 183 insertions(+), 124 deletions(-) create mode 100644 src/pipestream.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2fb58ac..b2801ce 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,6 +15,7 @@ SET(cpp_sources logread.cpp oftmpstream.cpp pidfile.cpp + pipestream.cpp pointer_func.cpp source_track_basics.cpp stringfunc.cpp diff --git a/src/pipestream.cpp b/src/pipestream.cpp new file mode 100644 index 0000000..d6bf13a --- /dev/null +++ b/src/pipestream.cpp @@ -0,0 +1,170 @@ + /* +The software in this package is distributed under the GNU General +Public License version 2 (with a special exception described below). + +A copy of GNU General Public License (GPL) is included in this distribution, +in the file COPYING.GPL. + +As a special exception, if other files instantiate templates or use macros +or inline functions from this file, or you compile this file and link it +with other works to produce a work based on this file, this file +does not by itself cause the resulting work to be covered +by the GNU General Public License. + +However the source code for this file must still be made available +in accordance with section (3) of the GNU General Public License. + +This exception does not invalidate any other reasons why a work based +on this file might be covered by the GNU General Public License. +*/ +/*************************************************************************** + inpipestream.cpp - C++ streambuffer wrapper + ------------------- + begin : Thu Dec 27 2001 + copyright : (C) 2001 by Intra2net AG + ***************************************************************************/ + +#include + +#include +#include +#include +#include +#include + +#include "exception.hxx" +#include "pipestream.hxx" + +inpipebuf::inpipebuf(const std::string& command) +{ + status_set = NULL; + exit_status = NULL; + + pipe = popen (command.c_str(), "r"); + if (pipe == NULL) + throw EXCEPTION (pipestream_error, "can't open program or permission denied"); + + // force underflow + setg (&buffer, &buffer, &buffer); +} + +inpipebuf::~inpipebuf() +{ + if (pipe != NULL) { + int pclose_exit = pclose (pipe); + + if (exit_status && pclose_exit != -1) { + *status_set = true; + *exit_status = pclose_exit; + } + + pipe = NULL; + } +} + +/** note: exit status only available after destruction */ +void inpipebuf::store_exit_status(bool *_status_set, int *_exit_status) +{ + status_set = _status_set; + exit_status = _exit_status; +} + +inpipebuf::int_type inpipebuf::underflow() +{ + if (gptr() < egptr()) + return traits_type::to_int_type(*gptr()); + + buffer = fgetc (pipe); + if (feof (pipe)) + { + // ERROR or EOF + return EOF; + } + + setg (&buffer, &buffer, &buffer+sizeof(char)); + + return traits_type::to_int_type(*gptr()); +} + +/** @brief runs command and returns it's output as string + * @param command the full command with all parameters + * @param exit_status the full exit status, use WEXITSTATUS to get the "regular" return code + * @returns the output (stderr) of the called program + */ +std::string pipe_to_string(const std::string& command, std::string *error, int *_exit_status) +{ + std::string result; + bool exit_set; + + try + { + inpipestream ips(command); + + ips.store_exit_status(&exit_set, _exit_status); + + char buffer[2048]; + while (ips.good()) + { + ips.read(buffer, sizeof(buffer)); + result.append(buffer, ips.gcount()); + } + } + catch (pipestream_error &e) + { + if (error) + *error=e.what(); + return ""; + } + catch(...) + { + throw; + } + + return result; +} + +outpipebuf::outpipebuf(const std::string& command) +{ + status_set = NULL; + exit_status = NULL; + + pipe = popen (command.c_str(), "w"); + if (pipe == NULL) + throw EXCEPTION (pipestream_error, "can't open program or permission denied"); +} + +outpipebuf::~outpipebuf() +{ + if (pipe != NULL) { + int pclose_exit = pclose (pipe); + + if (exit_status && pclose_exit != -1) { + *status_set = true; + *exit_status = pclose_exit; + } + + pipe = NULL; + } +} + +/** note: exit status only available after destruction */ +void outpipebuf::store_exit_status(bool *_status_set, int *_exit_status) +{ + status_set = _status_set; + exit_status = _exit_status; +} + +outpipebuf::int_type outpipebuf::overflow(int_type c) +{ + if (c != EOF) + { + if (fputc(c,pipe)==EOF) + return EOF; + } + return c; +} + +std::streamsize outpipebuf::xsputn(const char* s, std::streamsize num) +{ + return fwrite(s,num,1,pipe); +} diff --git a/src/pipestream.hxx b/src/pipestream.hxx index 734b99c..f0cf87b 100644 --- a/src/pipestream.hxx +++ b/src/pipestream.hxx @@ -31,11 +31,6 @@ on this file might be covered by the GNU General Public License. #include #include -#include -#include -#include - -#include "exception.hxx" /** @brief runs command and provides buffered input for it through pipe * @@ -57,54 +52,14 @@ protected: int *exit_status; public: - inpipebuf(const std::string& command) - { - status_set = NULL; - exit_status = NULL; - - pipe = popen (command.c_str(), "r"); - if (pipe == NULL) - throw EXCEPTION (pipestream_error, "can't open program or permission denied"); - - // force underflow - setg (&buffer, &buffer, &buffer); - } + inpipebuf(const std::string& command); - ~inpipebuf() - { - if (pipe != NULL) { - int pclose_exit = pclose (pipe); + ~inpipebuf(); - if (exit_status && pclose_exit != -1) { - *status_set = true; - *exit_status = pclose_exit; - } - - pipe = NULL; - } - } - - /** note: exit status only available after destruction */ - void store_exit_status(bool *_status_set, int *_exit_status) - { status_set = _status_set; exit_status = _exit_status; } + void store_exit_status(bool *_status_set, int *_exit_status); protected: - virtual int_type underflow() - { - if (gptr() < egptr()) - return traits_type::to_int_type(*gptr()); - - buffer = fgetc (pipe); - if (feof (pipe)) - { - // ERROR or EOF - return EOF; - } - - setg (&buffer, &buffer, &buffer+sizeof(char)); - - return traits_type::to_int_type(*gptr()); - } + virtual int_type underflow(); }; /** @brief stream around inpipebuf -- see comment there */ @@ -122,42 +77,7 @@ public: { buf.store_exit_status(_status_set, _exit_status); } }; -/** @brief runs command and returns it's output as string - * @param command the full command with all parameters - * @param exit_status the full exit status, use WEXITSTATUS to get the "regular" return code - * @returns the output (stderr) of the called program - */ -std::string pipe_to_string(const std::string& command, std::string *error=NULL, int *_exit_status=NULL) -{ - std::string result; - bool exit_set; - - try - { - inpipestream ips(command); - - ips.store_exit_status(&exit_set, _exit_status); - - char buffer[2048]; - while (ips.good()) - { - ips.read(buffer, sizeof(buffer)); - result.append(buffer, ips.gcount()); - } - } - catch (pipestream_error &e) - { - if (error) - *error=e.what(); - return ""; - } - catch(...) - { - throw; - } - - return result; -} +std::string pipe_to_string(const std::string& command, std::string *error=NULL, int *_exit_status=NULL); /** @brief runs command and provides buffered ouptput from it through pipe * @@ -174,49 +94,17 @@ protected: int *exit_status; public: - outpipebuf(const std::string& command) - { - status_set = NULL; - exit_status = NULL; - - pipe = popen (command.c_str(), "w"); - if (pipe == NULL) - throw EXCEPTION (pipestream_error, "can't open program or permission denied"); - } - - ~outpipebuf() - { - if (pipe != NULL) { - int pclose_exit = pclose (pipe); - - if (exit_status && pclose_exit != -1) { - *status_set = true; - *exit_status = pclose_exit; - } - - pipe = NULL; - } - } + outpipebuf(const std::string& command); + + ~outpipebuf(); /** note: exit status only available after destruction */ - void store_exit_status(bool *_status_set, int *_exit_status) - { status_set = _status_set; exit_status = _exit_status; } + void store_exit_status(bool *_status_set, int *_exit_status); protected: - virtual int_type overflow(int_type c) - { - if (c != EOF) - { - if (fputc(c,pipe)==EOF) - return EOF; - } - return c; - } - - virtual std::streamsize xsputn(const char* s, std::streamsize num) - { - return fwrite(s,num,1,pipe); - } + virtual int_type overflow(int_type c); + + virtual std::streamsize xsputn(const char* s, std::streamsize num); }; -- 1.7.1