Implement basic input buffer handling functions for IOImplementation
authorGerd von Egidy <gerd.von.egidy@intra2net.com>
Tue, 8 Sep 2015 17:38:46 +0000 (19:38 +0200)
committerGerd von Egidy <gerd.von.egidy@intra2net.com>
Tue, 8 Sep 2015 17:38:46 +0000 (19:38 +0200)
asyncio/async_io.cpp
asyncio/async_io.hpp
unittest/test_simpleio_basics.cpp

index c85edb5..a0e5d3a 100644 (file)
@@ -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
index 8291543..40fb47c 100644 (file)
@@ -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);
index e86ad7a..6fbc7be 100644 (file)
@@ -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<std::string, std::vector >()("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)