Statistics
| Branch: | Revision:

ffmpeg / libavformat / file.c @ 649b918c

History | View | Annotate | Download (3.18 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * Buffered file io for ffmpeg system
3 19720f15 Fabrice Bellard
 * Copyright (c) 2001 Fabrice Bellard
4 de6d9b64 Fabrice Bellard
 *
5 19720f15 Fabrice Bellard
 * 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 de6d9b64 Fabrice Bellard
 *
10 19720f15 Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
11 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14 de6d9b64 Fabrice Bellard
 *
15 19720f15 Fabrice Bellard
 * 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 de6d9b64 Fabrice Bellard
 */
19 8be1c656 Fabrice Bellard
#include "avformat.h"
20 de6d9b64 Fabrice Bellard
#include <fcntl.h>
21 8be1c656 Fabrice Bellard
#ifndef CONFIG_WIN32
22
#include <unistd.h>
23 de6d9b64 Fabrice Bellard
#include <sys/ioctl.h>
24
#include <sys/time.h>
25 8be1c656 Fabrice Bellard
#else
26
#include <io.h>
27
#define open(fname,oflag,pmode) _open(fname,oflag,pmode)
28
#endif /* CONFIG_WIN32 */
29 de6d9b64 Fabrice Bellard
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 a1dfc201 Fabrice Bellard
    strstart(filename, "file:", &filename);
39
40 ac9fe33d Gildas Bazin
    if (flags & URL_RDWR) {
41
        access = O_CREAT | O_TRUNC | O_RDWR;
42
    } else if (flags & URL_WRONLY) {
43 de6d9b64 Fabrice Bellard
        access = O_CREAT | O_TRUNC | O_WRONLY;
44
    } else {
45
        access = O_RDONLY;
46
    }
47 fad05f52 Wolfgang Hesseler
#if defined(CONFIG_WIN32) || defined(CONFIG_OS2) || defined(__CYGWIN__)
48 8be1c656 Fabrice Bellard
    access |= O_BINARY;
49
#endif
50 de6d9b64 Fabrice Bellard
    fd = open(filename, access, 0666);
51
    if (fd < 0)
52
        return -ENOENT;
53 7906085f Falk Hüffner
    h->priv_data = (void *)(size_t)fd;
54 de6d9b64 Fabrice Bellard
    return 0;
55
}
56
57
static int file_read(URLContext *h, unsigned char *buf, int size)
58
{
59 7906085f Falk Hüffner
    int fd = (size_t)h->priv_data;
60 de6d9b64 Fabrice Bellard
    return read(fd, buf, size);
61
}
62
63
static int file_write(URLContext *h, unsigned char *buf, int size)
64
{
65 7906085f Falk Hüffner
    int fd = (size_t)h->priv_data;
66 de6d9b64 Fabrice Bellard
    return write(fd, buf, size);
67
}
68
69
/* XXX: use llseek */
70
static offset_t file_seek(URLContext *h, offset_t pos, int whence)
71
{
72 7906085f Falk Hüffner
    int fd = (size_t)h->priv_data;
73 45d6de92 Sascha Sommer
#if defined(CONFIG_WIN32) && !defined(__CYGWIN__) 
74 8be1c656 Fabrice Bellard
    return _lseeki64(fd, pos, whence);
75
#else
76 de6d9b64 Fabrice Bellard
    return lseek(fd, pos, whence);
77 8be1c656 Fabrice Bellard
#endif
78 de6d9b64 Fabrice Bellard
}
79
80
static int file_close(URLContext *h)
81
{
82 7906085f Falk Hüffner
    int fd = (size_t)h->priv_data;
83 de6d9b64 Fabrice Bellard
    return close(fd);
84
}
85
86
URLProtocol file_protocol = {
87
    "file",
88
    file_open,
89
    file_read,
90
    file_write,
91
    file_seek,
92
    file_close,
93
};
94
95
/* pipe protocol */
96
97
static int pipe_open(URLContext *h, const char *filename, int flags)
98
{
99
    int fd;
100
101
    if (flags & URL_WRONLY) {
102
        fd = 1;
103
    } else {
104
        fd = 0;
105
    }
106 c43e7a66 Michael Niedermayer
#if defined(CONFIG_WIN32) || defined(CONFIG_OS2) || defined(__CYGWIN__)
107
    setmode(fd, O_BINARY);
108
#endif
109 7906085f Falk Hüffner
    h->priv_data = (void *)(size_t)fd;
110 f9a35124 Michael Niedermayer
    h->is_streamed = 1;
111 de6d9b64 Fabrice Bellard
    return 0;
112
}
113
114
static int pipe_read(URLContext *h, unsigned char *buf, int size)
115
{
116 7906085f Falk Hüffner
    int fd = (size_t)h->priv_data;
117 de6d9b64 Fabrice Bellard
    return read(fd, buf, size);
118
}
119
120
static int pipe_write(URLContext *h, unsigned char *buf, int size)
121
{
122 7906085f Falk Hüffner
    int fd = (size_t)h->priv_data;
123 de6d9b64 Fabrice Bellard
    return write(fd, buf, size);
124
}
125
126
static int pipe_close(URLContext *h)
127
{
128
    return 0;
129
}
130
131
URLProtocol pipe_protocol = {
132
    "pipe",
133
    pipe_open,
134
    pipe_read,
135
    pipe_write,
136
    NULL,
137
    pipe_close,
138
};