]> git.sesse.net Git - vlc/blobdiff - src/text/strings.c
Qt4 - Cleaning trailing spaces...
[vlc] / src / text / strings.c
index fb5c40fbbc9f3c72a64e11357617e053f86b7ff4..8fd825ac6faab150f6de2304f930e281973b64bc 100644 (file)
@@ -27,9 +27,6 @@
  * Preamble
  *****************************************************************************/
 #include <vlc/vlc.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
 #include <assert.h>
 
 /* Needed by str_format_time */
@@ -637,28 +634,38 @@ char *str_format_time( const char *tformat )
     return strdup( buffer );
 }
 
-#define INSERT_STRING( check, string )                              \
-                    if( check && string )                           \
+#define INSERT_STRING( string )                                     \
+                    if( string != NULL )                            \
                     {                                               \
                         int len = strlen( string );                 \
-                        dst = realloc( dst,                         \
-                                       i_size = i_size + len + 1 ); \
-                        strncpy( d, string, len+1 );                \
+                        dst = realloc( dst, i_size = i_size + len );\
+                        memcpy( (dst+d), string, len );             \
                         d += len;                                   \
+                        free( string );                             \
                     }                                               \
                     else                                            \
                     {                                               \
-                        *d = '-';                                   \
+                        *(dst+d) = '-';                             \
                         d++;                                        \
+                    }                                               \
+
+/* same than INSERT_STRING, except that string won't be freed */
+#define INSERT_STRING_NO_FREE( string )                             \
+                    {                                               \
+                        int len = strlen( string );                 \
+                        dst = realloc( dst, i_size = i_size + len );\
+                        memcpy( dst+d, string, len );               \
+                        d += len;                                   \
                     }
 char *__str_format_meta( vlc_object_t *p_object, const char *string )
 {
     const char *s = string;
-    char *dst = malloc( 1000 );
-    char *d = dst;
     int b_is_format = 0;
+    int b_empty_if_na = 0;
     char buf[10];
-    int i_size = strlen( string );
+    int i_size = strlen( string ) + 1; /* +1 to store '\0' */
+    char *dst = malloc( i_size );
+    int d = 0;
 
     playlist_t *p_playlist = pl_Yield( p_object );
     input_thread_t *p_input = p_playlist->p_input;
@@ -668,8 +675,6 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
     {
         vlc_object_yield( p_input );
         p_item = input_GetItem(p_input);
-        if( p_item )
-            vlc_mutex_lock( &p_item->lock );
     }
 
     sprintf( dst, string );
@@ -681,71 +686,92 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
             switch( *s )
             {
                 case 'a':
-                    INSERT_STRING( p_item && p_item->p_meta,
-                                   p_item->p_meta->psz_artist );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetArtist( p_item ) );
+                    }
                     break;
                 case 'b':
-                    INSERT_STRING( p_item && p_item->p_meta,
-                                   p_item->p_meta->psz_album );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetAlbum( p_item ) );
+                    }
                     break;
                 case 'c':
-                    INSERT_STRING( p_item && p_item->p_meta,
-                                   p_item->p_meta->psz_copyright );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetCopyright( p_item ) );
+                    }
                     break;
                 case 'd':
-                    INSERT_STRING( p_item && p_item->p_meta,
-                                   p_item->p_meta->psz_description );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetDescription( p_item ) );
+                    }
                     break;
                 case 'e':
-                    INSERT_STRING( p_item && p_item->p_meta,
-                                   p_item->p_meta->psz_encodedby );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetEncodedBy( p_item ) );
+                    }
                     break;
                 case 'g':
-                    INSERT_STRING( p_item && p_item->p_meta,
-                                   p_item->p_meta->psz_genre );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetGenre( p_item ) );
+                    }
                     break;
                 case 'l':
-                    INSERT_STRING( p_item && p_item->p_meta,
-                                   p_item->p_meta->psz_language );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetLanguage( p_item ) );
+                    }
                     break;
                 case 'n':
