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