|
Boost : |
Subject: Re: [boost] How to bind program termination with end-of-stream in Boost.Process 0.5?
From: alfC (alfredo.correa_at_[hidden])
Date: 2012-09-18 01:48:02
Hi Boris,
Thank you very much for your help. See below for proposed code for your library.
On Mon, Sep 17, 2012 at 7:29 PM, Alfredo Correa
<alfredo.correa_at_[hidden]> wrote:
> On Sun, Sep 16, 2012 at 11:54 AM, Boris Schaeling [via Boost]
> <ml-node+s2283326n4635834h32_at_[hidden]> wrote:
> I guess it is not not late to provide "convenience"
> pistream/postream/piostream classes that wrap up all the complexity.
If you don't mind, let me propose the following wrapper class for the
casual user. I had to play a little bit with protected inheritance to
make it work. I remember other cases where protected inheritance was
necessary in order to define derived stream classes. I kept it simple
to communicate the idea, I guess more template const Initializer& can
be passed through the constructor. An equivalent one can be made for
postream, and even piostream.
// ...
// Copyright (c) 2012 Alfredo Correa
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
/**
* \file boost/process/pistream.hpp
*
* Convenience pistream class prototype
*/
#include <boost/process.hpp> // version 0.5
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <string>
namespace boost{
namespace process{
class pistream :
protected pipe,
protected iostreams::file_descriptor_source,
public iostreams::stream<boost::iostreams::file_descriptor_source>{
public:
pistream(const std::string& cmd) :
pipe(create_pipe()),
file_descriptor_source(this->source, iostreams::close_handle),
stream<file_descriptor_source>((file_descriptor_source&)(*this)){
execute(
initializers::run_exe(cmd),
initializers::bind_stdout(iostreams::file_descriptor_sink(this->sink,
iostreams::close_handle))
);
}
};
}}
// example code
int main(){
boost::process::pistream pis("/usr/bin/ls");
std::string s;
while(std::getline(pis, s)){
std::cout << "read: " << s << std::endl;
}
}
Thanks,
Alfredo
>
> boost::pistream pis("/bin/ls");
> for(std::string line; std::getline(pis, line); ){
> std::cout << "line: " << line << std::endl;
> }
> std::cout << "end" << std::endl;
>
>>
>>> [...]ok, since there is no conclusive solution, so maybe to end this
>>> thread
>>> let me file this as a possible bug (or confirm it is the expected
>>> behavior), the symptom is that the program never ends (linux, fedora
>>> 17, boost 1.48):
>>
>> Can you try this program? It works for me:
>
> Thanks, the program worked (boost 1.48) and I posted the solution in
> stakoverflow: http://stackoverflow.com/a/12469478/225186
> I don't have any idea why this works vs. the other. I wonder if I have
> to do the same for doing output to the process.
>
> Thanks,
> Alfredo
>
>> -----
>> #include <boost/process.hpp>
>> #include <boost/iostreams/device/file_descriptor.hpp>
>> #include <boost/iostreams/stream.hpp>
>> #include <string>
>>
>> using namespace boost::process;
>> using namespace boost::process::initializers;
>> using namespace boost::iostreams;
>>
>> int main()
>> {
>> boost::process::pipe p = create_pipe();
>> {
>> file_descriptor_sink sink(p.sink, close_handle);
>> execute(run_exe("/bin/ls"), bind_stdout(sink));
>> }
>> file_descriptor_source source(p.source, close_handle);
>> stream<file_descriptor_source> is(source);
>> std::string s;
>> while(std::getline(is, s)){
>> std::cout << "read: " << s << std::endl;
>> }
>> std::clog << "end" << std::endl;
>> }
>> -----
>>
>> Boris
>>
>>> [...]
>>
>>
>> _______________________________________________
>> Unsubscribe & other changes:
>> http://lists.boost.org/mailman/listinfo.cgi/boost
>>
>>
>> ________________________________
>> If you reply to this email, your message will be added to the discussion
>> below:
>> http://boost.2283326.n4.nabble.com/How-to-bind-program-termination-with-end-of-stream-in-Boost-Process-0-5-tp4635497p4635834.html
>> To unsubscribe from How to bind program termination with end-of-stream in
>> Boost.Process 0.5?, click here.
>> NAML
>
>
>
> --
> Alfredo
-- Alfredo -- View this message in context: http://boost.2283326.n4.nabble.com/How-to-bind-program-termination-with-end-of-stream-in-Boost-Process-0-5-tp4635497p4635919.html Sent from the Boost - Dev mailing list archive at Nabble.com.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk