1 /*****************************************************************************
2 * dynamicoverlay_buffer.h : dynamic overlay buffer
3 *****************************************************************************
4 * Copyright (C) 2008 the VideoLAN team
7 * Author: Søren Bøg <avacore@videolan.org>
8 * Jean-Paul Saman <jpsaman@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
29 #include <vlc_common.h>
31 #include <vlc_filter.h>
35 #include "dynamicoverlay.h"
37 /*****************************************************************************
38 * buffer_t: Command and response buffer
39 *****************************************************************************/
41 int BufferInit( buffer_t *p_buffer )
43 memset( p_buffer, 0, sizeof( buffer_t ) );
44 p_buffer->p_memory = NULL;
45 p_buffer->p_begin = NULL;
50 int BufferDestroy( buffer_t *p_buffer )
52 if( p_buffer->p_memory != NULL )
54 free( p_buffer->p_memory );
56 p_buffer->p_memory = NULL;
57 p_buffer->p_begin = NULL;
62 char *BufferGetToken( buffer_t *p_buffer )
64 char *p_char = p_buffer->p_begin;
66 while( isspace( p_char[0] ) || p_char[0] == '\0' )
68 if( p_char <= (p_buffer->p_begin + p_buffer->i_length) )
76 int BufferAdd( buffer_t *p_buffer, const char *p_data, size_t i_len )
78 if( ( p_buffer->i_size - p_buffer->i_length -
79 ( p_buffer->p_begin - p_buffer->p_memory ) ) < i_len )
81 /* We'll have to do some rearranging to fit the new data. */
82 if( ( p_buffer->i_size - p_buffer->i_length ) >= i_len )
84 /* We have room in the current buffer, just need to move it */
85 memmove( p_buffer->p_memory, p_buffer->p_begin,
87 p_buffer->p_begin = p_buffer->p_memory;
91 // We need a bigger buffer
92 size_t i_newsize = 1024;
93 while( i_newsize < p_buffer->i_length + i_len )
95 /* TODO: Should I handle wrapping here? */
97 /* I'm not using realloc here, as I can avoid a memcpy/memmove in
98 some (most?) cases, and reset the start of the buffer. */
99 char *p_newdata = malloc( i_newsize );
100 if( p_newdata == NULL )
102 if( p_buffer->p_begin != NULL )
104 memcpy( p_newdata, p_buffer->p_begin, p_buffer->i_length );
105 free( p_buffer->p_memory );
107 p_buffer->p_memory = p_buffer->p_begin = p_newdata;
108 p_buffer->i_size = i_newsize;
112 /* Add the new data to the end of the current */
113 memcpy( p_buffer->p_begin + p_buffer->i_length, p_data, i_len );
114 p_buffer->i_length += i_len;
118 int BufferPrintf( buffer_t *p_buffer, const char *p_fmt, ... )
125 va_start( va, p_fmt );
127 i_len = vasprintf( &psz_data, p_fmt, va );
132 status = BufferAdd( p_buffer, psz_data, i_len );
137 int BufferDel( buffer_t *p_buffer, int i_len )
139 p_buffer->i_length -= i_len;
140 if( p_buffer->i_length == 0 )
142 /* No data, we can reset the buffer now. */
143 p_buffer->p_begin = p_buffer->p_memory;
147 p_buffer->p_begin += i_len;