removed ctype.h header
[libav.git] / libavformat / avio.c
1 /*
2 * Unbuffered io for ffmpeg system
3 * Copyright (c) 2001 Fabrice Bellard
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "avformat.h"
20
21 static int default_interrupt_cb(void);
22
23 URLProtocol *first_protocol = NULL;
24 URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
25
26 int register_protocol(URLProtocol *protocol)
27 {
28 URLProtocol **p;
29 p = &first_protocol;
30 while (*p != NULL) p = &(*p)->next;
31 *p = protocol;
32 protocol->next = NULL;
33 return 0;
34 }
35
36 int url_open(URLContext **puc, const char *filename, int flags)
37 {
38 URLContext *uc;
39 URLProtocol *up;
40 const char *p;
41 char proto_str[128], *q;
42 int err;
43
44 p = filename;
45 q = proto_str;
46 while (*p != '\0' && *p != ':') {
47 /* protocols can only contain alphabetic chars */
48 if (!isalpha(*p))
49 goto file_proto;
50 if ((q - proto_str) < sizeof(proto_str) - 1)
51 *q++ = *p;
52 p++;
53 }
54 /* if the protocol has length 1, we consider it is a dos drive */
55 if (*p == '\0' || (q - proto_str) <= 1) {
56 file_proto:
57 strcpy(proto_str, "file");
58 } else {
59 *q = '\0';
60 }
61
62 up = first_protocol;
63 while (up != NULL) {
64 if (!strcmp(proto_str, up->name))
65 goto found;
66 up = up->next;
67 }
68 err = -ENOENT;
69 goto fail;
70 found:
71 uc = av_malloc(sizeof(URLContext) + strlen(filename));
72 if (!uc) {
73 err = -ENOMEM;
74 goto fail;
75 }
76 strcpy(uc->filename, filename);
77 uc->prot = up;
78 uc->flags = flags;
79 uc->is_streamed = 0; /* default = not streamed */
80 uc->max_packet_size = 0; /* default: stream file */
81 err = up->url_open(uc, filename, flags);
82 if (err < 0) {
83 av_free(uc);
84 *puc = NULL;
85 return err;
86 }
87 *puc = uc;
88 return 0;
89 fail:
90 *puc = NULL;
91 return err;
92 }
93
94 int url_read(URLContext *h, unsigned char *buf, int size)
95 {
96 int ret;
97 if (h->flags & URL_WRONLY)
98 return -EIO;
99 ret = h->prot->url_read(h, buf, size);
100 return ret;
101 }
102
103 int url_write(URLContext *h, unsigned char *buf, int size)
104 {
105 int ret;
106 if (!(h->flags & (URL_WRONLY | URL_RDWR)))
107 return -EIO;
108 /* avoid sending too big packets */
109 if (h->max_packet_size && size > h->max_packet_size)
110 return -EIO;
111 ret = h->prot->url_write(h, buf, size);
112 return ret;
113 }
114
115 offset_t url_seek(URLContext *h, offset_t pos, int whence)
116 {
117 offset_t ret;
118
119 if (!h->prot->url_seek)
120 return -EPIPE;
121 ret = h->prot->url_seek(h, pos, whence);
122 return ret;
123 }
124
125 int url_close(URLContext *h)
126 {
127 int ret;
128
129 ret = h->prot->url_close(h);
130 av_free(h);
131 return ret;
132 }
133
134 int url_exist(const char *filename)
135 {
136 URLContext *h;
137 if (url_open(&h, filename, URL_RDONLY) < 0)
138 return 0;
139 url_close(h);
140 return 1;
141 }
142
143 offset_t url_filesize(URLContext *h)
144 {
145 offset_t pos, size;
146
147 pos = url_seek(h, 0, SEEK_CUR);
148 size = url_seek(h, 0, SEEK_END);
149 url_seek(h, pos, SEEK_SET);
150 return size;
151 }
152
153 /*
154 * Return the maximum packet size associated to packetized file
155 * handle. If the file is not packetized (stream like http or file on
156 * disk), then 0 is returned.
157 *
158 * @param h file handle
159 * @return maximum packet size in bytes
160 */
161 int url_get_max_packet_size(URLContext *h)
162 {
163 return h->max_packet_size;
164 }
165
166 void url_get_filename(URLContext *h, char *buf, int buf_size)
167 {
168 pstrcpy(buf, buf_size, h->filename);
169 }
170
171
172 static int default_interrupt_cb(void)
173 {
174 return 0;
175 }
176
177 /**
178 * The callback is called in blocking functions to test regulary if
179 * asynchronous interruption is needed. -EINTR is returned in this
180 * case by the interrupted function. 'NULL' means no interrupt
181 * callback is given.
182 */
183 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
184 {
185 if (!interrupt_cb)
186 interrupt_cb = default_interrupt_cb;
187 url_interrupt_cb = interrupt_cb;
188 }