]> git.sesse.net Git - vlc/blob - modules/video_filter/dynamicoverlay/dynamicoverlay_buffer.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / modules / video_filter / dynamicoverlay / dynamicoverlay_buffer.c
1 /*****************************************************************************
2  * dynamicoverlay_buffer.h : dynamic overlay buffer
3  *****************************************************************************
4  * Copyright (C) 2008-2009 the VideoLAN team
5  * $Id$
6  *
7  * Author: Søren Bøg <avacore@videolan.org>
8  *         Jean-Paul Saman <jpsaman@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <vlc_common.h>
30 #include <vlc_osd.h>
31 #include <vlc_filter.h>
32
33 #include <ctype.h>
34
35 #include "dynamicoverlay.h"
36
37 /*****************************************************************************
38  * buffer_t: Command and response buffer
39  *****************************************************************************/
40
41 int BufferInit( buffer_t *p_buffer )
42 {
43     memset( p_buffer, 0, sizeof( buffer_t ) );
44     p_buffer->p_memory = NULL;
45     p_buffer->p_begin = NULL;
46
47     return VLC_SUCCESS;
48 }
49
50 int BufferDestroy( buffer_t *p_buffer )
51 {
52     free( p_buffer->p_memory );
53     p_buffer->p_memory = NULL;
54     p_buffer->p_begin = NULL;
55
56     return VLC_SUCCESS;
57 }
58
59 char *BufferGetToken( buffer_t *p_buffer )
60 {
61     char *p_char = p_buffer->p_begin;
62
63     while( isspace( p_char[0] ) || p_char[0] == '\0' )
64     {
65         if( p_char <= (p_buffer->p_begin + p_buffer->i_length) )
66             p_char++;
67         else
68             return NULL;
69     }
70     return p_char;
71 }
72
73 int BufferAdd( buffer_t *p_buffer, const char *p_data, size_t i_len )
74 {
75     if( ( p_buffer->i_size - p_buffer->i_length -
76           ( p_buffer->p_begin - p_buffer->p_memory ) ) < i_len )
77     {
78         /* We'll have to do some rearranging to fit the new data. */
79         if( ( p_buffer->i_size - p_buffer->i_length ) >= i_len )
80         {
81             /* We have room in the current buffer, just need to move it */
82             memmove( p_buffer->p_memory, p_buffer->p_begin,
83                      p_buffer->i_length );
84             p_buffer->p_begin = p_buffer->p_memory;
85         }
86         else
87         {
88             // We need a bigger buffer
89             size_t i_newsize = 1024;
90             while( i_newsize < p_buffer->i_length + i_len )
91                 i_newsize *= 2;
92             /* TODO: Should I handle wrapping here? */
93
94             /* I'm not using realloc here, as I can avoid a memcpy/memmove in
95                some (most?) cases, and reset the start of the buffer. */
96             char *p_newdata = malloc( i_newsize );
97             if( p_newdata == NULL )
98                 return VLC_ENOMEM;
99             if( p_buffer->p_begin != NULL )
100             {
101                 memcpy( p_newdata, p_buffer->p_begin, p_buffer->i_length );
102                 free( p_buffer->p_memory );
103             }
104             p_buffer->p_memory = p_buffer->p_begin = p_newdata;
105             p_buffer->i_size = i_newsize;
106         }
107     }
108
109     /* Add the new data to the end of the current */
110     memcpy( p_buffer->p_begin + p_buffer->i_length, p_data, i_len );
111     p_buffer->i_length += i_len;
112     return VLC_SUCCESS;
113 }
114
115 int BufferPrintf( buffer_t *p_buffer, const char *p_fmt, ... )
116 {
117     int i_len;
118     int status;
119     char *psz_data;
120
121     va_list va;
122     va_start( va, p_fmt );
123
124     i_len = vasprintf( &psz_data, p_fmt, va );
125     va_end( va );
126     if( i_len == -1 )
127         return VLC_ENOMEM;
128
129     status = BufferAdd( p_buffer, psz_data, i_len );
130     free( psz_data );
131     return status;
132 }
133
134 int BufferDel( buffer_t *p_buffer, int i_len )
135 {
136     p_buffer->i_length -= i_len;
137     if( p_buffer->i_length == 0 )
138     {
139         /* No data, we can reset the buffer now. */
140         p_buffer->p_begin = p_buffer->p_memory;
141     }
142     else
143     {
144         p_buffer->p_begin += i_len;
145     }
146     return VLC_SUCCESS;
147 }