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

XML: return attribute value as const

This simplifies error and memory handling.
A few memory leaks were fixed.
parent 92864777
......@@ -71,8 +71,7 @@ struct xml_reader_t
module_t *p_module;
int (*pf_next_node) ( xml_reader_t *, const char ** );
char * (*pf_value) ( xml_reader_t * );
const char *(*pf_next_attr) ( xml_reader_t * );
const char *(*pf_next_attr) ( xml_reader_t *, const char ** );
int (*pf_use_dtd) ( xml_reader_t * );
};
......@@ -87,14 +86,10 @@ static inline int xml_ReaderNextNode( xml_reader_t *reader, const char **pval )
return reader->pf_next_node( reader, pval );
}
static inline char *xml_ReaderValue( xml_reader_t *reader )
static inline const char *xml_ReaderNextAttr( xml_reader_t *reader,
const char **pval )
{
return reader->pf_value( reader );
}
static inline const char *xml_ReaderNextAttr( xml_reader_t *reader )
{
return reader->pf_next_attr( reader );
return reader->pf_next_attr( reader, pval );
}
static inline int xml_ReaderUseDTD( xml_reader_t *reader )
......
......@@ -567,18 +567,13 @@ static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader )
else if( !strcasecmp( "resolution", node ) &&
( i_metadata_level == 1) )
{
const char *attr;
while( (attr = xml_ReaderNextAttr( p_xml_reader )) )
const char *attr, *val;
while( (attr = xml_ReaderNextAttr( p_xml_reader, &val )) )
{
char *psz_value = xml_ReaderValue ( p_xml_reader );
if( !psz_value )
continue;
if( !strcasecmp( "x", attr ) )
p_sys->i_original_width = atoi( psz_value );
p_sys->i_original_width = atoi( val );
else if( !strcasecmp( "y", attr ) )
p_sys->i_original_height = atoi( psz_value );
free( psz_value );
p_sys->i_original_height = atoi( val );
}
}
else if( !strcasecmp( "styles", node ) && (i_style_level == 0) )
......@@ -611,40 +606,31 @@ static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader )
}
}
const char *attr;
while( (attr = xml_ReaderNextAttr( p_xml_reader )) )
const char *attr, *val;
while( (attr = xml_ReaderNextAttr( p_xml_reader, &val )) )
{
char *psz_value = xml_ReaderValue ( p_xml_reader );
if( !psz_value )
continue;
if( !strcasecmp( "name", attr ) )
{
free( p_ssa_style->psz_stylename );
p_ssa_style->psz_stylename = strdup( psz_value );
p_ssa_style->psz_stylename = strdup( val );
}
free( psz_value );
}
}
else if( !strcasecmp( "fontstyle", node ) && (i_style_level == 2) )
{
const char *attr;
while( (attr = xml_ReaderNextAttr( p_xml_reader )) )
const char *attr, *val;
while( (attr = xml_ReaderNextAttr( p_xml_reader, &val )) )
{
char *psz_value = xml_ReaderValue ( p_xml_reader );
if( !psz_value )
continue;
if( !strcasecmp( "face", attr ) )
{
free( p_ssa_style->font_style.psz_fontname );
p_ssa_style->font_style.psz_fontname = strdup( psz_value );
p_ssa_style->font_style.psz_fontname = strdup( val );
}
else if( !strcasecmp( "size", attr ) )
{
if( ( *psz_value == '+' ) || ( *psz_value == '-' ) )
if( ( *val == '+' ) || ( *val == '-' ) )
{
int i_value = atoi( psz_value );
int i_value = atoi( val );
if( ( i_value >= -5 ) && ( i_value <= 5 ) )
p_ssa_style->font_style.i_font_size +=
......@@ -655,137 +641,131 @@ static void ParseUSFHeaderTags( decoder_t *p_dec, xml_reader_t *p_xml_reader )
p_ssa_style->font_style.i_font_size = i_value;
}
else
p_ssa_style->font_style.i_font_size = atoi( psz_value );
p_ssa_style->font_style.i_font_size = atoi( val );
}
else if( !strcasecmp( "italic", attr ) )
{
if( !strcasecmp( "yes", psz_value ))
if( !strcasecmp( "yes", val ))
p_ssa_style->font_style.i_style_flags |= STYLE_ITALIC;
else
p_ssa_style->font_style.i_style_flags &= ~STYLE_ITALIC;
}
else if( !strcasecmp( "weight", attr ) )
{
if( !strcasecmp( "bold", psz_value ))
if( !strcasecmp( "bold", val ))
p_ssa_style->font_style.i_style_flags |= STYLE_BOLD;
else
p_ssa_style->font_style.i_style_flags &= ~STYLE_BOLD;
}
else if( !strcasecmp( "underline", attr ) )
{
if( !strcasecmp( "yes", psz_value ))
if( !strcasecmp( "yes", val ))
p_ssa_style->font_style.i_style_flags |= STYLE_UNDERLINE;
else
p_ssa_style->font_style.i_style_flags &= ~STYLE_UNDERLINE;
}
else if( !strcasecmp( "color", attr ) )
{
if( *psz_value == '#' )
if( *val == '#' )
{
unsigned long col = strtol(psz_value+1, NULL, 16);
unsigned long col = strtol(val+1, NULL, 16);
p_ssa_style->font_style.i_font_color = (col & 0x00ffffff);
p_ssa_style->font_style.i_font_alpha = (col >> 24) & 0xff;
}
}
else if( !strcasecmp( "outline-color", attr ) )
{
if( *psz_value == '#' )
if( *val == '#' )
{
unsigned long col = strtol(psz_value+1, NULL, 16);
unsigned long col = strtol(val+1, NULL, 16);
p_ssa_style->font_style.i_outline_color = (col & 0x00ffffff);
p_ssa_style->font_style.i_outline_alpha = (col >> 24) & 0xff;
}
}
else if( !strcasecmp( "outline-level", attr ) )
{
p_ssa_style->font_style.i_outline_width = atoi( psz_value );
p_ssa_style->font_style.i_outline_width = atoi( val );
}
else if( !strcasecmp( "shadow-color", attr ) )
{
if( *psz_value == '#' )
if( *val == '#' )
{
unsigned long col = strtol(psz_value+1, NULL, 16);
unsigned long col = strtol(val+1, NULL, 16);
p_ssa_style->font_style.i_shadow_color = (col & 0x00ffffff);
p_ssa_style->font_style.i_shadow_alpha = (col >> 24) & 0xff;
}
}
else if( !strcasecmp( "shadow-level", attr ) )
{
p_ssa_style->font_style.i_shadow_width = atoi( psz_value );
p_ssa_style->font_style.i_shadow_width = atoi( val );
}
else if( !strcasecmp( "back-color", attr ) )
{
if( *psz_value == '#' )
if( *val == '#' )
{
unsigned long col = strtol(psz_value+1, NULL, 16);
unsigned long col = strtol(val+1, NULL, 16);
p_ssa_style->font_style.i_karaoke_background_color = (col & 0x00ffffff);
p_ssa_style->font_style.i_karaoke_background_alpha = (col >> 24) & 0xff;
}
}
else if( !strcasecmp( "spacing", attr ) )
{
p_ssa_style->font_style.i_spacing = atoi( psz_value );
p_ssa_style->font_style.i_spacing = atoi( val );
}
free( psz_value );
}
}
else if( !strcasecmp( "position", node ) && (i_style_level == 2) )
{
const char *attr;
while( (attr = xml_ReaderNextAttr( p_xml_reader )) )
const char *attr, *val;
while( (attr = xml_ReaderNextAttr( p_xml_reader, &val )) )
{
char *psz_value = xml_ReaderValue ( p_xml_reader );
if( !psz_value )
continue;
if( !strcasecmp( "alignment", attr ) )
{
if( !strcasecmp( "TopLeft", psz_value ) )
if( !strcasecmp( "TopLeft", val ) )
p_ssa_style->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;
else if( !strcasecmp( "TopCenter", psz_value ) )
else if( !strcasecmp( "TopCenter", val ) )
p_ssa_style->i_align = SUBPICTURE_ALIGN_TOP;
else if( !strcasecmp( "TopRight", psz_value ) )
else if( !strcasecmp( "TopRight", val ) )
p_ssa_style->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT;
else if( !strcasecmp( "MiddleLeft", psz_value ) )
else if( !strcasecmp( "MiddleLeft", val ) )
p_ssa_style->i_align = SUBPICTURE_ALIGN_LEFT;
else if( !strcasecmp( "MiddleCenter", psz_value ) )
else if( !strcasecmp( "MiddleCenter", val ) )
p_ssa_style->i_align = 0;
else if( !strcasecmp( "MiddleRight", psz_value ) )
else if( !strcasecmp( "MiddleRight", val ) )
p_ssa_style->i_align = SUBPICTURE_ALIGN_RIGHT;
else if( !strcasecmp( "BottomLeft", psz_value ) )
else if( !strcasecmp( "BottomLeft", val ) )
p_ssa_style->i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT;
else if( !strcasecmp( "BottomCenter", psz_value ) )
else if( !strcasecmp( "BottomCenter", val ) )
p_ssa_style->i_align = SUBPICTURE_ALIGN_BOTTOM;
else if( !strcasecmp( "BottomRight", psz_value ) )
else if( !strcasecmp( "BottomRight", val ) )
p_ssa_style->i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT;
}
else if( !strcasecmp( "horizontal-margin", attr ) )
{
if( strchr( psz_value, '%' ) )
if( strchr( val, '%' ) )
{
p_ssa_style->i_margin_h = 0;
p_ssa_style->i_margin_percent_h = atoi( psz_value );
p_ssa_style->i_margin_percent_h = atoi( val );
}
else
{
p_ssa_style->i_margin_h = atoi( psz_value );
p_ssa_style->i_margin_h = atoi( val );
p_ssa_style->i_margin_percent_h = 0;
}
}
else if( !strcasecmp( "vertical-margin", attr ) )
{
if( strchr( psz_value, '%' ) )
if( strchr( val, '%' ) )
{
p_ssa_style->i_margin_v = 0;
p_ssa_style->i_margin_percent_v = atoi( psz_value );
p_ssa_style->i_margin_percent_v = atoi( val );
}
else
{
p_ssa_style->i_margin_v = atoi( psz_value );
p_ssa_style->i_margin_v = atoi( val );
p_ssa_style->i_margin_percent_v = 0;
}
}
free( psz_value );
}
}
break;
......
......@@ -119,24 +119,16 @@ static int Demux( demux_t *p_demux )
}
// Read the attributes
const char *attr;
while( (attr = xml_ReaderNextAttr( p_xml_reader )) != NULL )
const char *attr, *value;
while( (attr = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL )
{
char *psz_value = xml_ReaderValue( p_xml_reader );
if( !psz_value )
{
free( psz_value );
goto end;
}
if( !strcmp( attr, "num_entries" ) )
msg_Dbg( p_demux, "playlist has %d entries", atoi(psz_value) );
msg_Dbg( p_demux, "playlist has %d entries", atoi(value) );
else if( !strcmp( attr, "label" ) )
input_item_SetName( p_current_input, psz_value );
input_item_SetName( p_current_input, value );
else
msg_Warn( p_demux, "stray attribute %s with value %s in element"
" 'playlist'", attr, psz_value );
free( psz_value );
" <playlist>", attr, value );
}
p_subitems = input_item_node_Create( p_current_input );
......@@ -155,24 +147,18 @@ static int Demux( demux_t *p_demux )
goto end;
// Read the attributes
while( (attr = xml_ReaderNextAttr( p_xml_reader )) )
while( (attr = xml_ReaderNextAttr( p_xml_reader, &value )) )
{
char *psz_value = xml_ReaderValue( p_xml_reader );
if( !psz_value )
{
free( psz_value );
goto end;
}
if( !strcmp( psz_elname, "entry" ) &&
!strcmp( attr, "Playstring" ) )
{
psz_mrl = psz_value;
free( psz_mrl );
psz_mrl = strdup( value );
}
else
{
msg_Warn( p_demux, "unexpected attribute %s in element %s",
msg_Warn( p_demux, "unexpected attribute %s in <%s>",
attr, psz_elname );
free( psz_value );
}
}
break;
......
......@@ -131,32 +131,22 @@ static bool parse_plist_node( demux_t *p_demux, input_item_node_t *p_input_node,
xml_elem_hnd_t *p_handlers )
{
VLC_UNUSED(p_track); VLC_UNUSED(psz_element);
const char *attr;
char *psz_value;
const char *attr, *value;
bool b_version_found = false;
/* read all playlist attributes */
while( (attr = xml_ReaderNextAttr( p_xml_reader )) )
while( (attr = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL )
{
psz_value = xml_ReaderValue( p_xml_reader );
if( !psz_value )
{
msg_Err( p_demux, "invalid xml stream @ <plist>" );
free( psz_value );
return false;
}
/* attribute: version */
if( !strcmp( attr, "version" ) )
{
b_version_found = true;
if( strcmp( psz_value, "1.0" ) )
if( strcmp( value, "1.0" ) )
msg_Warn( p_demux, "unsupported iTunes Media Library version" );
}
/* unknown attribute */
else
msg_Warn( p_demux, "invalid <plist> attribute:\"%s\"", attr );
free( psz_value );
}
/* attribute version is mandatory !!! */
......
......@@ -130,46 +130,30 @@ static int Demux( demux_t *p_demux )
b_image = true;
// Read the attributes
const char *attr;
while( (attr = xml_ReaderNextAttr( p_xml_reader )) )
const char *attr, *value;
while( (attr = xml_ReaderNextAttr( p_xml_reader, &value )) )
{
char *psz_value = xml_ReaderValue( p_xml_reader );
if( !psz_value )
{
free( psz_value );
goto error;
}
if( !strcmp( node, "enclosure" ) )
{
const char **p = NULL;
if( !strcmp( attr, "url" ) )
{
free( psz_item_mrl );
psz_item_mrl = psz_value;
}
p = &psz_item_mrl;
else if( !strcmp( attr, "length" ) )
{
free( psz_item_size );
psz_item_size = psz_value;
}
p = &psz_item_size;
else if( !strcmp( attr, "type" ) )
p = &psz_item_type;
if( p != NULL )
{
free( psz_item_type );
psz_item_type = psz_value;
free( *p );
*p = strdup( value );
}
else
{
msg_Dbg( p_demux,"unhandled attribute %s in element %s",
msg_Dbg( p_demux,"unhandled attribute %s in <%s>",
attr, node );
free( psz_value );
}
}
else
{
msg_Dbg( p_demux,"unhandled attribute %s in element %s",
msg_Dbg( p_demux,"unhandled attribute %s in <%s>",
attr, node );
free( psz_value );
}
}
break;
}
......
......@@ -145,82 +145,73 @@ static int Demux( demux_t *p_demux )
}
}
const char *attrname;
while( (attrname = xml_ReaderNextAttr( p_xml_reader )) != NULL )
const char *attrname, *value;
while( (attrname = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL )
{
char *psz_attrvalue = xml_ReaderValue( p_xml_reader );
if( !psz_attrvalue )
{
free( psz_attrvalue );
goto error;
}
if( !strcmp( attrname, "autoplay" ) )
b_autoplay = !strcmp( psz_attrvalue, "true" );
b_autoplay = !strcmp( value, "true" );
else if( !strcmp( attrname, "controler" ) )
b_controler = !strcmp( psz_attrvalue, "false" );
b_controler = !strcmp( attrname, "false" );
else if( !strcmp( attrname, "fullscreen" ) )
{
if( !strcmp( psz_attrvalue, "double" ) )
if( !strcmp( value, "double" ) )
fullscreen = FULLSCREEN_DOUBLE;
else if( !strcmp( psz_attrvalue, "half" ) )
else if( !strcmp( value, "half" ) )
fullscreen = FULLSCREEN_HALF;
else if( !strcmp( psz_attrvalue, "current" ) )
else if( !strcmp( value, "current" ) )
fullscreen = FULLSCREEN_CURRENT;
else if( !strcmp( psz_attrvalue, "full" ) )
else if( !strcmp( value, "full" ) )
fullscreen = FULLSCREEN_FULL;
else
fullscreen = FULLSCREEN_NORMAL;
}
else if( !strcmp( attrname, "href" ) )
{
psz_href = psz_attrvalue;
psz_attrvalue = NULL;
free( psz_href );
psz_href = strdup( value );
}
else if( !strcmp( attrname, "kioskmode" ) )
b_kioskmode = !strcmp( psz_attrvalue, "true" );
b_kioskmode = !strcmp( value, "true" );
else if( !strcmp( attrname, "loop" ) )
{
if( !strcmp( psz_attrvalue, "true" ) )
if( !strcmp( value, "true" ) )
loop = LOOP_TRUE;
else if( !strcmp( psz_attrvalue, "palindrome" ) )
else if( !strcmp( value, "palindrome" ) )
loop = LOOP_PALINDROME;
else
loop = LOOP_FALSE;
}
else if( !strcmp( attrname, "movieid" ) )
i_movieid = atoi( psz_attrvalue );
i_movieid = atoi( value );
else if( !strcmp( attrname, "moviename" ) )
{
psz_moviename = psz_attrvalue;
psz_attrvalue = NULL;
free( psz_moviename );
psz_moviename = strdup( value );
}
else if( !strcmp( attrname, "playeveryframe" ) )
b_playeveryframe = !strcmp( psz_attrvalue, "true" );
b_playeveryframe = !strcmp( value, "true" );
else if( !strcmp( attrname, "qtnext" ) )
{
psz_qtnext = psz_attrvalue;
psz_attrvalue = NULL;
free( psz_qtnext );
psz_qtnext = strdup( value );
}
else if( !strcmp( attrname, "quitwhendone" ) )
b_quitwhendone = !strcmp( psz_attrvalue, "true" );
b_quitwhendone = !strcmp( value, "true" );
else if( !strcmp( attrname, "src" ) )
{
psz_src = psz_attrvalue;
psz_attrvalue = NULL;
free( psz_src );
psz_src = strdup( value );
}
else if( !strcmp( attrname, "mimetype" ) )
{
psz_mimetype = psz_attrvalue;
psz_attrvalue = NULL;
free( psz_mimetype );
psz_mimetype = strdup( value );
}
else if( !strcmp( attrname, "volume" ) )
i_volume = atoi( psz_attrvalue );
i_volume = atoi( value );
else
msg_Dbg( p_demux, "Attribute %s with value %s isn't valid",
attrname, psz_attrvalue );
free( psz_attrvalue );
attrname, value );
}
msg_Dbg( p_demux, "autoplay: %s (unused by VLC)",
......
......@@ -131,12 +131,6 @@ error:
return i_ret;
}
#define GET_VALUE( a ) \
if( !strcmp( attrname, #a ) ) \
{ \
free(psz_ ## a); \
psz_ ## a = psz_attrvalue; \
}
/* <genrelist>
* <genre name="the name"></genre>
* ...
......@@ -158,24 +152,18 @@ static int DemuxGenre( demux_t *p_demux, xml_reader_t *p_xml_reader,
if( !strcmp( node, "genre" ) )
{
// Read the attributes
const char *attrname;
while( (attrname = xml_ReaderNextAttr( p_xml_reader )) )
const char *name, *value;
while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) )
{
char *psz_attrvalue = xml_ReaderValue( p_xml_reader );
if( !psz_attrvalue )
if( !strcmp( name, "name" ) )
{
free( psz_attrvalue );
break;
free(psz_name);
psz_name = strdup( value );
}
GET_VALUE( name )
else
{
msg_Warn( p_demux,
"unexpected attribute %s in element %s",
attrname, node );
free( psz_attrvalue );
}
"unexpected attribute %s in <%s>",
name, node );
}
}
break;
......@@ -259,54 +247,53 @@ static int DemuxStation( demux_t *p_demux, xml_reader_t *p_xml_reader,
// Read the attributes
if( !strcmp( node, "tunein" ) )
{
const char *attrname;
while( (attrname = xml_ReaderNextAttr( p_xml_reader )) )
const char *name, *value;
while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) )
{
char *psz_attrvalue = xml_ReaderValue( p_xml_reader );
if( !psz_attrvalue )
if( !strcmp( name, "base" ) )
{
free( psz_attrvalue );
return -1;
free( psz_base );
psz_base = strdup( value );
}
GET_VALUE( base )
else
{
msg_Warn( p_demux,
"unexpected attribute %s in element %s",
attrname, node );
free( psz_attrvalue );
}
"unexpected attribute %s in <%s>",
name, node );
}
}
else if( !strcmp( node, "station" ) )
{
const char *attrname;
while( (attrname = xml_ReaderNextAttr( p_xml_reader )) )
const char *name, *value;
while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) )
{
char *psz_attrvalue = xml_ReaderValue( p_xml_reader );
if( !psz_attrvalue )
char **p = NULL;
if( !strcmp( name, "name" ) )
p = &psz_name;
else if ( !strcmp( name, "mt" ) )
p = &psz_mt;
else if ( !strcmp( name, "id" ) )
p = &psz_id;
else if ( !strcmp( name, "br" ) )
p = &psz_br;
else if ( !strcmp( name, "genre" ) )
p = &psz_genre;
else if ( !strcmp( name, "ct" ) )
p = &psz_ct;
else if ( !strcmp( name, "lc" ) )
p = &psz_lc;
else if ( !strcmp( name, "rt" ) )
p = &psz_rt;
else if ( !strcmp( name, "load" ) )
p = &psz_load;
if( p != NULL )
{
free( psz_attrvalue );
return -1;
free( *p );
*p = strdup( value );
}
GET_VALUE( name )
else GET_VALUE( mt )
else GET_VALUE( id )
else GET_VALUE( br )
else GET_VALUE( genre )
else GET_VALUE( ct )
else GET_VALUE( lc )
else GET_VALUE( rt )
else GET_VALUE( load )
else
{
msg_Warn( p_demux,
"unexpected attribute %s in element %s",
attrname, node );
free( psz_attrvalue );
}
"unexpected attribute %s in <%s>",
name, node );
}
}
break;
......
......@@ -173,21 +173,15 @@ static bool parse_playlist_node COMPLEX_INTERFACE
};
/* read all playlist attributes */
const char *name;
while( (name = xml_ReaderNextAttr( p_xml_reader )) != NULL )
const char *name, *value;
while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL )
{
psz_value = xml_ReaderValue( p_xml_reader );
if( !psz_value )
{
msg_Err( p_demux, "invalid xml stream @ <playlist>" );
goto end;
}
/* attribute: version */
if( !strcmp( name, "version" ) )
{
b_version_found = true;
if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
msg_Warn( p_demux, "unsupported XSPF version" );
if( strcmp( value, "0" ) && strcmp( value, "1" ) )
msg_Warn( p_demux, "unsupported XSPF version %s", value );
}
/* attribute: xmlns */
else if( !strcmp( name, "xmlns" ) || !strcmp( name, "xmlns:vlc" ) )
......@@ -200,8 +194,6 @@ static bool parse_playlist_node COMPLEX_INTERFACE
/* unknown attribute */
else
msg_Warn( p_demux, "invalid <playlist> attribute: \"%s\"", name);
free( psz_value );
}
/* attribute version is mandatory !!! */
if( !b_version_found )
......@@ -590,37 +582,27 @@ static bool parse_extension_node COMPLEX_INTERFACE
};