Mercurial > mplayer.hg
annotate url.c @ 899:f51b1ad62410
*** empty log message ***
author | gabucino |
---|---|
date | Mon, 28 May 2001 19:16:06 +0000 |
parents | e350849ff400 |
children | ede5785faa53 |
rev | line source |
---|---|
833 | 1 #include <string.h> |
2 #include <stdlib.h> | |
3 #include <stdio.h> | |
4 | |
5 #include "url.h" | |
6 | |
7 URL_t* | |
8 set_url(char* url) { | |
9 int pos1, pos2; | |
10 URL_t* Curl; | |
11 char *ptr1, *ptr2; | |
12 | |
13 // Create the URL container | |
14 Curl = (URL_t*)malloc(sizeof(URL_t)); | |
15 if( Curl==NULL ) { | |
16 printf("Memory allocation failed!\n"); | |
17 exit(1); | |
18 } | |
840 | 19 // Initialisation of the URL container members |
20 Curl->url = NULL; | |
21 Curl->protocol = NULL; | |
22 Curl->hostname = NULL; | |
23 Curl->file = NULL; | |
24 Curl->port = 0; | |
25 | |
833 | 26 // Copy the url in the URL container |
27 Curl->url = (char*)malloc(strlen(url)+1); | |
28 if( Curl->url==NULL ) { | |
29 printf("Memory allocation failed!\n"); | |
30 exit(1); | |
31 } | |
32 strcpy(Curl->url, url); | |
33 | |
34 // extract the protocol | |
35 ptr1 = strstr(url, "://"); | |
36 if( ptr1==NULL ) { | |
840 | 37 printf("Malformed URL or not an URL!\n"); |
833 | 38 return NULL; |
39 } | |
40 pos1 = ptr1-url; | |
41 Curl->protocol = (char*)malloc(pos1+1); | |
42 strncpy(Curl->protocol, url, pos1); | |
43 Curl->protocol[pos1] = '\0'; | |
44 | |
45 // look if the port is given | |
46 ptr2 = strstr(ptr1+3, ":"); | |
47 if( ptr2==NULL ) { | |
48 // No port is given | |
49 // Look if a path is given | |
50 ptr2 = strstr(ptr1+3, "/"); | |
51 if( ptr2==NULL ) { | |
52 // No path/filename | |
53 // So we have an URL like http://www.hostname.com | |
54 pos2 = strlen(url); | |
55 } else { | |
56 // We have an URL like http://www.hostname.com/file.txt | |
57 pos2 = ptr2-url; | |
58 } | |
59 } else { | |
60 // We have an URL beginning like http://www.hostname.com:1212 | |
61 // Get the port number | |
62 Curl->port = atoi(ptr2+1); | |
63 pos2 = ptr2-url; | |
64 } | |
65 // copy the hostname in the URL container | |
66 Curl->hostname = (char*)malloc(strlen(url)+1); | |
67 if( Curl->hostname==NULL ) { | |
68 printf("Memory allocation failed!\n"); | |
69 exit(1); | |
70 } | |
71 strncpy(Curl->hostname, ptr1+3, pos2-pos1-3); | |
840 | 72 Curl->hostname[pos2-pos1-3] = '\0'; |
833 | 73 |
74 // Look if a path is given | |
75 ptr2 = strstr(ptr1+3, "/"); | |
76 if( ptr2!=NULL ) { | |
77 // A path/filename is given | |
78 // check if it's not a trailing '/' | |
79 if( strlen(ptr2)>1 ) { | |
80 // 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
|
81 Curl->file = (char*)malloc(strlen(ptr2)+1); |
840 | 82 if( Curl->file==NULL ) { |
833 | 83 printf("Memory allocation failed!\n"); |
84 exit(1); | |
85 } | |
869
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
86 Curl->file[0]='/'; |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
87 strcpy(Curl->file+1, ptr2+1); |
833 | 88 } |
869
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
89 } |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
90 // Check if a filenme was given or set else set it with '/' |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
91 if( Curl->file==NULL ) { |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
92 Curl->file = (char*)malloc(2); |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
93 if( Curl->file==NULL ) { |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
94 printf("Memory allocation failed!\n"); |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
95 exit(1); |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
96 } |
e350849ff400
Url given without a filename/path get the filename/path '/'
bertrand
parents:
840
diff
changeset
|
97 strcpy(Curl->file, "/"); |
833 | 98 } |
99 | |
100 return Curl; | |
101 } | |
102 | |
103 void | |
104 free_url(URL_t* url) { | |
105 if(url) return; | |
106 if(url->url) free(url->url); | |
107 if(url->protocol) free(url->protocol); | |
108 if(url->hostname) free(url->hostname); | |
840 | 109 if(url->file) free(url->file); |
833 | 110 free(url); |
111 } |