From 8c7dee81e897e98a423e47230ac821a7edab80de Mon Sep 17 00:00:00 2001 From: Lynix Date: Mon, 26 Sep 2016 12:35:16 +0200 Subject: [PATCH] Core/Stream: Fix ReadLine Former-commit-id: 81e89bb4b11264371d7cbcf9cf5275ec0ce3f7ba [formerly 8ac037f30540faebc51efa5953fc01d9659a1f19] [formerly b599cef23b34132171373aee1cca81daf259b65e [formerly 053ef5248717e56f5e943f6dbdddf8092ae9dda2]] Former-commit-id: c14803c0471a6bfa347f0f512886dae2d8cc7871 [formerly dc174bdb2cb49e68b57b85267ac72178d7e202af] Former-commit-id: 815fe25a962845d091de7a02a0c0ff2e62f45b51 --- src/Nazara/Core/Stream.cpp | 45 +++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/src/Nazara/Core/Stream.cpp b/src/Nazara/Core/Stream.cpp index b36b67d85..f82591fed 100644 --- a/src/Nazara/Core/Stream.cpp +++ b/src/Nazara/Core/Stream.cpp @@ -44,14 +44,19 @@ namespace Nz } /*! - * \brief Reads characters in the stream + * \brief Reads a line from the stream + * + * Reads the stream until a line separator or the end of the stream is found. + * + * If lineSize does not equal zero, it represents the maximum character count to be read from the stream. + * + * \param lineSize Maximum number of characters to read, or zero for no limit + * * \return Line containing characters * - * \param lineSize Number of characters to read, if lineSize is 0, read as much as possible - * - * \remark Produces a NazaraWarning if cursor position could not be reset + * \remark With the text stream option, "\r\n" is treated as "\n" + * \remark The line separator character is not returned as part of the string */ - String Stream::ReadLine(unsigned int lineSize) { String line; @@ -71,19 +76,33 @@ namespace Nz if (ptr) { std::ptrdiff_t pos = ptr - buffer; - - if (m_streamOptions & StreamOption_Text && pos > 0 && buffer[pos - 1] == '\r') - line.Append(buffer, pos - 1); - else - line.Append(buffer, pos); + if (ptr != buffer) + { + if (m_streamOptions & StreamOption_Text && buffer[pos - 1] == '\r') + line.Append(buffer, pos - 1); + else + line.Append(buffer, pos); + } if (!SetCursorPos(GetCursorPos() - readSize + pos + 1)) - NazaraWarning("Failed to reset cursos pos"); + NazaraWarning("Failed to reset cursor pos"); - break; + if (!line.IsEmpty()) + break; } else - line.Append(buffer, readSize); + { + std::size_t length = readSize; + if (m_streamOptions & StreamOption_Text && buffer[length - 1] == '\r') + { + if (!SetCursorPos(GetCursorPos() - 1)) + NazaraWarning("Failed to reset cursor pos"); + + length--; + } + + line.Append(buffer, length); + } } while (readSize == bufferSize); }