Code for checking xvg files in testutils.
[alexxy/gromacs.git] / src / gromacs / utility / stringstream.cpp
index 7a93d3472c8d752fcc2c462f6d5ce04575d4d65e..77e2388ea2487cb7067790e9d13cf1e1c3b49687 100644 (file)
@@ -45,6 +45,9 @@
 
 #include <string>
 
+#include "gromacs/utility/arrayref.h"
+#include "gromacs/utility/stringutil.h"
+
 namespace gmx
 {
 
@@ -57,4 +60,40 @@ void StringOutputStream::close()
 {
 }
 
+StringInputStream::StringInputStream(const std::string &input)
+    : input_(input), pos_(0)
+{
+}
+
+StringInputStream::StringInputStream(ConstArrayRef<const char *> const &input)
+    : input_(joinStrings(input.begin(), input.end(), "\n")), pos_(0)
+{
+    input_.append("\n");
+}
+
+bool StringInputStream::readLine(std::string *line)
+{
+    if (pos_ == input_.size())
+    {
+        line->clear();
+        return false;
+    }
+    else
+    {
+        size_t newpos = input_.find("\n", pos_);
+        if (newpos == std::string::npos)
+        {
+            newpos = input_.size();
+        }
+        else
+        {
+            // To include the newline as well!
+            newpos += 1;
+        }
+        line->assign(input_.substr(pos_, newpos-pos_));
+        pos_ = newpos;
+        return true;
+    }
+}
+
 } // namespace gmx