-                    INSERT_STRING( p_item && p_item->p_meta,
-                                   p_item->p_meta->psz_tracknum );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetTrackNum( p_item ) );
+                    }
                     break;
                 case 'p':
-                    INSERT_STRING( p_item && p_item->p_meta,
-                                   p_item->p_meta->psz_nowplaying );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetNowPlaying( p_item ) );
+                    }
                     break;
                 case 'r':
-                    INSERT_STRING( p_item && p_item->p_meta,
-                                   p_item->p_meta->psz_rating );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetRating( p_item ) );
+                    }
                     break;
                 case 's':
                 {
-                    char *lang;
+                    char *lang = NULL;
                     if( p_input )
-                    {
-                        lang = var_GetString( p_input, "sub-language" );
-                    }
-                    else
-                    {
-                        lang = strdup( "-" );
-                    }
-                    INSERT_STRING( 1, lang );
-                    free( lang );
+                        lang = var_GetNonEmptyString( p_input, "sub-language" );
+                    if( lang == NULL )
+                        lang = strdup( b_empty_if_na ? "" : "-" );
+                    INSERT_STRING( lang );
                     break;
                 }
                 case 't':
-                    INSERT_STRING( p_item && p_item->p_meta,
-                                   p_item->p_meta->psz_title );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetTitle( p_item ) );
+                    }
                     break;
                 case 'u':
-                    INSERT_STRING( p_item && p_item->p_meta,
-                                   p_item->p_meta->psz_url );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetURL( p_item ) );
+                    }
                     break;
                 case 'A':
-                    INSERT_STRING( p_item && p_item->p_meta,
-                                   p_item->p_meta->psz_date );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetDate( p_item ) );
+                    }
                     break;
                 case 'B':
                     if( p_input )
@@ -755,9 +781,9 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
                     }
                     else
                     {
-                        sprintf( buf, "-" );
+                        sprintf( buf, b_empty_if_na ? "" : "-" );
                     }
-                    INSERT_STRING( 1, buf );
+                    INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'C':
                     if( p_input )
@@ -767,26 +793,30 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
                     }
                     else
                     {
-                        sprintf( buf, "-" );
+                        sprintf( buf, b_empty_if_na ? "" : "-" );
                     }
-                    INSERT_STRING( 1, buf );
+                    INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'D':
                     if( p_item )
                     {
+                        mtime_t i_duration = input_item_GetDuration( p_item );
                         sprintf( buf, "%02d:%02d:%02d",
-                                 (int)(p_item->i_duration/(3600000000)),
-                                 (int)((p_item->i_duration/(60000000))%60),
-                                 (int)((p_item->i_duration/1000000)%60) );
+                                 (int)(i_duration/(3600000000)),
+                                 (int)((i_duration/(60000000))%60),
+                                 (int)((i_duration/1000000)%60) );
                     }
                     else
                     {
-                        sprintf( buf, "--:--:--" );
+                        sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
                     }
-                    INSERT_STRING( 1, buf );
+                    INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'F':
-                    INSERT_STRING( p_item, p_item->psz_uri );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetURI( p_item ) );
+                    }
                     break;
                 case 'I':
                     if( p_input )
@@ -796,40 +826,41 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
                     }
                     else
                     {
-                        sprintf( buf, "-" );
+                        sprintf( buf, b_empty_if_na ? "" : "-" );
                     }
-                    INSERT_STRING( 1, buf );
+                    INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'L':
                     if( p_item && p_input )
                     {
+                        mtime_t i_duration = input_item_GetDuration( p_item );
+                        int64_t i_time = p_input->i_time;
                         sprintf( buf, "%02d:%02d:%02d",
-                     (int)((p_item->i_duration-p_input->i_time)/(3600000000)),
-                     (int)(((p_item->i_duration-p_input->i_time)/(60000000))%60),
-                     (int)(((p_item->i_duration-p_input->i_time)/1000000)%60) );
+                     (int)( ( i_duration - i_time ) / 3600000000 ),
+                     (int)( ( ( i_duration - i_time ) / 60000000 ) % 60 ),
+                     (int)( ( ( i_duration - i_time ) / 1000000 ) % 60 ) );
                     }
                     else
                     {
-                        sprintf( buf, "--:--:--" );
+                        sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
                     }
