Implement getline() for IOImplementation.
[libasyncio] / asyncio / async_io.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
  */