]> git.sesse.net Git - vlc/blobdiff - src/text/strings.c
omxil-dr: reorient video
[vlc] / src / text / strings.c
index d5268d6bc0f502d519a8de917e1063bb39835682..55738a7f64eb33c0d29afbfecce767a7e1839c81 100644 (file)
 /* Needed by str_format_meta */
 #include <vlc_input.h>
 #include <vlc_meta.h>
-#include <vlc_playlist.h>
-#include <vlc_aout_intf.h>
+#include <vlc_aout.h>
 
 #include <vlc_strings.h>
-#include <vlc_url.h>
 #include <vlc_charset.h>
-#include <vlc_fs.h>
 #include <libvlc.h>
 #include <errno.h>
 
-/**
- * Decode encoded URI component. See also decode_URI().
- * \return decoded duplicated string
- */
-char *decode_URI_duplicate( const char *psz )
-{
-    char *psz_dup = strdup( psz );
-    decode_URI( psz_dup );
-    return psz_dup;
-}
-
-/**
- * Decode an encoded URI component in place.
- * <b>This function does NOT decode entire URIs.</b>
- * It decodes components (e.g. host name, directory, file name).
- * Decoded URIs do not exist in the real world (see RFC3986 §2.4).
- * Complete URIs are always "encoded" (or they are syntaxically invalid).
- *
- * Note that URI encoding is different from Javascript escaping. Especially,
- * white spaces and Unicode non-ASCII code points are encoded differently.
- *
- * \return psz on success, NULL if it was not properly encoded
- */
-char *decode_URI( char *psz )
-{
-    unsigned char *in = (unsigned char *)psz, *out = in, c;
-
-    if( psz == NULL )
-        return NULL;
-
-    while( ( c = *in++ ) != '\0' )
-    {
-        switch( c )
-        {
-            case '%':
-            {
-                char hex[3];
-
-                if( ( ( hex[0] = *in++ ) == 0 )
-                 || ( ( hex[1] = *in++ ) == 0 ) )
-                    return NULL;
-
-                hex[2] = '\0';
-                *out++ = (unsigned char)strtoul( hex, NULL, 0x10 );
-                break;
-            }
-
-            default:
-                /* Inserting non-ASCII or non-printable characters is unsafe,
-                 * and no sane browser will send these unencoded */
-                if( ( c < 32 ) || ( c > 127 ) )
-                    *out++ = '?';
-                else
-                    *out++ = c;
-        }
-    }
-    *out = '\0';
-    return psz;
-}
-
-static inline bool isurisafe( int c )
-{
-    /* These are the _unreserved_ URI characters (RFC3986 §2.3) */
-    return ( (unsigned char)( c - 'a' ) < 26 )
-            || ( (unsigned char)( c - 'A' ) < 26 )
-            || ( (unsigned char)( c - '0' ) < 10 )
-            || ( strchr( "-._~", c ) != NULL );
-}
-
-static char *encode_URI_bytes (const char *psz_uri, size_t len)
-{
-    char *psz_enc = malloc (3 * len + 1), *out = psz_enc;
-    if (psz_enc == NULL)
-        return NULL;
-
-    for (size_t i = 0; i < len; i++)
-    {
-        static const char hex[16] = "0123456789ABCDEF";
-        uint8_t c = *psz_uri;
-
-        if( isurisafe( c ) )
-            *out++ = c;
-        /* This is URI encoding, not HTTP forms:
-         * Space is encoded as '%20', not '+'. */
-        else
-        {
-            *out++ = '%';
-            *out++ = hex[c >> 4];
-            *out++ = hex[c & 0xf];
-        }
-        psz_uri++;
-    }
-    *out++ = '\0';
-
-    out = realloc (psz_enc, out - psz_enc);
-    return out ? out : psz_enc; /* realloc() can fail (safe) */
-}
-
-/**
- * Encodes a URI component (RFC3986 §2).
- *
- * @param psz_uri nul-terminated UTF-8 representation of the component.
- * Obviously, you can't pass a URI containing a nul character, but you don't
- * want to do that, do you?
- *
- * @return encoded string (must be free()'d), or NULL for ENOMEM.
- */
-char *encode_URI_component( const char *psz_uri )
-{
-    return encode_URI_bytes (psz_uri, strlen (psz_uri));
-}
-
-
 static const struct xml_entity_s
 {
     char    psz_entity[8];
@@ -529,7 +413,7 @@ size_t vlc_b64_decode_binary_to_buffer( uint8_t *p_dst, size_t i_dst, const char
     {
         const int c = b64[(unsigned int)*p];
         if( c == -1 )
-            continue;
+            break;
 
         switch( i_level )
         {
@@ -607,6 +491,7 @@ char *str_format_time( const char *tformat )
             char *ret = realloc (str, len + 1);
             return ret ? ret : str; /* <- this cannot fail */
         }
+        free (str);
     }
     assert (0);
 }
@@ -626,7 +511,7 @@ static void format_duration (char *buf, size_t len, int64_t duration)
 #define INSERT_STRING( string )                                     \
                     if( string != NULL )                            \
                     {                                               \
-                        int len = strlen( string );                 \
+                        size_t len = strlen( string );              \
                         dst = xrealloc( dst, i_size = i_size + len );\
                         memcpy( (dst+d), string, len );             \
                         d += len;                                   \
@@ -636,29 +521,24 @@ static void format_duration (char *buf, size_t len, int64_t duration)
 /* same than INSERT_STRING, except that string won't be freed */
 #define INSERT_STRING_NO_FREE( string )                             \
                     {                                               \
-                        int len = strlen( string );                 \
+                        size_t len = strlen( string );              \
                         dst = xrealloc( dst, i_size = i_size + len );\
                         memcpy( dst+d, string, len );               \
                         d += len;                                   \
                     }
-#undef str_format_meta
-char *str_format_meta( vlc_object_t *p_object, const char *string )
+char *str_format_meta( input_thread_t *p_input, const char *s )
 {
-    const char *s = string;
+    char *dst = strdup( s );
+    if( unlikely(dst == NULL) )
+        return NULL;
+
+    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_empty_if_na = false;
     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( pl_Get(p_object) );
-    input_item_t *p_item = NULL;
-    if( p_input )
-    {
-        p_item = input_GetItem(p_input);
-    }
 
     while( *s )
     {
@@ -668,33 +548,23 @@ char *str_format_meta( vlc_object_t *p_object, const char *string )
             {
                 case 'a':
                     if( p_item )
-                    {
                         INSERT_STRING( input_item_GetArtist( p_item ) );
-                    }
                     break;
                 case 'b':
                     if( p_item )
-                    {
                         INSERT_STRING( input_item_GetAlbum( p_item ) );
-                    }
                     break;
                 case 'c':
                     if( p_item )
-                    {
                         INSERT_STRING( input_item_GetCopyright( p_item ) );
-                    }
                     break;
                 case 'd':
                     if( p_item )
-                    {
                         INSERT_STRING( input_item_GetDescription( p_item ) );
-                    }
                     break;
                 case 'e':
                     if( p_item )
-                    {
                         INSERT_STRING( input_item_GetEncodedBy( p_item ) );
-                    }
                     break;
                 case 'f':
                     if( p_item && p_item->p_stats )
@@ -710,33 +580,23 @@ char *str_format_meta( vlc_object_t *p_object, const char *string )
                     break;
                 case 'g':
                     if( p_item )
-                    {
                         INSERT_STRING( input_item_GetGenre( p_item ) );
-                    }
                     break;
                 case 'l':
                     if( p_item )
-                    {
                         INSERT_STRING( input_item_GetLanguage( p_item ) );
-                    }
                     break;
                 case 'n':
                     if( p_item )
-                    {
                         INSERT_STRING( input_item_GetTrackNum( p_item ) );
-                    }
                     break;
                 case 'p':
                     if( p_item )
-                    {
                         INSERT_STRING( input_item_GetNowPlaying( p_item ) );
-                    }
                     break;
                 case 'r':
                     if( p_item )
-                    {
                         INSERT_STRING( input_item_GetRating( p_item ) );
-                    }
                     break;
                 case 's':
                     {
@@ -750,38 +610,28 @@ char *str_format_meta( vlc_object_t *p_object, const char *string )
                     }
                 case 't':
                     if( p_item )
-                    {
                         INSERT_STRING( input_item_GetTitle( p_item ) );
-                    }
                     break;
                 case 'u':
                     if( p_item )
-                    {
                         INSERT_STRING( input_item_GetURL( p_item ) );
-                    }
                     break;
                 case 'A':
                     if( p_item )
-                    {
                         INSERT_STRING( input_item_GetDate( p_item ) );
-                    }
                     break;
                 case 'B':
                     if( p_input )
-                    {
                         snprintf( buf, 10, "%"PRId64,
                                   var_GetInteger( p_input, "bit-rate" )/1000 );
-                    }
                     else
                         strcpy( buf, b_empty_if_na ? "" : "-" );
                     INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'C':
                     if( p_input )
-                    {
                         snprintf( buf, 10, "%"PRId64,
                                   var_GetInteger( p_input, "chapter" ) );
-                    }
                     else
                         strcpy( buf, b_empty_if_na ? "" : "-" );
                     INSERT_STRING_NO_FREE( buf );
@@ -798,16 +648,12 @@ char *str_format_meta( vlc_object_t *p_object, const char *string )
                     break;
                 case 'F':
                     if( p_item )
-                    {
                         INSERT_STRING( input_item_GetURI( p_item ) );
-                    }
                     break;
                 case 'I':
                     if( p_input )
-                    {
                         snprintf( buf, 10, "%"PRId64,
                                   var_GetInteger( p_input, "title" ) );
-                    }
                     else
                         strcpy( buf, b_empty_if_na ? "" : "-" );
                     INSERT_STRING_NO_FREE( buf );
@@ -826,31 +672,25 @@ char *str_format_meta( vlc_object_t *p_object, const char *string )
                     break;
                 case 'N':
                     if( p_item )
-                    {
                         INSERT_STRING( input_item_GetName( p_item ) );
-                    }
                     break;
                 case 'O':
-                    {
-                        char *lang = NULL;
-                        if( p_input )
-                            lang = var_GetNonEmptyString( p_input,
-                                                          "audio-language" );
-                        if( lang == NULL )
-                            lang = strdup( b_empty_if_na ? "" : "-" );
-                        INSERT_STRING( lang );
-                        break;
-                    }
+                {
+                    char *lang = NULL;
+                    if( p_input )
+                        lang = var_GetNonEmptyString( p_input,
+                                                      "audio-language" );
+                    if( lang == NULL )
+                        lang = strdup( b_empty_if_na ? "" : "-" );
+                    INSERT_STRING( lang );
+                    break;
+                }
                 case 'P':
                     if( p_input )
-                    {
                         snprintf( buf, 10, "%2.1lf",
                                   var_GetFloat( p_input, "position" ) * 100. );
-                    }
                     else
-                    {
                         snprintf( buf, 10, b_empty_if_na ? "" : "--.-%%" );
-                    }
                     INSERT_STRING_NO_FREE( buf );
                     break;
                 case 'R':
@@ -885,17 +725,24 @@ char *str_format_meta( vlc_object_t *p_object, const char *string )
                     break;
                 case 'U':
                     if( p_item )
-                    {
                         INSERT_STRING( input_item_GetPublisher( p_item ) );
-                    }
                     break;
                 case 'V':
                 {
-                    float vol = aout_VolumeGet( p_object );
-                    if( vol >= 0. )
+                    float vol = 0.f;
+
+                    if( p_input )
+                    {
+                        audio_output_t *aout = input_GetAout( p_input );
+                        if( aout )
+                        {
+                            vol = aout_VolumeGet( aout );
+                            vlc_object_release( aout );
+                        }
+                    }
+                    if( vol >= 0.f )
                     {
-                        snprintf( buf, 10, "%ld",
-                                  lroundf(vol * AOUT_VOLUME_DEFAULT ) );
+                        snprintf( buf, 10, "%ld", lroundf(vol * 256.f) );
                         INSERT_STRING_NO_FREE( buf );
                     }
                     else
@@ -910,17 +757,17 @@ char *str_format_meta( vlc_object_t *p_object, const char *string )
                     if( p_item )
                     {
                         char *psz_now_playing = input_item_GetNowPlaying( p_item );
-                        if ( psz_now_playing == NULL )
+                        if( EMPTY_STR( psz_now_playing ) )
                         {
                             char *psz_temp = input_item_GetTitleFbName( p_item );
                             char *psz_artist = input_item_GetArtist( p_item );
-                            if( !EMPTY_STR( psz_temp ) )
+                            if( !EMPTY_STR( psz_artist ) )
                             {
-                                INSERT_STRING( psz_temp );
-                                if ( !EMPTY_STR( psz_artist ) )
+                                INSERT_STRING( psz_artist );
+                                if ( !EMPTY_STR( psz_temp ) )
                                     INSERT_STRING_NO_FREE( " - " );
                             }
-                            INSERT_STRING( psz_artist );
+                            INSERT_STRING( psz_temp );
                         }
                         else
                             INSERT_STRING( psz_now_playing );
@@ -953,27 +800,11 @@ char *str_format_meta( vlc_object_t *p_object, const char *string )
     }
     *(dst+d) = '\0';
 
-    if( p_input )
-        vlc_object_release( p_input );
-
     return dst;
 }
 #undef INSERT_STRING
 #undef INSERT_STRING_NO_FREE
 
-#undef str_format
-/**
- * Apply str format time and str format meta
- */
-char *str_format( vlc_object_t *p_this, const char *psz_src )
-{
-    char *psz_buf1, *psz_buf2;
-    psz_buf1 = str_format_time( psz_src );
-    psz_buf2 = str_format_meta( p_this, psz_buf1 );
-    free( psz_buf1 );
-    return psz_buf2;
-}
-
 /**
  * Remove forbidden, potentially forbidden and otherwise evil characters from
  * filenames. This includes slashes, and popular characters like colon
@@ -1036,7 +867,7 @@ void filename_sanitize( char *str )
  */
 void path_sanitize( char *str )
 {
-#if defined( WIN32 ) || defined( __OS2__ )
+#if defined( _WIN32 ) || defined( __OS2__ )
     /* check drive prefix if path is absolute */
     if( (((unsigned char)(str[0] - 'A') < 26)
       || ((unsigned char)(str[0] - 'a') < 26)) && (':' == str[1]) )
@@ -1047,7 +878,7 @@ void path_sanitize( char *str )
 #if defined( __APPLE__ )
         if( *str == ':' )
             *str = '_';
-#elif defined( WIN32 ) || defined( __OS2__ )
+#elif defined( _WIN32 ) || defined( __OS2__ )
         if( strchr( "*\"?:|<>", *str ) )
             *str = '_';
         if( *str == '/' )
@@ -1057,235 +888,6 @@ void path_sanitize( char *str )
     }
 }
 
-#include <vlc_url.h>
-#ifdef WIN32
-# include <io.h>
-#endif
-
-/**
- * Convert a file path to a URI.
- * If already a URI, return a copy of the string.
- * @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, const char *scheme)
-{
-    if (path == NULL)
-        return NULL;
-    if (scheme == NULL && !strcmp (path, "-"))
-        return strdup ("fd://0"); // standard input
-    if (strstr (path, "://") != NULL)
-        return strdup (path); /* Already a URI */
-    /* Note: VLC cannot handle URI schemes without double slash after the
-     * scheme name (such as mailto: or news:). */
-
-    char *buf;
-
-#ifdef __OS2__
-    char p[strlen (path) + 1];
-
-    for (buf = p; *path; buf++, path++)
-        *buf = (*path == '/') ? DIR_SEP_CHAR : *path;
-    *buf = '\0';
-
-    path = p;
-#endif
-
-#if defined( WIN32 ) || defined( __OS2__ )
-    /* Drive letter */
-    if (isalpha ((unsigned char)path[0]) && (path[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 */
-#if !defined( WIN32 ) && !defined( __OS2__ )
-        if (scheme != NULL)
-            return NULL; /* remote files not supported */
-
-        /* \\host\share\path -> smb://host/share/path */
-        if (strchr (path + 2, '\\') != NULL)
-        {   /* Convert backslashes to slashes */
-            char *dup = strdup (path);
-            if (dup == NULL)
-                return NULL;
-            for (size_t i = 2; dup[i]; i++)
-                if (dup[i] == '\\')
-                    dup[i] = DIR_SEP_CHAR;
-
-            char *ret = make_URI (dup, scheme);
-            free (dup);
-            return ret;
-        }
-# define SMB_SCHEME "smb"
-#else
-        /* \\host\share\path -> file://host/share/path */
-# define SMB_SCHEME "file"
-#endif
-        size_t hostlen = strcspn (path + 2, DIR_SEP);
-
-        buf = malloc (sizeof (SMB_SCHEME) + 3 + hostlen);
-        if (buf != NULL)
-            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)
-    {   /* Relative path: prepend the current working directory */
-        char *cwd, *ret;
-
-        if ((cwd = vlc_getcwd ()) == NULL)
-            return NULL;
-        if (asprintf (&buf, "%s"DIR_SEP"%s", cwd, path) == -1)
-            buf = NULL;
-
-        free (cwd);
-        ret = (buf != NULL) ? make_URI (buf, scheme) : NULL;
-        free (buf);
-        return ret;
-    }
-    else
-    if (asprintf (&buf, "%s://", scheme ? scheme : "file") == -1)
-        buf = NULL;
-    if (buf == NULL)
-        return NULL;
-
-    assert (path[0] == DIR_SEP_CHAR);
-
-    /* Absolute file path */
-    for (const char *ptr = path + 1;; ptr++)
-    {
-        size_t len = strcspn (ptr, DIR_SEP);
-        char *component = encode_URI_bytes (ptr, len);
-        if (component == NULL)
-        {
-            free (buf);
-            return NULL;
-        }
-        char *uri;
-        int val = asprintf (&uri, "%s/%s", buf, component);
-        free (component);
-        free (buf);
-        if (val == -1)
-            return NULL;
-        buf = uri;
-        ptr += len;
-        if (*ptr == '\0')
-            return buf;
-    }
-}
-
-/**
- * Tries to convert a URI to a local (UTF-8-encoded) file path.
- * @param url URI to convert
- * @return NULL on error, a nul-terminated string otherwise
- * (use free() to release it)
- */
-char *make_path (const char *url)
-{
-    char *ret = NULL;
-    char *end;
-
-    char *path = strstr (url, "://");
-    if (path == NULL)
-        return NULL; /* unsupported scheme or invalid syntax */
-
-    end = memchr (url, '/', path - url);
-    size_t schemelen = ((end != NULL) ? end : path) - url;
-    path += 3; /* skip "://" */
-
-    /* Remove HTML anchor if present */
-    end = strchr (path, '#');
-    if (end)
-        path = strndup (path, end - path);
-    else
-        path = strdup (path);
-    if (unlikely(path == NULL))
-        return NULL; /* boom! */
-
-    /* Decode path */
-    decode_URI (path);
-
-    if (schemelen == 4 && !strncasecmp (url, "file", 4))
-    {
-#if (!defined (WIN32) && !defined (__OS2__)) || defined (UNDER_CE)
-        /* Leading slash => local path */
-        if (*path == '/')
-            return path;
-        /* Local path disguised as a remote one */
-        if (!strncasecmp (path, "localhost/", 10))
-            return memmove (path, path + 9, strlen (path + 9) + 1);
-#else
-        /* cannot start with a space */
-        if (*path == ' ')
-            goto out;
-        for (char *p = strchr (path, '/'); p; p = strchr (p + 1, '/'))
-            *p = '\\';
-
-        /* Leading backslash => local path */
-        if (*path == '\\')
-            return memmove (path, path + 1, strlen (path + 1) + 1);
-        /* Local path disguised as a remote one */
-        if (!strncasecmp (path, "localhost\\", 10))
-            return memmove (path, path + 10, strlen (path + 10) + 1);
-        /* UNC path */
-        if (*path && asprintf (&ret, "\\\\%s", path) == -1)
-            ret = NULL;
-#endif
-        /* non-local path :-( */
-    }
-    else
-    if (schemelen == 2 && !strncasecmp (url, "fd", 2))
-    {
-        int fd = strtol (path, &end, 0);
-
-        if (*end)
-            goto out;
-
-#if !defined( WIN32 ) && !defined( __OS2__ )
-        switch (fd)
-        {
-            case 0:
-                ret = strdup ("/dev/stdin");
-                break;
-            case 1:
-                ret = strdup ("/dev/stdout");
-                break;
-            case 2:
-                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
-            ret = NULL;
-#endif
-    }
-
-out:
-    free (path);
-    return ret; /* unknown scheme */
-}
-
 /*
   Decodes a duration as defined by ISO 8601
   http://en.wikipedia.org/wiki/ISO_8601#Durations