Mercurial > mplayer.hg
annotate libmpdemux/url.c @ 3343:58ab1490a0be
dunnowhat Q/A de biztos jo
author | gabucino |
---|---|
date | Wed, 05 Dec 2001 23:07:05 +0000 |
parents | 9fad4017ec37 |
children | fb9de639ed30 |
rev | line source |
---|---|
902 | 1 /* |
2 * URL Helper | |
3 * by Bertrand Baudet <bertrand_baudet@yahoo.com> | |
4 * (C) 2001, MPlayer team. | |
5 * | |
6 * TODO: | |
7 * Extract the username/password if present | |
8 */ | |
9 | |
833 | 10 #include <string.h> |
11 #include <stdlib.h> | |
12 #include <stdio.h> | |
13 | |
14 #include "url.h" | |
15 | |
16 URL_t* | |
902 | 17 url_new(char* url) { |
833 | 18 int pos1, pos2; |
19 URL_t* Curl; | |
20 char *ptr1, *ptr2; | |
21 | |
22 // Create the URL container | |
23 Curl = (URL_t*)malloc(sizeof(URL_t)); | |
24 if( Curl==NULL ) { | |
25 printf("Memory allocation failed!\n"); | |
1631 | 26 return NULL; |
833 | 27 } |
840 | 28 // Initialisation of the URL container members |
902 | 29 memset( Curl, 0, sizeof(URL_t) ); |
840 | 30 |
833 | 31 // Copy the url in the URL container |
32 Curl->url = (char*)malloc(strlen(url)+1); | |
33 if( Curl->url==NULL ) { | |
34 printf("Memory allocation failed!\n"); | |
1631 | 35 return NULL; |
833 | 36 } |
37 strcpy(Curl->url, url); | |
38 | |
39 // extract the protocol | |
40 ptr1 = strstr(url, "://"); | |
41 if( ptr1==NULL ) { | |
997 | 42 printf("Not an URL!\n"); |
833 | 43 return NULL; |
44 } | |
45 pos1 = ptr1-url; | |
46 Curl->protocol = (char*)malloc(pos1+1); | |
47 strncpy(Curl->protocol, url, pos1); | |
48 Curl->protocol[pos1] = '\0'; | |
49 | |
50 // look if the port is given | |
51 ptr2 = strstr(ptr1+3, ":"); | |
52 if( ptr2==NULL ) { | |
53 // No port is given | |
54 // Look if a path is given | |
55 ptr2 = strstr(ptr1+3, "/"); | |
56 if( ptr2==NULL ) { | |
57 // No path/filename | |
58 // So we have an URL like http://www.hostname.com | |
59 pos2 = strlen(url); | |
60 } else { | |
61 // We have an URL like http://www.hostname.com/file.txt | |
62 pos2 = ptr2-url; | |
63 } | |
64 } else { | |
65 // We have an URL beginning like http://www.hostname.com:1212 | |
66 // Get the port number | |
67 Curl->port = atoi(ptr2+1); | |
68 pos2 = ptr2-url; | |
69 } | |
70 // copy the hostname in the URL container | |
997 | 71 Curl->hostname = (char*)malloc(pos2-pos1-3+1); |
833 | 72 if( Curl->hostname==NULL ) { |
73 printf("Memory allocation failed!\n"); | |
1631 | 74 return NULL; |
833 | 75 } |
76 strncpy(Curl->hostname, ptr1+3, pos2-pos1-3); | |
840 | 77 Curl->hostname[pos2-pos1-3] = '\0'; |
833 | 78 |
79 // Look if a path is given | |
80 ptr2 = strstr(ptr1+3, "/"); | |
81 if( ptr2!=NULL ) { | |
82 // A path/filename is given | |
83 // check if it's not a trailing '/' | |
84 if( strlen(ptr2)>1 ) { | |
85 // copy the path/filename in the URL container | |
869
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
86 Curl->file = (char*)malloc(strlen(ptr2)+1); |
840 | 87 if( Curl->file==NULL ) { |
833 | 88 printf("Memory allocation failed!\n"); |
1631 | 89 return NULL; |
833 | 90 } |
997 | 91 strcpy(Curl->file, ptr2); |
833 | 92 } |
869
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
93 } |
997 | 94 // Check if a filenme was given or set, else set it with '/' |
869
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
95 if( Curl->file==NULL ) { |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
96 Curl->file = (char*)malloc(2); |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
97 if( Curl->file==NULL ) { |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
98 printf("Memory allocation failed!\n"); |
1631 | 99 return NULL; |
869
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
100 } |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
101 strcpy(Curl->file, "/"); |
833 | 102 } |
103 | |
104 return Curl; | |
105 } | |
106 | |
3040 | 107 URL_t * |
108 url_copy(URL_t* url) { | |
109 URL_t *dup_url; | |
110 | |
111 if( url==NULL ) return NULL; | |
112 dup_url = (URL_t*)malloc(sizeof(URL_t)); | |
113 if( dup_url==NULL ) { | |
114 printf("Memory allocation failed!\n"); | |
115 return NULL; | |
116 } | |
117 memcpy( dup_url, url, sizeof(URL_t) ); | |
118 | |
119 return dup_url; | |
120 } | |
121 | |
122 | |
833 | 123 void |
902 | 124 url_free(URL_t* url) { |
1530
3effaac0ddb7
silly bug fixed - thanx Szekeres Istvan <szekeres@webvilag.com>
arpi
parents:
997
diff
changeset
|
125 if(!url) return; |
833 | 126 if(url->url) free(url->url); |
127 if(url->protocol) free(url->protocol); | |
128 if(url->hostname) free(url->hostname); | |
840 | 129 if(url->file) free(url->file); |
902 | 130 if(url->username) free(url->username); |
131 if(url->password) free(url->password); | |
833 | 132 free(url); |
133 } |