1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-12 10:04:14 +02:00

Add ioReadLineParam() to allow return on eof.

ioReadLine() errors on eof because it has previously been used only for protocol reads.

Returning on eof is handy for reading lines from files where eof is not considered an error.
This commit is contained in:
David Steele 2019-08-28 10:46:54 -04:00
parent a605117a23
commit d1675b7e91
3 changed files with 29 additions and 5 deletions

View File

@ -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
***********************************************************************************************************************************/

View File

@ -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);
/***********************************************************************************************************************************

View File

@ -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);