NptStreams.h

00001 /*****************************************************************
00002 |
00003 |   Neptune - Byte Streams
00004 |
00005 |   (c) 2001-2003 Gilles Boccon-Gibod
00006 |   Author: Gilles Boccon-Gibod (bok@bok.net)
00007 |
00008  ****************************************************************/
00009 
00010 #ifndef _NPT_STREAMS_H_
00011 #define _NPT_STREAMS_H_
00012 
00013 /*----------------------------------------------------------------------
00014 |    includes
00015 +---------------------------------------------------------------------*/
00016 #include "NptTypes.h"
00017 #include "NptReferences.h"
00018 #include "NptConstants.h"
00019 #include "NptResults.h"
00020 #include "NptDataBuffer.h"
00021 #include "NptStrings.h"
00022 
00023 /*----------------------------------------------------------------------
00024 |    class references
00025 +---------------------------------------------------------------------*/
00026 class NPT_String;
00027 
00028 /*----------------------------------------------------------------------
00029 |    constants
00030 +---------------------------------------------------------------------*/
00031 const int NPT_ERROR_READ_FAILED  = NPT_ERROR_BASE_IO - 0;
00032 const int NPT_ERROR_WRITE_FAILED = NPT_ERROR_BASE_IO - 1;
00033 const int NPT_ERROR_EOS          = NPT_ERROR_BASE_IO - 2;
00034 
00035 /*----------------------------------------------------------------------
00036 |    NPT_InputStream
00037 +---------------------------------------------------------------------*/
00038 class NPT_InputStream
00039 {
00040  public:
00041     // constructor and destructor
00042     virtual ~NPT_InputStream() {};
00043 
00044     // methods
00045     virtual NPT_Result Load(NPT_DataBuffer& buffer, NPT_Size max_read = 0);
00046     virtual NPT_Result Read(void*     buffer, 
00047                             NPT_Size  bytes_to_read, 
00048                             NPT_Size* bytes_read = NULL) = 0;
00049     virtual NPT_Result ReadFully(void*     buffer, 
00050                                  NPT_Size  bytes_to_read);
00051     virtual NPT_Result Seek(NPT_Position offset) = 0;
00052     virtual NPT_Result Skip(NPT_Position offset);
00053     virtual NPT_Result Tell(NPT_Position& offset) = 0;
00054     virtual NPT_Result GetSize(NPT_Size& size) = 0;
00055     virtual NPT_Result GetAvailable(NPT_Size& available) = 0;
00056 };
00057 
00058 typedef NPT_Reference<NPT_InputStream> NPT_InputStreamReference;
00059 
00060 /*----------------------------------------------------------------------
00061 |    NPT_OutputStream
00062 +---------------------------------------------------------------------*/
00063 class NPT_OutputStream
00064 {
00065 public:
00066     // constructor and destructor
00067     virtual ~NPT_OutputStream() {};
00068 
00069     // methods
00070     virtual NPT_Result Write(const void* buffer, 
00071                              NPT_Size    bytes_to_write, 
00072                              NPT_Size*   bytes_written = NULL) = 0;
00073     virtual NPT_Result WriteFully(const void* buffer, 
00074                                   NPT_Size    bytes_to_write);
00075     virtual NPT_Result WriteString(const char* string_buffer);
00076     virtual NPT_Result WriteLine(const char* line_buffer);
00077     virtual NPT_Result Seek(NPT_Position offset) = 0;
00078     virtual NPT_Result Tell(NPT_Position& offset) = 0;
00079     virtual NPT_Result Flush() { return NPT_SUCCESS; }
00080 };
00081 
00082 typedef NPT_Reference<NPT_OutputStream> NPT_OutputStreamReference;
00083 
00084 /*----------------------------------------------------------------------
00085 |    NPT_StreamToStreamCopy
00086 +---------------------------------------------------------------------*/
00087 NPT_Result NPT_StreamToStreamCopy(NPT_InputStream&  from, 
00088                                   NPT_OutputStream& to,
00089                                   NPT_Position      offset = 0,
00090                                   NPT_Size          size   = 0 /* 0 means the entire stream */);
00091 
00092 /*----------------------------------------------------------------------
00093 |    NPT_DelegatingInputStream
00094 |
00095 |    Use this class as a base class if you need to inherit both from
00096 |    NPT_InputStream and NPT_OutputStream which share the Seek and Tell
00097 |    method. In this case, you override the  base-specific version of 
00098 |    those methods, InputSeek, InputTell, instead of the Seek and Tell 
00099 |    methods.
00100 +---------------------------------------------------------------------*/
00101 class NPT_DelegatingInputStream : public NPT_InputStream
00102 {
00103 public:
00104     // NPT_InputStream methods
00105     NPT_Result Seek(NPT_Position offset) {
00106         return InputSeek(offset);
00107     }
00108     NPT_Result Tell(NPT_Position& offset) {
00109         return InputTell(offset);
00110     }
00111 
00112 private:
00113     // methods
00114     virtual NPT_Result InputSeek(NPT_Position  offset) = 0;
00115     virtual NPT_Result InputTell(NPT_Position& offset) = 0;
00116 };
00117 
00118 /*----------------------------------------------------------------------
00119 |    NPT_DelegatingOutputStream
00120 |
00121 |    Use this class as a base class if you need to inherit both from
00122 |    NPT_InputStream and NPT_OutputStream which share the Seek and Tell
00123 |    method. In this case, you override the  base-specific version of 
00124 |    those methods, OutputSeek and OutputTell, instead of the Seek and 
00125 |    Tell methods.
00126 +---------------------------------------------------------------------*/
00127 class NPT_DelegatingOutputStream : public NPT_OutputStream
00128 {
00129 public:
00130     // NPT_OutputStream methods
00131     NPT_Result Seek(NPT_Position offset) {
00132         return OutputSeek(offset);
00133     }
00134     NPT_Result Tell(NPT_Position& offset) {
00135         return OutputTell(offset);
00136     }
00137 
00138 private:
00139     // methods
00140     virtual NPT_Result OutputSeek(NPT_Position  offset) = 0;
00141     virtual NPT_Result OutputTell(NPT_Position& offset) = 0;
00142 };
00143 
00144 /*----------------------------------------------------------------------
00145 |    NPT_MemoryStream
00146 +---------------------------------------------------------------------*/
00147 class NPT_MemoryStream : 
00148     public NPT_DelegatingInputStream,
00149     public NPT_DelegatingOutputStream
00150 {
00151 public:
00152     // constructor and destructor
00153     NPT_MemoryStream(NPT_Size initial_capacity = 0);
00154     NPT_MemoryStream(const void* data, NPT_Size size);
00155     virtual ~NPT_MemoryStream() {}
00156 
00157     // accessors
00158     const NPT_DataBuffer& GetBuffer() const { return m_Buffer; }
00159 
00160     // NPT_InputStream methods
00161     NPT_Result Read(void*     buffer, 
00162                     NPT_Size  bytes_to_read, 
00163                     NPT_Size* bytes_read = NULL);
00164     NPT_Result GetSize(NPT_Size& size)  { 
00165         size = m_Buffer.GetDataSize();    
00166         return NPT_SUCCESS;
00167     }
00168     NPT_Result GetAvailable(NPT_Size& available) { 
00169         available = m_Buffer.GetDataSize()-m_ReadOffset; 
00170         return NPT_SUCCESS;
00171     }
00172 
00173     // NPT_OutputStream methods
00174     NPT_Result Write(const void* buffer, 
00175                      NPT_Size    bytes_to_write, 
00176                      NPT_Size*   bytes_written = NULL);
00177 
00178     // methods delegated to m_Buffer
00179     const NPT_Byte* GetData() const { return m_Buffer.GetData(); }
00180     NPT_Byte*       UseData()       { return m_Buffer.UseData(); }
00181     NPT_Size        GetDataSize() const { return m_Buffer.GetDataSize(); }
00182 
00183     // methods
00184     NPT_Result SetSize(NPT_Size size);
00185 
00186 private:
00187     // NPT_DelegatingInputStream methods
00188     NPT_Result InputSeek(NPT_Position offset);
00189     NPT_Result InputTell(NPT_Position& offset) { 
00190         offset = m_ReadOffset; 
00191         return NPT_SUCCESS;
00192     }
00193 
00194     // NPT_DelegatingOutputStream methods
00195     NPT_Result OutputSeek(NPT_Position offset);
00196     NPT_Result OutputTell(NPT_Position& offset) {
00197         offset = m_WriteOffset; 
00198         return NPT_SUCCESS;
00199     }
00200 
00201 protected:
00202     // members
00203     NPT_DataBuffer m_Buffer;
00204     NPT_Position   m_ReadOffset;
00205     NPT_Position   m_WriteOffset;
00206 };
00207 
00208 typedef NPT_Reference<NPT_MemoryStream> NPT_MemoryStreamReference;
00209 
00210 /*----------------------------------------------------------------------
00211 |   NPT_StringOutputStream
00212 +---------------------------------------------------------------------*/
00213 class NPT_StringOutputStream : public NPT_OutputStream
00214 {
00215 public:
00216     // methods
00217     NPT_StringOutputStream(NPT_Size size = 4096);
00218     NPT_StringOutputStream(NPT_String* storage);
00219     virtual ~NPT_StringOutputStream() ;
00220 
00221     const NPT_String& GetString() const { return *m_String; }
00222     NPT_Result Reset() { if (m_String) m_String->SetLength(0); return NPT_SUCCESS; }
00223 
00224     // NPT_OutputStream methods
00225     NPT_Result Write(const void* buffer, NPT_Size bytes_to_write, NPT_Size* bytes_written = NULL);
00226 
00227     NPT_Result Seek(NPT_Position /*offset*/)  { return NPT_ERROR_NOT_SUPPORTED;   }
00228     NPT_Result Tell(NPT_Position& offset) { offset = m_String->GetLength(); return NPT_SUCCESS; }
00229 
00230 protected:
00231     NPT_String* m_String;
00232     bool        m_StringIsOwned;
00233 };
00234 
00235 typedef NPT_Reference<NPT_StringOutputStream> NPT_StringOutputStreamReference;
00236 
00237 #endif // _NPT_STREAMS_H_