2004-03-13 22:06:53 +08:00
|
|
|
/*
|
|
|
|
* ZoneMinder Flexible Memory Interface, $Date$, $Revision$
|
2006-01-17 18:56:30 +08:00
|
|
|
* Copyright (C) 2003, 2004, 2005, 2006 Philip Coombes
|
2004-03-13 22:06:53 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ZM_BUFFER_H
|
|
|
|
#define ZM_BUFFER_H
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
#include "zm.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2004-03-13 22:06:53 +08:00
|
|
|
class Buffer
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
unsigned char *storage;
|
|
|
|
unsigned int allocation;
|
|
|
|
unsigned int size;
|
|
|
|
unsigned char *head;
|
|
|
|
unsigned char *tail;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Buffer() : storage( 0 ), allocation( 0 ), size( 0 ), head( 0 ), tail( 0 )
|
|
|
|
{
|
|
|
|
}
|
2008-07-16 16:35:59 +08:00
|
|
|
Buffer( unsigned int p_size ) : allocation( p_size ), size( 0 )
|
2004-03-13 22:06:53 +08:00
|
|
|
{
|
|
|
|
head = storage = new unsigned char[allocation];
|
|
|
|
tail = head;
|
|
|
|
}
|
|
|
|
Buffer( const unsigned char *p_storage, unsigned int p_size ) : allocation( p_size ), size( p_size )
|
|
|
|
{
|
|
|
|
head = storage = new unsigned char[size];
|
|
|
|
memcpy( storage, p_storage, size );
|
|
|
|
tail = head + size;
|
|
|
|
}
|
|
|
|
Buffer( const Buffer &buffer ) : allocation( buffer.size ), size( buffer.size )
|
|
|
|
{
|
|
|
|
head = storage = new unsigned char[size];
|
|
|
|
memcpy( storage, buffer.storage, size );
|
|
|
|
tail = head + size;
|
|
|
|
}
|
|
|
|
~Buffer()
|
|
|
|
{
|
|
|
|
delete[] storage;
|
|
|
|
}
|
|
|
|
unsigned char *Head() const { return( head ); }
|
|
|
|
unsigned char *Tail() const { return( tail ); }
|
|
|
|
unsigned int Size() const { return( size ); }
|
|
|
|
unsigned int Size( unsigned int p_size )
|
|
|
|
{
|
|
|
|
if ( size < p_size )
|
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
Expand( p_size-size );
|
2004-03-13 22:06:53 +08:00
|
|
|
}
|
|
|
|
return( size );
|
|
|
|
}
|
|
|
|
//unsigned int Allocation() const { return( allocation ); }
|
|
|
|
|
|
|
|
void Empty()
|
|
|
|
{
|
|
|
|
size = 0;
|
2008-07-16 16:35:59 +08:00
|
|
|
head = tail = storage;
|
2004-03-13 22:06:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Assign( const unsigned char *p_storage, unsigned int p_size );
|
|
|
|
unsigned int Assign( const Buffer &buffer )
|
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
return( Assign( buffer.head, buffer.size ) );
|
2004-03-13 22:06:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Consume( unsigned int count )
|
|
|
|
{
|
2005-05-06 00:43:36 +08:00
|
|
|
if ( count > size )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Warning( "Attempt to consume %d bytes of buffer, size is only %d bytes", count, size );
|
2005-05-06 00:43:36 +08:00
|
|
|
count = size;
|
|
|
|
}
|
2004-03-13 22:06:53 +08:00
|
|
|
head += count;
|
|
|
|
size -= count;
|
2005-05-14 22:40:17 +08:00
|
|
|
return( count );
|
2004-03-13 22:06:53 +08:00
|
|
|
}
|
|
|
|
unsigned int Shrink( unsigned int count )
|
|
|
|
{
|
2005-05-06 00:43:36 +08:00
|
|
|
if ( count > size )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Warning( "Attempt to shrink buffer by %d bytes, size is only %d bytes", count, size );
|
2005-05-06 00:43:36 +08:00
|
|
|
count = size;
|
|
|
|
}
|
2004-03-13 22:06:53 +08:00
|
|
|
size -= count;
|
|
|
|
if ( tail > head + size )
|
|
|
|
tail = head + size;
|
2005-05-14 22:40:17 +08:00
|
|
|
return( count );
|
2004-03-13 22:06:53 +08:00
|
|
|
}
|
|
|
|
unsigned int Expand( unsigned int count );
|
|
|
|
unsigned char *Extract( unsigned int p_size )
|
|
|
|
{
|
2005-06-13 01:05:19 +08:00
|
|
|
if ( p_size > size )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Warning( "Attempt to extract %d bytes of buffer, size is only %d bytes", p_size, size );
|
2005-06-13 01:05:19 +08:00
|
|
|
p_size = size;
|
|
|
|
}
|
2004-03-13 22:06:53 +08:00
|
|
|
unsigned char *old_head = head;
|
|
|
|
head += p_size;
|
|
|
|
size -= p_size;
|
|
|
|
return( old_head );
|
|
|
|
}
|
|
|
|
unsigned int Append( const unsigned char *p_storage, unsigned int p_size )
|
|
|
|
{
|
|
|
|
Expand( p_size );
|
|
|
|
memcpy( tail, p_storage, p_size );
|
|
|
|
tail += p_size;
|
|
|
|
return( size );
|
|
|
|
}
|
|
|
|
unsigned int Append( const Buffer &buffer )
|
|
|
|
{
|
|
|
|
return( Append( buffer.head, buffer.size ) );
|
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
Buffer &operator=( const Buffer &buffer )
|
|
|
|
{
|
|
|
|
Assign( buffer );
|
|
|
|
return( *this );
|
|
|
|
}
|
2004-03-13 22:06:53 +08:00
|
|
|
Buffer &operator+=( const Buffer &buffer )
|
|
|
|
{
|
|
|
|
Append( buffer );
|
|
|
|
return( *this );
|
|
|
|
}
|
|
|
|
Buffer &operator+=( unsigned int count )
|
|
|
|
{
|
|
|
|
Expand( count );
|
|
|
|
return( *this );
|
|
|
|
}
|
|
|
|
Buffer &operator-=( unsigned int count )
|
|
|
|
{
|
|
|
|
Consume( count );
|
|
|
|
return( *this );
|
|
|
|
}
|
|
|
|
operator unsigned char *() const
|
|
|
|
{
|
|
|
|
return( head );
|
|
|
|
}
|
|
|
|
operator char *() const
|
|
|
|
{
|
|
|
|
return( (char *)head );
|
|
|
|
}
|
2005-05-14 22:40:17 +08:00
|
|
|
unsigned char operator[](int index) const
|
2004-03-13 22:06:53 +08:00
|
|
|
{
|
2005-05-14 22:40:17 +08:00
|
|
|
return( *(head+index) );
|
2004-03-13 22:06:53 +08:00
|
|
|
}
|
|
|
|
operator int () const
|
|
|
|
{
|
|
|
|
return( (int)size );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // ZM_BUFFER_H
|