From: Gerd von Egidy Date: Tue, 8 Sep 2015 18:13:45 +0000 (+0200) Subject: Implement getline() for IOImplementation. X-Git-Tag: v0.3~2 X-Git-Url: http://developer.intra2net.com/git/?p=libasyncio;a=commitdiff_plain;h=054f19f426698af8dad5558cd29ee62569307dbd Implement getline() for IOImplementation. --- diff --git a/asyncio/async_io.cpp b/asyncio/async_io.cpp index a0e5d3a..8b96df7 100644 --- a/asyncio/async_io.cpp +++ b/asyncio/async_io.cpp @@ -1076,6 +1076,29 @@ std::string::size_type IOImplementation::shortenInput(std::string::size_type len return real_len; } +/** + * Read and remove a line from the input buffer. + * @param delim the line ending character, \\n by default. + * @returns A full line including the line ending if available, an empty string otherwise. + */ +std::string IOImplementation::getline(char delim) +{ + std::string line; + std::string::size_type pos=m_input_buffer.find(delim); + + // no line ending in the buffer? + if (pos==std::string::npos) + return line; + + // copy the line including the delimiter + line=m_input_buffer.substr(0,pos+1); + + m_input_buffer.erase(0,pos+1); + + return line; +} + + /* * implementation of SimpleIO */ diff --git a/asyncio/async_io.hpp b/asyncio/async_io.hpp index 40fb47c..56e7b3c 100644 --- a/asyncio/async_io.hpp +++ b/asyncio/async_io.hpp @@ -236,6 +236,7 @@ class IOImplementation virtual std::string getInputClear(); virtual bool inputAvailable() const; virtual std::string::size_type shortenInput(std::string::size_type len); + virtual std::string getline(char delim='\n'); protected: diff --git a/unittest/test_simpleio_basics.cpp b/unittest/test_simpleio_basics.cpp index 6fbc7be..6de16ee 100644 --- a/unittest/test_simpleio_basics.cpp +++ b/unittest/test_simpleio_basics.cpp @@ -826,6 +826,39 @@ BOOST_AUTO_TEST_CASE(DirectProcessImplementationTestEcho) BOOST_CHECK_EQUAL( false, proc.inputAvailable()); } // eo SimpleProcessTestEcho +BOOST_AUTO_TEST_CASE(DirectProcessImplementationGetline) +{ + DOUT("enter SimpleProcessTestEcho"); + bool res; + BOOST_REQUIRE(backend); + + ProcessImplementation proc( + "/bin/sh", + TransientPushBackFiller()("-c")("echo -n 1; sleep 1; echo 2; echo") + ); + + res= proc.startProcess(); + BOOST_CHECK_EQUAL(true, res); + + res= backend->doOneStep(800); + BOOST_CHECK_EQUAL(true, res); + + // echo -n 1 should be out by now + + BOOST_CHECK_EQUAL( std::string(""), proc.getline()); + + for(int i=20; i-->0 && (proc.processState()!= ProcessState::stopped || !proc.eof());) + { + backend->doOneStep(100); + } + + 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("12\n"), proc.getline()); + BOOST_CHECK_EQUAL( std::string("\n"), proc.getline()); + BOOST_CHECK_EQUAL( false, proc.inputAvailable()); +} // eo SimpleProcessTestEcho BOOST_AUTO_TEST_CASE(CallOut1) {