]> git.sesse.net Git - vlc/blobdiff - src/text/strings.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / text / strings.c
index c09056403b88046e725fa26c8f0aaebc8d6b156f..096f72df58709d9487d07524361dbf7d393b7fc9 100644 (file)
@@ -95,10 +95,6 @@ char *decode_URI( char *psz )
                 break;
             }
 
-            case '+': /* This is HTTP forms, not URI decoding... */
-                *out++ = ' ';
-                break;
-
             default:
                 /* Inserting non-ASCII or non-printable characters is unsafe,
                  * and no sane browser will send these unencoded */
@@ -109,7 +105,6 @@ char *decode_URI( char *psz )
         }
     }
     *out = '\0';
-    EnsureUTF8( psz );
     return psz;
 }
 
@@ -602,6 +597,18 @@ char *str_format_time( const char *tformat )
     assert (0);
 }
 
+static void format_duration (char *buf, size_t len, int64_t duration)
+{
+    lldiv_t d;
+    int sec;
+
+    duration /= CLOCK_FREQ;
+    d = lldiv (duration, 60);
+    sec = d.rem;
+    d = lldiv (d.quot, 60);
+    snprintf (buf, len, "%02lld:%02d:%02d", d.quot, (int)d.rem, sec);
+}
+
 #define INSERT_STRING( string )                                     \
                     if( string != NULL )                            \
                     {                                               \
@@ -637,10 +644,8 @@ char *str_format_meta( vlc_object_t *p_object, const char *string )
     if( !dst ) return NULL;
     int d = 0;
 
-    playlist_t *p_playlist = pl_Hold( p_object );
-    input_thread_t *p_input = playlist_CurrentInput( p_playlist );
+    input_thread_t *p_input = playlist_CurrentInput( pl_Get(p_object) );
     input_item_t *p_item = NULL;
-    pl_Release( p_object );
     if( p_input )
     {
         p_item = input_GetItem(p_input);
@@ -686,14 +691,12 @@ char *str_format_meta( vlc_object_t *p_object, const char *string )
                     if( p_item && p_item->p_stats )
                     {
                         vlc_mutex_lock( &p_item->p_stats->lock );
-                        snprintf( buf, 10, "%d",
+                        snprintf( buf, 10, "%"PRIi64,
                                   p_item->p_stats->i_displayed_pictures );
                         vlc_mutex_unlock( &p_item->p_stats->lock );
                     }
                     else
-                    {
-                        sprintf( buf, b_empty_if_na ? "" : "-" );
-                    }
+                        strcpy( buf, b_empty_if_na ? "" : "-" );
                     INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'g':
@@ -757,40 +760,31 @@ char *str_format_meta( vlc_object_t *p_object, const char *string )
                 case 'B':
                     if( p_input )
                     {
-                        snprintf( buf, 10, "%d",
+                        snprintf( buf, 10, "%"PRId64,
                                   var_GetInteger( p_input, "bit-rate" )/1000 );
                     }
                     else
-                    {
-                        sprintf( buf, b_empty_if_na ? "" : "-" );
-                    }
+                        strcpy( buf, b_empty_if_na ? "" : "-" );
                     INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'C':
                     if( p_input )
                     {
-                        snprintf( buf, 10, "%d",
+                        snprintf( buf, 10, "%"PRId64,
                                   var_GetInteger( p_input, "chapter" ) );
                     }
                     else
-                    {
-                        sprintf( buf, b_empty_if_na ? "" : "-" );
-                    }
+                        strcpy( buf, b_empty_if_na ? "" : "-" );
                     INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'D':
                     if( p_item )
                     {
                         mtime_t i_duration = input_item_GetDuration( p_item );
-                        snprintf( buf, 10, "%02d:%02d:%02d",
-                                 (int)(i_duration/(3600000000)),
-                                 (int)((i_duration/(60000000))%60),
-                                 (int)((i_duration/1000000)%60) );
+                        format_duration (buf, sizeof (buf), i_duration);
                     }
                     else
-                    {
-                        snprintf( buf, 10, b_empty_if_na ? "" : "--:--:--" );
-                    }
+                        strcpy( buf, b_empty_if_na ? "" : "--:--:--" );
                     INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'F':
@@ -802,13 +796,11 @@ char *str_format_meta( vlc_object_t *p_object, const char *string )
                 case 'I':
                     if( p_input )
                     {
-                        snprintf( buf, 10, "%d",
+                        snprintf( buf, 10, "%"PRId64,
                                   var_GetInteger( p_input, "title" ) );
                     }
                     else
-                    {
-                        sprintf( buf, b_empty_if_na ? "" : "-" );
-                    }
+                        strcpy( buf, b_empty_if_na ? "" : "-" );
                     INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'L':
@@ -816,15 +808,11 @@ char *str_format_meta( vlc_object_t *p_object, const char *string )
                     {
                         mtime_t i_duration = input_item_GetDuration( p_item );
                         int64_t i_time = var_GetTime( p_input, "time" );
-                        snprintf( buf, 10, "%02d:%02d:%02d",
-                     (int)( ( i_duration - i_time ) / 3600000000 ),
-                     (int)( ( ( i_duration - i_time ) / 60000000 ) % 60 ),
-                     (int)( ( ( i_duration - i_time ) / 1000000 ) % 60 ) );
+                        format_duration( buf, sizeof(buf),
+                                         i_duration - i_time );
                     }
                     else
-                    {
-                        snprintf( buf, 10, b_empty_if_na ? "" : "--:--:--" );
-                    }
+                        strcpy( buf, b_empty_if_na ? "" : "--:--:--" );
                     INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'N':
@@ -863,9 +851,7 @@ char *str_format_meta( vlc_object_t *p_object, const char *string )
                         snprintf( buf, 10, "%.3f", f );
                     }
                     else
-                    {
-                        sprintf( buf, b_empty_if_na ? "" : "-" );
-                    }
+                        strcpy( buf, b_empty_if_na ? "" : "-" );
                     INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'S':
@@ -875,24 +861,17 @@ char *str_format_meta( vlc_object_t *p_object, const char *string )
                         snprintf( buf, 10, "%d.%d", r/1000, (r/100)%10 );
                     }
                     else
-                    {
-                        sprintf( buf, b_empty_if_na ? "" : "-" );
-                    }
+                        strcpy( buf, b_empty_if_na ? "" : "-" );
                     INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'T':
                     if( p_input )
                     {
                         int64_t i_time = var_GetTime( p_input, "time" );
-                        snprintf( buf, 10, "%02d:%02d:%02d",
-                            (int)( i_time / ( 3600000000 ) ),
-                            (int)( ( i_time / ( 60000000 ) ) % 60 ),
-                            (int)( ( i_time / 1000000 ) % 60 ) );
+                        format_duration( buf, sizeof(buf), i_time );
                     }
                     else
-                    {
-                        snprintf( buf, 10, b_empty_if_na ? "" :  "--:--:--" );
-                    }
+                        strcpy( buf, b_empty_if_na ? "" :  "--:--:--" );
                     INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'U':
@@ -964,10 +943,12 @@ char *str_format( vlc_object_t *p_this, const char *psz_src )
 /**
  * Remove forbidden characters from filenames (including slashes)
  */
-char* filename_sanitize( const char *str_origin )
+void filename_sanitize( char *str )
 {
-    char *str = strdup( str_origin );
+#if defined( WIN32 )
     char *str_base = str;
+#endif
+
     if( *str == '.' && (str[1] == '\0' || (str[1] == '.' && str[2] == '\0' ) ) )
     {
         while( *str )
@@ -975,7 +956,7 @@ char* filename_sanitize( const char *str_origin )
             *str = '_';
             str++;
         }
-        return str_base;
+        return;
     }
 
 #if defined( WIN32 )
@@ -1016,8 +997,6 @@ char* filename_sanitize( const char *str_origin )
         *str-- = '_';
     }
 #endif
-
-    return str_base;
 }
 
 /**
@@ -1047,19 +1026,23 @@ void path_sanitize( char *str )
 }
 
 #include <vlc_url.h>
+#ifdef WIN32
+# include <io.h>
+#endif
 
 /**
  * Convert a file path to an URI.
  * If already an URI, return a copy of the string.
- * @path path path to convert (or URI to copy)
+ * @param path path to convert (or URI to copy)
+ * @param scheme URI scheme to use (default is auto: "file", "fd" or "smb")
  * @return a nul-terminated URI string (use free() to release it),
  * or NULL in case of error
  */
-char *make_URI (const char *path)
+char *make_URI (const char *path, const char *scheme)
 {
     if (path == NULL)
         return NULL;
-    if (!strcmp (path, "-"))
+    if (scheme == NULL && !strcmp (path, "-"))
         return strdup ("fd://0"); // standard input
     if (strstr (path, "://") != NULL)
         return strdup (path); /* Already an URI */
@@ -1068,20 +1051,28 @@ char *make_URI (const char *path)
 
     char *buf;
 #ifdef WIN32
+    /* Drive letter */
     if (isalpha (path[0]) && (path[1] == ':'))
     {
-        if (asprintf (&buf, "file:///%c:", path[0]) == -1)
+        if (asprintf (&buf, "%s:///%c:", scheme ? scheme : "file",
+                      path[0]) == -1)
             buf = NULL;
         path += 2;
+# warning Drive letter-relative path not implemented!
+        if (path[0] != DIR_SEP_CHAR)
+            return NULL;
     }
     else
 #endif
     if (!strncmp (path, "\\\\", 2))
     {   /* Windows UNC paths */
 #ifndef WIN32
+        if (scheme != NULL)
+            return NULL; /* remote files not supported */
+
         /* \\host\share\path -> smb://host/share/path */
         if (strchr (path + 2, '\\') != NULL)
-        {   /* Convert antislashes to slashes */
+        {   /* Convert backslashes to slashes */
             char *dup = strdup (path);
             if (dup == NULL)
                 return NULL;
@@ -1089,7 +1080,7 @@ char *make_URI (const char *path)
                 if (dup[i] == '\\')
                     dup[i] = DIR_SEP_CHAR;
 
-            char *ret = make_URI (dup);
+            char *ret = make_URI (dup, scheme);
             free (dup);
             return ret;
         }
@@ -1105,6 +1096,9 @@ char *make_URI (const char *path)
             snprintf (buf, sizeof (SMB_SCHEME) + 3 + hostlen,
                       SMB_SCHEME"://%s", path + 2);
         path += 2 + hostlen;
+
+        if (path[0] == '\0')
+            return buf; /* Hostname without path */
     }
     else
     if (path[0] != DIR_SEP_CHAR)
@@ -1115,12 +1109,13 @@ char *make_URI (const char *path)
             return NULL;
         if (asprintf (&buf, "%s/%s", cwd, path) == -1)
             return NULL;
-        char *ret = make_URI (buf);
+        char *ret = make_URI (buf, scheme);
         free (buf);
         return ret;
     }
     else
-        buf = strdup ("file://");
+    if (asprintf (&buf, "%s://", scheme ? scheme : "file") == -1)
+        buf = NULL;
     if (buf == NULL)
         return NULL;
 
@@ -1183,18 +1178,20 @@ char *make_path (const char *url)
     if (schemelen == 4 && !strncasecmp (url, "file", 4))
     {
 #if (DIR_SEP_CHAR != '/')
-        for (char *p = strchr (path, '/'); p; p = strchr (p, '/'))
-            *p == DIR_SEP_CHAR;
+        for (char *p = strchr (path, '/'); p; p = strchr (p + 1, '/'))
+            *p = DIR_SEP_CHAR;
 #endif
+        /* Leading slash => local path */
         if (*path == DIR_SEP_CHAR)
+#if !defined (WIN32) || defined (UNDER_CE)
             return path;
+#else
+            return memmove (path, path + 1, strlen (path + 1) + 1);
+#endif
 
         /* Local path disguised as a remote one (MacOS X) */
         if (!strncasecmp (path, "localhost"DIR_SEP, 10))
-        {
-            memmove (path, path + 9, strlen (path + 9) + 1);
-            return path;
-        }
+            return memmove (path, path + 9, strlen (path + 9) + 1);
 
 #ifdef WIN32
         if (*path && asprintf (&ret, "\\\\%s", path) == -1)
@@ -1220,13 +1217,14 @@ char *make_path (const char *url)
                 ret = strdup ("/dev/stdout");
                 break;
             case 2:
-                ret = strdup ("/dev/strerr");
+                ret = strdup ("/dev/stderr");
                 break;
             default:
                 if (asprintf (&ret, "/dev/fd/%d", fd) == -1)
                     ret = NULL;
         }
 #else
+        /* XXX: Does this work on WinCE? */
         if (fd < 2)
             ret = strdup ("CON");
         else