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
stream_t *p_stream;
module_t *p_module;
int (*pf_next_node) ( xml_reader_t * );
char * (*pf_name) ( xml_reader_t * );
int (*pf_next_node) ( xml_reader_t *, const char ** );
char * (*pf_value) ( 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
VLC_EXPORT( void, xml_ReaderDelete, (xml_reader_t *) );
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 );
}
static inline char *xml_ReaderName( xml_reader_t *reader )
{
return reader->pf_name( reader );
return reader->pf_next_node( reader, pval );
}
static inline char *xml_ReaderValue( xml_reader_t *reader )
......
......@@ -528,37 +528,29 @@ static int ParseImageAttachments( decoder_t *p_dec )
static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader )
{
decoder_sys_t *p_sys = p_dec->p_sys;
char *psz_node;
const char *node;
ssa_style_t *p_ssa_style = NULL;
int i_style_level = 0;
int i_metadata_level = 0;
int type;
while( (type = xml_ReaderNextNode( p_xml_reader )) > 0 )
while( (type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
{
switch( type )
{
case XML_READER_ENDELEM:
psz_node = xml_ReaderName( p_xml_reader );
if( !psz_node )
break;
switch (i_style_level)
{
case 0:
if( !strcasecmp( "metadata", psz_node ) && (i_metadata_level == 1) )
{
if( !strcasecmp( "metadata", node ) && (i_metadata_level == 1) )
i_metadata_level--;
}
break;
case 1:
if( !strcasecmp( "styles", psz_node ) )
{
if( !strcasecmp( "styles", node ) )
i_style_level--;
}
break;
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 );
......@@ -567,20 +559,12 @@ static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader )
}
break;
}
free( psz_node );
break;
case XML_READER_STARTELEM:
psz_node = xml_ReaderName( p_xml_reader );
if( !psz_node )
break;
if( !strcasecmp( "metadata", psz_node ) && (i_style_level == 0) )
{
case XML_READER_STARTELEM:
if( !strcasecmp( "metadata", node ) && (i_style_level == 0) )
i_metadata_level++;
}
else if( !strcasecmp( "resolution", psz_node ) &&
else if( !strcasecmp( "resolution", node ) &&
( i_metadata_level == 1) )
{
const char *attr;
......@@ -597,27 +581,23 @@ static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader )
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++;
}
else if( !strcasecmp( "style", psz_node ) && (i_style_level == 1) )
else if( !strcasecmp( "style", node ) && (i_style_level == 1) )
{
i_style_level++;
p_ssa_style = calloc( 1, sizeof(ssa_style_t) );
if( !p_ssa_style )
{
free( psz_node );
if( unlikely(!p_ssa_style) )
return;
}
/* All styles are supposed to default to Default, and then
* one or more settings are over-ridden.
* At the moment this only effects styles defined AFTER
* Default in the XML
*/
int i;
for( i = 0; i < p_sys->i_ssa_styles; i++ )
for( int i = 0; i < p_sys->i_ssa_styles; i++ )
{
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 )
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;
while( (attr = xml_ReaderNextAttr( p_xml_reader )) )
......@@ -749,7 +729,7 @@ static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader )
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;
while( (attr = xml_ReaderNextAttr( 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_node );
break;
}
}
......@@ -991,16 +969,13 @@ static void ParseUSFHeader( decoder_t *p_dec )
p_xml_reader = xml_ReaderCreate( p_dec, p_sub );
if( likely(p_xml_reader) )
{
/* Look for Root Node */
if( xml_ReaderNextNode( p_xml_reader ) == XML_READER_STARTELEM )
{
char *psz_node = xml_ReaderName( p_xml_reader );
const char *node;
if( !strcasecmp( "usfsubtitles", psz_node ) )
/* Look for Root Node */
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 );
}
stream_Delete( p_sub );
......
......@@ -73,6 +73,7 @@ static int Demux( demux_t *p_demux )
xml_reader_t *p_xml_reader = NULL;
char *psz_elname = NULL;
const char *node;
input_item_t *p_input;
char *psz_mrl = NULL, *psz_title = NULL, *psz_genre = NULL;
char *psz_now = NULL, *psz_listeners = NULL, *psz_bitrate = NULL;
......@@ -84,42 +85,38 @@ static int Demux( demux_t *p_demux )
p_xml_reader = xml_ReaderCreate( p_demux, p_demux->s );
if( !p_xml_reader )
goto end;
return -1;
/* xml */
/* 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)" );
goto end;
}
if( ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
strcmp( psz_elname, "WinampXML" ) )
if( strcmp( node, "WinampXML" ) )
{
msg_Err( p_demux, "invalid root node: %s", psz_elname );
msg_Err( p_demux, "invalid root node: %s", node );
goto end;
}
FREENULL( psz_elname );
/* root node should not have any attributes, and should only
* contain the "playlist 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 )
{
msg_Err( p_demux, "invalid file (no child node)" );
goto end;
}
if( ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL ||
strcmp( psz_elname, "playlist" ) )
if( strcmp( node, "playlist" ) )
{
msg_Err( p_demux, "invalid child node %s", psz_elname );
msg_Err( p_demux, "invalid child node %s", node );
goto end;
}
FREENULL( psz_elname );
// Read the attributes
const char *attr;
......@@ -144,7 +141,7 @@ static int Demux( demux_t *p_demux )
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
switch( i_ret )
......@@ -153,8 +150,8 @@ static int Demux( demux_t *p_demux )
{
// Read the element name
free( psz_elname );
psz_elname = xml_ReaderName( p_xml_reader );
if( !psz_elname )
psz_elname = strdup( node );
if( unlikely(!psz_elname) )
goto end;
// Read the attributes
......@@ -180,55 +177,41 @@ static int Demux( demux_t *p_demux )
}
break;
}
case XML_READER_TEXT:
{
char *psz_text = xml_ReaderValue( p_xml_reader );
if( IsWhitespace( psz_text ) )
{
free( psz_text );
char **p;
if( psz_elname == NULL )
break;
if( IsWhitespace( node ) )
break;
}
if( !strcmp( psz_elname, "Name" ) )
{
psz_title = psz_text;
}
p = &psz_title;
else if( !strcmp( psz_elname, "Genre" ) )
{
psz_genre = psz_text;
}
p = &psz_genre;
else if( !strcmp( psz_elname, "Nowplaying" ) )
{
psz_now = psz_text;
}
p = &psz_now;
else if( !strcmp( psz_elname, "Listeners" ) )
{
psz_listeners = psz_text;
}
p = &psz_listeners;
else if( !strcmp( psz_elname, "Bitrate" ) )
{
psz_bitrate = psz_text;
}
else if( !strcmp( psz_elname, "" ) )
{
free( psz_text );
}
p = &psz_bitrate;
else
{
msg_Warn( p_demux, "unexpected text in element '%s'",
msg_Warn( p_demux, "unexpected text in element <%s>",
psz_elname );
free( psz_text );
break;
}
free( *p );
*p = strdup( node );
break;
}
// End element
case XML_READER_ENDELEM:
{
// Read the element name
free( psz_elname );
psz_elname = xml_ReaderName( p_xml_reader );
if( !psz_elname )
goto end;
if( !strcmp( psz_elname, "entry" ) )
if( !strcmp( node, "entry" ) )
{
p_input = input_item_New( p_demux, psz_mrl, psz_title );
if( psz_now )
......@@ -249,9 +232,7 @@ static int Demux( demux_t *p_demux )
FREENULL( psz_listeners );
FREENULL( psz_now );
}
free( psz_elname );
psz_elname = strdup( "" );
FREENULL( psz_elname );
break;
}
}
......
......@@ -68,7 +68,7 @@ void Close_iTML( vlc_object_t *p_this )
int Demux( demux_t *p_demux )
{
xml_reader_t *p_xml_reader;
char *psz_name = NULL;
const char *node;
input_item_t *p_current_input = GetCurrentItem(p_demux);
p_demux->p_sys->i_ntracks = 0;
......@@ -82,19 +82,19 @@ int Demux( demux_t *p_demux )
int type;
do
{
type = xml_ReaderNextNode( p_xml_reader );
type = xml_ReaderNextNode( p_xml_reader, &node );
if( type <= 0 )
{
msg_Err( p_demux, "can't read xml stream" );
goto end;
}
} while( type != XML_READER_STARTELEM );
}
while( type != XML_READER_STARTELEM );
/* checking root node name */
psz_name = xml_ReaderName( p_xml_reader );
if( !psz_name || strcmp( psz_name, "plist" ) )
if( strcmp( node, "plist" ) )
{
msg_Err( p_demux, "invalid root node name: %s", psz_name );
msg_Err( p_demux, "invalid root node <%s>", node );
goto end;
}
......@@ -108,7 +108,6 @@ int Demux( demux_t *p_demux )
vlc_gc_decref(p_current_input);
end:
free( psz_name );
if( 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,
const char *psz_element, xml_elem_hnd_t *p_handlers )
{
int i_node;
char *psz_name = NULL;
const char *node;
char *psz_value = NULL;
char *psz_key = NULL;
xml_elem_hnd_t *p_handler = NULL;
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 )
{
case XML_READER_STARTELEM:
/* element start tag */
psz_name = xml_ReaderName( p_xml_reader );
if( !psz_name || !*psz_name )
case XML_READER_STARTELEM:
if( !*node )
{
msg_Err( p_demux, "invalid xml stream" );
msg_Err( p_demux, "invalid XML stream" );
goto end;
}
/* choose handler */
for( p_handler = p_handlers;
p_handler->name && strcmp( psz_name, p_handler->name );
p_handler->name && strcmp( node, p_handler->name );
p_handler++ );
if( !p_handler->name )
{
msg_Err( p_demux, "unexpected element <%s>", psz_name );
msg_Err( p_demux, "unexpected element <%s>", node );
goto end;
}
FREE_NAME();
/* complex content is parsed in a separate function */
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,
}
break;
case XML_READER_TEXT:
/* simple element content */
case XML_READER_TEXT:
free( psz_value );
psz_value = xml_ReaderValue( p_xml_reader );
if( !psz_value )
{
msg_Err( p_demux, "invalid xml stream" );
psz_value = strdup( node );
if( unlikely(!psz_value) )
goto end;
}
break;
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;
}
case XML_READER_ENDELEM:
/* leave if the current parent node <track> is terminated */
if( !strcmp( psz_name, psz_element ) )
if( !strcmp( node, psz_element ) )
{
b_ret = true;
goto end;
}
/* there MUST have been a start tag for that element 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>",
psz_name );
node );
goto end;
}
/* special case: key */
......@@ -268,12 +256,10 @@ static bool parse_dict( demux_t *p_demux, input_item_node_t *p_input_node,
p_handler = NULL;
break;
}
FREE_NAME();
}
msg_Err( p_demux, "unexpected end of XML data" );
end:
free( psz_name );
free( psz_value );
free( psz_key );
return b_ret;
......@@ -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_track); VLC_UNUSED(p_handlers);
char *psz_endname;
const char *node;
int type;
while( (type = xml_ReaderNextNode( p_xml_reader )) > 0 )
{
if( type == XML_READER_ENDELEM )
{
psz_endname = xml_ReaderName( p_xml_reader );
if( !psz_endname )
return false;
if( !strcmp( psz_element, psz_endname ) )
{
free( psz_endname );
while( (type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
if( type == XML_READER_ENDELEM && !strcmp( psz_element, node ) )
return true;
}
else
free( psz_endname );
}
}
return false;
}
......@@ -26,11 +26,10 @@
*/
/* defines */
#define FREE_NAME() FREENULL( psz_name )
#define FREE_VALUE() FREENULL( psz_value )
#define FREE_KEY() FREENULL( psz_key )
#define FREE_ATT() do{ FREE_NAME();FREE_VALUE(); }while(0)
#define FREE_ATT_KEY() do{ FREE_NAME();FREE_VALUE();FREE_KEY();} while(0)
#define FREE_ATT() FREE_VALUE()
#define FREE_ATT_KEY() do{ FREE_VALUE();FREE_KEY();} while(0)
#define UNKNOWN_CONTENT 0
#define SIMPLE_CONTENT 1
......
......@@ -86,6 +86,7 @@ static int Demux( demux_t *p_demux )
char *psz_item_subtitle = NULL;
char *psz_item_summary = NULL;
char *psz_art_url = NULL;
const char *node;
int i_type;
input_item_t *p_input;
input_item_node_t *p_subitems = NULL;
......@@ -98,42 +99,35 @@ static int Demux( demux_t *p_demux )
/* xml */
/* check root node */
if( xml_ReaderNextNode( p_xml_reader ) != XML_READER_STARTELEM
|| ( psz_elname = xml_ReaderName( p_xml_reader ) ) == NULL)
if( xml_ReaderNextNode( p_xml_reader, &node ) != XML_READER_STARTELEM )
{
msg_Err( p_demux, "invalid file (no root node)" );
goto error;
}
if( strcmp( psz_elname, "rss" ) )
if( strcmp( node, "rss" ) )
{
msg_Err( p_demux, "invalid root node: %s", psz_elname );
msg_Err( p_demux, "invalid root node <%s>", node );
goto error;
}
FREENULL( psz_elname );
p_subitems = input_item_node_Create( p_current_input );
while( (i_type = xml_ReaderNextNode( p_xml_reader )) > 0 )
while( (i_type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
{
switch( i_type )
{
case XML_READER_STARTELEM:
{
// Read the element name
free( psz_elname );
psz_elname = xml_ReaderName( p_xml_reader );
if( !psz_elname )
psz_elname = strdup( node );
if( unlikely(!node) )
goto error;
if( !strcmp( psz_elname, "item" ) )
{
if( !strcmp( node, "item" ) )
b_item = true;
}
else if( !strcmp( psz_elname, "image" ) )
{
else if( !strcmp( node, "image" ) )
b_image = true;
}
// Read the attributes
const char *attr;
......@@ -146,7 +140,7 @@ static int Demux( demux_t *p_demux )
goto error;
}
if( !strcmp( psz_elname, "enclosure" ) )
if( !strcmp( node, "enclosure" ) )
{
if( !strcmp( attr, "url" ) )
{
......@@ -166,68 +160,64 @@ static int Demux( demux_t *p_demux )
else
{
msg_Dbg( p_demux,"unhandled attribute %s in element %s",
attr, psz_elname );
attr, node );
free( psz_value );
}
}
else
{
msg_Dbg( p_demux,"unhandled attribute %s in element %s",
attr, psz_elname );
attr, node );
free( psz_value );
}
}
break;
}
case XML_READER_TEXT:
{
if(!psz_elname) break;
char *psz_text = xml_ReaderValue( p_xml_reader );
#define SET_DATA( field, name ) \
else if( !strcmp( psz_elname, name ) ) \
{ \
field = psz_text; \
}
/* item specific meta data */
if( b_item == true )
if( b_item )
{
char **p;
if( !strcmp( psz_elname, "title" ) )
{
psz_item_name = psz_text;
}
p = &psz_item_name;
else if( !strcmp( psz_elname, "itunes:author" ) ||
!strcmp( psz_elname, "author" ) )
{ /* <author> isn't standard iTunes podcast stuff */
psz_item_author = psz_text;
}
/* <author> isn't standard iTunes podcast stuff */
p = &psz_item_author;
else if( !strcmp( psz_elname, "itunes:summary" ) ||
!strcmp( psz_elname, "description" ) )
{ /* <description> isn't standard iTunes podcast stuff */
psz_item_summary = psz_text;
}
SET_DATA( psz_item_date, "pubDate" )
SET_DATA( psz_item_category, "itunes:category" )
SET_DATA( psz_item_duration, "itunes:duration" )
SET_DATA( psz_item_keywords, "itunes:keywords" )
SET_DATA( psz_item_subtitle, "itunes:subtitle" )
/* <description> isn't standard iTunes podcast stuff */
p = &psz_item_summary;
else if( !strcmp( psz_elname, "pubDate" ) )
p = &psz_item_date;
else if( !strcmp( psz_elname, "itunes:category" ) )
p = &psz_item_category;
else if( !strcmp( psz_elname, "itunes:duration" ) )
p = &psz_item_duration;
else if( !strcmp( psz_elname, "itunes:keywords" ) )
p = &psz_item_keywords;
else if( !strcmp( psz_elname, "itunes:subtitle" ) )
p = &psz_item_subtitle;
else
free( psz_text );
}
#undef SET_DATA
break;
free( *p );
*p = strdup( node );
}
/* toplevel meta data */
else if( b_image == false )
else if( !b_image )
{
if( !strcmp( psz_elname, "title" ) )
{
input_item_SetName( p_current_input, psz_text );
}
input_item_SetName( p_current_input, node );
#define ADD_GINFO( info, name ) \
else if( !strcmp( psz_elname, name ) ) \
input_item_AddInfo( p_current_input, _("Podcast Info"), \
info, "%s", psz_text );
info, "%s", node );
ADD_GINFO( _("Podcast Link"), "link" )
ADD_GINFO( _("Podcast Copyright"), "copyright" )
ADD_GINFO( _("Podcast Category"), "itunes:category" )
......@@ -239,46 +229,52 @@ static int Demux( demux_t *p_demux )
{ /* <description> isn't standard iTunes podcast stuff */
input_item_AddInfo( p_current_input,
_( "Podcast Info" ), _( "Podcast Summary" ),
"%s", psz_text );
"%s", node );
}
free( psz_text );
}
else
{
if( !strcmp( psz_elname, "url" ) )
{
free( psz_art_url );
psz_art_url = psz_text;
psz_art_url = strdup( node );
}
else
{
msg_Dbg( p_demux, "unhandled text in element '%s'",
msg_Dbg( p_demux, "unhandled text in element <%s>",
psz_elname );
free( psz_text );
}
}
break;
}
// End element
case XML_READER_ENDELEM:
{
// Read the element name
free( psz_elname );
psz_elname = xml_ReaderName( p_xml_reader );
if( !psz_elname )
goto error;
if( !strcmp( psz_elname, "item" ) )
FREENULL( psz_elname );
if( !strcmp( node, "item" ) )
{
if( psz_item_mrl == NULL )
{
msg_Err( p_demux, "invalid XML (no enclosure markup)" );
goto error;
}
p_input = input_item_New( p_demux, psz_item_mrl, psz_item_name );
if( p_input == NULL ) break;
FREENULL( psz_item_mrl );
FREENULL( psz_item_name );
if( p_input == NULL )
break; /* FIXME: meta data memory leaks? */