diff --git a/src/common/io/read.c b/src/common/io/read.c index 7473f5ae9..3eca18d1c 100644 --- a/src/common/io/read.c +++ b/src/common/io/read.c @@ -216,11 +216,11 @@ Read linefeed-terminated string The entire string to search for must fit within a single buffer. ***********************************************************************************************************************************/ String * -ioReadLine(IoRead *this) +ioReadLineParam(IoRead *this, bool allowEof) { FUNCTION_LOG_BEGIN(logLevelTrace); FUNCTION_LOG_PARAM(IO_READ, this); - FUNCTION_LOG_PARAM(BUFFER, this->output); + FUNCTION_LOG_PARAM(BOOL, allowEof); FUNCTION_LOG_END(); ASSERT(this != NULL); @@ -269,9 +269,14 @@ ioReadLine(IoRead *this) THROW_FMT(FileReadError, "unable to find line in %zu byte buffer", bufSize(this->output)); if (ioReadEof(this)) - THROW(FileReadError, "unexpected eof while reading line"); - - ioReadInternal(this, this->output, false); + { + if (allowEof) + result = strNewN((char *)bufPtr(this->output), bufUsed(this->output)); + else + THROW(FileReadError, "unexpected eof while reading line"); + } + else + ioReadInternal(this, this->output, false); } } while (result == NULL); @@ -279,6 +284,19 @@ ioReadLine(IoRead *this) FUNCTION_LOG_RETURN(STRING, result); } +/*********************************************************************************************************************************** +Read linefeed-terminated string and error on eof +***********************************************************************************************************************************/ +String * +ioReadLine(IoRead *this) +{ + FUNCTION_LOG_BEGIN(logLevelTrace); + FUNCTION_LOG_PARAM(IO_READ, this); + FUNCTION_LOG_END(); + + FUNCTION_LOG_RETURN(STRING, ioReadLineParam(this, false)); +} + /*********************************************************************************************************************************** Close the IO ***********************************************************************************************************************************/ diff --git a/src/common/io/read.h b/src/common/io/read.h index 29f324da0..d0da91e39 100644 --- a/src/common/io/read.h +++ b/src/common/io/read.h @@ -26,6 +26,7 @@ Functions bool ioReadOpen(IoRead *this); size_t ioRead(IoRead *this, Buffer *buffer); String *ioReadLine(IoRead *this); +String *ioReadLineParam(IoRead *this, bool allowEof); void ioReadClose(IoRead *this); /*********************************************************************************************************************************** diff --git a/test/src/module/common/ioTest.c b/test/src/module/common/ioTest.c index 92314f67c..c3d48c956 100644 --- a/test/src/module/common/ioTest.c +++ b/test/src/module/common/ioTest.c @@ -431,6 +431,11 @@ testRun(void) ioReadOpen(read); TEST_ERROR(ioReadLine(read), FileReadError, "unable to find line in 10 byte buffer"); + // Read line without eof + read = ioBufferReadNew(BUFSTRDEF("1234")); + ioReadOpen(read); + TEST_RESULT_STR(strPtr(ioReadLineParam(read, true)), "1234", "read line without eof"); + // Read IO into a buffer // ------------------------------------------------------------------------------------------------------------------------- ioBufferSizeSet(8);