]> git.sesse.net Git - vlc/blobdiff - src/text/strings.c
playlist: Make sure we don't pl_Release(p_playlist).
[vlc] / src / text / strings.c
index 750d565109777fe8bc17bf80d31e19b42ed6b466..5d1395782375435cf10a18dedb84537c235b7578 100644 (file)
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
 #include <assert.h>
 
 /* Needed by str_format_time */
@@ -555,7 +559,7 @@ size_t vlc_b64_decode_binary_to_buffer( uint8_t *p_dst, size_t i_dst, const char
     int i_level;
     int i_last;
 
-    for( i_level = 0, i_last = 0; i_dst > 0 && *p != '\0'; i_dst--, p++ )
+    for( i_level = 0, i_last = 0; (size_t)( p_dst - p_start ) < i_dst && *p != '\0'; p++ )
     {
         const int c = b64[(unsigned int)*p];
         if( c == -1 )
@@ -614,136 +618,165 @@ char *str_format_time( const char *tformat )
 {
     char buffer[255];
     time_t curtime;
-#if defined(HAVE_LOCALTIME_R)
     struct tm loctime;
-#else
-    struct tm *loctime;
-#endif
 
     /* Get the current time.  */
     curtime = time( NULL );
 
     /* Convert it to local time representation.  */
-#if defined(HAVE_LOCALTIME_R)
     localtime_r( &curtime, &loctime );
     strftime( buffer, 255, tformat, &loctime );
-#else
-    loctime = localtime( &curtime );
-    strftime( buffer, 255, tformat, loctime );
-#endif
     return strdup( buffer );
 }
 
-#define INSERT_STRING( check, string )                              \
-                    if( check )                                     \
+#define INSERT_STRING( string )                                     \
+                    if( string != NULL )                            \
                     {                                               \
-                        psz_meta = string;                          \
-                        if( psz_meta )                              \
-                        {                                           \
-                            int len = strlen( string );             \
-                            dst = realloc( dst,                     \
-                                   i_size = i_size + len + 1 );     \
-                            strncpy( d, psz_meta, len+1 );          \
-                            d += len;                               \
-                        }                                           \
-                        else                                        \
-                        {                                           \
-                                *d = '-';                           \
-                                d++;                                \
-                        }                                           \
-                    }
+                        int len = strlen( string );                 \
+                        dst = realloc( dst, i_size = i_size + len );\
+                        memcpy( (dst+d), string, len );             \
+                        d += len;                                   \
+                        free( string );                             \
+                    }                                               \
+                    else                                            \
+                    {                                               \
+                        *(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 + 1 );     \
-                            strncpy( d, string, len+1 );            \
-                            d += len;                               \
-                            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 = strdup( string );
+    if( !dst ) return NULL;
+    int d = 0;
 
     playlist_t *p_playlist = pl_Yield( p_object );
-    input_thread_t *p_input = p_playlist->p_input;
+    input_thread_t *p_input = playlist_CurrentInput( p_playlist );
     input_item_t *p_item = NULL;
     pl_Release( p_object );
     if( p_input )
     {
-        vlc_object_yield( p_input );
         p_item = input_GetItem(p_input);
     }
 
-    sprintf( dst, string );
-
     while( *s )
     {
         if( b_is_format )
         {
             switch( *s )
             {
-                char *psz_meta; /* used by INSERT_STRING */
                 case 'a':
-                    INSERT_STRING( p_item, input_item_GetArtist(p_item) );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetArtist( p_item ) );
+                    }
                     break;
                 case 'b':
-                    INSERT_STRING( p_item, input_item_GetAlbum(p_item) );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetAlbum( p_item ) );
+                    }
                     break;
                 case 'c':
-                    INSERT_STRING( p_item, input_item_GetCopyright(p_item) );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetCopyright( p_item ) );
+                    }
                     break;
                 case 'd':
-                    INSERT_STRING( p_item, input_item_GetDescription(p_item) );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetDescription( p_item ) );
+                    }
                     break;
                 case 'e':
