From de3f8f4f6eab5d5c873c91cc93a01fe2e8a82363 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 18 Sep 2009 19:37:06 +0000 Subject: Added a handy readLine function, I've been meaning to do this for a while. --- src/stream.cpp | 15 +++++++++++++++ src/stream.h | 8 ++++++++ 2 files changed, 23 insertions(+) diff --git a/src/stream.cpp b/src/stream.cpp index 290afb3..01edb38 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -15,6 +15,21 @@ Bu::Stream::~Stream() { } +Bu::FString Bu::Stream::readLine() +{ + Bu::FString sRet; + + for(;;) + { + char s; + if( read( &s, 1 ) == 0 ) + return sRet; + if( s == '\n' || s == '\r' ) + return sRet; + sRet.append( s ); + } +} + size_t Bu::Stream::write( const Bu::FString &sBuf ) { return write( sBuf.getStr(), sBuf.getSize() ); diff --git a/src/stream.h b/src/stream.h index f0f1af3..527dc20 100644 --- a/src/stream.h +++ b/src/stream.h @@ -45,6 +45,14 @@ namespace Bu */ virtual size_t read( void *pBuf, size_t nBytes ) = 0; + /** + * Attempts to read a complete line from the stream. This will stop + * reading when it has reached the end of the stream, or runs out of + * data in a non-blocking stream. + *@returns The line read, not including newline character. + */ + virtual Bu::FString readLine(); + /** * Write data to the stream. *@param pBuf (const void *) The data to be written. -- cgit v1.2.3