source: libebml/src/MemIOCallback.cpp @ 1443

Revision 1443, 3.6 KB checked in by astrange, 3 years ago (diff)

Merge libebml and libmatroska changes.

Line 
1/****************************************************************************
2** libebml : parse EBML files, see http://embl.sourceforge.net/
3**
4** <file/class description>
5**
6** Copyright (C) 2003 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.cpp 693 2004-07-31 08:56:28Z robux4 $
32        \author Jory Stone <jcsston @ toughguy.net>
33*/
34
35#include "ebml/MemIOCallback.h"
36#include "ebml/Debug.h"
37#include "ebml/EbmlConfig.h"
38
39START_LIBEBML_NAMESPACE
40
41MemIOCallback::MemIOCallback(uint64 DefaultSize)
42{
43        //The default size of the buffer is 128 bytes
44        dataBuffer = (binary *)malloc(DefaultSize);
45        if (dataBuffer == NULL) {
46                mOk = false;
47                std::stringstream Msg;
48                Msg << "Failed to alloc memory block of size ";
49// not working with VC6         Msg << DefaultSize;
50                mLastErrorStr = Msg.str();                             
51                return;
52        }
53       
54        dataBufferMemorySize = DefaultSize;
55        dataBufferPos = 0;
56        dataBufferTotalSize = 0;
57        mOk = true;
58}
59
60MemIOCallback::~MemIOCallback()
61{
62        if (dataBuffer != NULL)
63                free(dataBuffer);
64}
65
66uint32 MemIOCallback::read(void *Buffer, size_t Size)
67{
68        if (Buffer == NULL || Size < 1)
69                return 0;
70        //If the size is larger than than the amount left in the buffer
71        if (Size + dataBufferPos > dataBufferTotalSize)
72        {
73                //We will only return the remaining data
74                memcpy(Buffer, dataBuffer + dataBufferPos, dataBufferTotalSize - dataBufferPos);
75        uint64 oldDataPos = dataBufferPos;
76                dataBufferPos = dataBufferTotalSize;
77                return dataBufferTotalSize - oldDataPos;
78        }
79               
80        //Well... We made it here, so do a quick and simple copy
81        memcpy(Buffer, dataBuffer+dataBufferPos, Size);
82        dataBufferPos += Size;
83
84        return Size;
85}
86
87void MemIOCallback::setFilePointer(int64 Offset, seek_mode Mode)
88{
89        if (Mode == seek_beginning)
90                dataBufferPos = Offset;
91        else if (Mode == seek_current)
92                dataBufferPos = dataBufferPos + Offset;
93        else if (Mode == seek_end)
94                dataBufferPos = dataBufferTotalSize + Offset;
95}
96
97size_t MemIOCallback::write(const void *Buffer, size_t Size)
98{
99        if (dataBufferMemorySize < dataBufferPos + Size)
100        {
101                //We need more memory!
102                dataBuffer = (binary *)realloc((void *)dataBuffer, dataBufferPos + Size);               
103        }
104        memcpy(dataBuffer+dataBufferPos, Buffer, Size);
105        dataBufferPos += Size;
106        if (dataBufferPos > dataBufferTotalSize)
107                dataBufferTotalSize = dataBufferPos;
108
109        return Size;
110}
111
112uint32 MemIOCallback::write(IOCallback & IOToRead, size_t Size)
113{
114        if (dataBufferMemorySize < dataBufferPos + Size)
115        {
116                //We need more memory!
117                dataBuffer = (binary *)realloc((void *)dataBuffer, dataBufferPos + Size);               
118        }
119        IOToRead.readFully(&dataBuffer[dataBufferPos], Size);
120        dataBufferTotalSize = Size;
121        return Size;
122}
123
124END_LIBEBML_NAMESPACE
Note: See TracBrowser for help on using the repository browser.