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:
parent
a605117a23
commit
d1675b7e91
@ -216,11 +216,11 @@ Read linefeed-terminated string
|
|||||||
The entire string to search for must fit within a single buffer.
|
The entire string to search for must fit within a single buffer.
|
||||||
***********************************************************************************************************************************/
|
***********************************************************************************************************************************/
|
||||||
String *
|
String *
|
||||||
ioReadLine(IoRead *this)
|
ioReadLineParam(IoRead *this, bool allowEof)
|
||||||
{
|
{
|
||||||
FUNCTION_LOG_BEGIN(logLevelTrace);
|
FUNCTION_LOG_BEGIN(logLevelTrace);
|
||||||
FUNCTION_LOG_PARAM(IO_READ, this);
|
FUNCTION_LOG_PARAM(IO_READ, this);
|
||||||
FUNCTION_LOG_PARAM(BUFFER, this->output);
|
FUNCTION_LOG_PARAM(BOOL, allowEof);
|
||||||
FUNCTION_LOG_END();
|
FUNCTION_LOG_END();
|
||||||
|
|
||||||
ASSERT(this != NULL);
|
ASSERT(this != NULL);
|
||||||
@ -269,9 +269,14 @@ ioReadLine(IoRead *this)
|
|||||||
THROW_FMT(FileReadError, "unable to find line in %zu byte buffer", bufSize(this->output));
|
THROW_FMT(FileReadError, "unable to find line in %zu byte buffer", bufSize(this->output));
|
||||||
|
|
||||||
if (ioReadEof(this))
|
if (ioReadEof(this))
|
||||||
THROW(FileReadError, "unexpected eof while reading line");
|
{
|
||||||
|
if (allowEof)
|
||||||
ioReadInternal(this, this->output, false);
|
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);
|
while (result == NULL);
|
||||||
@ -279,6 +284,19 @@ ioReadLine(IoRead *this)
|
|||||||
FUNCTION_LOG_RETURN(STRING, result);
|
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
|
Close the IO
|
||||||
***********************************************************************************************************************************/
|
***********************************************************************************************************************************/
|
||||||
|
@ -26,6 +26,7 @@ Functions
|
|||||||
bool ioReadOpen(IoRead *this);
|
bool ioReadOpen(IoRead *this);
|
||||||
size_t ioRead(IoRead *this, Buffer *buffer);
|
size_t ioRead(IoRead *this, Buffer *buffer);
|
||||||
String *ioReadLine(IoRead *this);
|
String *ioReadLine(IoRead *this);
|
||||||
|
String *ioReadLineParam(IoRead *this, bool allowEof);
|
||||||
void ioReadClose(IoRead *this);
|
void ioReadClose(IoRead *this);
|
||||||
|
|
||||||
/***********************************************************************************************************************************
|
/***********************************************************************************************************************************
|
||||||
|
@ -431,6 +431,11 @@ testRun(void)
|
|||||||
ioReadOpen(read);
|
ioReadOpen(read);
|
||||||
TEST_ERROR(ioReadLine(read), FileReadError, "unable to find line in 10 byte buffer");
|
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
|
// Read IO into a buffer
|
||||||
// -------------------------------------------------------------------------------------------------------------------------
|
// -------------------------------------------------------------------------------------------------------------------------
|
||||||
ioBufferSizeSet(8);
|
ioBufferSizeSet(8);
|
||||||
|
Loading…
Reference in New Issue
Block a user