#ifndef I_MEMORY_STREAM_H__ #define I_MEMORY_STREAM_H__ #include "IDataStream.h" namespace LifeCore { class IMemoryStream : public LifeCore::IDataStream { private: char* pData; size_t position; size_t stmLength; bool userData; public: IMemoryStream() { userData = true; pData = 0; stmLength = 0; position = 0; } IMemoryStream(size_t size) { userData = false; pData = new char[size]; stmLength = size; position = 0; } IMemoryStream(char* _pData, size_t dataSize) { userData = true; pData = _pData; stmLength = dataSize; position = 0; } ~IMemoryStream() { if(!userData && pData !=0) { delete[] pData; } } int stride; int vertexCount; char* GetData() { return pData; } void Clear(int val) { memset(pData, val, stmLength); } void Close() { } void Reset(char* _pData, size_t dataSize) { pData = _pData; stmLength = dataSize; position = 0; } size_t Read(void* pBuffer, size_t countBytes) { size_t diff = stmLength - position; if(diff <=0 || !countBytes)return 0; if(countBytes > diff) countBytes = diff; memcpy(pBuffer, pData + position, countBytes); position+=countBytes; return countBytes; } size_t ReadInt8(char& data) { return Read(&data, sizeof(char)); } size_t ReadInt16(short& data) { return Read(&data, sizeof(short)); } size_t ReadInt32(int& data) { return Read(&data, sizeof(int)); } #define DEF_WRITE(type_name)\ size_t Write(type_name val) \ { return Write(&val, sizeof(type_name)); } \ DEF_WRITE(short); DEF_WRITE(int); DEF_WRITE(char); DEF_WRITE(float); DEF_WRITE(double); DEF_WRITE(long long); DEF_WRITE(unsigned short); DEF_WRITE(unsigned int); DEF_WRITE(unsigned char); size_t Write(const void* pBuffer, size_t countBytes) { size_t diff = stmLength - position; if(diff <=0)return 0; //if(countBytes > diff) // countBytes = diff; memcpy(pData + position, pBuffer, countBytes); position+=countBytes; return countBytes; } void Seek(size_t _position) { position = _position; if(position < 0) position = 0; if(position >= stmLength && stmLength !=0) position = stmLength - 1; } size_t Tell() { return position; } size_t Length() { return stmLength; } bool CanRead() { return true; } bool CanWrite() { return true; } }; } #endif