NptBufferedStreams.h

00001 /*****************************************************************
00002 |
00003 |   Neptune - Buffered Byte Stream
00004 |
00005 |   (c) 2001-2006 Gilles Boccon-Gibod
00006 |   Author: Gilles Boccon-Gibod (bok@bok.net)
00007 |
00008  ****************************************************************/
00009 
00010 #ifndef _NPT_BUFFERED_STREAMS_H_
00011 #define _NPT_BUFFERED_STREAMS_H_
00012 
00013 /*----------------------------------------------------------------------
00014 |   includes
00015 +---------------------------------------------------------------------*/
00016 #include "NptStreams.h"
00017 #include "NptTypes.h"
00018 #include "NptConstants.h"
00019 #include "NptStrings.h"
00020 #include "NptDebug.h"
00021 
00022 /*----------------------------------------------------------------------
00023 |   NPT_BufferedStream
00024 +---------------------------------------------------------------------*/
00025 const NPT_Size NPT_BUFFERED_BYTE_STREAM_DEFAULT_SIZE = 4096;
00026 
00027 /*----------------------------------------------------------------------
00028 |   NPT_BufferedByteStream
00029 +---------------------------------------------------------------------*/
00030 class NPT_BufferedInputStream : public NPT_InputStream
00031 {
00032 public:
00033     // constructors and destructor
00034     NPT_BufferedInputStream(NPT_InputStreamReference& stream,
00035                             NPT_Size buffer_size = NPT_BUFFERED_BYTE_STREAM_DEFAULT_SIZE);
00036     ~NPT_BufferedInputStream();
00037 
00038     // methods
00039     virtual NPT_Result ReadLine(NPT_String& line,
00040                                 NPT_Size    max_chars = 4096);
00041     virtual NPT_Result ReadLine(char*     buffer, 
00042                                 NPT_Size  buffer_size,
00043                                 NPT_Size* chars_read);
00044     virtual NPT_Result SetBufferSize(NPT_Size size);
00045 
00046     // NPT_InputStream methods
00047     NPT_Result Read(void*     buffer, 
00048                     NPT_Size  bytes_to_read, 
00049                     NPT_Size* bytes_read = NULL);
00050     NPT_Result Seek(NPT_Position offset);
00051     NPT_Result Tell(NPT_Position& offset);
00052     NPT_Result GetSize(NPT_Size& size);
00053     NPT_Result GetAvailable(NPT_Size& available);
00054 
00055 private:
00056     // members
00057     NPT_InputStreamReference m_Source;
00058     bool                     m_SkipNewline;
00059     bool                     m_Eos;
00060     struct {
00061         NPT_Byte*    data;
00062         NPT_Position offset;
00063         NPT_Size     valid;
00064         NPT_Size     size;
00065     } m_Buffer;
00066 
00067     // methods
00068     NPT_Result FillBuffer();
00069     NPT_Result ReleaseBuffer();
00070 };
00071 
00072 typedef NPT_Reference<NPT_BufferedInputStream> NPT_BufferedInputStreamReference;
00073 
00074 #endif // _NPT_BUFFERED_STREAMS_H_