jack: use real latency for buffer date
[vlc.git] / include / vlc_url.h
blob714a3f24da5f46047ae2985477c853c826dbab6b
1 /*****************************************************************************
2 * vlc_url.h: URL related macros
3 *****************************************************************************
4 * Copyright (C) 2002-2006 the VideoLAN team
5 * $Id$
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * RĂ©mi Denis-Courmont <rem # videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #ifndef VLC_URL_H
26 # define VLC_URL_H
28 /**
29 * \file
30 * This file defines functions for manipulating URL in vlc
33 struct vlc_url_t
35 char *psz_protocol;
36 char *psz_username;
37 char *psz_password;
38 char *psz_host;
39 int i_port;
41 char *psz_path;
43 char *psz_option;
45 char *psz_buffer; /* to be freed */
48 VLC_EXPORT( char *, decode_URI_duplicate, ( const char *psz ) );
49 VLC_EXPORT( char *, decode_URI, ( char *psz ) );
50 VLC_EXPORT( char *, encode_URI_component, ( const char *psz ) );
51 VLC_EXPORT( char *, make_URI, ( const char *path ) );
53 /*****************************************************************************
54 * vlc_UrlParse:
55 *****************************************************************************
56 * option : if != 0 then path is split at this char
58 * format [protocol://[login[:password]@]][host[:port]]/path[OPTIONoption]
59 *****************************************************************************/
60 static inline void vlc_UrlParse( vlc_url_t *url, const char *psz_url,
61 char option )
63 char *psz_dup;
64 char *psz_parse;
65 char *p;
66 char *p2;
68 url->psz_protocol = NULL;
69 url->psz_username = NULL;
70 url->psz_password = NULL;
71 url->psz_host = NULL;
72 url->i_port = 0;
73 url->psz_path = NULL;
74 url->psz_option = NULL;
76 if( psz_url == NULL )
78 url->psz_buffer = NULL;
79 return;
81 url->psz_buffer = psz_parse = psz_dup = strdup( psz_url );
83 /* Search a valid protocol */
84 p = strstr( psz_parse, ":/" );
85 if( p != NULL )
87 for( p2 = psz_parse; p2 < p; p2++ )
89 #define I(i,a,b) ( (a) <= (i) && (i) <= (b) )
90 if( !I(*p2, 'a', 'z' ) && !I(*p2, 'A', 'Z') && !I(*p2, '0', '9') && *p2 != '+' && *p2 != '-' && *p2 != '.' )
92 p = NULL;
93 break;
95 #undef I
99 if( p != NULL )
101 /* we have a protocol */
103 /* skip :// */
104 *p++ = '\0';
105 if( p[1] == '/' )
106 p += 2;
107 url->psz_protocol = psz_parse;
108 psz_parse = p;
110 p = strchr( psz_parse, '@' );
111 p2 = strchr( psz_parse, '/' );
112 if( p != NULL && ( p2 != NULL ? p < p2 : 1 ) )
114 /* We have a login */
115 url->psz_username = psz_parse;
116 *p++ = '\0';
118 psz_parse = strchr( psz_parse, ':' );
119 if( psz_parse != NULL )
121 /* We have a password */
122 *psz_parse++ = '\0';
123 url->psz_password = psz_parse;
124 decode_URI( url->psz_password );
126 decode_URI( url->psz_username );
127 psz_parse = p;
130 p = strchr( psz_parse, '/' );
131 if( !p || psz_parse < p )
133 /* We have a host[:port] */
134 url->psz_host = strdup( psz_parse );
135 if( p )
137 url->psz_host[p - psz_parse] = '\0';
140 if( *url->psz_host == '[' )
142 /* Ipv6 address */
143 p2 = strchr( url->psz_host, ']' );
144 if( p2 )
146 p2 = strchr( p2, ':' );
149 else
151 p2 = strchr( url->psz_host, ':' );
153 if( p2 )
155 *p2++ = '\0';
156 url->i_port = atoi( p2 );
159 psz_parse = p;
161 /* Now parse psz_path and psz_option */
162 if( psz_parse )
164 url->psz_path = psz_parse;
165 if( option != '\0' )
167 p = strchr( url->psz_path, option );
168 if( p )
170 *p++ = '\0';
171 url->psz_option = p;
177 /*****************************************************************************
178 * vlc_UrlClean:
179 *****************************************************************************/
180 static inline void vlc_UrlClean( vlc_url_t *url )
182 free( url->psz_buffer );
183 free( url->psz_host );
185 url->psz_protocol = NULL;
186 url->psz_username = NULL;
187 url->psz_password = NULL;
188 url->psz_host = NULL;
189 url->i_port = 0;
190 url->psz_path = NULL;
191 url->psz_option = NULL;
193 url->psz_buffer = NULL;
196 #include <ctype.h>
198 /** Check whether a given string is not a valid URL and must hence be
199 * encoded */
200 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
202 const char *ptr;
204 for( ptr = psz_url; *ptr; ptr++ )
206 char c = *ptr;
208 if( c == '%' )
210 if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
211 return 1; /* not encoded */
212 ptr += 2;
214 else
215 if( ( (unsigned char)( c - 'a' ) < 26 )
216 || ( (unsigned char)( c - 'A' ) < 26 )
217 || ( (unsigned char)( c - '0' ) < 10 )
218 || ( strchr( "-_.", c ) != NULL ) )
219 return 1;
221 return 0; /* looks fine - but maybe it is not encoded */
224 #endif