Implement getline() for IOImplementation.
authorGerd von Egidy <gerd.von.egidy@intra2net.com>
Tue, 8 Sep 2015 18:13:45 +0000 (20:13 +0200)
committerGerd von Egidy <gerd.von.egidy@intra2net.com>
Tue, 8 Sep 2015 18:13:45 +0000 (20:13 +0200)
asyncio/async_io.cpp
asyncio/async_io.hpp
unittest/test_simpleio_basics.cpp

index a0e5d3a..8b96df7 100644 (file)
@@ -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
  */
index 40fb47c..56e7b3c 100644 (file)
@@ -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:
 
index 6fbc7be..6de16ee 100644 (file)
@@ -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<std::string, std::vector >()("-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)
 {