[vlc-devel] commit: Improved a bit SRT support. (Laurent Aimar )
git version control
git at videolan.org
Tue Feb 24 23:47:01 CET 2009
vlc | branch: master | Laurent Aimar <fenrir at videolan.org> | Mon Feb 23 21:08:35 2009 +0100| [5c7ef93f15cddd8fbc8104f3899543d447c90de1] | committer: Laurent Aimar
Improved a bit SRT support.
Added missings realloc/malloc checks.
Added support for not closed html tags (only if missing at the end).
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=5c7ef93f15cddd8fbc8104f3899543d447c90de1
---
modules/codec/subtitles/subsdec.c | 85 +++++++++++++++++++++++++-----------
1 files changed, 59 insertions(+), 26 deletions(-)
diff --git a/modules/codec/subtitles/subsdec.c b/modules/codec/subtitles/subsdec.c
index d7c6dd6..e0db173 100644
--- a/modules/codec/subtitles/subsdec.c
+++ b/modules/codec/subtitles/subsdec.c
@@ -598,24 +598,26 @@ static void HtmlCopy( char **ppsz_html, char **ppsz_subtitle, const char *psz_te
static char *CreateHtmlSubtitle( int *pi_align, char *psz_subtitle )
{
- char *psz_tag = malloc( ( strlen( psz_subtitle ) / 3 ) + 1 );
- if( !psz_tag ) return NULL;
- size_t i_buf_size = strlen( psz_subtitle ) + 100;
- char *psz_html_start = malloc( i_buf_size );
- bool b_has_align = false;
-
+ /* */
+ char *psz_tag = malloc( ( strlen( psz_subtitle ) / 3 ) + 1 );
+ if( !psz_tag )
+ return NULL;
psz_tag[ 0 ] = '\0';
+ /* */
+ size_t i_buf_size = strlen( psz_subtitle ) + 100;
+ char *psz_html_start = malloc( i_buf_size );
+ char *psz_html = psz_html_start;
if( psz_html_start == NULL )
{
free( psz_tag );
return NULL;
}
-
- char *psz_html = psz_html_start;
+ psz_html[0] = '\0';
- strcpy( psz_html, "<text>" );
- psz_html += 6;
+ bool b_has_align = false;
+
+ HtmlPut( &psz_html, "<text>" );
/* */
while( *psz_subtitle )
@@ -877,27 +879,58 @@ static char *CreateHtmlSubtitle( int *pi_align, char *psz_subtitle )
if( ( size_t )( psz_html - psz_html_start ) > i_buf_size - 50 )
{
- int i_len = psz_html - psz_html_start;
+ const int i_len = psz_html - psz_html_start;
i_buf_size += 200;
- psz_html_start = realloc( psz_html_start, i_buf_size );
- psz_html = psz_html_start + i_len;
- *psz_html = '\0';
+ char *psz_new = realloc( psz_html_start, i_buf_size );
+ if( !psz_new )
+ break;
+ psz_html_start = psz_new;
+ psz_html = &psz_new[i_len];
}
}
- strcpy( psz_html, "</text>" );
- psz_html += 7;
-
- if( psz_tag[ 0 ] != '\0' )
+ if( psz_html_start )
{
- /* Not well formed -- kill everything */
- free( psz_html_start );
- psz_html_start = NULL;
- }
- else if( psz_html_start )
- {
- /* Shrink the memory requirements */
- psz_html_start = realloc( psz_html_start, psz_html - psz_html_start + 1 );
+ static const char *psz_text_close = "</text>";
+ static const char *psz_tag_long = "/font>";
+
+ /* Realloc for closing tags and shrink memory */
+ const size_t i_length = (size_t)( psz_html - psz_html_start );
+
+ const size_t i_size = i_length + strlen(psz_tag_long) * strlen(psz_tag) + strlen(psz_text_close) + 1;
+ char *psz_new = realloc( psz_html_start, i_size );
+ if( psz_new )
+ {
+ psz_html_start = psz_new;
+ psz_html = &psz_new[i_length];
+
+ /* Close not well formed subtitle */
+ while( *psz_tag )
+ {
+ /* */
+ char *psz_last = &psz_tag[strlen(psz_tag)-1];
+ switch( *psz_last )
+ {
+ case 'b':
+ HtmlPut( &psz_html, "</b>" );
+ break;
+ case 'i':
+ HtmlPut( &psz_html, "</i>" );
+ break;
+ case 'u':
+ HtmlPut( &psz_html, "</u>" );
+ break;
+ case 'f':
+ HtmlPut( &psz_html, "/font>" );
+ break;
+ case 'I':
+ break;
+ }
+
+ *psz_last = '\0';
+ }
+ HtmlPut( &psz_html, psz_text_close );
+ }
}
free( psz_tag );
More information about the vlc-devel
mailing list