-                    INSERT_STRING( 1, buf );
+                    INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'N':
-                    INSERT_STRING( p_item, p_item->psz_name );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetName( p_item ) );
+                    }
                     break;
                 case 'O':
                 {
-                    char *lang;
+                    char *lang = NULL;
                     if( p_input )
-                    {
-                        lang = var_GetString( p_input, "audio-language" );
-                    }
-                    else
-                    {
-                        lang = strdup( "-" );
-                    }
-                    INSERT_STRING( 1, lang );
-                    free( lang );
+                        lang = var_GetNonEmptyString( p_input,
+                                                      "audio-language" );
+                    if( lang == NULL )
+                        lang = strdup( b_empty_if_na ? "" : "-" );
+                    INSERT_STRING( lang );
                     break;
                 }
                 case 'P':
@@ -840,9 +871,9 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
                     }
                     else
                     {
-                        sprintf( buf, "--.-%%" );
+                        sprintf( buf, b_empty_if_na ? "" : "--.-%%" );
                     }
-                    INSERT_STRING( 1, buf );
+                    INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'R':
                     if( p_input )
@@ -852,9 +883,9 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
                     }
                     else
                     {
-                        sprintf( buf, "-" );
+                        sprintf( buf, b_empty_if_na ? "" : "-" );
                     }
-                    INSERT_STRING( 1, buf );
+                    INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'S':
                     if( p_input )
@@ -864,67 +895,71 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
                     }
                     else
                     {
-                        sprintf( buf, "-" );
+                        sprintf( buf, b_empty_if_na ? "" : "-" );
                     }
-                    INSERT_STRING( 1, buf );
+                    INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'T':
                     if( p_input )
                     {
                         sprintf( buf, "%02d:%02d:%02d",
-                                 (int)(p_input->i_time/(3600000000)),
-                                 (int)((p_input->i_time/(60000000))%60),
-                                 (int)((p_input->i_time/1000000)%60) );
+                            (int)( p_input->i_time / ( 3600000000 ) ),
+                            (int)( ( p_input->i_time / ( 60000000 ) ) % 60 ),
+                            (int)( ( p_input->i_time / 1000000 ) % 60 ) );
                     }
                     else
                     {
-                        sprintf( buf, "--:--:--" );
+                        sprintf( buf, b_empty_if_na ? "" :  "--:--:--" );
                     }
-                    INSERT_STRING( 1, buf );
+                    INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'U':
-                    INSERT_STRING( p_item && p_item->p_meta,
-                                   p_item->p_meta->psz_publisher );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetPublisher( p_item ) );
+                    }
                     break;
                 case 'V':
                 {
                     audio_volume_t volume;
                     aout_VolumeGet( p_object, &volume );
                     snprintf( buf, 10, "%d", volume );
-                    INSERT_STRING( 1, buf );
+                    INSERT_STRING_NO_FREE( buf );
                     break;
                 }
                 case '_':
-                    *d = '\n';
+                    *(dst+d) = '\n';
                     d++;
                     break;
 
+                case ' ':
+                    b_empty_if_na = 1;
+                    break;
+
                 default:
-                    *d = *s;
+                    *(dst+d) = *s;
                     d++;
                     break;
             }
-            b_is_format = 0;
+            if( *s != ' ' )
+                b_is_format = 0;
         }
         else if( *s == '$' )
         {
             b_is_format = 1;
+            b_empty_if_na = 0;
         }
         else
         {
-            *d = *s;
+            *(dst+d) = *s;
             d++;
         }
         s++;
     }
-    *d = '\0';
+    *(dst+d) = '\0';
 
     if( p_input )
-    {
         vlc_object_release( p_input );
-        if( p_item )
-            vlc_mutex_unlock( &p_item->lock );
-    }
 
     return dst;
 }