gstreamer_core/gst/gsturi.c
branchRCL_3
changeset 30 7e817e7e631c
parent 29 567bb019e3e3
equal deleted inserted replaced
29:567bb019e3e3 30:7e817e7e631c
    62 
    62 
    63 
    63 
    64 GType
    64 GType
    65 gst_uri_handler_get_type (void)
    65 gst_uri_handler_get_type (void)
    66 {
    66 {
    67   static volatile gsize urihandler_type = 0;
    67   static GType urihandler_type = 0;
    68 
    68 
    69   if (g_once_init_enter (&urihandler_type)) {
    69   if (G_UNLIKELY (urihandler_type == 0)) {
    70     GType _type;
       
    71     static const GTypeInfo urihandler_info = {
    70     static const GTypeInfo urihandler_info = {
    72       sizeof (GstURIHandlerInterface),
    71       sizeof (GstURIHandlerInterface),
    73       gst_uri_handler_base_init,
    72       gst_uri_handler_base_init,
    74       NULL,
    73       NULL,
    75       NULL,
    74       NULL,
    79       0,
    78       0,
    80       NULL,
    79       NULL,
    81       NULL
    80       NULL
    82     };
    81     };
    83 
    82 
    84     _type = g_type_register_static (G_TYPE_INTERFACE,
    83     urihandler_type = g_type_register_static (G_TYPE_INTERFACE,
    85         "GstURIHandler", &urihandler_info, 0);
    84         "GstURIHandler", &urihandler_info, 0);
    86 
    85 
    87     GST_DEBUG_CATEGORY_INIT (gst_uri_handler_debug, "GST_URI", GST_DEBUG_BOLD,
    86     GST_DEBUG_CATEGORY_INIT (gst_uri_handler_debug, "GST_URI", GST_DEBUG_BOLD,
    88         "handling of URIs");
    87         "handling of URIs");
    89     g_once_init_leave (&urihandler_type, _type);
       
    90   }
    88   }
    91   return urihandler_type;
    89   return urihandler_type;
    92 }
    90 }
    93 
       
    94 static void
    91 static void
    95 gst_uri_handler_base_init (gpointer g_class)
    92 gst_uri_handler_base_init (gpointer g_class)
    96 {
    93 {
    97   static gboolean initialized = FALSE;
    94   static gboolean initialized = FALSE;
    98 
    95 
   240   first_digit = hex_to_int (*scanner++);
   237   first_digit = hex_to_int (*scanner++);
   241   if (first_digit < 0) {
   238   if (first_digit < 0) {
   242     return -1;
   239     return -1;
   243   }
   240   }
   244 
   241 
   245   second_digit = hex_to_int (*scanner);
   242   second_digit = hex_to_int (*scanner++);
   246   if (second_digit < 0) {
   243   if (second_digit < 0) {
   247     return -1;
   244     return -1;
   248   }
   245   }
   249 
   246 
   250   return (first_digit << 4) | second_digit;
   247   return (first_digit << 4) | second_digit;
   310   g_assert (uri != NULL);
   307   g_assert (uri != NULL);
   311   g_assert (endptr != NULL);
   308   g_assert (endptr != NULL);
   312 
   309 
   313   if (g_ascii_isalpha (*check)) {
   310   if (g_ascii_isalpha (*check)) {
   314     check++;
   311     check++;
   315     while (g_ascii_isalnum (*check) || *check == '+'
   312     while (g_ascii_isalnum (*check))
   316         || *check == '-' || *check == '.')
       
   317       check++;
   313       check++;
   318   }
   314   }
   319 
   315 
   320   *endptr = check;
   316   *endptr = check;
   321 }
   317 }
   323 /**
   319 /**
   324  * gst_uri_protocol_is_valid:
   320  * gst_uri_protocol_is_valid:
   325  * @protocol: A string
   321  * @protocol: A string
   326  *
   322  *
   327  * Tests if the given string is a valid protocol identifier. Protocols
   323  * Tests if the given string is a valid protocol identifier. Protocols
   328  * must consist of alphanumeric characters, '+', '-' and '.' and must
   324  * must consist of alphanumeric characters and not start with a number.
   329  * start with a alphabetic character. See RFC 3986 Section 3.1.
       
   330  *
   325  *
   331  * Returns: TRUE if the string is a valid protocol identifier, FALSE otherwise.
   326  * Returns: TRUE if the string is a valid protocol identifier, FALSE otherwise.
   332  */
   327  */
   333 #ifdef __SYMBIAN32__
   328 #ifdef __SYMBIAN32__
   334 EXPORT_C
   329 EXPORT_C
   349 /**
   344 /**
   350  * gst_uri_is_valid:
   345  * gst_uri_is_valid:
   351  * @uri: A URI string
   346  * @uri: A URI string
   352  *
   347  *
   353  * Tests if the given string is a valid URI identifier. URIs start with a valid
   348  * Tests if the given string is a valid URI identifier. URIs start with a valid
   354  * scheme followed by ":" and maybe a string identifying the location.
   349  * protocol followed by "://" and maybe a string identifying the location.
   355  *
   350  *
   356  * Returns: TRUE if the string is a valid URI
   351  * Returns: TRUE if the string is a valid URI
   357  */
   352  */
   358 #ifdef __SYMBIAN32__
   353 #ifdef __SYMBIAN32__
   359 EXPORT_C
   354 EXPORT_C
   366 
   361 
   367   g_return_val_if_fail (uri != NULL, FALSE);
   362   g_return_val_if_fail (uri != NULL, FALSE);
   368 
   363 
   369   gst_uri_protocol_check_internal (uri, &endptr);
   364   gst_uri_protocol_check_internal (uri, &endptr);
   370 
   365 
   371   return *endptr == ':';
   366   return (*endptr == ':' && *(endptr + 1) == '/' && *(endptr + 2) == '/');
   372 }
   367 }
   373 
   368 
   374 /**
   369 /**
   375  * gst_uri_get_protocol:
   370  * gst_uri_get_protocol:
   376  * @uri: A URI string
   371  * @uri: A URI string
   390   gchar *colon;
   385   gchar *colon;
   391 
   386 
   392   g_return_val_if_fail (uri != NULL, NULL);
   387   g_return_val_if_fail (uri != NULL, NULL);
   393   g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
   388   g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
   394 
   389 
   395   colon = strstr (uri, ":");
   390   colon = strstr (uri, "://");
   396 
   391 
   397   return g_ascii_strdown (uri, colon - uri);
   392   return g_strdown (g_strndup (uri, colon - uri));
   398 }
   393 }
   399 
   394 
   400 /**
   395 /**
   401  * gst_uri_has_protocol:
   396  * gst_uri_has_protocol:
   402  * @uri: an URI string
   397  * @uri: an URI string
   419 
   414 
   420   g_return_val_if_fail (uri != NULL, FALSE);
   415   g_return_val_if_fail (uri != NULL, FALSE);
   421   g_return_val_if_fail (protocol != NULL, FALSE);
   416   g_return_val_if_fail (protocol != NULL, FALSE);
   422   g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
   417   g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
   423 
   418 
   424   colon = strstr (uri, ":");
   419   colon = strstr (uri, "://");
   425 
   420 
   426   if (colon == NULL)
   421   if (colon == NULL)
   427     return FALSE;
   422     return FALSE;
   428 
   423 
   429   return (g_ascii_strncasecmp (uri, protocol, (gsize) (colon - uri)) == 0);
   424   return (strncmp (uri, protocol, (gsize) (colon - uri)) == 0);
   430 }
   425 }
   431 
   426 
   432 /**
   427 /**
   433  * gst_uri_get_location:
   428  * gst_uri_get_location:
   434  * @uri: A URI string
   429  * @uri: A URI string
   453 
   448 
   454   g_return_val_if_fail (uri != NULL, NULL);
   449   g_return_val_if_fail (uri != NULL, NULL);
   455   g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
   450   g_return_val_if_fail (gst_uri_is_valid (uri), NULL);
   456 
   451 
   457   colon = strstr (uri, "://");
   452   colon = strstr (uri, "://");
   458   if (!colon)
       
   459     return NULL;
       
   460 
   453 
   461   unescaped = unescape_string (colon + 3, "/");
   454   unescaped = unescape_string (colon + 3, "/");
   462 
   455 
   463   /* On Windows an URI might look like file:///c:/foo/bar.txt or
   456   /* On Windows an URI might look like file:///c:/foo/bar.txt or
   464    * file:///c|/foo/bar.txt (some Netscape versions) and we want to
   457    * file:///c|/foo/bar.txt (some Netscape versions) and we want to
   473     g_memmove (unescaped, unescaped + 1, strlen (unescaped + 1) + 1);
   466     g_memmove (unescaped, unescaped + 1, strlen (unescaped + 1) + 1);
   474   }
   467   }
   475 #endif
   468 #endif
   476 
   469 
   477   GST_LOG ("extracted location '%s' from URI '%s'", GST_STR_NULL (unescaped),
   470   GST_LOG ("extracted location '%s' from URI '%s'", GST_STR_NULL (unescaped),
   478       uri);
   471       uri);;
   479   return unescaped;
   472   return unescaped;
   480 }
   473 }
   481 
   474 
   482 /**
   475 /**
   483  * gst_uri_construct:
   476  * gst_uri_construct:
   494 #endif
   487 #endif
   495 
   488 
   496 gchar *
   489 gchar *
   497 gst_uri_construct (const gchar * protocol, const gchar * location)
   490 gst_uri_construct (const gchar * protocol, const gchar * location)
   498 {
   491 {
   499   char *escaped, *proto_lowercase;
   492   char *escaped;
   500   char *retval;
   493   char *retval;
   501 
   494 
   502   g_return_val_if_fail (gst_uri_protocol_is_valid (protocol), NULL);
   495   g_return_val_if_fail (gst_uri_protocol_is_valid (protocol), NULL);
   503   g_return_val_if_fail (location != NULL, NULL);
   496   g_return_val_if_fail (location != NULL, NULL);
   504 
   497 
   505   proto_lowercase = g_ascii_strdown (protocol, -1);
       
   506   escaped = escape_string (location);
   498   escaped = escape_string (location);
   507   retval = g_strdup_printf ("%s://%s", proto_lowercase, escaped);
   499   retval = g_strdup_printf ("%s://%s", protocol, escaped);
   508   g_free (escaped);
   500   g_free (escaped);
   509   g_free (proto_lowercase);
       
   510 
   501 
   511   return retval;
   502   return retval;
   512 }
   503 }
   513 
   504 
   514 typedef struct
   505 typedef struct
   685 
   676 
   686   g_return_val_if_fail (GST_IS_URI_HANDLER (handler), GST_URI_UNKNOWN);
   677   g_return_val_if_fail (GST_IS_URI_HANDLER (handler), GST_URI_UNKNOWN);
   687 
   678 
   688   iface = GST_URI_HANDLER_GET_INTERFACE (handler);
   679   iface = GST_URI_HANDLER_GET_INTERFACE (handler);
   689   g_return_val_if_fail (iface != NULL, GST_URI_UNKNOWN);
   680   g_return_val_if_fail (iface != NULL, GST_URI_UNKNOWN);
   690   g_return_val_if_fail (iface->get_type != NULL
   681   g_return_val_if_fail (iface->get_type != NULL, GST_URI_UNKNOWN);
   691       || iface->get_type_full != NULL, GST_URI_UNKNOWN);
   682   ret = iface->get_type ();
   692 
       
   693   if (iface->get_type != NULL)
       
   694     ret = iface->get_type ();
       
   695   else
       
   696     ret = iface->get_type_full (G_OBJECT_TYPE (handler));
       
   697   g_return_val_if_fail (GST_URI_TYPE_IS_VALID (ret), GST_URI_UNKNOWN);
   683   g_return_val_if_fail (GST_URI_TYPE_IS_VALID (ret), GST_URI_UNKNOWN);
   698 
   684 
   699   return ret;
   685   return ret;
   700 }
   686 }
   701 
   687 
   784 
   770 
   785 gboolean
   771 gboolean
   786 gst_uri_handler_set_uri (GstURIHandler * handler, const gchar * uri)
   772 gst_uri_handler_set_uri (GstURIHandler * handler, const gchar * uri)
   787 {
   773 {
   788   GstURIHandlerInterface *iface;
   774   GstURIHandlerInterface *iface;
   789   gboolean ret;
       
   790   gchar *new_uri, *protocol, *location, *colon;
       
   791 
   775 
   792   g_return_val_if_fail (GST_IS_URI_HANDLER (handler), FALSE);
   776   g_return_val_if_fail (GST_IS_URI_HANDLER (handler), FALSE);
   793   g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
   777   g_return_val_if_fail (gst_uri_is_valid (uri), FALSE);
   794 
   778 
   795   iface = GST_URI_HANDLER_GET_INTERFACE (handler);
   779   iface = GST_URI_HANDLER_GET_INTERFACE (handler);
   796   g_return_val_if_fail (iface != NULL, FALSE);
   780   g_return_val_if_fail (iface != NULL, FALSE);
   797   g_return_val_if_fail (iface->set_uri != NULL, FALSE);
   781   g_return_val_if_fail (iface->set_uri != NULL, FALSE);
   798 
   782   return iface->set_uri (handler, uri);
   799   protocol = gst_uri_get_protocol (uri);
       
   800 
       
   801   colon = strstr (uri, ":");
       
   802   location = g_strdup (colon);
       
   803 
       
   804   new_uri = g_strdup_printf ("%s%s", protocol, location);
       
   805 
       
   806   ret = iface->set_uri (handler, uri);
       
   807 
       
   808   g_free (new_uri);
       
   809   g_free (location);
       
   810   g_free (protocol);
       
   811 
       
   812   return ret;
       
   813 }
   783 }
   814 
   784 
   815 /**
   785 /**
   816  * gst_uri_handler_new_uri:
   786  * gst_uri_handler_new_uri:
   817  * @handler: A #GstURIHandler
   787  * @handler: A #GstURIHandler