Commit | Line | Data |
---|---|---|
de6d9b64 FB |
1 | /* |
2 | * Buffered file 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 | */ |
8be1c656 | 19 | #include "avformat.h" |
de6d9b64 | 20 | #include <fcntl.h> |
8be1c656 FB |
21 | #ifndef CONFIG_WIN32 |
22 | #include <unistd.h> | |
de6d9b64 | 23 | #include <sys/ioctl.h> |
de6d9b64 | 24 | #include <sys/time.h> |
8be1c656 FB |
25 | #else |
26 | #include <io.h> | |
27 | #define open(fname,oflag,pmode) _open(fname,oflag,pmode) | |
28 | #endif /* CONFIG_WIN32 */ | |
de6d9b64 | 29 | |
de6d9b64 FB |
30 | |
31 | /* standard file protocol */ | |
32 | ||
33 | static int file_open(URLContext *h, const char *filename, int flags) | |
34 | { | |
35 | int access; | |
36 | int fd; | |
37 | ||
a1dfc201 FB |
38 | strstart(filename, "file:", &filename); |
39 | ||
de6d9b64 FB |
40 | if (flags & URL_WRONLY) { |
41 | access = O_CREAT | O_TRUNC | O_WRONLY; | |
42 | } else { | |
43 | access = O_RDONLY; | |
44 | } | |
fad05f52 | 45 | #if defined(CONFIG_WIN32) || defined(CONFIG_OS2) || defined(__CYGWIN__) |
8be1c656 FB |
46 | access |= O_BINARY; |
47 | #endif | |
de6d9b64 FB |
48 | fd = open(filename, access, 0666); |
49 | if (fd < 0) | |
50 | return -ENOENT; | |
51 | h->priv_data = (void *)fd; | |
52 | return 0; | |
53 | } | |
54 | ||
55 | static int file_read(URLContext *h, unsigned char *buf, int size) | |
56 | { | |
57 | int fd = (int)h->priv_data; | |
58 | return read(fd, buf, size); | |
59 | } | |
60 | ||
61 | static int file_write(URLContext *h, unsigned char *buf, int size) | |
62 | { | |
63 | int fd = (int)h->priv_data; | |
64 | return write(fd, buf, size); | |
65 | } | |
66 | ||
67 | /* XXX: use llseek */ | |
68 | static offset_t file_seek(URLContext *h, offset_t pos, int whence) | |
69 | { | |
70 | int fd = (int)h->priv_data; | |
8be1c656 FB |
71 | #ifdef CONFIG_WIN32 |
72 | return _lseeki64(fd, pos, whence); | |
73 | #else | |
de6d9b64 | 74 | return lseek(fd, pos, whence); |
8be1c656 | 75 | #endif |
de6d9b64 FB |
76 | } |
77 | ||
78 | static int file_close(URLContext *h) | |
79 | { | |
80 | int fd = (int)h->priv_data; | |
81 | return close(fd); | |
82 | } | |
83 | ||
84 | URLProtocol file_protocol = { | |
85 | "file", | |
86 | file_open, | |
87 | file_read, | |
88 | file_write, | |
89 | file_seek, | |
90 | file_close, | |
91 | }; | |
92 | ||
93 | /* pipe protocol */ | |
94 | ||
95 | static int pipe_open(URLContext *h, const char *filename, int flags) | |
96 | { | |
97 | int fd; | |
98 | ||
99 | if (flags & URL_WRONLY) { | |
100 | fd = 1; | |
101 | } else { | |
102 | fd = 0; | |
103 | } | |
104 | h->priv_data = (void *)fd; | |
105 | return 0; | |
106 | } | |
107 | ||
108 | static int pipe_read(URLContext *h, unsigned char *buf, int size) | |
109 | { | |
110 | int fd = (int)h->priv_data; | |
111 | return read(fd, buf, size); | |
112 | } | |
113 | ||
114 | static int pipe_write(URLContext *h, unsigned char *buf, int size) | |
115 | { | |
116 | int fd = (int)h->priv_data; | |
117 | return write(fd, buf, size); | |
118 | } | |
119 | ||
120 | static int pipe_close(URLContext *h) | |
121 | { | |
122 | return 0; | |
123 | } | |
124 | ||
125 | URLProtocol pipe_protocol = { | |
126 | "pipe", | |
127 | pipe_open, | |
128 | pipe_read, | |
129 | pipe_write, | |
130 | NULL, | |
131 | pipe_close, | |
132 | }; |