Skip to content
Browse files

Move the process capture functionality to its own file and class

  • Loading branch information...
1 parent 9f18f1a commit a82bce37606c7b4315b9ee42ee85facf1db91dc9 @sschuberth committed Feb 24, 2012
Showing with 73 additions and 48 deletions.
  1. +1 −0 build/vs2010/is-procap.vcxproj
  2. +3 −48 source/is-procap.cpp
  3. +69 −0 source/processcapture.h
View
1 build/vs2010/is-procap.vcxproj
@@ -62,6 +62,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
+ <ClCompile Include="..\..\source\processcapture.h" />
<ClCompile Include="..\..\source\is-procap.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
View
51 source/is-procap.cpp
@@ -1,55 +1,10 @@
-#include <string>
-
-#include <boost/array.hpp>
-#include <boost/asio.hpp>
-#include <boost/filesystem.hpp>
-#include <boost/process.hpp>
-
-boost::asio::io_service ioservice;
-boost::array<char,4096> buffer;
-
-boost::process::pipe in(ioservice);
-
-void end_read(const boost::system::error_code &ec,std::size_t bytes_transferred);
-
-void begin_read()
-{
- // Register a handler for the asynchronous read.
- in.async_read_some(
- boost::asio::buffer(buffer)
- , boost::bind(&end_read,boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred)
- );
-}
-
-void end_read(const boost::system::error_code &ec,std::size_t bytes_transferred)
-{
- if (!ec) {
- // If there is no error, process the output and re-register the handler.
- std::cout << std::string(buffer.data(),bytes_transferred) << std::flush;
- begin_read();
- }
-}
+#include "processcapture.h"
int main()
{
- std::string exe=boost::process::find_executable_in_path("ping");
- std::vector<std::string> args;
- args.push_back("www.heise.de");
-
- // Replace STDOUT in the child process with an asynchronous pipe.
- boost::process::context ctx;
- ctx.streams[boost::process::stdout_id]=boost::process::behavior::async_pipe();
-
- // Launch the child process.
- boost::process::child c=boost::process::create_child(exe,args,ctx);
-
- // Assign the child's STDOUT handle to our pipe.
- boost::process::handle h=c.get_handle(boost::process::stdout_id);
- in.assign(h.release());
-
- begin_read();
+ ProcessCapture procap(boost::process::find_executable_in_path("ping"),"www.heise.de");
- ioservice.run();
+ procap();
system("pause");
}
View
69 source/processcapture.h
@@ -0,0 +1,69 @@
+#include <boost/array.hpp>
+#include <boost/asio.hpp>
+#include <boost/filesystem.hpp>
+#include <boost/process.hpp>
+
+class ProcessCapture
+{
+ public:
+
+ typedef std::vector<std::string> Arguments;
+
+ ProcessCapture(const std::string& exe,const std::string& arg="")
+ : exe(exe)
+ , args(1,arg)
+ , in(ioservice)
+ {}
+
+ ProcessCapture(const std::string& exe,const Arguments& args)
+ : exe(exe)
+ , args(args)
+ , in(ioservice)
+ {}
+
+ void begin_read();
+
+ void end_read(const boost::system::error_code &ec,std::size_t bytes_transferred) {
+ if (!ec) {
+ // If there is no error, process the output and re-register the handler.
+ std::cout << std::string(buffer.data(),bytes_transferred) << std::flush;
+ begin_read();
+ }
+ }
+
+ void operator()() {
+ // Replace STDOUT in the child process with an asynchronous pipe.
+ boost::process::context ctx;
+ ctx.streams[boost::process::stdout_id]=boost::process::behavior::async_pipe();
+
+ // Launch the child process.
+ boost::process::child c=boost::process::create_child(exe,args,ctx);
+
+ // Assign the child's STDOUT handle to our pipe.
+ boost::process::handle h=c.get_handle(boost::process::stdout_id);
+ in.assign(h.release());
+
+ begin_read();
+
+ ioservice.run();
+ }
+
+ private:
+
+ std::string exe;
+ Arguments args;
+
+ boost::asio::io_service ioservice;
+ boost::array<char,4096> buffer;
+
+ boost::process::pipe in;
+};
+
+inline void ProcessCapture::begin_read()
+{
+ // Register a handler for the asynchronous read.
+ in.async_read_some(
+ boost::asio::buffer(buffer)
+ , boost::bind(&ProcessCapture::end_read,this,boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred)
+ );
+}

0 comments on commit a82bce3

Please sign in to comment.
Something went wrong with that request. Please try again.