1/****************************************************************************
2** libebml : parse EBML files, see http://embl.sourceforge.net/
3**
4** <file/class description>
5**
6** Copyright (C) 2003-2004 Jory Stone.  All rights reserved.
7**
8** This library is free software; you can redistribute it and/or
9** modify it under the terms of the GNU Lesser General Public
10** License as published by the Free Software Foundation; either
11** version 2.1 of the License, or (at your option) any later version.
12**
13** This library is distributed in the hope that it will be useful,
14** but WITHOUT ANY WARRANTY; without even the implied warranty of
15** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16** Lesser General Public License for more details.
17**
18** You should have received a copy of the GNU Lesser General Public
19** License along with this library; if not, write to the Free Software
20** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21**
22** See http://www.matroska.org/license/lgpl/ for LGPL licensing information.
23**
24** Contact license@matroska.org if any conditions of this licensing are
25** not clear to you.
26**
27**********************************************************************/
28
29/*!
30	\file
31	\version \$Id: MemIOCallback.h 1298 2008-02-21 22:14:18Z mosu $
32	\author Jory Stone <jcsston @ toughguy.net>
33*/
34#ifndef LIBEBML_MEMIOCALLBACK_H
35#define LIBEBML_MEMIOCALLBACK_H
36
37#include "IOCallback.h"
38#include <string>
39#if (!defined(__BEOS__) && !defined(__HAIKU__))
40#include <sstream>
41#else
42#include <strstream>
43#define stringstream strstream
44#endif
45
46START_LIBEBML_NAMESPACE
47
48class EBML_DLL_API MemIOCallback : public IOCallback
49{
50public:
51	MemIOCallback(uint64 DefaultSize = 128);
52	~MemIOCallback();
53
54	/*!
55		Use this to copy some data to the Buffer from this classes data
56	*/
57	uint32 read(void *Buffer, size_t Size);
58
59	/*!
60		Seek to the specified position. The mode can have either SEEK_SET, SEEK_CUR
61		or SEEK_END. The callback should return true(1) if the seek operation succeeded
62		or false (0), when the seek fails.
63	*/
64	void setFilePointer(int64 Offset, seek_mode Mode=seek_beginning);
65
66	/*!
67		This callback just works like its read pendant. It returns the number of bytes written.
68	*/
69	size_t write(const void *Buffer, size_t Size);
70
71	/*!
72		Although the position is always positive, the return value of this callback is signed to
73		easily allow negative values for returning errors. When an error occurs, the implementor
74		should return -1 and the file pointer otherwise.
75
76		If an error occurs, an exception should be thrown.
77	*/
78	virtual uint64 getFilePointer() {return dataBufferPos;};
79
80	/*!
81		The close callback flushes the file buffers to disk and closes the file. When using the stdio
82		library, this is equivalent to calling fclose. When the close is not successful, an exception
83		should be thrown.
84	*/
85	void close() {};
86
87	binary *GetDataBuffer() const {return dataBuffer;};
88	uint64 GetDataBufferSize() {return dataBufferTotalSize;};
89	void SetDataBufferSize(uint64 newDataBufferSize) {dataBufferTotalSize = newDataBufferSize;};
90	/*!
91		Use this to write some data from another IOCallback
92	*/
93	uint32 write(IOCallback & IOToRead, size_t Size);
94
95	bool IsOk() { return mOk; };
96	const std::string &GetLastErrorStr() { return mLastErrorStr; };
97protected:
98	bool mOk;
99	std::string mLastErrorStr;
100
101	binary *dataBuffer;
102	/*!
103		Postion where we start 'writing' to the dataBuffer
104	*/
105	uint64 dataBufferPos;
106	/*!
107		Size of the data in the dataBuffer
108	*/
109	uint64 dataBufferTotalSize;
110	/*!
111		Size of the memory malloc()/realloc()
112	*/
113	uint64 dataBufferMemorySize;
114};
115
116END_LIBEBML_NAMESPACE
117
118#endif // LIBEBML_MEMIOCALLBACK_H
119