Mercurial > mplayer.hg
annotate url.c @ 1560:a3d84360aa81
exception stuff fixed
author | arpi |
---|---|
date | Thu, 16 Aug 2001 18:45:01 +0000 |
parents | 3effaac0ddb7 |
children | 09284c9c2b49 |
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"); | |
26 exit(1); | |
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"); | |
35 exit(1); | |
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"); | |
74 exit(1); | |
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"); |
89 exit(1); | |
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"); |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
99 exit(1); |
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 | |
107 void | |
902 | 108 url_free(URL_t* url) { |
1530
3effaac0ddb7
silly bug fixed - thanx Szekeres Istvan <szekeres@webvilag.com>
arpi
parents:
997
diff
changeset
|
109 if(!url) return; |
833 | 110 if(url->url) free(url->url); |
111 if(url->protocol) free(url->protocol); | |
112 if(url->hostname) free(url->hostname); | |
840 | 113 if(url->file) free(url->file); |
902 | 114 if(url->username) free(url->username); |
115 if(url->password) free(url->password); | |
833 | 116 free(url); |
117 } |