+
+static int mrl_Parse( mrl_t *p_mrl, char *psz_mrl )
+{
+ char * psz_dup = strdup( psz_mrl );
+ char * psz_parser = psz_dup;
+ char * psz_access = "";
+ char * psz_way = "";
+ char * psz_name = "";
+
+ /* *** first parse psz_dest */
+ while( *psz_parser && *psz_parser != ':' )
+ {
+ if( *psz_parser == '{' )
+ {
+ while( *psz_parser && *psz_parser != '}' )
+ {
+ psz_parser++;
+ }
+ if( *psz_parser )
+ {
+ psz_parser++;
+ }
+ }
+ else
+ {
+ psz_parser++;
+ }
+ }
+#if defined( WIN32 ) || defined( UNDER_CE )
+ if( psz_parser - psz_dup == 1 )
+ {
+ /* msg_Warn( p_sout, "drive letter %c: found in source string",
+ *psz_dup ) ; */
+ psz_parser = "";
+ }
+#endif
+
+ if( !*psz_parser )
+ {
+ psz_access = psz_way = "";
+ psz_name = psz_dup;
+ }
+ else
+ {
+ *psz_parser++ = '\0';
+
+ /* let's skip '//' */
+ if( psz_parser[0] == '/' && psz_parser[1] == '/' )
+ {
+ psz_parser += 2 ;
+ }
+
+ psz_name = psz_parser ;
+
+ /* Come back to parse the access and mux plug-ins */
+ psz_parser = psz_dup;
+
+ if( !*psz_parser )
+ {
+ /* No access */
+ psz_access = "";
+ }
+ else if( *psz_parser == '/' )
+ {
+ /* No access */
+ psz_access = "";
+ psz_parser++;
+ }
+ else
+ {
+ psz_access = psz_parser;
+
+ while( *psz_parser && *psz_parser != '/' )
+ {
+ if( *psz_parser == '{' )
+ {
+ while( *psz_parser && *psz_parser != '}' )
+ {
+ psz_parser++;
+ }
+ if( *psz_parser )
+ {
+ psz_parser++;
+ }
+ }
+ else
+ {
+ psz_parser++;
+ }
+ }
+
+ if( *psz_parser == '/' )
+ {
+ *psz_parser++ = '\0';
+ }
+ }
+
+ if( !*psz_parser )
+ {
+ /* No mux */
+ psz_way = "";
+ }
+ else
+ {
+ psz_way = psz_parser;
+ }
+ }
+
+ p_mrl->psz_access = strdup( psz_access );
+ p_mrl->psz_way = strdup( psz_way );
+ p_mrl->psz_name = strdup( psz_name );
+
+ free( psz_dup );
+ return( VLC_SUCCESS );
+}
+
+
+/* mrl_Clean: clean p_mrl after a call to mrl_Parse */
+static void mrl_Clean( mrl_t *p_mrl )
+{
+ FREE( p_mrl->psz_access );
+ FREE( p_mrl->psz_way );
+ FREE( p_mrl->psz_name );
+}
+
+
+/****************************************************************************
+ ****************************************************************************
+ **
+ **
+ **
+ ****************************************************************************
+ ****************************************************************************/
+
+/* create a complete chain */
+/* chain format:
+ module{option=*:option=*}[:module{option=*:...}]
+ */
+
+static char *_strndup( char *str, int i_len )
+{
+ char *p;
+
+ p = malloc( i_len + 1 );
+ strncpy( p, str, i_len );
+ p[i_len] = '\0';
+
+ return( p );
+}
+
+/*
+ * parse module{options=str, option="str "}:
+ * return a pointer on the rest
+ * XXX: psz_chain is modified
+ */
+#define SKIPSPACE( p ) { while( *p && ( *p == ' ' || *p == '\t' ) ) p++; }
+/* go accross " " and { } */
+static char *_get_chain_end( char *str )
+{
+ char *p = str;
+
+ SKIPSPACE( p );
+
+ for( ;; )
+ {
+ if( *p == '{' || *p == '"' || *p == '\'')
+ {
+ char c;
+
+ if( *p == '{' )
+ {
+ c = '}';
+ }
+ else
+ {
+ c = *p;
+ }
+ p++;
+
+ for( ;; )
+ {
+ if( *p == '\0' )
+ {
+ return p;
+ }
+
+ if( *p == c )
+ {
+ p++;
+ return p;
+ }
+ else if( *p == '{' && c == '}' )
+ {
+ p = _get_chain_end( p );
+ }
+ else
+ {
+ p++;
+ }
+ }
+ }
+ else if( *p == '\0' || *p == ',' || *p == '}' || *p == ' ' || *p == '\t' )
+ {
+ return p;
+ }
+ else
+ {
+ p++;
+ }
+ }
+}
+
+char * sout_cfg_parser( char **ppsz_name, sout_cfg_t **pp_cfg, char *psz_chain )
+{
+ sout_cfg_t *p_cfg = NULL;
+ char *p = psz_chain;
+
+ *ppsz_name = NULL;
+ *pp_cfg = NULL;
+
+ if( p == NULL )
+ {
+ return NULL;
+ }
+
+ SKIPSPACE( p );
+
+ while( *p && *p != '{' && *p != ':' && *p != ' ' && *p != '\t' )
+ {
+ p++;
+ }
+
+ if( p == psz_chain )
+ {
+ return NULL;
+ }
+
+ *ppsz_name = _strndup( psz_chain, p - psz_chain );
+
+ /* fprintf( stderr, "name=%s - rest=%s\n", *ppsz_name, p ); */
+
+ SKIPSPACE( p );
+
+ if( *p == '{' )
+ {
+ char *psz_name;
+
+ p++;
+
+ for( ;; )
+ {
+ sout_cfg_t cfg;
+
+ SKIPSPACE( p );
+
+ psz_name = p;
+
+ while( *p && *p != '=' && *p != ',' && *p != '}' && *p != ' ' && *p != '\t' )
+ {
+ p++;
+ }
+
+ /* fprintf( stderr, "name=%s - rest=%s\n", psz_name, p ); */
+ if( p == psz_name )
+ {
+ fprintf( stderr, "invalid options (empty)" );
+ break;
+ }
+
+ cfg.psz_name = _strndup( psz_name, p - psz_name );
+
+ SKIPSPACE( p );
+
+ if( *p == '=' )
+ {
+ char *end;
+
+ p++;
+#if 0
+ SKIPSPACE( p );
+
+ if( *p == '"' )
+ {
+ char *end;
+
+ p++;
+ end = strchr( p, '"' );
+
+ if( end )
+ {
+/* fprintf( stderr, "##%s -- %s\n", p, end ); */
+ cfg.psz_value = _strndup( p, end - p );
+ p = end + 1;
+ }
+ else
+ {
+ cfg.psz_value = strdup( p );
+ p += strlen( p );
+ }
+
+ }
+ else
+ {
+ psz_value = p;
+ while( *p && *p != ',' && *p != '}' && *p != ' ' && *p != '\t' )
+ {
+ p++;
+ }
+ cfg.psz_value = _strndup( psz_value, p - psz_value );
+ }
+#endif
+ end = _get_chain_end( p );
+ if( end <= p )
+ {
+ cfg.psz_value = NULL;
+ }
+ else
+ {
+ if( *p == '\'' || *p =='"' || *p == '{' )
+ {
+ p++;
+ end--;
+ }
+ if( end <= p )
+ {
+ cfg.psz_value = NULL;
+ }
+ else
+ {
+ cfg.psz_value = _strndup( p, end - p );
+ }
+ }
+
+ p = end;
+ SKIPSPACE( p );
+ }
+ else
+ {
+ cfg.psz_value = NULL;
+ }
+
+ cfg.p_next = NULL;
+ if( p_cfg )
+ {
+ p_cfg->p_next = malloc( sizeof( sout_cfg_t ) );
+ memcpy( p_cfg->p_next, &cfg, sizeof( sout_cfg_t ) );
+
+ p_cfg = p_cfg->p_next;
+ }
+ else
+ {
+ p_cfg = malloc( sizeof( sout_cfg_t ) );
+ memcpy( p_cfg, &cfg, sizeof( sout_cfg_t ) );
+
+ *pp_cfg = p_cfg;
+ }
+
+ if( *p == ',' )
+ {
+ p++;
+ }
+
+ if( *p == '}' )
+ {
+ p++;
+
+ break;
+ }
+ }
+ }
+
+ if( *p == ':' )
+ {
+ return( strdup( p + 1 ) );
+ }
+
+ return( NULL );
+}
+
+static void sout_cfg_free( sout_cfg_t *p_cfg )
+{
+ while( p_cfg != NULL )
+ {
+ sout_cfg_t *p_next;
+
+ p_next = p_cfg->p_next;
+
+ FREE( p_cfg->psz_name );
+ FREE( p_cfg->psz_value );
+ free( p_cfg );
+
+ p_cfg = p_next;
+ }
+}
+
+
+/*
+ * XXX name and p_cfg are used (-> do NOT free them)
+ */
+sout_stream_t *sout_stream_new( sout_instance_t *p_sout,
+ char *psz_chain )
+{
+ sout_stream_t *p_stream;
+
+ if( !psz_chain )
+ {
+ msg_Err( p_sout, "invalid chain" );
+ return NULL;
+ }
+
+ p_stream = vlc_object_create( p_sout, sizeof( sout_stream_t ) );
+
+ if( !p_stream )
+ {
+ msg_Err( p_sout, "out of memory" );
+ return NULL;
+ }
+
+ p_stream->p_sout = p_sout;
+ p_stream->p_sys = NULL;
+
+ p_stream->psz_next =
+ sout_cfg_parser( &p_stream->psz_name, &p_stream->p_cfg, psz_chain);
+
+ msg_Dbg( p_sout, "stream=`%s'", p_stream->psz_name );
+
+ p_stream->p_module =
+ module_NeedStrict( p_stream, "sout stream", p_stream->psz_name );
+
+ if( !p_stream->p_module )
+ {
+ sout_stream_delete( p_stream );
+ return NULL;
+ }
+
+ return p_stream;
+}
+
+void sout_stream_delete( sout_stream_t *p_stream )
+{
+ msg_Dbg( p_stream, "destroying chain... (name=%s)", p_stream->psz_name );
+ if( p_stream->p_module ) module_Unneed( p_stream, p_stream->p_module );
+
+ FREE( p_stream->psz_name );
+ FREE( p_stream->psz_next );
+
+ sout_cfg_free( p_stream->p_cfg );
+
+ msg_Dbg( p_stream, "destroying chain done" );
+ vlc_object_destroy( p_stream );
+}
+
+static char *_sout_stream_url_to_chain( vlc_object_t *p_this, char *psz_url )
+{
+ mrl_t mrl;
+ char *psz_chain, *p;
+
+ mrl_Parse( &mrl, psz_url );
+ p = psz_chain = malloc( 500 + strlen( mrl.psz_way ) +
+ strlen( mrl.psz_access ) +
+ strlen( mrl.psz_name ) );
+
+
+ if( config_GetInt( p_this, "sout-display" ) )
+ {
+ p += sprintf( p, "duplicate{dst=display,dst=std{mux=\"%s\",access=\"%s\",url=\"%s\"}}",
+ mrl.psz_way, mrl.psz_access, mrl.psz_name );
+ }
+ else
+ {
+ p += sprintf( p, "std{mux=\"%s\",access=\"%s\",url=\"%s\"}",
+ mrl.psz_way, mrl.psz_access, mrl.psz_name );
+ }
+
+ mrl_Clean( &mrl );
+ return( psz_chain );
+}
+
+/*****************************************************************************/
+static module_t *__module_NeedStrict( vlc_object_t *p_obj, const char *psz_capacity, const char *psz_name )
+{
+ module_t *p_module;
+
+ if( !psz_name || !*psz_name )
+ {
+ p_module = module_Need( p_obj, psz_capacity, psz_name );
+ }
+ else
+ {
+ char *psz_name_strict = malloc( strlen( psz_name ) + 6 );
+ strcpy( psz_name_strict, psz_name );
+ strcat( psz_name_strict, ",none" );
+
+ p_module = module_Need( p_obj, psz_capacity, psz_name_strict );
+
+ free( psz_name_strict );
+ }
+
+ return p_module;
+}
+
+