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

str_format_meta: clean up

parent 27da8366
...@@ -46,7 +46,6 @@ ...@@ -46,7 +46,6 @@
#include <vlc_strings.h> #include <vlc_strings.h>
#include <vlc_charset.h> #include <vlc_charset.h>
#include <vlc_fs.h>
#include <libvlc.h> #include <libvlc.h>
#include <errno.h> #include <errno.h>
...@@ -512,7 +511,7 @@ static void format_duration (char *buf, size_t len, int64_t duration) ...@@ -512,7 +511,7 @@ static void format_duration (char *buf, size_t len, int64_t duration)
#define INSERT_STRING( string ) \ #define INSERT_STRING( string ) \
if( string != NULL ) \ if( string != NULL ) \
{ \ { \
int len = strlen( string ); \ size_t len = strlen( string ); \
dst = xrealloc( dst, i_size = i_size + len );\ dst = xrealloc( dst, i_size = i_size + len );\
memcpy( (dst+d), string, len ); \ memcpy( (dst+d), string, len ); \
d += len; \ d += len; \
...@@ -522,28 +521,25 @@ static void format_duration (char *buf, size_t len, int64_t duration) ...@@ -522,28 +521,25 @@ static void format_duration (char *buf, size_t len, int64_t duration)
/* same than INSERT_STRING, except that string won't be freed */ /* same than INSERT_STRING, except that string won't be freed */
#define INSERT_STRING_NO_FREE( string ) \ #define INSERT_STRING_NO_FREE( string ) \
{ \ { \
int len = strlen( string ); \ size_t len = strlen( string ); \
dst = xrealloc( dst, i_size = i_size + len );\ dst = xrealloc( dst, i_size = i_size + len );\
memcpy( dst+d, string, len ); \ memcpy( dst+d, string, len ); \
d += len; \ d += len; \
} }
char *str_format_meta( playlist_t *p_object, const char *string ) char *str_format_meta( playlist_t *p_playlist, const char *s )
{ {
const char *s = string; char *dst = strdup( s );
if( unlikely(dst == NULL) )
return NULL;
input_thread_t *p_input = playlist_CurrentInput( p_playlist );
input_item_t *p_item = p_input ? input_GetItem(p_input) : NULL;
size_t i_size = strlen( s ) + 1; /* +1 to store '\0' */
size_t d = 0;
bool b_is_format = false; bool b_is_format = false;
bool b_empty_if_na = false; bool b_empty_if_na = false;
char buf[10]; char buf[10];
int i_size = strlen( string ) + 1; /* +1 to store '\0' */
char *dst = strdup( string );
if( !dst ) return NULL;
int d = 0;
input_thread_t *p_input = playlist_CurrentInput( p_object );
input_item_t *p_item = NULL;
if( p_input )
{
p_item = input_GetItem(p_input);
}
while( *s ) while( *s )
{ {
...@@ -553,33 +549,23 @@ char *str_format_meta( playlist_t *p_object, const char *string ) ...@@ -553,33 +549,23 @@ char *str_format_meta( playlist_t *p_object, const char *string )
{ {
case 'a': case 'a':
if( p_item ) if( p_item )
{
INSERT_STRING( input_item_GetArtist( p_item ) ); INSERT_STRING( input_item_GetArtist( p_item ) );
}
break; break;
case 'b': case 'b':
if( p_item ) if( p_item )
{
INSERT_STRING( input_item_GetAlbum( p_item ) ); INSERT_STRING( input_item_GetAlbum( p_item ) );
}
break; break;
case 'c': case 'c':
if( p_item ) if( p_item )
{
INSERT_STRING( input_item_GetCopyright( p_item ) ); INSERT_STRING( input_item_GetCopyright( p_item ) );
}
break; break;
case 'd': case 'd':
if( p_item ) if( p_item )
{
INSERT_STRING( input_item_GetDescription( p_item ) ); INSERT_STRING( input_item_GetDescription( p_item ) );
}
break; break;
case 'e': case 'e':
if( p_item ) if( p_item )
{
INSERT_STRING( input_item_GetEncodedBy( p_item ) ); INSERT_STRING( input_item_GetEncodedBy( p_item ) );
}
break; break;
case 'f': case 'f':
if( p_item && p_item->p_stats ) if( p_item && p_item->p_stats )
...@@ -595,33 +581,23 @@ char *str_format_meta( playlist_t *p_object, const char *string ) ...@@ -595,33 +581,23 @@ char *str_format_meta( playlist_t *p_object, const char *string )
break; break;
case 'g': case 'g':
if( p_item ) if( p_item )
{
INSERT_STRING( input_item_GetGenre( p_item ) ); INSERT_STRING( input_item_GetGenre( p_item ) );
}
break; break;
case 'l': case 'l':
if( p_item ) if( p_item )
{
INSERT_STRING( input_item_GetLanguage( p_item ) ); INSERT_STRING( input_item_GetLanguage( p_item ) );
}
break; break;
case 'n': case 'n':
if( p_item ) if( p_item )
{
INSERT_STRING( input_item_GetTrackNum( p_item ) ); INSERT_STRING( input_item_GetTrackNum( p_item ) );
}
break; break;
case 'p': case 'p':
if( p_item ) if( p_item )
{
INSERT_STRING( input_item_GetNowPlaying( p_item ) ); INSERT_STRING( input_item_GetNowPlaying( p_item ) );
}
break; break;
case 'r': case 'r':
if( p_item ) if( p_item )
{
INSERT_STRING( input_item_GetRating( p_item ) ); INSERT_STRING( input_item_GetRating( p_item ) );
}
break; break;
case 's': case 's':
{ {
...@@ -635,38 +611,28 @@ char *str_format_meta( playlist_t *p_object, const char *string ) ...@@ -635,38 +611,28 @@ char *str_format_meta( playlist_t *p_object, const char *string )
} }
case 't': case 't':
if( p_item ) if( p_item )
{
INSERT_STRING( input_item_GetTitle( p_item ) ); INSERT_STRING( input_item_GetTitle( p_item ) );
}
break; break;
case 'u': case 'u':
if( p_item ) if( p_item )
{
INSERT_STRING( input_item_GetURL( p_item ) ); INSERT_STRING( input_item_GetURL( p_item ) );
}
break; break;
case 'A': case 'A':
if( p_item ) if( p_item )
{
INSERT_STRING( input_item_GetDate( p_item ) ); INSERT_STRING( input_item_GetDate( p_item ) );
}
break; break;
case 'B': case 'B':
if( p_input ) if( p_input )
{
snprintf( buf, 10, "%"PRId64, snprintf( buf, 10, "%"PRId64,
var_GetInteger( p_input, "bit-rate" )/1000 ); var_GetInteger( p_input, "bit-rate" )/1000 );
}
else else
strcpy( buf, b_empty_if_na ? "" : "-" ); strcpy( buf, b_empty_if_na ? "" : "-" );
INSERT_STRING_NO_FREE( buf ); INSERT_STRING_NO_FREE( buf );
break; break;
case 'C': case 'C':
if( p_input ) if( p_input )
{
snprintf( buf, 10, "%"PRId64, snprintf( buf, 10, "%"PRId64,
var_GetInteger( p_input, "chapter" ) ); var_GetInteger( p_input, "chapter" ) );
}
else else
strcpy( buf, b_empty_if_na ? "" : "-" ); strcpy( buf, b_empty_if_na ? "" : "-" );
INSERT_STRING_NO_FREE( buf ); INSERT_STRING_NO_FREE( buf );
...@@ -683,16 +649,12 @@ char *str_format_meta( playlist_t *p_object, const char *string ) ...@@ -683,16 +649,12 @@ char *str_format_meta( playlist_t *p_object, const char *string )
break; break;
case 'F': case 'F':
if( p_item ) if( p_item )
{
INSERT_STRING( input_item_GetURI( p_item ) ); INSERT_STRING( input_item_GetURI( p_item ) );
}
break; break;
case 'I': case 'I':
if( p_input ) if( p_input )
{
snprintf( buf, 10, "%"PRId64, snprintf( buf, 10, "%"PRId64,
var_GetInteger( p_input, "title" ) ); var_GetInteger( p_input, "title" ) );
}
else else
strcpy( buf, b_empty_if_na ? "" : "-" ); strcpy( buf, b_empty_if_na ? "" : "-" );
INSERT_STRING_NO_FREE( buf ); INSERT_STRING_NO_FREE( buf );
...@@ -711,9 +673,7 @@ char *str_format_meta( playlist_t *p_object, const char *string ) ...@@ -711,9 +673,7 @@ char *str_format_meta( playlist_t *p_object, const char *string )
break; break;
case 'N': case 'N':
if( p_item ) if( p_item )
{
INSERT_STRING( input_item_GetName( p_item ) ); INSERT_STRING( input_item_GetName( p_item ) );
}
break; break;
case 'O': case 'O':
{ {
...@@ -728,14 +688,10 @@ char *str_format_meta( playlist_t *p_object, const char *string ) ...@@ -728,14 +688,10 @@ char *str_format_meta( playlist_t *p_object, const char *string )
} }
case 'P': case 'P':
if( p_input ) if( p_input )
{
snprintf( buf, 10, "%2.1lf", snprintf( buf, 10, "%2.1lf",
var_GetFloat( p_input, "position" ) * 100. ); var_GetFloat( p_input, "position" ) * 100. );
}
else else
{
snprintf( buf, 10, b_empty_if_na ? "" : "--.-%%" ); snprintf( buf, 10, b_empty_if_na ? "" : "--.-%%" );
}
INSERT_STRING_NO_FREE( buf ); INSERT_STRING_NO_FREE( buf );
break; break;
case 'R': case 'R':
...@@ -770,14 +726,12 @@ char *str_format_meta( playlist_t *p_object, const char *string ) ...@@ -770,14 +726,12 @@ char *str_format_meta( playlist_t *p_object, const char *string )
break; break;
case 'U': case 'U':
if( p_item ) if( p_item )
{
INSERT_STRING( input_item_GetPublisher( p_item ) ); INSERT_STRING( input_item_GetPublisher( p_item ) );
}
break; break;
case 'V': case 'V':
{ {
float vol = playlist_VolumeGet( p_object ); float vol = playlist_VolumeGet( p_object );
if( vol >= 0. ) if( vol >= 0.f )
{ {
snprintf( buf, 10, "%ld", snprintf( buf, 10, "%ld",
lroundf(vol * AOUT_VOLUME_DEFAULT ) ); lroundf(vol * AOUT_VOLUME_DEFAULT ) );
......
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