From b9c8a8e54013e28a4c3e19a138f43eef587193ad Mon Sep 17 00:00:00 2001 From: Gerd von Egidy Date: Tue, 8 Sep 2015 19:38:46 +0200 Subject: [PATCH] Implement basic input buffer handling functions for IOImplementation --- asyncio/async_io.cpp | 43 +++++++++++++++++++++++++++++++++++++ asyncio/async_io.hpp | 5 ++++ unittest/test_simpleio_basics.cpp | 34 +++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 0 deletions(-) diff --git a/asyncio/async_io.cpp b/asyncio/async_io.cpp index c85edb5..a0e5d3a 100644 --- a/asyncio/async_io.cpp +++ b/asyncio/async_io.cpp @@ -1032,6 +1032,49 @@ void IOImplementation::doWrite() } } // eo IOImplementation::doWrite +/** + * Return a copy of the input buffer. + */ +std::string IOImplementation::getInput() const +{ + return m_input_buffer; +} + +/** + * Return the input buffer and clear it. + */ +std::string IOImplementation::getInputClear() +{ + std::string retbuf; + + retbuf.swap(m_input_buffer); + + return retbuf; +} + +/** + * Return true if there are bytes available in the input buffer. + */ +bool IOImplementation::inputAvailable() const +{ + return !m_input_buffer.empty(); +} + +/** + * Cut off the first len bytes from the input buffer. + * @returns The number of bytes actually shortened + */ +std::string::size_type IOImplementation::shortenInput(std::string::size_type len) +{ + std::string::size_type real_len=len; + if (real_len > m_input_buffer.size()) + real_len=m_input_buffer.size(); + + if (real_len > 0) + m_input_buffer.erase(0,real_len); + + return real_len; +} /* * implementation of SimpleIO diff --git a/asyncio/async_io.hpp b/asyncio/async_io.hpp index 8291543..40fb47c 100644 --- a/asyncio/async_io.hpp +++ b/asyncio/async_io.hpp @@ -232,6 +232,11 @@ class IOImplementation virtual bool writable() const; virtual bool empty() const; + virtual std::string getInput() const; + virtual std::string getInputClear(); + virtual bool inputAvailable() const; + virtual std::string::size_type shortenInput(std::string::size_type len); + protected: void addFilter(FilterBasePtr filter); diff --git a/unittest/test_simpleio_basics.cpp b/unittest/test_simpleio_basics.cpp index e86ad7a..6fbc7be 100644 --- a/unittest/test_simpleio_basics.cpp +++ b/unittest/test_simpleio_basics.cpp @@ -791,6 +791,40 @@ BOOST_AUTO_TEST_CASE(SignaledProcessTermination) BOOST_CHECK_EQUAL( Signal::USR1 , proc.exitCode()>>8 ); } // eo SignaledProcessTermination +/** + * fork an echo subprocess and read back the output. Directly use ProcessImplementation without helper class. + */ +BOOST_AUTO_TEST_CASE(DirectProcessImplementationTestEcho) +{ + DOUT("enter SimpleProcessTestEcho"); + bool res; + BOOST_REQUIRE(backend); + + ProcessImplementation proc( + "/bin/echo", + TransientPushBackFiller()("Eine")("Zeichenkette") + ); + + res= proc.startProcess(); + BOOST_CHECK_EQUAL(true, res); + + res= backend->doOneStep(200); + BOOST_CHECK_EQUAL(true, res); + for(int i=20; i-->0 && (proc.processState()!= ProcessState::stopped || !proc.eof());) + { + backend->doOneStep(10); + } + + BOOST_CHECK_EQUAL( ProcessState(ProcessState::stopped), proc.processState() ); + BOOST_CHECK_EQUAL( true, proc.eof() ); + BOOST_CHECK_EQUAL( 0, proc.exitCode() ); + BOOST_CHECK_EQUAL( std::string("Eine Zeichenkette\n"), proc.getInput()); + BOOST_CHECK_EQUAL( 5, proc.shortenInput(5)); + BOOST_CHECK_EQUAL( std::string("Zeichenkette\n"), proc.getInput()); + BOOST_CHECK_EQUAL( true, proc.inputAvailable()); + BOOST_CHECK_EQUAL( std::string("Zeichenkette\n"), proc.getInputClear()); + BOOST_CHECK_EQUAL( false, proc.inputAvailable()); +} // eo SimpleProcessTestEcho BOOST_AUTO_TEST_CASE(CallOut1) -- 1.7.1