/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * Copyright (C) 2004-2012 HUBzero Foundation, LLC * * Author: George A. Howlett */ #include #include #ifndef NV_READBUFFER_H #define NV_READBUFFER_H namespace nv { /** * \brief Buffered input for reading socket */ class ReadBuffer { public: enum BufferStatus { ENDFILE=-1, OK, ERROR, CONTINUE, }; ReadBuffer(int fd, size_t bufferSize); virtual ~ReadBuffer(); BufferStatus getLine(size_t *numBytesPtr, unsigned char **bytesPtr); BufferStatus followingData(unsigned char *out, size_t numBytes); /** * \brief Check if buffer contains data ending in a newline * * \return bool indicating if getLine can be called without blocking */ bool isLineAvailable() { return (nextNewLine() > 0); } /** * \brief Get the file descriptor this object reads from */ int file() { return _fd; } /** * \brief Get the status of the last read */ BufferStatus status() { return _lastStatus; } private: unsigned char *_bytes; /**< New-ed buffer to hold read * characters. */ size_t _bufferSize; /**< # of bytes allocated for buffer */ size_t _fill; /**< # of bytes used in the buffer */ size_t _mark; /**< Starting point of the unconsumed * data in the buffer. */ int _fd; /**< File descriptor to get data. */ BufferStatus _lastStatus; /**< Status of last read operation. */ BufferStatus doFill(); void flush() { _fill = _mark = 0; } size_t nextNewLine(); }; } #endif