From 69ae879c585039297951821e353f7c1ca12b6cb8 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 7 Aug 2014 00:06:02 +0200 Subject: input/TextInputStream: return char* Revert to the old API before commit e9e55b08, removing unnecessary bloat. --- src/input/TextInputStream.cxx | 25 +++++++++++++------------ src/input/TextInputStream.hxx | 8 ++------ 2 files changed, 15 insertions(+), 18 deletions(-) (limited to 'src/input') diff --git a/src/input/TextInputStream.cxx b/src/input/TextInputStream.cxx index d7cb440b3..b90eeacc0 100644 --- a/src/input/TextInputStream.cxx +++ b/src/input/TextInputStream.cxx @@ -27,9 +27,10 @@ #include #include -bool TextInputStream::ReadLine(std::string &line) +char * +TextInputStream::ReadLine() { - const char *src, *p; + char *src, *p; do { size_t nbytes; @@ -46,33 +47,33 @@ bool TextInputStream::ReadLine(std::string &line) buffer.Append(nbytes); else if (error.IsDefined()) { LogError(error); - return false; + return nullptr; } } else nbytes = 0; auto src_p = buffer.Read(); if (src_p.IsEmpty()) - return false; + return nullptr; src = src_p.data; - p = reinterpret_cast(memchr(src, '\n', src_p.size)); + p = reinterpret_cast(memchr(src, '\n', src_p.size)); if (p == nullptr && nbytes == 0) { /* end of file (or line too long): terminate the current line */ dest = buffer.Write(); assert(!dest.IsEmpty()); - dest[0] = '\n'; - buffer.Append(1); + dest[0] = 0; + buffer.Clear(); + return src; } } while (p == nullptr); - size_t length = p - src + 1; + buffer.Consume(p - src + 1); + while (p > src && IsWhitespaceOrNull(p[-1])) --p; - - line = std::string(src, p - src); - buffer.Consume(length); - return true; + *p = 0; + return src; } diff --git a/src/input/TextInputStream.hxx b/src/input/TextInputStream.hxx index 873ecdbda..6f39d22cf 100644 --- a/src/input/TextInputStream.hxx +++ b/src/input/TextInputStream.hxx @@ -22,8 +22,6 @@ #include "util/StaticFifoBuffer.hxx" -#include - class InputStream; class TextInputStream { @@ -46,11 +44,9 @@ public: /** * Reads the next line from the stream with newline character stripped. * - * @param line a string to put result to - * @return true if line is read successfully, false on end of file - * or error + * @return a pointer to the line, or nullptr on end-of-file or error */ - bool ReadLine(std::string &line); + char *ReadLine(); }; #endif -- cgit v1.2.3