Commit | Line | Data |
---|---|---|
de6d9b64 FB |
1 | /* |
2 | * Buffered file io for ffmpeg system | |
3 | * Copyright (c) 2001 Gerard Lantau | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program 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 | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 | */ | |
19 | #include <stdlib.h> | |
20 | #include <stdio.h> | |
21 | #include <unistd.h> | |
22 | #include <fcntl.h> | |
23 | #include <sys/ioctl.h> | |
24 | #include <errno.h> | |
25 | #include <sys/time.h> | |
26 | ||
27 | #include "avformat.h" | |
28 | ||
29 | /* standard file protocol */ | |
30 | ||
31 | static int file_open(URLContext *h, const char *filename, int flags) | |
32 | { | |
33 | int access; | |
34 | int fd; | |
35 | ||
36 | if (flags & URL_WRONLY) { | |
37 | access = O_CREAT | O_TRUNC | O_WRONLY; | |
38 | } else { | |
39 | access = O_RDONLY; | |
40 | } | |
41 | fd = open(filename, access, 0666); | |
42 | if (fd < 0) | |
43 | return -ENOENT; | |
44 | h->priv_data = (void *)fd; | |
45 | return 0; | |
46 | } | |
47 | ||
48 | static int file_read(URLContext *h, unsigned char *buf, int size) | |
49 | { | |
50 | int fd = (int)h->priv_data; | |
51 | return read(fd, buf, size); | |
52 | } | |
53 | ||
54 | static int file_write(URLContext *h, unsigned char *buf, int size) | |
55 | { | |
56 | int fd = (int)h->priv_data; | |
57 | return write(fd, buf, size); | |
58 | } | |
59 | ||
60 | /* XXX: use llseek */ | |
61 | static offset_t file_seek(URLContext *h, offset_t pos, int whence) | |
62 | { | |
63 | int fd = (int)h->priv_data; | |
64 | return lseek(fd, pos, whence); | |
65 | } | |
66 | ||
67 | static int file_close(URLContext *h) | |
68 | { | |
69 | int fd = (int)h->priv_data; | |
70 | return close(fd); | |
71 | } | |
72 | ||
73 | URLProtocol file_protocol = { | |
74 | "file", | |
75 | file_open, | |
76 | file_read, | |
77 | file_write, | |
78 | file_seek, | |
79 | file_close, | |
80 | }; | |
81 | ||
82 | /* pipe protocol */ | |
83 | ||
84 | static int pipe_open(URLContext *h, const char *filename, int flags) | |
85 | { | |
86 | int fd; | |
87 | ||
88 | if (flags & URL_WRONLY) { | |
89 | fd = 1; | |
90 | } else { | |
91 | fd = 0; | |
92 | } | |
93 | h->priv_data = (void *)fd; | |
94 | return 0; | |
95 | } | |
96 | ||
97 | static int pipe_read(URLContext *h, unsigned char *buf, int size) | |
98 | { | |
99 | int fd = (int)h->priv_data; | |
100 | return read(fd, buf, size); | |
101 | } | |
102 | ||
103 | static int pipe_write(URLContext *h, unsigned char *buf, int size) | |
104 | { | |
105 | int fd = (int)h->priv_data; | |
106 | return write(fd, buf, size); | |
107 | } | |
108 | ||
109 | static int pipe_close(URLContext *h) | |
110 | { | |
111 | return 0; | |
112 | } | |
113 | ||
114 | URLProtocol pipe_protocol = { | |
115 | "pipe", | |
116 | pipe_open, | |
117 | pipe_read, | |
118 | pipe_write, | |
119 | NULL, | |
120 | pipe_close, | |
121 | }; |