#ifndef _NPT_STREAMS_H_
#define _NPT_STREAMS_H_
#include "NptTypes.h"
#include "NptReferences.h"
#include "NptConstants.h"
#include "NptResults.h"
#include "NptDataBuffer.h"
#include "NptStrings.h"
class NPT_String;
const int NPT_ERROR_READ_FAILED = NPT_ERROR_BASE_IO - 0;
const int NPT_ERROR_WRITE_FAILED = NPT_ERROR_BASE_IO - 1;
const int NPT_ERROR_EOS = NPT_ERROR_BASE_IO - 2;
class NPT_InputStream
{
public:
virtual ~NPT_InputStream() {};
virtual NPT_Result Load(NPT_DataBuffer& buffer, NPT_Size max_read = 0);
virtual NPT_Result Read(void* buffer,
NPT_Size bytes_to_read,
NPT_Size* bytes_read = NULL) = 0;
virtual NPT_Result ReadFully(void* buffer,
NPT_Size bytes_to_read);
virtual NPT_Result Seek(NPT_Position offset) = 0;
virtual NPT_Result Skip(NPT_Size offset);
virtual NPT_Result Tell(NPT_Position& offset) = 0;
virtual NPT_Result GetSize(NPT_LargeSize& size) = 0;
virtual NPT_Result GetAvailable(NPT_LargeSize& available) = 0;
NPT_Result ReadUI64(NPT_UInt64& value);
NPT_Result ReadUI32(NPT_UInt32& value);
NPT_Result ReadUI24(NPT_UInt32& value);
NPT_Result ReadUI16(NPT_UInt16& value);
NPT_Result ReadUI08(NPT_UInt8& value);
};
typedef NPT_Reference<NPT_InputStream> NPT_InputStreamReference;
class NPT_OutputStream
{
public:
virtual ~NPT_OutputStream() {};
virtual NPT_Result Write(const void* buffer,
NPT_Size bytes_to_write,
NPT_Size* bytes_written = NULL) = 0;
virtual NPT_Result WriteFully(const void* buffer,
NPT_Size bytes_to_write);
virtual NPT_Result WriteString(const char* string_buffer);
virtual NPT_Result WriteLine(const char* line_buffer);
virtual NPT_Result Seek(NPT_Position offset) = 0;
virtual NPT_Result Tell(NPT_Position& offset) = 0;
virtual NPT_Result Flush() { return NPT_SUCCESS; }
NPT_Result WriteUI64(NPT_UInt64 value);
NPT_Result WriteUI32(NPT_UInt32 value);
NPT_Result WriteUI24(NPT_UInt32 value);
NPT_Result WriteUI16(NPT_UInt16 value);
NPT_Result WriteUI08(NPT_UInt8 value);
};
typedef NPT_Reference<NPT_OutputStream> NPT_OutputStreamReference;
NPT_Result NPT_StreamToStreamCopy(NPT_InputStream& from,
NPT_OutputStream& to,
NPT_Position offset = 0,
NPT_LargeSize size = 0,
NPT_LargeSize* bytes_written = NULL);
class NPT_DelegatingInputStream : public NPT_InputStream
{
public:
NPT_Result Seek(NPT_Position offset) {
return InputSeek(offset);
}
NPT_Result Tell(NPT_Position& offset) {
return InputTell(offset);
}
private:
virtual NPT_Result InputSeek(NPT_Position offset) = 0;
virtual NPT_Result InputTell(NPT_Position& offset) = 0;
};
class NPT_DelegatingOutputStream : public NPT_OutputStream
{
public:
NPT_Result Seek(NPT_Position offset) {
return OutputSeek(offset);
}
NPT_Result Tell(NPT_Position& offset) {
return OutputTell(offset);
}
private:
virtual NPT_Result OutputSeek(NPT_Position offset) = 0;
virtual NPT_Result OutputTell(NPT_Position& offset) = 0;
};
class NPT_MemoryStream :
public NPT_DelegatingInputStream,
public NPT_DelegatingOutputStream
{
public:
NPT_MemoryStream(NPT_Size initial_capacity = 0);
NPT_MemoryStream(const void* data, NPT_Size size);
virtual ~NPT_MemoryStream() {}
const NPT_DataBuffer& GetBuffer() const { return m_Buffer; }
NPT_Result Read(void* buffer,
NPT_Size bytes_to_read,
NPT_Size* bytes_read = NULL);
NPT_Result GetSize(NPT_LargeSize& size) {
size = m_Buffer.GetDataSize();
return NPT_SUCCESS;
}
NPT_Result GetAvailable(NPT_LargeSize& available) {
available = (NPT_LargeSize)m_Buffer.GetDataSize()-m_ReadOffset;
return NPT_SUCCESS;
}
NPT_Result Write(const void* buffer,
NPT_Size bytes_to_write,
NPT_Size* bytes_written = NULL);
const NPT_Byte* GetData() const { return m_Buffer.GetData(); }
NPT_Byte* UseData() { return m_Buffer.UseData(); }
NPT_Size GetDataSize() const { return m_Buffer.GetDataSize(); }
NPT_Size GetBufferSize() const { return m_Buffer.GetBufferSize();}
NPT_Result SetDataSize(NPT_Size size);
private:
NPT_Result InputSeek(NPT_Position offset);
NPT_Result InputTell(NPT_Position& offset) {
offset = m_ReadOffset;
return NPT_SUCCESS;
}
NPT_Result OutputSeek(NPT_Position offset);
NPT_Result OutputTell(NPT_Position& offset) {
offset = m_WriteOffset;
return NPT_SUCCESS;
}
protected:
NPT_DataBuffer m_Buffer;
NPT_Size m_ReadOffset;
NPT_Size m_WriteOffset;
};
typedef NPT_Reference<NPT_MemoryStream> NPT_MemoryStreamReference;
class NPT_StringOutputStream : public NPT_OutputStream
{
public:
NPT_StringOutputStream(NPT_Size size = 4096);
NPT_StringOutputStream(NPT_String* storage);
virtual ~NPT_StringOutputStream() ;
const NPT_String& GetString() const { return *m_String; }
NPT_Result Reset() { if (m_String) m_String->SetLength(0); return NPT_SUCCESS; }
NPT_Result Write(const void* buffer, NPT_Size bytes_to_write, NPT_Size* bytes_written = NULL);
NPT_Result Seek(NPT_Position ) { return NPT_ERROR_NOT_SUPPORTED; }
NPT_Result Tell(NPT_Position& offset) { offset = m_String->GetLength(); return NPT_SUCCESS; }
protected:
NPT_String* m_String;
bool m_StringIsOwned;
};
typedef NPT_Reference<NPT_StringOutputStream> NPT_StringOutputStreamReference;
class NPT_SubInputStream : public NPT_InputStream
{
public:
NPT_SubInputStream(NPT_InputStreamReference& source,
NPT_Position start,
NPT_LargeSize size);
virtual NPT_Result Read(void* buffer,
NPT_Size bytes_to_read,
NPT_Size* bytes_read = NULL) = 0;
virtual NPT_Result Seek(NPT_Position offset) = 0;
virtual NPT_Result Tell(NPT_Position& offset) = 0;
virtual NPT_Result GetSize(NPT_LargeSize& size) = 0;
virtual NPT_Result GetAvailable(NPT_LargeSize& available) = 0;
private:
NPT_InputStreamReference m_Source;
NPT_Position m_Position;
NPT_Position m_Start;
NPT_LargeSize m_Size;
};
class NPT_NullOutputStream : public NPT_OutputStream
{
public:
NPT_NullOutputStream() {}
virtual ~NPT_NullOutputStream() {}
NPT_Result Write(const void* buffer, NPT_Size bytes_to_write, NPT_Size* bytes_written = NULL);
NPT_Result Seek(NPT_Position ) { return NPT_ERROR_NOT_SUPPORTED; }
NPT_Result Tell(NPT_Position& ) { return NPT_ERROR_NOT_SUPPORTED; }
};
typedef NPT_Reference<NPT_NullOutputStream> NPT_NullOutputStreamReference;
#endif