]> git.sesse.net Git - vlc/blob - modules/video_filter/dynamicoverlay/dynamicoverlay_buffer.c
Fix encoding (and writing).
[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     if( p_buffer->p_memory != NULL )
53     {
54         free( p_buffer->p_memory );
55     }
56     p_buffer->p_memory = NULL;
57     p_buffer->p_begin = NULL;
58
59     return VLC_SUCCESS;
60 }
61
62 char *BufferGetToken( buffer_t *p_buffer )
63 {
64     char *p_char = p_buffer->p_begin;
65
66     while( isspace( p_char[0] ) || p_char[0] == '\0' )
67     {
68         if( p_char <= (p_buffer->p_begin + p_buffer->i_length) )
69             p_char++;
70         else
71             return NULL;
72     }
73     return p_char;
74 }
75
76 int BufferAdd( buffer_t *p_buffer, const char *p_data, size_t i_len )
77 {
78     if( ( p_buffer->i_size - p_buffer->i_length -
79           ( p_buffer->p_begin - p_buffer->p_memory ) ) < i_len )
80     {
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 )
83         {
84             /* We have room in the current buffer, just need to move it */
85             memmove( p_buffer->p_memory, p_buffer->p_begin,
86                      p_buffer->i_length );
87             p_buffer->p_begin = p_buffer->p_memory;
88         }
89         else
90         {
91             // We need a bigger buffer
92             size_t i_newsize = 1024;
93             while( i_newsize < p_buffer->i_length + i_len )
94                 i_newsize *= 2;
95             /* TODO: Should I handle wrapping here? */
96
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 )
101                 return VLC_ENOMEM;
102             if( p_buffer->p_begin != NULL )
103             {
104                 memcpy( p_newdata, p_buffer->p_begin, p_buffer->i_length );
105                 free( p_buffer->p_memory );
106             }
107             p_buffer->p_memory = p_buffer->p_begin = p_newdata;
108             p_buffer->i_size = i_newsize;
109         }
110     }
111
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;
115     return VLC_SUCCESS;
116 }
117
118 int BufferPrintf( buffer_t *p_buffer, const char *p_fmt, ... )
119 {
120     int i_len;
121     int status;
122     char *psz_data;
123
124     va_list va;
125     va_start( va, p_fmt );
126
127     i_len = vasprintf( &psz_data, p_fmt, va );
128     va_end( va );
129     if( i_len == -1 )
130         return VLC_ENOMEM;
131
132     status = BufferAdd( p_buffer, psz_data, i_len );
133     free( psz_data );
134     return status;
135 }
136
137 int BufferDel( buffer_t *p_buffer, int i_len )
138 {
139     p_buffer->i_length -= i_len;
140     if( p_buffer->i_length == 0 )
141     {
142         /* No data, we can reset the buffer now. */
143         p_buffer->p_begin = p_buffer->p_memory;
144     }
145     else
146     {
147         p_buffer->p_begin += i_len;
148     }
149     return VLC_SUCCESS;
150 }