warnings are errors
[libav.git] / libavformat / avio.c
CommitLineData
de6d9b64
FB
1/*
2 * Unbuffered io for ffmpeg system
19720f15 3 * Copyright (c) 2001 Fabrice Bellard
de6d9b64 4 *
19720f15
FB
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.
de6d9b64 9 *
19720f15 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
19720f15
FB
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
de6d9b64 18 */
de6d9b64 19#include "avformat.h"
5d5fef0a 20#include <ctype.h>
de6d9b64
FB
21
22URLProtocol *first_protocol = NULL;
23
24int register_protocol(URLProtocol *protocol)
25{
26 URLProtocol **p;
27 p = &first_protocol;
28 while (*p != NULL) p = &(*p)->next;
29 *p = protocol;
30 protocol->next = NULL;
31 return 0;
32}
33
34int url_open(URLContext **puc, const char *filename, int flags)
35{
36 URLContext *uc;
37 URLProtocol *up;
38 const char *p;
39 char proto_str[128], *q;
40 int err;
41
42 p = filename;
43 q = proto_str;
44 while (*p != '\0' && *p != ':') {
5d5fef0a
FB
45 /* protocols can only contain alphabetic chars */
46 if (!isalpha(*p))
47 goto file_proto;
de6d9b64
FB
48 if ((q - proto_str) < sizeof(proto_str) - 1)
49 *q++ = *p;
50 p++;
51 }
a74127c0
FB
52 /* if the protocol has length 1, we consider it is a dos drive */
53 if (*p == '\0' || (q - proto_str) <= 1) {
5d5fef0a 54 file_proto:
de6d9b64
FB
55 strcpy(proto_str, "file");
56 } else {
57 *q = '\0';
58 }
59
60 up = first_protocol;
61 while (up != NULL) {
a74127c0 62 if (!strcmp(proto_str, up->name))
de6d9b64
FB
63 goto found;
64 up = up->next;
65 }
8a9488b5
FB
66 err = -ENOENT;
67 goto fail;
de6d9b64 68 found:
f746a046 69 uc = av_malloc(sizeof(URLContext) + strlen(filename));
8a9488b5
FB
70 if (!uc) {
71 err = -ENOMEM;
72 goto fail;
73 }
f746a046 74 strcpy(uc->filename, filename);
de6d9b64
FB
75 uc->prot = up;
76 uc->flags = flags;
77 uc->is_streamed = 0; /* default = not streamed */
8a9488b5 78 uc->max_packet_size = 0; /* default: stream file */
de6d9b64
FB
79 err = up->url_open(uc, filename, flags);
80 if (err < 0) {
1ea4f593 81 av_free(uc);
de6d9b64
FB
82 *puc = NULL;
83 return err;
84 }
85 *puc = uc;
86 return 0;
8a9488b5
FB
87 fail:
88 *puc = NULL;
89 return err;
de6d9b64
FB
90}
91
92int url_read(URLContext *h, unsigned char *buf, int size)
93{
94 int ret;
95 if (h->flags & URL_WRONLY)
96 return -EIO;
97 ret = h->prot->url_read(h, buf, size);
98 return ret;
99}
100
101int url_write(URLContext *h, unsigned char *buf, int size)
102{
103 int ret;
8a9488b5 104 if (!(h->flags & (URL_WRONLY | URL_RDWR)))
de6d9b64 105 return -EIO;
8a9488b5
FB
106 /* avoid sending too big packets */
107 if (h->max_packet_size && size > h->max_packet_size)
108 return -EIO;
de6d9b64
FB
109 ret = h->prot->url_write(h, buf, size);
110 return ret;
111}
112
113offset_t url_seek(URLContext *h, offset_t pos, int whence)
114{
115 offset_t ret;
116
117 if (!h->prot->url_seek)
118 return -EPIPE;
119 ret = h->prot->url_seek(h, pos, whence);
120 return ret;
121}
122
de6d9b64
FB
123int url_close(URLContext *h)
124{
125 int ret;
126
127 ret = h->prot->url_close(h);
1ea4f593 128 av_free(h);
de6d9b64
FB
129 return ret;
130}
131
132int url_exist(const char *filename)
133{
134 URLContext *h;
135 if (url_open(&h, filename, URL_RDONLY) < 0)
136 return 0;
137 url_close(h);
138 return 1;
139}
140
141offset_t url_filesize(URLContext *h)
142{
143 offset_t pos, size;
144
145 pos = url_seek(h, 0, SEEK_CUR);
146 size = url_seek(h, 0, SEEK_END);
147 url_seek(h, pos, SEEK_SET);
148 return size;
149}
8a9488b5
FB
150
151/*
152 * Return the maximum packet size associated to packetized file
153 * handle. If the file is not packetized (stream like http or file on
154 * disk), then 0 is returned.
155 *
156 * @param h file handle
157 * @return maximum packet size in bytes
158 */
159int url_get_max_packet_size(URLContext *h)
160{
161 return h->max_packet_size;
162}
f746a046
FB
163
164void url_get_filename(URLContext *h, char *buf, int buf_size)
165{
166 pstrcpy(buf, buf_size, h->filename);
167}