-                    INSERT_STRING( p_item, input_item_GetEncodedBy(p_item) );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetEncodedBy( p_item ) );
+                    }
+                    break;
+                case 'f':
+                    if( p_item && p_item->p_stats )
+                    {
+                        snprintf( buf, 10, "%d",
+                                  p_item->p_stats->i_displayed_pictures );
+                    }
+                    else
+                    {
+                        sprintf( buf, b_empty_if_na ? "" : "-" );
+                    }
+                    INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'g':
-                    INSERT_STRING( p_item, input_item_GetGenre(p_item) );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetGenre( p_item ) );
+                    }
                     break;
                 case 'l':
-                    INSERT_STRING( p_item, input_item_GetLanguage(p_item) );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetLanguage( p_item ) );
+                    }
                     break;
                 case 'n':
-                    INSERT_STRING( p_item, input_item_GetTrackNum(p_item) );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetTrackNum( p_item ) );
+                    }
                     break;
                 case 'p':
-                    INSERT_STRING( p_item, input_item_GetNowPlaying(p_item) );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetNowPlaying( p_item ) );
+                    }
                     break;
                 case 'r':
-                    INSERT_STRING( p_item, input_item_GetRating(p_item) );
+                    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 = var_GetNonEmptyString( p_input, "sub-language" );
+                    if( lang == NULL )
                         lang = strdup( b_empty_if_na ? "" : "-" );
-                    }
-                    INSERT_STRING( 1, lang );
+                    INSERT_STRING( lang );
                     break;
                 }
                 case 't':
-                    INSERT_STRING( p_item, input_item_GetTitle(p_item) );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetTitle( p_item ) );
+                    }
                     break;
                 case 'u':
-                    INSERT_STRING( p_item, input_item_GetURL(p_item) );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetURL( p_item ) );
+                    }
                     break;
                 case 'A':
-                    INSERT_STRING( p_item, input_item_GetDate(p_item) );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetDate( p_item ) );
+                    }
                     break;
                 case 'B':
                     if( p_input )
@@ -785,7 +818,10 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
                     INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'F':
-                    INSERT_STRING( p_item, input_item_GetURI( p_item ) );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetURI( p_item ) );
+                    }
                     break;
                 case 'I':
                     if( p_input )
@@ -816,20 +852,20 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
                     INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'N':
-                    INSERT_STRING( p_item, input_item_GetName( p_item ) );
+                    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 = var_GetNonEmptyString( p_input,
+                                                      "audio-language" );
+                    if( lang == NULL )
                         lang = strdup( b_empty_if_na ? "" : "-" );
-                    }
-                    INSERT_STRING( 1, lang );
+                    INSERT_STRING( lang );
                     break;
                 }
                 case 'P':
@@ -883,7 +919,10 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
                     INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'U':
-                    INSERT_STRING( p_item, input_item_GetPublisher(p_item) );
+                    if( p_item )
+                    {
+                        INSERT_STRING( input_item_GetPublisher( p_item ) );
+                    }
                     break;
                 case 'V':
                 {
@@ -894,7 +933,7 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
                     break;
                 }
                 case '_':
-                    *d = '\n';
+                    *(dst+d) = '\n';
                     d++;
                     break;
 
@@ -903,7 +942,7 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
                     break;
 
                 default:
-                    *d = *s;
+                    *(dst+d) = *s;
                     d++;
                     break;
             }
@@ -917,12 +956,12 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
         }
         else
         {
-            *d = *s;
+            *(dst+d) = *s;
             d++;
         }
         s++;
     }
-    *d = '\0';
+    *(dst+d) = '\0';
 
     if( p_input )
         vlc_object_release( p_input );
@@ -962,7 +1001,9 @@ void filename_sanitize( char *str )
         switch( *str )
         {
             case '/':
-#ifdef WIN32
+#if defined( __APPLE__ )
+            case ':':
+#elif defined( WIN32 )
             case '\\':
             case '*':
             case '"':
@@ -997,7 +1038,10 @@ void path_sanitize( char *str )
 #endif
     while( *str )
     {
-#ifdef WIN32
+#if defined( __APPLE__ )
+        if( *str == ':' )
+            *str = '_';
+#elif defined( WIN32 )
         switch( *str )
         {
             case '*':