LRAUV  revA
LineReader Class Reference

Utility for the concrete concern of reading lines from a given file descriptor in a non-blocking fashion. More...

#include <LineReader.h>

Public Member Functions

 LineReader (int fd, size_t bufferSize=1024, bool debug=false)
 Constructor. More...
 
 ~LineReader ()
 
int readLine (char *buf, size_t bufSize)
 Reads a line (or part of it depending on given output buffer size). More...
 

Protected Attributes

int fd_
 

Private Member Functions

 LineReader (const LineReader &old)
 
void adjustBuffer (size_t bufSize)
 
int fillInBuffer ()
 
void updateBuffer (size_t processed)
 
int nbRead (char *buffer, size_t bufferLength)
 

Private Attributes

char * buffer_
 
size_t bufferSize_
 
size_t bufferRead_
 
bool debug_
 

Detailed Description

Utility for the concrete concern of reading lines from a given file descriptor in a non-blocking fashion.

A line is any sequence of characters ending with '
'. Although somewhat similar with functionality offered by other classes, this separated module facilitates reuse in various scenarios. For example, to do line reads from stdin in combination with reads from sockets, etc. This class was added during the initial development of the socket-based interface with the ESP.

Constructor & Destructor Documentation

LineReader::LineReader ( int  fd,
size_t  bufferSize = 1024,
bool  debug = false 
)
inline

Constructor.

Parameters
fdfile descriptor to read data from
bufferSizesize for internal buffer; default: 1024
debugflag for ad hoc debug printing; default: false

References buffer_, and bufferSize_.

LineReader::~LineReader ( )
inline

References buffer_.

LineReader::LineReader ( const LineReader old)
private

Member Function Documentation

void LineReader::adjustBuffer ( size_t  bufSize)
private
int LineReader::fillInBuffer ( )
private
int LineReader::nbRead ( char *  buffer,
size_t  bufferLength 
)
private
int LineReader::readLine ( char *  buf,
size_t  bufSize 
)

Reads a line (or part of it depending on given output buffer size).

The underlying read operation is non-blocking so multiple calls can be done until a complete line is received or until the size of data already received is equal or greater than the output buffer size.

Parameters
bufoutput buffer
bufSizesize of output buffer
Returns
0: : Nothing new received since last processed read. Call again later. -1; : Some bytes have been received (in the internal buffer) but not a complete line yet and the Nothing is transferred to output buffer just yet. len : positive integer indicating number of bytes received and transferred to output buffer. Caller can simply use the condition buf[len - 1] == '
' to verify complete line reception. (Note that buf[len - 1] != '
' implies len == bufSize.) -2 : Some error has occurred.

Referenced by ESPComm::readLine().

void LineReader::updateBuffer ( size_t  processed)
private

Member Data Documentation

char* LineReader::buffer_
private

Referenced by LineReader(), and ~LineReader().

size_t LineReader::bufferRead_
private
size_t LineReader::bufferSize_
private

Referenced by LineReader().

bool LineReader::debug_
private
int LineReader::fd_
protected

The documentation for this class was generated from the following files: