Commit ad10f794 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

XML: return node (element name or text value) as const

This simplifies memory handling in most cases.
parent d4546731
...@@ -70,8 +70,7 @@ struct xml_reader_t ...@@ -70,8 +70,7 @@ struct xml_reader_t
stream_t *p_stream; stream_t *p_stream;
module_t *p_module; module_t *p_module;
int (*pf_next_node) ( xml_reader_t * ); int (*pf_next_node) ( xml_reader_t *, const char ** );
char * (*pf_name) ( xml_reader_t * );
char * (*pf_value) ( xml_reader_t * ); char * (*pf_value) ( xml_reader_t * );
const char *(*pf_next_attr) ( xml_reader_t * ); const char *(*pf_next_attr) ( xml_reader_t * );
...@@ -83,14 +82,9 @@ VLC_EXPORT( xml_reader_t *, xml_ReaderCreate, (vlc_object_t *, stream_t *) LIBVL ...@@ -83,14 +82,9 @@ VLC_EXPORT( xml_reader_t *, xml_ReaderCreate, (vlc_object_t *, stream_t *) LIBVL
VLC_EXPORT( void, xml_ReaderDelete, (xml_reader_t *) ); VLC_EXPORT( void, xml_ReaderDelete, (xml_reader_t *) );
VLC_EXPORT( xml_reader_t *, xml_ReaderReset, (xml_reader_t *, stream_t *) LIBVLC_USED ); VLC_EXPORT( xml_reader_t *, xml_ReaderReset, (xml_reader_t *, stream_t *) LIBVLC_USED );
static inline int xml_ReaderNextNode( xml_reader_t *reader ) static inline int xml_ReaderNextNode( xml_reader_t *reader, const char **pval )
{ {
return reader->pf_next_node( reader ); return reader->pf_next_node( reader, pval );
}
static inline char *xml_ReaderName( xml_reader_t *reader )
{
return reader->pf_name( reader );
} }
static inline char *xml_ReaderValue( xml_reader_t *reader ) static inline char *xml_ReaderValue( xml_reader_t *reader )
......
...@@ -528,37 +528,29 @@ static int ParseImageAttachments( decoder_t *p_dec ) ...@@ -528,37 +528,29 @@ static int ParseImageAttachments( decoder_t *p_dec )
static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader ) static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader )
{ {
decoder_sys_t *p_sys = p_dec->p_sys; decoder_sys_t *p_sys = p_dec->p_sys;
char *psz_node; const char *node;
ssa_style_t *p_ssa_style = NULL; ssa_style_t *p_ssa_style = NULL;
int i_style_level = 0; int i_style_level = 0;
int i_metadata_level = 0; int i_metadata_level = 0;
int type; int type;
while( (type = xml_ReaderNextNode( p_xml_reader )) > 0 ) while( (type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
{ {
switch( type ) switch( type )
{ {
case XML_READER_ENDELEM: case XML_READER_ENDELEM:
psz_node = xml_ReaderName( p_xml_reader );
if( !psz_node )
break;
switch (i_style_level) switch (i_style_level)
{ {
case 0: case 0:
if( !strcasecmp( "metadata", psz_node ) && (i_metadata_level == 1) ) if( !strcasecmp( "metadata", node ) && (i_metadata_level == 1) )
{
i_metadata_level--; i_metadata_level--;
}
break; break;
case 1: case 1:
if( !strcasecmp( "styles", psz_node ) ) if( !strcasecmp( "styles", node ) )
{
i_style_level--; i_style_level--;
}
break; break;
case 2: case 2:
if( !strcasecmp( "style", psz_node ) ) if( !strcasecmp( "style", node ) )
{ {
TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_ssa_style ); TAB_APPEND( p_sys->i_ssa_styles, p_sys->pp_ssa_styles, p_ssa_style );
...@@ -567,20 +559,12 @@ static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader ) ...@@ -567,20 +559,12 @@ static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader )
} }
break; break;
} }
free( psz_node );
break; break;
case XML_READER_STARTELEM:
psz_node = xml_ReaderName( p_xml_reader );
if( !psz_node ) case XML_READER_STARTELEM:
break; if( !strcasecmp( "metadata", node ) && (i_style_level == 0) )
if( !strcasecmp( "metadata", psz_node ) && (i_style_level == 0) )
{
i_metadata_level++; i_metadata_level++;
} else if( !strcasecmp( "resolution", node ) &&
else if( !strcasecmp( "resolution", psz_node ) &&
( i_metadata_level == 1) ) ( i_metadata_level == 1) )
{ {
const char *attr; const char *attr;
...@@ -597,27 +581,23 @@ static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader ) ...@@ -597,27 +581,23 @@ static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader )
free( psz_value ); free( psz_value );
} }
} }
else if( !strcasecmp( "styles", psz_node ) && (i_style_level == 0) ) else if( !strcasecmp( "styles", node ) && (i_style_level == 0) )
{ {
i_style_level++; i_style_level++;
} }
else if( !strcasecmp( "style", psz_node ) && (i_style_level == 1) ) else if( !strcasecmp( "style", node ) && (i_style_level == 1) )
{ {
i_style_level++; i_style_level++;
p_ssa_style = calloc( 1, sizeof(ssa_style_t) ); p_ssa_style = calloc( 1, sizeof(ssa_style_t) );
if( !p_ssa_style ) if( unlikely(!p_ssa_style) )
{
free( psz_node );
return; return;
}
/* All styles are supposed to default to Default, and then /* All styles are supposed to default to Default, and then
* one or more settings are over-ridden. * one or more settings are over-ridden.
* At the moment this only effects styles defined AFTER * At the moment this only effects styles defined AFTER
* Default in the XML * Default in the XML
*/ */
int i; for( int i = 0; i < p_sys->i_ssa_styles; i++ )
for( i = 0; i < p_sys->i_ssa_styles; i++ )
{ {
if( !strcasecmp( p_sys->pp_ssa_styles[i]->psz_stylename, "Default" ) ) if( !strcasecmp( p_sys->pp_ssa_styles[i]->psz_stylename, "Default" ) )
{ {
...@@ -646,7 +626,7 @@ static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader ) ...@@ -646,7 +626,7 @@ static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader )
free( psz_value ); free( psz_value );
} }
} }
else if( !strcasecmp( "fontstyle", psz_node ) && (i_style_level == 2) ) else if( !strcasecmp( "fontstyle", node ) && (i_style_level == 2) )
{ {
const char *attr; const char *attr;
while( (attr = xml_ReaderNextAttr( p_xml_reader )) ) while( (attr = xml_ReaderNextAttr( p_xml_reader )) )
...@@ -749,7 +729,7 @@ static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader ) ...@@ -749,7 +729,7 @@ static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader )
free( psz_value ); free( psz_value );
} }
} }
else if( !strcasecmp( "position", psz_node ) && (i_style_level == 2) ) else if( !strcasecmp( "position", node ) && (i_style_level == 2) )
{ {
const char *attr; const char *attr;
while( (attr = xml_ReaderNextAttr( p_xml_reader )) ) while( (attr = xml_ReaderNextAttr( p_xml_reader )) )
...@@ -808,8 +788,6 @@ static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader ) ...@@ -808,8 +788,6 @@ static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader )
free( psz_value ); free( psz_value );
} }
} }
free( psz_node );
break; break;
} }
} }
...@@ -991,16 +969,13 @@ static void ParseUSFHeader( decoder_t *p_dec ) ...@@ -991,16 +969,13 @@ static void ParseUSFHeader( decoder_t *p_dec )
p_xml_reader = xml_ReaderCreate( p_dec, p_sub ); p_xml_reader = xml_ReaderCreate( p_dec, p_sub );
if( likely(p_xml_reader) ) if( likely(p_xml_reader) )
{ {
/* Look for Root Node */ const char *node;
if( xml_ReaderNextNode( p_xml_reader ) == XML_READER_STARTELEM )
{
char *psz_node = xml_ReaderName( p_xml_reader );
if( !strcasecmp( "usfsubtitles", psz_node ) ) /* Look for Root Node */
ParseUSFHeaderTags( p_dec, p_xml_reader ); if( xml_ReaderNextNode( p_xml_reader, &node ) == XML_READER_STARTELEM
&& !strcasecmp( "usfsubtitles", node ) )
ParseUSFHeaderTags( p_dec, p_xml_reader );
free( psz_node );
}
xml_ReaderDelete( p_xml_reader ); xml_ReaderDelete( p_xml_reader );
} }
stream_Delete( p_sub ); stream_Delete( p_sub );
......
...@@ -73,6 +73,7 @@ static int Demux( demux_t *p_demux ) ...@@ -73,6 +73,7 @@ static int Demux( demux_t *p_demux )
xml_reader_t *p_xml_reader = NULL; xml_reader_t *p_xml_reader = NULL;
char *psz_elname = NULL; char *psz_elname = NULL;
const char *node;
input_item_t *p_input; input_item_t *p_input;
char *psz_mrl = NULL, *psz_title = NULL, *psz_genre = NULL; char *psz_mrl = NULL, *psz_title = NULL, *psz_genre = NULL;
char *psz_now = NULL, *psz_listeners = NULL, *psz_bitrate = NULL; char *psz_now = NULL, *psz_listeners = NULL, *psz_bitrate = NULL;
...@@ -84,42 +85,38 @@ static int Demux( demux_t *p_demux ) ...@@ -84,42 +85,38 @@ static int Demux( demux_t *p_demux )
p_xml_reader = xml_ReaderCreate( p_demux, p_demux->s ); p_xml_reader = xml_ReaderCreate( p_demux, p_demux->s );
if( !p_xml_reader ) if( !p_xml_reader )
goto end; return -1;
/* xml */ /* xml */
/* check root node */ /* check root node */
if( xml_ReaderNextNode( p_xml_reader ) != XML_READER_STARTELEM ) if( xml_ReaderNextNode( p_xml_reader, &node ) != XML_READER_STARTELEM )
{ {
msg_Err( p_demux, "invalid file (no root node)" ); msg_Err( p_demux, "invalid file (no root node)" );
goto end; goto end;
} }
if( ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL || if( strcmp( node, "WinampXML" ) )
strcmp( psz_elname, "WinampXML" ) )
{ {
msg_Err( p_demux, "invalid root node: %s", psz_elname ); msg_Err( p_demux, "invalid root node: %s", node );
goto end; goto end;
} }
FREENULL( psz_elname );
/* root node should not have any attributes, and should only /* root node should not have any attributes, and should only
* contain the "playlist node */ * contain the "playlist node */
/* Skip until 1st child node */ /* Skip until 1st child node */
while( (i_ret = xml_ReaderNextNode( p_xml_reader )) != XML_READER_STARTELEM ) while( (i_ret = xml_ReaderNextNode( p_xml_reader, &node )) != XML_READER_STARTELEM )
if( i_ret <= 0 ) if( i_ret <= 0 )
{ {
msg_Err( p_demux, "invalid file (no child node)" ); msg_Err( p_demux, "invalid file (no child node)" );
goto end; goto end;
} }
if( ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL || if( strcmp( node, "playlist" ) )
strcmp( psz_elname, "playlist" ) )
{ {
msg_Err( p_demux, "invalid child node %s", psz_elname ); msg_Err( p_demux, "invalid child node %s", node );
goto end; goto end;
} }
FREENULL( psz_elname );
// Read the attributes // Read the attributes
const char *attr; const char *attr;
...@@ -144,7 +141,7 @@ static int Demux( demux_t *p_demux ) ...@@ -144,7 +141,7 @@ static int Demux( demux_t *p_demux )
p_subitems = input_item_node_Create( p_current_input ); p_subitems = input_item_node_Create( p_current_input );
while( (i_ret = xml_ReaderNextNode( p_xml_reader )) > 0 ) while( (i_ret = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
{ {
// Get the node type // Get the node type
switch( i_ret ) switch( i_ret )
...@@ -153,8 +150,8 @@ static int Demux( demux_t *p_demux ) ...@@ -153,8 +150,8 @@ static int Demux( demux_t *p_demux )
{ {
// Read the element name // Read the element name
free( psz_elname ); free( psz_elname );
psz_elname = xml_ReaderName( p_xml_reader ); psz_elname = strdup( node );
if( !psz_elname ) if( unlikely(!psz_elname) )
goto end; goto end;
// Read the attributes // Read the attributes
...@@ -180,55 +177,41 @@ static int Demux( demux_t *p_demux ) ...@@ -180,55 +177,41 @@ static int Demux( demux_t *p_demux )
} }
break; break;
} }
case XML_READER_TEXT: case XML_READER_TEXT:
{ {
char *psz_text = xml_ReaderValue( p_xml_reader ); char **p;
if( IsWhitespace( psz_text ) )
{ if( psz_elname == NULL )
free( psz_text ); break;
if( IsWhitespace( node ) )
break; break;
}
if( !strcmp( psz_elname, "Name" ) ) if( !strcmp( psz_elname, "Name" ) )
{ p = &psz_title;
psz_title = psz_text;
}
else if( !strcmp( psz_elname, "Genre" ) ) else if( !strcmp( psz_elname, "Genre" ) )
{ p = &psz_genre;
psz_genre = psz_text;
}
else if( !strcmp( psz_elname, "Nowplaying" ) ) else if( !strcmp( psz_elname, "Nowplaying" ) )
{ p = &psz_now;
psz_now = psz_text;
}
else if( !strcmp( psz_elname, "Listeners" ) ) else if( !strcmp( psz_elname, "Listeners" ) )
{ p = &psz_listeners;
psz_listeners = psz_text;
}
else if( !strcmp( psz_elname, "Bitrate" ) ) else if( !strcmp( psz_elname, "Bitrate" ) )
{ p = &psz_bitrate;
psz_bitrate = psz_text;
}
else if( !strcmp( psz_elname, "" ) )
{
free( psz_text );
}
else else
{ {
msg_Warn( p_demux, "unexpected text in element '%s'", msg_Warn( p_demux, "unexpected text in element <%s>",
psz_elname ); psz_elname );
free( psz_text ); break;
} }
free( *p );
*p = strdup( node );
break; break;
} }
// End element // End element
case XML_READER_ENDELEM: case XML_READER_ENDELEM:
{ {
// Read the element name // Read the element name
free( psz_elname ); if( !strcmp( node, "entry" ) )
psz_elname = xml_ReaderName( p_xml_reader );
if( !psz_elname )
goto end;
if( !strcmp( psz_elname, "entry" ) )
{ {
p_input = input_item_New( p_demux, psz_mrl, psz_title ); p_input = input_item_New( p_demux, psz_mrl, psz_title );
if( psz_now ) if( psz_now )
...@@ -249,9 +232,7 @@ static int Demux( demux_t *p_demux ) ...@@ -249,9 +232,7 @@ static int Demux( demux_t *p_demux )
FREENULL( psz_listeners ); FREENULL( psz_listeners );
FREENULL( psz_now ); FREENULL( psz_now );
} }
free( psz_elname ); FREENULL( psz_elname );
psz_elname = strdup( "" );
break; break;
} }
} }
......
...@@ -68,7 +68,7 @@ void Close_iTML( vlc_object_t *p_this ) ...@@ -68,7 +68,7 @@ void Close_iTML( vlc_object_t *p_this )
int Demux( demux_t *p_demux ) int Demux( demux_t *p_demux )
{ {
xml_reader_t *p_xml_reader; xml_reader_t *p_xml_reader;
char *psz_name = NULL; const char *node;
input_item_t *p_current_input = GetCurrentItem(p_demux); input_item_t *p_current_input = GetCurrentItem(p_demux);
p_demux->p_sys->i_ntracks = 0; p_demux->p_sys->i_ntracks = 0;
...@@ -82,19 +82,19 @@ int Demux( demux_t *p_demux ) ...@@ -82,19 +82,19 @@ int Demux( demux_t *p_demux )
int type; int type;
do do
{ {
type = xml_ReaderNextNode( p_xml_reader ); type = xml_ReaderNextNode( p_xml_reader, &node );
if( type <= 0 ) if( type <= 0 )
{ {
msg_Err( p_demux, "can't read xml stream" ); msg_Err( p_demux, "can't read xml stream" );
goto end; goto end;
} }
} while( type != XML_READER_STARTELEM ); }
while( type != XML_READER_STARTELEM );
/* checking root node name */ /* checking root node name */
psz_name = xml_ReaderName( p_xml_reader ); if( strcmp( node, "plist" ) )
if( !psz_name || strcmp( psz_name, "plist" ) )
{ {
msg_Err( p_demux, "invalid root node name: %s", psz_name ); msg_Err( p_demux, "invalid root node <%s>", node );
goto end; goto end;
} }
...@@ -108,7 +108,6 @@ int Demux( demux_t *p_demux ) ...@@ -108,7 +108,6 @@ int Demux( demux_t *p_demux )
vlc_gc_decref(p_current_input); vlc_gc_decref(p_current_input);
end: end:
free( psz_name );
if( p_xml_reader ) if( p_xml_reader )
xml_ReaderDelete( p_xml_reader ); xml_ReaderDelete( p_xml_reader );
...@@ -177,34 +176,32 @@ static bool parse_dict( demux_t *p_demux, input_item_node_t *p_input_node, ...@@ -177,34 +176,32 @@ static bool parse_dict( demux_t *p_demux, input_item_node_t *p_input_node,
const char *psz_element, xml_elem_hnd_t *p_handlers ) const char *psz_element, xml_elem_hnd_t *p_handlers )
{ {
int i_node; int i_node;
char *psz_name = NULL; const char *node;
char *psz_value = NULL; char *psz_value = NULL;
char *psz_key = NULL; char *psz_key = NULL;
xml_elem_hnd_t *p_handler = NULL; xml_elem_hnd_t *p_handler = NULL;
bool b_ret = false; bool b_ret = false;
while( (i_node = xml_ReaderNextNode( p_xml_reader )) > 0 ) while( (i_node = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
{ {
switch( i_node ) switch( i_node )
{ {
/* element start tag */
case XML_READER_STARTELEM: case XML_READER_STARTELEM:
/* element start tag */ if( !*node )
psz_name = xml_ReaderName( p_xml_reader );
if( !psz_name || !*psz_name )
{ {
msg_Err( p_demux, "invalid xml stream" ); msg_Err( p_demux, "invalid XML stream" );
goto end; goto end;
} }
/* choose handler */ /* choose handler */
for( p_handler = p_handlers; for( p_handler = p_handlers;
p_handler->name && strcmp( psz_name, p_handler->name ); p_handler->name && strcmp( node, p_handler->name );
p_handler++ ); p_handler++ );
if( !p_handler->name ) if( !p_handler->name )
{ {
msg_Err( p_demux, "unexpected element <%s>", psz_name ); msg_Err( p_demux, "unexpected element <%s>", node );
goto end; goto end;
} }
FREE_NAME();
/* complex content is parsed in a separate function */ /* complex content is parsed in a separate function */
if( p_handler->type == COMPLEX_CONTENT ) if( p_handler->type == COMPLEX_CONTENT )
{ {
...@@ -220,37 +217,28 @@ static bool parse_dict( demux_t *p_demux, input_item_node_t *p_input_node, ...@@ -220,37 +217,28 @@ static bool parse_dict( demux_t *p_demux, input_item_node_t *p_input_node,
} }
break; break;
/* simple element content */
case XML_READER_TEXT: case XML_READER_TEXT:
/* simple element content */
free( psz_value ); free( psz_value );
psz_value = xml_ReaderValue( p_xml_reader ); psz_value = strdup( node );
if( !psz_value ) if( unlikely(!psz_value) )
{
msg_Err( p_demux, "invalid xml stream" );
goto end; goto end;
}
break; break;
/* element end tag */
case XML_READER_ENDELEM: case XML_READER_ENDELEM:
/* element end tag */
psz_name = xml_ReaderName( p_xml_reader );
if( !psz_name )
{
msg_Err( p_demux, "invalid xml stream" );
goto end;
}
/* leave if the current parent node <track> is terminated */ /* leave if the current parent node <track> is terminated */
if( !strcmp( psz_name, psz_element ) ) if( !strcmp( node, psz_element ) )
{ {
b_ret = true; b_ret = true;
goto end; goto end;
} }
/* there MUST have been a start tag for that element name */ /* there MUST have been a start tag for that element name */
if( !p_handler || !p_handler->name if( !p_handler || !p_handler->name
|| strcmp( p_handler->name, psz_name ) ) || strcmp( p_handler->name, node ) )
{ {
msg_Err( p_demux, "there's no open element left for <%s>", msg_Err( p_demux, "there's no open element left for <%s>",
psz_name ); node );
goto end; goto end;
} }
/* special case: key */ /* special case: key */
...@@ -268,12 +256,10 @@ static bool parse_dict( demux_t *p_demux, input_item_node_t *p_input_node, ...@@ -268,12 +256,10 @@ static bool parse_dict( demux_t *p_demux, input_item_node_t *p_input_node,
p_handler = NULL; p_handler = NULL;
break; break;
} }
FREE_NAME();
} }
msg_Err( p_demux, "unexpected end of XML data" ); msg_Err( p_demux, "unexpected end of XML data" );
end: end:
free( psz_name );
free( psz_value ); free( psz_value );
free( psz_key ); free( psz_key );
return b_ret; return b_ret;
...@@ -461,24 +447,11 @@ static bool skip_element( demux_t *p_demux, input_item_node_t *p_input_node, ...@@ -461,24 +447,11 @@ static bool skip_element( demux_t *p_demux, input_item_node_t *p_input_node,
{ {
VLC_UNUSED(p_demux); VLC_UNUSED(p_input_node); VLC_UNUSED(p_demux); VLC_UNUSED(p_input_node);
VLC_UNUSED(p_track); VLC_UNUSED(p_handlers); VLC_UNUSED(p_track); VLC_UNUSED(p_handlers);
char *psz_endname; const char *node;
int type; int type;
while( (type = xml_ReaderNextNode( p_xml_reader )) > 0 ) while( (type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
{ if( type == XML_READER_ENDELEM && !strcmp( psz_element, node ) )
if( type == XML_READER_ENDELEM ) return true;
{
psz_endname = xml_ReaderName( p_xml_reader );
if( !psz_endname )
return false;
if( !strcmp( psz_element, psz_endname ) )
{
free( psz_endname );
return true;
}
else
free( psz_endname );
}
}
return false; return false;
} }
...@@ -26,11 +26,10 @@ ...@@ -26,11 +26,10 @@
*/ */
/* defines */ /* defines */
#define FREE_NAME() FREENULL( psz_name )
#define FREE_VALUE() FREENULL( psz_value ) #define FREE_VALUE() FREENULL( psz_value )
#define FREE_KEY() FREENULL( psz_key ) #define FREE_KEY() FREENULL( psz_key )
#define FREE_ATT() do{ FREE_NAME();FREE_VALUE(); }while(0) #define FREE_ATT() FREE_VALUE()
#define FREE_ATT_KEY() do{ FREE_NAME();FREE_VALUE();FREE_KEY();} while(0) #define FREE_ATT_KEY() do{ FREE_VALUE();FREE_KEY();} while(0)
#define UNKNOWN_CONTENT 0 #define UNKNOWN_CONTENT 0
#define SIMPLE_CONTENT 1 #define SIMPLE_CONTENT 1
......
This diff is collapsed.
...@@ -103,7 +103,7 @@ void Close_QTL( vlc_object_t *p_this ) ...@@ -103,7 +103,7 @@ void Close_QTL( vlc_object_t *p_this )
static int Demux( demux_t *p_demux ) static int Demux( demux_t *p_demux )
{ {
xml_reader_t *p_xml_reader; xml_reader_t *p_xml_reader;
char *psz_eltname = NULL; const char *node;
input_item_t *p_input; input_item_t *p_input;
int i_ret = -1; int i_ret = -1;
...@@ -130,26 +130,20 @@ static int Demux( demux_t *p_demux ) ...@@ -130,26 +130,20 @@ static int Demux( demux_t *p_demux )
goto error; goto error;
/* check root node */ /* check root node */
if( xml_ReaderNextNode( p_xml_reader ) != XML_READER_STARTELEM || if( xml_ReaderNextNode( p_xml_reader, &node ) != XML_READER_STARTELEM
( psz_eltname = xml_ReaderName( p_xml_reader ) ) == NULL || || strcmp( node, "embed" ) )
strcmp( psz_eltname, "embed" ) )
{ {
msg_Err( p_demux, "invalid root node: %s", psz_eltname ); msg_Err( p_demux, "invalid root node <%s>", node );
free( psz_eltname );
psz_eltname = NULL;
/* second line has <?quicktime tag ... so we try to skip it */ /* second line has <?quicktime tag ... so we try to skip it */
msg_Dbg( p_demux, "trying to read one more node" ); msg_Dbg( p_demux, "trying to read one more node" );
if( xml_ReaderNextNode( p_xml_reader ) != XML_READER_STARTELEM || if( xml_ReaderNextNode( p_xml_reader, &node ) != XML_READER_STARTELEM
( psz_eltname = xml_ReaderName( p_xml_reader ) ) == NULL || || strcmp( node, "embed" ) )
strcmp( psz_eltname, "embed" ) )
{ {
msg_Err( p_demux, "invalid root node: %s", psz_eltname ); msg_Err( p_demux, "invalid root node <%s>", node );
free( psz_eltname );
goto error; goto error;
} }
} }
free( psz_eltname );
const char *attrname; const char *attrname;
while( (attrname = xml_ReaderNextAttr( p_xml_reader )) != NULL ) while( (attrname = xml_ReaderNextAttr( p_xml_reader )) != NULL )
......
...@@ -80,7 +80,7 @@ void Close_Shoutcast( vlc_object_t *p_this ) ...@@ -80,7 +80,7 @@ void Close_Shoutcast( vlc_object_t *p_this )
static int Demux( demux_t *p_demux ) static int Demux( demux_t *p_demux )
{ {
xml_reader_t *p_xml_reader = NULL; xml_reader_t *p_xml_reader = NULL;
char *psz_eltname = NULL; const char *node;
int i_ret = -1; int i_ret = -1;
input_item_t *p_current_input = GetCurrentItem(p_demux); input_item_t *p_current_input = GetCurrentItem(p_demux);
input_item_node_t *p_input_node = NULL; input_item_node_t *p_input_node = NULL;
...@@ -90,23 +90,21 @@ static int Demux( demux_t *p_demux ) ...@@ -90,23 +90,21 @@ static int Demux( demux_t *p_demux )
goto error; goto error;
/* check root node */ /* check root node */
if( xml_ReaderNextNode( p_xml_reader ) != XML_READER_STARTELEM ) if( xml_ReaderNextNode( p_xml_reader, &node ) != XML_READER_STARTELEM )
{ {
msg_Err( p_demux, "invalid file (no root node)" ); msg_Err( p_demux, "invalid file (no root node)" );
goto error; goto error;
} }
if( ( psz_eltname = xml_ReaderName( p_xml_reader ) ) == NULL || if( strcmp( node, "genrelist" ) && strcmp( node, "stationlist" ) )
( strcmp( psz_eltname, "genrelist" )
&& strcmp( psz_eltname, "stationlist" ) ) )
{ {
msg_Err( p_demux, "invalid root node: %s", psz_eltname ); msg_Err( p_demux, "invalid root node <%s>", node );
goto error; goto error;
} }
p_input_node = input_item_node_Create( p_current_input ); p_input_node = input_item_node_Create( p_current_input );
if( !strcmp( psz_eltname, "genrelist" ) ) if( !strcmp( node, "genrelist" ) )
{ {
/* we're reading a genre list */ /* we're reading a genre list */
if( DemuxGenre( p_demux, p_xml_reader, p_input_node ) ) if( DemuxGenre( p_demux, p_xml_reader, p_input_node ) )
...@@ -128,7 +126,6 @@ static int Demux( demux_t *p_demux ) ...@@ -128,7 +126,6 @@ static int Demux( demux_t *p_demux )
error: error:
if( p_xml_reader ) if( p_xml_reader )
xml_ReaderDelete( p_xml_reader ); xml_ReaderDelete( p_xml_reader );
free( psz_eltname );
if( p_input_node ) input_item_node_Delete( p_input_node ); if( p_input_node ) input_item_node_Delete( p_input_node );
vlc_gc_decref(p_current_input); vlc_gc_decref(p_current_input);
return i_ret; return i_ret;
...@@ -148,21 +145,17 @@ error: ...@@ -148,21 +145,17 @@ error:
static int DemuxGenre( demux_t *p_demux, xml_reader_t *p_xml_reader, static int DemuxGenre( demux_t *p_demux, xml_reader_t *p_xml_reader,
input_item_node_t *p_input_node ) input_item_node_t *p_input_node )
{ {
const char *node;
char *psz_name = NULL; /* genre name */ char *psz_name = NULL; /* genre name */
int type; int type;
while( (type = xml_ReaderNextNode( p_xml_reader )) > 0 ) while( (type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
{ {
switch( type ) switch( type )
{ {
case XML_READER_STARTELEM: case XML_READER_STARTELEM:
{ {
// Read the element name if( !strcmp( node, "genre" ) )
char *psz_eltname = xml_ReaderName( p_xml_reader );
if( !psz_eltname )
goto error;
if( !strcmp( psz_eltname, "genre" ) )
{ {
// Read the attributes // Read the attributes
const char *attrname; const char *attrname;
...@@ -180,31 +173,21 @@ static int DemuxGenre( demux_t *p_demux, xml_reader_t *p_xml_reader, ...@@ -180,31 +173,21 @@ static int DemuxGenre( demux_t *p_demux, xml_reader_t *p_xml_reader,
{ {
msg_Warn( p_demux, msg_Warn( p_demux,
"unexpected attribute %s in element %s", "unexpected attribute %s in element %s",
attrname, psz_eltname ); attrname, node );
free( psz_attrvalue ); free( psz_attrvalue );
} }
} }
} }
free( psz_eltname );
break; break;
} }
case XML_READER_TEXT:
break;
// End element
case XML_READER_ENDELEM: case XML_READER_ENDELEM:
{ if( !strcmp( node, "genre" ) && psz_name != NULL )
// Read the element name
char *psz_eltname = xml_ReaderName( p_xml_reader );
if( !psz_eltname )
goto error;
if( !strcmp( psz_eltname, "genre" ) )
{ {
char* psz_mrl; char* psz_mrl;
if( asprintf( &psz_mrl, SHOUTCAST_BASE_URL "?genre=%s", if( asprintf( &psz_mrl, SHOUTCAST_BASE_URL "?genre=%s",
psz_name ) != -1 ) psz_name ) != -1 )
{ {
input_item_t *p_input; input_item_t *p_input;
p_input = input_item_New( p_demux, psz_mrl, psz_name ); p_input = input_item_New( p_demux, psz_mrl, psz_name );
...@@ -215,13 +198,10 @@ static int DemuxGenre( demux_t *p_demux, xml_reader_t *p_xml_reader, ...@@ -215,13 +198,10 @@ static int DemuxGenre( demux_t *p_demux, xml_reader_t *p_xml_reader,
} }
FREENULL( psz_name ); FREENULL( psz_name );
} }
free( psz_eltname );
break; break;
}
} }
} }
error:
free( psz_name ); free( psz_name );
return 0; return 0;
} }
...@@ -268,20 +248,16 @@ static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader, ...@@ -268,20 +248,16 @@ static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader,
char *psz_rt = NULL; /* rating for shoutcast TV */ char *psz_rt = NULL; /* rating for shoutcast TV */
char *psz_load = NULL; /* load for shoutcast TV */ char *psz_load = NULL; /* load for shoutcast TV */
char *psz_eltname = NULL; /* tag name */ const char *node; /* tag name */
int i_type; int i_type;
while( (i_type = xml_ReaderNextNode( p_xml_reader )) > 0 ) while( (i_type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
{ {
switch( i_type ) switch( i_type )
{ {
case XML_READER_STARTELEM: case XML_READER_STARTELEM:
// Read the element name
psz_eltname = xml_ReaderName( p_xml_reader );
if( !psz_eltname ) return -1;
// Read the attributes // Read the attributes
if( !strcmp( psz_eltname, "tunein" ) ) if( !strcmp( node, "tunein" ) )
{ {
const char *attrname; const char *attrname;
while( (attrname = xml_ReaderNextAttr( p_xml_reader )) ) while( (attrname = xml_ReaderNextAttr( p_xml_reader )) )
...@@ -289,7 +265,6 @@ static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader, ...@@ -289,7 +265,6 @@ static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader,
char *psz_attrvalue = xml_ReaderValue( p_xml_reader ); char *psz_attrvalue = xml_ReaderValue( p_xml_reader );
if( !psz_attrvalue ) if( !psz_attrvalue )
{ {
free( psz_eltname );
free( psz_attrvalue ); free( psz_attrvalue );
return -1; return -1;
} }
...@@ -299,12 +274,12 @@ static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader, ...@@ -299,12 +274,12 @@ static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader,
{ {
msg_Warn( p_demux, msg_Warn( p_demux,
"unexpected attribute %s in element %s", "unexpected attribute %s in element %s",
attrname, psz_eltname ); attrname, node );
free( psz_attrvalue ); free( psz_attrvalue );
} }
} }
} }
else if( !strcmp( psz_eltname, "station" ) ) else if( !strcmp( node, "station" ) )
{ {
const char *attrname; const char *attrname;
while( (attrname = xml_ReaderNextAttr( p_xml_reader )) ) while( (attrname = xml_ReaderNextAttr( p_xml_reader )) )
...@@ -312,7 +287,6 @@ static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader, ...@@ -312,7 +287,6 @@ static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader,
char *psz_attrvalue = xml_ReaderValue( p_xml_reader ); char *psz_attrvalue = xml_ReaderValue( p_xml_reader );
if( !psz_attrvalue ) if( !psz_attrvalue )
{ {
free( psz_eltname );
free( psz_attrvalue ); free( psz_attrvalue );
return -1; return -1;
} }
...@@ -330,23 +304,16 @@ static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader, ...@@ -330,23 +304,16 @@ static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader,
{ {
msg_Warn( p_demux, msg_Warn( p_demux,
"unexpected attribute %s in element %s", "unexpected attribute %s in element %s",
attrname, psz_eltname ); attrname, node );
free( psz_attrvalue ); free( psz_attrvalue );
} }
} }
} }
free( psz_eltname );
break;
case XML_READER_TEXT:
break; break;
// End element // End element
case XML_READER_ENDELEM: case XML_READER_ENDELEM:
// Read the element name if( !strcmp( node, "station" ) &&
psz_eltname = xml_ReaderName( p_xml_reader );
if( !psz_eltname ) return -1;
if( !strcmp( psz_eltname, "station" ) &&
( psz_base || ( psz_rt && psz_load && ( psz_base || ( psz_rt && psz_load &&
( b_adult || strcmp( psz_rt, "NC17" ) ) ) ) ) ( b_adult || strcmp( psz_rt, "NC17" ) ) ) ) )
{ {
...@@ -399,10 +366,10 @@ static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader, ...@@ -399,10 +366,10 @@ static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader,
FREENULL( psz_rt ); FREENULL( psz_rt );
FREENULL( psz_load ); FREENULL( psz_load );
} }
free( psz_eltname );
break; break;
} }
} }
/* FIXME: leaks on missing ENDELEMENT? */
return 0; return 0;
} }
......
This diff is collapsed.
...@@ -132,6 +132,7 @@ void XMLParser::LoadCatalog() ...@@ -132,6 +132,7 @@ void XMLParser::LoadCatalog()
bool XMLParser::parse() bool XMLParser::parse()
{ {
const char *node;
int type; int type;
int ret; int ret;
...@@ -139,7 +140,7 @@ bool XMLParser::parse() ...@@ -139,7 +140,7 @@ bool XMLParser::parse()
m_errors = false; m_errors = false;
while( (ret = xml_ReaderNextNode( m_pReader )) > 0 ) while( (ret = xml_ReaderNextNode( m_pReader, &node )) > 0 )
{ {
if( m_errors ) return false; if( m_errors ) return false;
...@@ -147,10 +148,6 @@ bool XMLParser::parse() ...@@ -147,10 +148,6 @@ bool XMLParser::parse()
{ {
case XML_READER_STARTELEM: case XML_READER_STARTELEM:
{ {
// Read the element name
char *eltName = xml_ReaderName( m_pReader );
if( !eltName ) return false;
// Read the attributes // Read the attributes
AttrList_t attributes; AttrList_t attributes;
const char *name; const char *name;
...@@ -165,8 +162,7 @@ bool XMLParser::parse() ...@@ -165,8 +162,7 @@ bool XMLParser::parse()
attributes[strdup(name)] = value; attributes[strdup(name)] = value;
} }
handleBeginElement( eltName, attributes ); handleBeginElement( node, attributes );
free( eltName );
map<const char*, const char*, ltstr> ::iterator it = map<const char*, const char*, ltstr> ::iterator it =
attributes.begin(); attributes.begin();
...@@ -182,12 +178,7 @@ bool XMLParser::parse() ...@@ -182,12 +178,7 @@ bool XMLParser::parse()
// End element // End element
case XML_READER_ENDELEM: case XML_READER_ENDELEM:
{ {
// Read the element name handleEndElement( node );
char *eltName = xml_ReaderName( m_pReader );
if( !eltName ) return false;
handleEndElement( eltName );
free( eltName );
break; break;
} }
} }
......
...@@ -2160,11 +2160,11 @@ static int RenderHtml( filter_t *p_filter, subpicture_region_t *p_region_out, ...@@ -2160,11 +2160,11 @@ static int RenderHtml( filter_t *p_filter, subpicture_region_t *p_region_out,
if( p_xml_reader ) if( p_xml_reader )
{ {
/* Look for Root Node */ /* Look for Root Node */
if( xml_ReaderNextNode( p_xml_reader ) == XML_READER_STARTELEM ) const char *node;
{
char *psz_node = xml_ReaderName( p_xml_reader );
if( !strcasecmp( "karaoke", psz_node ) ) if( xml_ReaderNextNode( p_xml_reader, &node ) == XML_READER_STARTELEM )
{
if( !strcasecmp( "karaoke", node ) )
{ {
/* We're going to have to render the text a number /* We're going to have to render the text a number
* of times to show the progress marker on the text. * of times to show the progress marker on the text.
...@@ -2172,20 +2172,19 @@ static int RenderHtml( filter_t *p_filter, subpicture_region_t *p_region_out, ...@@ -2172,20 +2172,19 @@ static int RenderHtml( filter_t *p_filter, subpicture_region_t *p_region_out,
var_SetBool( p_filter, "text-rerender", true ); var_SetBool( p_filter, "text-rerender", true );
b_karaoke = true; b_karaoke = true;
} }
else if( !strcasecmp( "text", psz_node ) ) else if( !strcasecmp( "text", node ) )
{ {
b_karaoke = false; b_karaoke = false;
} }
else else
{ {
/* Only text and karaoke tags are supported */ /* Only text and karaoke tags are supported */
msg_Dbg( p_filter, "Unsupported top-level tag '%s' ignored.", psz_node ); msg_Dbg( p_filter, "Unsupported top-level tag <%s> ignored.",
node );
p_filter->p_sys->p_xml = xml_ReaderReset( p_xml_reader, NULL ); p_filter->p_sys->p_xml = xml_ReaderReset( p_xml_reader, NULL );
p_xml_reader = NULL; p_xml_reader = NULL;
rv = VLC_EGENERIC; rv = VLC_EGENERIC;
} }
free( psz_node );
} }
} }
......
...@@ -84,13 +84,11 @@ static int vlclua_xml_create( lua_State *L ) ...@@ -84,13 +84,11 @@ static int vlclua_xml_create( lua_State *L )
* XML Reader * XML Reader
*****************************************************************************/ *****************************************************************************/
static int vlclua_xml_reader_next_node( lua_State * ); static int vlclua_xml_reader_next_node( lua_State * );
static int vlclua_xml_reader_name( lua_State * );
static int vlclua_xml_reader_value( lua_State * ); static int vlclua_xml_reader_value( lua_State * );
static int vlclua_xml_reader_next_attr( lua_State * ); static int vlclua_xml_reader_next_attr( lua_State * );
static const luaL_Reg vlclua_xml_reader_reg[] = { static const luaL_Reg vlclua_xml_reader_reg[] = {
{ "next_node", vlclua_xml_reader_next_node }, { "next_node", vlclua_xml_reader_next_node },
{ "name", vlclua_xml_reader_name },
{ "value", vlclua_xml_reader_value }, { "value", vlclua_xml_reader_value },
{ "next_attr", vlclua_xml_reader_next_attr }, { "next_attr", vlclua_xml_reader_next_attr },
{ NULL, NULL } { NULL, NULL }
...@@ -131,14 +129,7 @@ static int vlclua_xml_create_reader( lua_State *L ) ...@@ -131,14 +129,7 @@ static int vlclua_xml_create_reader( lua_State *L )
static int vlclua_xml_reader_next_node( lua_State *L ) static int vlclua_xml_reader_next_node( lua_State *L )
{ {
xml_reader_t *p_reader = *(xml_reader_t**)luaL_checkudata( L, 1, "xml_reader" ); xml_reader_t *p_reader = *(xml_reader_t**)luaL_checkudata( L, 1, "xml_reader" );
lua_pushinteger( L, xml_ReaderNextNode( p_reader ) ); lua_pushinteger( L, xml_ReaderNextNode( p_reader, NULL ) );
return 1;
}
static int vlclua_xml_reader_name( lua_State *L )
{
xml_reader_t *p_reader = *(xml_reader_t**)luaL_checkudata( L, 1, "xml_reader" );
lua_pushstring( L, xml_ReaderName( p_reader ) );
return 1; return 1;
} }
......
...@@ -558,83 +558,40 @@ static int ProcessNodes( filter_t *p_filter, ...@@ -558,83 +558,40 @@ static int ProcessNodes( filter_t *p_filter,
if( rv != VLC_SUCCESS ) if( rv != VLC_SUCCESS )
return rv; return rv;
const char *node;
int type; int type;
while ( (type = xml_ReaderNextNode( p_xml_reader )) > 0 ) while ( (type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
{ {
switch ( type ) switch ( type )
{ {
case XML_READER_ENDELEM: case XML_READER_ENDELEM:
psz_node = xml_ReaderName( p_xml_reader ); if( !strcasecmp( "font", node ) )
if( psz_node ) PopFont( &p_fonts );
{ else if( !strcasecmp( "b", node ) )
if( !strcasecmp( "font", psz_node ) ) b_bold = false;
PopFont( &p_fonts ); else if( !strcasecmp( "i", node ) )
else if( !strcasecmp( "b", psz_node ) ) b_italic = false;
b_bold = false; else if( !strcasecmp( "u", node ) )
else if( !strcasecmp( "i", psz_node ) ) b_uline = false;
b_italic = false; else if( !strcasecmp( "s", node ) )
else if( !strcasecmp( "u", psz_node ) ) b_through = false;
b_uline = false;
else if( !strcasecmp( "s", psz_node ) )
b_through = false;
free( psz_node );
}
break; break;
case XML_READER_STARTELEM:
psz_node = xml_ReaderName( p_xml_reader );
if( psz_node )
{
if( !strcasecmp( "font", psz_node ) )
rv = HandleFontAttributes( p_xml_reader, &p_fonts, i_scale );
else if( !strcasecmp( "b", psz_node ) )
b_bold = true;
else if( !strcasecmp( "i", psz_node ) )
b_italic = true;
else if( !strcasecmp( "u", psz_node ) )
b_uline = true;
else if( !strcasecmp( "s", psz_node ) )
b_through = true;
else if( !strcasecmp( "br", psz_node ) )
{
SetupLine( p_filter, "\n", &psz_text,
pi_runs, ppi_run_lengths, ppp_styles,
GetStyleFromFontStack( p_sys,
&p_fonts,
b_bold,
b_italic,
b_uline,
b_through) );
}
else if( !strcasecmp( "k", psz_node ) )
{
/* Only valid in karaoke */
if( b_karaoke )
{
if( *pi_k_runs > 0 )
{
SetKaraokeLen( *pi_runs, *ppi_run_lengths,
*pi_k_runs, *ppi_k_run_lengths );
}
SetupKaraoke( p_xml_reader, pi_k_runs,
ppi_k_run_lengths, ppi_k_durations );
}
}
free( psz_node ); case XML_READER_STARTELEM:
} if( !strcasecmp( "font", node ) )
break; rv = HandleFontAttributes( p_xml_reader, &p_fonts, i_scale );
case XML_READER_TEXT: else if( !strcasecmp( "b", node ) )
psz_node = xml_ReaderValue( p_xml_reader ); b_bold = true;
if( psz_node ) else if( !strcasecmp( "i", node ) )
b_italic = true;
else if( !strcasecmp( "u", node ) )
b_uline = true;
else if( !strcasecmp( "s", node ) )
b_through = true;
else if( !strcasecmp( "br", node ) )
{ {
/* */ SetupLine( p_filter, "\n", &psz_text,
HandleWhiteSpace( psz_node );
resolve_xml_special_chars( psz_node );
SetupLine( p_filter, psz_node, &psz_text,
pi_runs, ppi_run_lengths, ppp_styles, pi_runs, ppi_run_lengths, ppp_styles,
GetStyleFromFontStack( p_sys, GetStyleFromFontStack( p_sys,
&p_fonts, &p_fonts,
...@@ -642,9 +599,41 @@ static int ProcessNodes( filter_t *p_filter, ...@@ -642,9 +599,41 @@ static int ProcessNodes( filter_t *p_filter,
b_italic, b_italic,
b_uline, b_uline,
b_through) ); b_through) );
free( psz_node ); }
else if( !strcasecmp( "k", node ) )
{
/* Only valid in karaoke */
if( b_karaoke )
{
if( *pi_k_runs > 0 )
SetKaraokeLen( *pi_runs, *ppi_run_lengths,
*pi_k_runs, *ppi_k_run_lengths );
SetupKaraoke( p_xml_reader, pi_k_runs,
ppi_k_run_lengths, ppi_k_durations );
}
} }
break; break;
case XML_READER_TEXT:
{
char *psz_node = strdup( node );
if( unlikely(!psz_node) )
break;
HandleWhiteSpace( psz_node );
resolve_xml_special_chars( psz_node );
SetupLine( p_filter, psz_node, &psz_text,
pi_runs, ppi_run_lengths, ppp_styles,
GetStyleFromFontStack( p_sys,
&p_fonts,
b_bold,
b_italic,
b_uline,
b_through) );
free( psz_node );
break;
}
} }
if( rv != VLC_SUCCESS ) if( rv != VLC_SUCCESS )
{ {
......
...@@ -60,8 +60,7 @@ vlc_module_begin () ...@@ -60,8 +60,7 @@ vlc_module_begin ()
vlc_module_end () vlc_module_end ()
static int ReaderNextNode( xml_reader_t * ); static int ReaderNextNode( xml_reader_t *, const char ** );
static char *ReaderName( xml_reader_t * );
static char *ReaderValue( xml_reader_t * ); static char *ReaderValue( xml_reader_t * );
static const char *ReaderNextAttr( xml_reader_t * ); static const char *ReaderNextAttr( xml_reader_t * );
...@@ -138,9 +137,16 @@ static void ReaderErrorHandler( void *p_arg, const char *p_msg, ...@@ -138,9 +137,16 @@ static void ReaderErrorHandler( void *p_arg, const char *p_msg,
msg_Err( p_reader, "XML parser error (line %d) : %s", line, p_msg ); msg_Err( p_reader, "XML parser error (line %d) : %s", line, p_msg );
} }
struct xml_reader_sys_t
{
xmlTextReaderPtr xml;
char *node;
};
static int ReaderOpen( vlc_object_t *p_this ) static int ReaderOpen( vlc_object_t *p_this )
{ {
xml_reader_t *p_reader = (xml_reader_t *)p_this; xml_reader_t *p_reader = (xml_reader_t *)p_this;
xml_reader_sys_t *p_sys = malloc( sizeof( *p_sys ) );
xmlTextReaderPtr p_libxml_reader; xmlTextReaderPtr p_libxml_reader;
if( !xmlHasFeature( XML_WITH_THREAD ) ) if( !xmlHasFeature( XML_WITH_THREAD ) )
...@@ -150,22 +156,25 @@ static int ReaderOpen( vlc_object_t *p_this ) ...@@ -150,22 +156,25 @@ static int ReaderOpen( vlc_object_t *p_this )
xmlInitParser(); xmlInitParser();
vlc_mutex_unlock( &lock ); vlc_mutex_unlock( &lock );
if( unlikely(!p_sys) )
return VLC_ENOMEM;
p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_reader->p_stream, p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_reader->p_stream,
NULL, NULL, 0 ); NULL, NULL, 0 );
if( !p_libxml_reader ) if( !p_libxml_reader )
{ {
msg_Err( p_this, "failed to create XML parser" ); free( p_sys );
return VLC_ENOMEM; return VLC_ENOMEM;
} }
p_reader->p_sys = (void *)p_libxml_reader;
/* Set the error handler */ /* Set the error handler */
xmlTextReaderSetErrorHandler( p_libxml_reader, xmlTextReaderSetErrorHandler( p_libxml_reader,
ReaderErrorHandler, p_reader ); ReaderErrorHandler, p_reader );
p_sys->xml = p_libxml_reader;
p_sys->node = NULL;
p_reader->p_sys = p_sys;
p_reader->pf_next_node = ReaderNextNode; p_reader->pf_next_node = ReaderNextNode;
p_reader->pf_name = ReaderName;
p_reader->pf_value = ReaderValue; p_reader->pf_value = ReaderValue;
p_reader->pf_next_attr = ReaderNextAttr; p_reader->pf_next_attr = ReaderNextAttr;
p_reader->pf_use_dtd = ReaderUseDTD; p_reader->pf_use_dtd = ReaderUseDTD;
...@@ -176,13 +185,16 @@ static int ReaderOpen( vlc_object_t *p_this ) ...@@ -176,13 +185,16 @@ static int ReaderOpen( vlc_object_t *p_this )
static void ReaderClose( vlc_object_t *p_this ) static void ReaderClose( vlc_object_t *p_this )
{ {
xml_reader_t *p_reader = (xml_reader_t *)p_this; xml_reader_t *p_reader = (xml_reader_t *)p_this;
xml_reader_sys_t *p_sys = p_reader->p_sys;
xmlFreeTextReader( (void *)p_reader->p_sys ); xmlFreeTextReader( p_sys->xml );
#ifdef LIBXML_GETS_A_CLUE_ABOUT_REENTRANCY_AND_MEMORY_LEAKS #ifdef LIBXML_GETS_A_CLUE_ABOUT_REENTRANCY_AND_MEMORY_LEAKS
vlc_mutex_lock( &lock ); vlc_mutex_lock( &lock );
xmlCleanupParser(); xmlCleanupParser();
vlc_mutex_unlock( &lock ); vlc_mutex_unlock( &lock );
#endif #endif
free( p_sys->node );
free( p_sys );
} }
static int ReaderUseDTD ( xml_reader_t *p_reader ) static int ReaderUseDTD ( xml_reader_t *p_reader )
...@@ -196,10 +208,17 @@ static int ReaderUseDTD ( xml_reader_t *p_reader ) ...@@ -196,10 +208,17 @@ static int ReaderUseDTD ( xml_reader_t *p_reader )
return VLC_SUCCESS; return VLC_SUCCESS;
} }
static int ReaderNextNode( xml_reader_t *p_reader ) static int ReaderNextNode( xml_reader_t *p_reader, const char **pval )
{ {
xml_reader_sys_t *p_sys = p_reader->p_sys;
const xmlChar *node;
int ret;
free( p_sys->node );
p_sys->node = NULL;
skip: skip:
switch( xmlTextReaderRead( (void *)p_reader->p_sys ) ) switch( xmlTextReaderRead( p_sys->xml ) )
{ {
case 0: /* EOF */ case 0: /* EOF */
return 0; return 0;
...@@ -207,28 +226,38 @@ skip: ...@@ -207,28 +226,38 @@ skip:
return -1; return -1;
} }
switch( xmlTextReaderNodeType( (void *)p_reader->p_sys ) ) switch( xmlTextReaderNodeType( p_sys->xml ) )
{ {
case XML_READER_TYPE_ELEMENT: case XML_READER_TYPE_ELEMENT:
return XML_READER_STARTELEM; node = xmlTextReaderConstName( p_sys->xml );
ret = XML_READER_STARTELEM;
break;
case XML_READER_TYPE_END_ELEMENT: case XML_READER_TYPE_END_ELEMENT:
return XML_READER_ENDELEM; node = xmlTextReaderConstName( p_sys->xml );
ret = XML_READER_ENDELEM;
break;
case XML_READER_TYPE_CDATA: case XML_READER_TYPE_CDATA:
case XML_READER_TYPE_TEXT: case XML_READER_TYPE_TEXT:
return XML_READER_TEXT; node = xmlTextReaderConstValue( p_sys->xml );
ret = XML_READER_TEXT;
break;
case -1: case -1:
return -1; return -1;
default: default:
goto skip; goto skip;
} }
}
static char *ReaderName( xml_reader_t *p_reader ) if( unlikely(node == NULL) )
{ return -1;
const xmlChar *psz_name =
xmlTextReaderConstName( (void *)p_reader->p_sys );
return psz_name ? strdup( (const char *)psz_name ) : NULL; p_sys->node = strdup( (const char *)node );
if( pval != NULL )
*pval = p_sys->node;
return likely(p_sys->node != NULL) ? ret : -1;
} }
static char *ReaderValue( xml_reader_t *p_reader ) static char *ReaderValue( xml_reader_t *p_reader )
......
...@@ -679,6 +679,7 @@ static bool ParseFeed( filter_t *p_filter, xml_reader_t *p_xml_reader, ...@@ -679,6 +679,7 @@ static bool ParseFeed( filter_t *p_filter, xml_reader_t *p_xml_reader,
rss_feed_t *p_feed ) rss_feed_t *p_feed )
{ {
VLC_UNUSED(p_filter); VLC_UNUSED(p_filter);
const char *node;
char *psz_eltname = NULL; char *psz_eltname = NULL;
bool b_is_item = false; bool b_is_item = false;
...@@ -687,21 +688,20 @@ static bool ParseFeed( filter_t *p_filter, xml_reader_t *p_xml_reader, ...@@ -687,21 +688,20 @@ static bool ParseFeed( filter_t *p_filter, xml_reader_t *p_xml_reader,
int i_item = 0; int i_item = 0;
int type; int type;
while( (type = xml_ReaderNextNode( p_xml_reader )) > 0 ) while( (type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
{ {
switch( type ) switch( type )
{ {
case XML_READER_STARTELEM: case XML_READER_STARTELEM:
free( psz_eltname );
psz_eltname = xml_ReaderName( p_xml_reader );
if( !psz_eltname )
goto end;
#ifdef RSS_DEBUG #ifdef RSS_DEBUG
msg_Dbg( p_filter, "element name: %s", psz_eltname ); msg_Dbg( p_filter, "element <%s>", node );
#endif #endif
psz_eltname = strdup( node );
if( unlikely(!psz_eltname) )
goto end;
/* rss or atom */ /* rss or atom */
if( !strcmp( psz_eltname, "item" ) || !strcmp( psz_eltname, "entry" ) ) if( !strcmp( node, "item" ) || !strcmp( node, "entry" ) )
{ {
b_is_item = true; b_is_item = true;
p_feed->i_items++; p_feed->i_items++;
...@@ -712,12 +712,12 @@ static bool ParseFeed( filter_t *p_filter, xml_reader_t *p_xml_reader, ...@@ -712,12 +712,12 @@ static bool ParseFeed( filter_t *p_filter, xml_reader_t *p_xml_reader,
p_feed->p_items[p_feed->i_items-1].psz_link = NULL; p_feed->p_items[p_feed->i_items-1].psz_link = NULL;
} }
/* rss */ /* rss */
else if( !strcmp( psz_eltname, "image" ) ) else if( !strcmp( node, "image" ) )
{ {
b_is_image = true; b_is_image = true;
} }
/* atom */ /* atom */
else if( !strcmp( psz_eltname, "link" ) ) else if( !strcmp( node, "link" ) )
{ {
const char *name; const char *name;
char *psz_href = NULL; char *psz_href = NULL;
...@@ -772,42 +772,32 @@ static bool ParseFeed( filter_t *p_filter, xml_reader_t *p_xml_reader, ...@@ -772,42 +772,32 @@ static bool ParseFeed( filter_t *p_filter, xml_reader_t *p_xml_reader,
break; break;
case XML_READER_ENDELEM: case XML_READER_ENDELEM:
free( psz_eltname ); FREENULL( psz_eltname );
psz_eltname = xml_ReaderName( p_xml_reader );
if( !psz_eltname )
goto end;
#ifdef RSS_DEBUG #ifdef RSS_DEBUG
msg_Dbg( p_filter, "element end : %s", psz_eltname ); msg_Dbg( p_filter, "element end </%s>", node );
#endif #endif
/* rss or atom */ /* rss or atom */
if( !strcmp( psz_eltname, "item" ) || !strcmp( psz_eltname, "entry" ) ) if( !strcmp( node, "item" ) || !strcmp( node, "entry" ) )
{ {
b_is_item = false; b_is_item = false;
i_item++; i_item++;
} }
/* rss */ /* rss */
else if( !strcmp( psz_eltname, "image" ) ) else if( !strcmp( node, "image" ) )
{ {
b_is_image = false; b_is_image = false;
} }
FREENULL( psz_eltname );
break; break;
case XML_READER_TEXT: case XML_READER_TEXT:
{ {
if( !psz_eltname ) if( !psz_eltname )
break; break;
char *psz_eltvalue = xml_ReaderValue( p_xml_reader );
if( !psz_eltvalue )
goto end;
char *psz_clean = removeWhiteChars( psz_eltvalue ); char *psz_eltvalue = removeWhiteChars( node );
free( psz_eltvalue );
psz_eltvalue = psz_clean;
#ifdef RSS_DEBUG #ifdef RSS_DEBUG
msg_Dbg( p_filter, " text : <%s>", psz_eltvalue ); msg_Dbg( p_filter, " text : \"%s\"", psz_eltvalue );
#endif #endif
/* Is it an item ? */ /* Is it an item ? */
if( b_is_item ) if( b_is_item )
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment