]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/file.c
Make the pipe URLProtocol share read and write functions with the file URLProtocol
[frescor/ffmpeg.git] / libavformat / file.c
1 /*
2  * Buffered file io for ffmpeg system
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #include "avformat.h"
22 #include "avstring.h"
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <sys/time.h>
26
27
28 /* standard file protocol */
29
30 static int file_open(URLContext *h, const char *filename, int flags)
31 {
32     int access;
33     int fd;
34
35     av_strstart(filename, "file:", &filename);
36
37     if (flags & URL_RDWR) {
38         access = O_CREAT | O_TRUNC | O_RDWR;
39     } else if (flags & URL_WRONLY) {
40         access = O_CREAT | O_TRUNC | O_WRONLY;
41     } else {
42         access = O_RDONLY;
43     }
44 #ifdef O_BINARY
45     access |= O_BINARY;
46 #endif
47     fd = open(filename, access, 0666);
48     if (fd < 0)
49         return AVERROR(ENOENT);
50     h->priv_data = (void *)(size_t)fd;
51     return 0;
52 }
53
54 static int file_read(URLContext *h, unsigned char *buf, int size)
55 {
56     int fd = (size_t)h->priv_data;
57     return read(fd, buf, size);
58 }
59
60 static int file_write(URLContext *h, unsigned char *buf, int size)
61 {
62     int fd = (size_t)h->priv_data;
63     return write(fd, buf, size);
64 }
65
66 /* XXX: use llseek */
67 static offset_t file_seek(URLContext *h, offset_t pos, int whence)
68 {
69     int fd = (size_t)h->priv_data;
70     return lseek(fd, pos, whence);
71 }
72
73 static int file_close(URLContext *h)
74 {
75     int fd = (size_t)h->priv_data;
76     return close(fd);
77 }
78
79 URLProtocol file_protocol = {
80     "file",
81     file_open,
82     file_read,
83     file_write,
84     file_seek,
85     file_close,
86 };
87
88 /* pipe protocol */
89
90 static int pipe_open(URLContext *h, const char *filename, int flags)
91 {
92     int fd;
93
94     if (flags & URL_WRONLY) {
95         fd = 1;
96     } else {
97         fd = 0;
98     }
99 #ifdef O_BINARY
100     setmode(fd, O_BINARY);
101 #endif
102     h->priv_data = (void *)(size_t)fd;
103     h->is_streamed = 1;
104     return 0;
105 }
106
107 static int pipe_read(URLContext *h, unsigned char *buf, int size)
108 {
109     int fd = (size_t)h->priv_data;
110     return read(fd, buf, size);
111 }
112
113 URLProtocol pipe_protocol = {
114     "pipe",
115     pipe_open,
116     file_read,
117     file_write,
118     NULL,
119     pipe_close,
120 };