1 /****************************************************************************
2 ** libebml : parse EBML files, see http://embl.sourceforge.net/
4 ** <file/class description>
6 ** Copyright (C) 2003 Jory Stone. All rights reserved.
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.
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.
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
22 ** See http://www.matroska.org/license/lgpl/ for LGPL licensing information.
24 ** Contact license@matroska.org if any conditions of this licensing are
27 **********************************************************************/
31 \version \$Id: MemIOCallback.cpp 693 2004-07-31 08:56:28Z robux4 $
32 \author Jory Stone <jcsston @ toughguy.net>
35 #include "ebml/MemIOCallback.h"
36 #include "ebml/Debug.h"
37 #include "ebml/EbmlConfig.h"
39 START_LIBEBML_NAMESPACE
41 MemIOCallback::MemIOCallback(uint64 DefaultSize
)
43 //The default size of the buffer is 128 bytes
44 dataBuffer
= (binary
*)malloc(DefaultSize
);
45 if (dataBuffer
== NULL
) {
47 std::stringstream Msg
;
48 Msg
<< "Failed to alloc memory block of size ";
49 // not working with VC6 Msg << DefaultSize;
50 mLastErrorStr
= Msg
.str();
54 dataBufferMemorySize
= DefaultSize
;
56 dataBufferTotalSize
= 0;
60 MemIOCallback::~MemIOCallback()
62 if (dataBuffer
!= NULL
)
66 uint32
MemIOCallback::read(void *Buffer
, size_t Size
)
68 if (Buffer
== NULL
|| Size
< 1)
70 //If the size is larger than than the amount left in the buffer
71 if (Size
+ dataBufferPos
> dataBufferTotalSize
)
73 //We will only return the remaining data
74 memcpy(Buffer
, dataBuffer
+ dataBufferPos
, dataBufferTotalSize
- dataBufferPos
);
75 dataBufferPos
= dataBufferTotalSize
;
76 return dataBufferTotalSize
- dataBufferPos
;
79 //Well... We made it here, so do a quick and simple copy
80 memcpy(Buffer
, dataBuffer
+dataBufferPos
, Size
);
81 dataBufferPos
+= Size
;
86 void MemIOCallback::setFilePointer(int64 Offset
, seek_mode Mode
)
88 if (Mode
== seek_beginning
)
89 dataBufferPos
= Offset
;
90 else if (Mode
== seek_current
)
91 dataBufferPos
= dataBufferPos
+ Offset
;
92 else if (Mode
== seek_end
)
93 dataBufferPos
= dataBufferTotalSize
+ Offset
;
96 size_t MemIOCallback::write(const void *Buffer
, size_t Size
)
98 if (dataBufferMemorySize
< dataBufferPos
+ Size
)
100 //We need more memory!
101 dataBuffer
= (binary
*)realloc((void *)dataBuffer
, dataBufferPos
+ Size
);
103 memcpy(dataBuffer
+dataBufferPos
, Buffer
, Size
);
104 dataBufferPos
+= Size
;
105 if (dataBufferPos
> dataBufferTotalSize
)
106 dataBufferTotalSize
= dataBufferPos
;
111 uint32
MemIOCallback::write(IOCallback
& IOToRead
, size_t Size
)
113 if (dataBufferMemorySize
< dataBufferPos
+ Size
)
115 //We need more memory!
116 dataBuffer
= (binary
*)realloc((void *)dataBuffer
, dataBufferPos
+ Size
);
118 IOToRead
.readFully(&dataBuffer
[dataBufferPos
], Size
);
119 dataBufferTotalSize
= Size
;
123 END_LIBEBML_NAMESPACE