Mercurial > mplayer.hg
annotate stream/stream_file.c @ 30761:b41ac9f6e26f
Reindent.
author | reimar |
---|---|
date | Mon, 01 Mar 2010 19:21:59 +0000 |
parents | c05fbacce55f |
children | e10ba171be06 |
rev | line source |
---|---|
30426
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
29920
diff
changeset
|
1 /* |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
29920
diff
changeset
|
2 * This file is part of MPlayer. |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
29920
diff
changeset
|
3 * |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
29920
diff
changeset
|
4 * MPlayer is free software; you can redistribute it and/or modify |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
29920
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
29920
diff
changeset
|
6 * the Free Software Foundation; either version 2 of the License, or |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
29920
diff
changeset
|
7 * (at your option) any later version. |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
29920
diff
changeset
|
8 * |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
29920
diff
changeset
|
9 * MPlayer is distributed in the hope that it will be useful, |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
29920
diff
changeset
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
29920
diff
changeset
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
29920
diff
changeset
|
12 * GNU General Public License for more details. |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
29920
diff
changeset
|
13 * |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
29920
diff
changeset
|
14 * You should have received a copy of the GNU General Public License along |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
29920
diff
changeset
|
15 * with MPlayer; if not, write to the Free Software Foundation, Inc., |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
29920
diff
changeset
|
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
ce0122361a39
Add license header to all files missing it in the stream subdirectory.
diego
parents:
29920
diff
changeset
|
17 */ |
9794 | 18 |
19 #include "config.h" | |
20 | |
16748
ec02252fbaa6
we need stdio.h for SEEK_SET on mingw, patch by Zuxy <zuxy.meng at gmail.com>
faust3
parents:
15461
diff
changeset
|
21 #include <stdio.h> |
9794 | 22 #include <sys/types.h> |
23 #include <sys/stat.h> | |
24 #include <fcntl.h> | |
25 #include <unistd.h> | |
26 | |
27 #include "mp_msg.h" | |
28 #include "stream.h" | |
29 #include "help_mp.h" | |
17012 | 30 #include "m_option.h" |
31 #include "m_struct.h" | |
9794 | 32 |
33 static struct stream_priv_s { | |
34 char* filename; | |
15461
7c272bfba96f
fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents:
15452
diff
changeset
|
35 char *filename2; |
9794 | 36 } stream_priv_dflts = { |
15452 | 37 NULL, NULL |
9794 | 38 }; |
39 | |
40 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f) | |
41 /// URL definition | |
25242 | 42 static const m_option_t stream_opts_fields[] = { |
15461
7c272bfba96f
fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents:
15452
diff
changeset
|
43 {"string", ST_OFF(filename), CONF_TYPE_STRING, 0, 0 ,0, NULL}, |
7c272bfba96f
fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents:
15452
diff
changeset
|
44 {"filename", ST_OFF(filename2), CONF_TYPE_STRING, 0, 0 ,0, NULL}, |
9794 | 45 { NULL, NULL, 0, 0, 0, 0, NULL } |
46 }; | |
25691 | 47 static const struct m_struct_st stream_opts = { |
9794 | 48 "file", |
49 sizeof(struct stream_priv_s), | |
50 &stream_priv_dflts, | |
51 stream_opts_fields | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27727
diff
changeset
|
52 }; |
9794 | 53 |
54 static int fill_buffer(stream_t *s, char* buffer, int max_len){ | |
55 int r = read(s->fd,buffer,max_len); | |
56 return (r <= 0) ? -1 : r; | |
57 } | |
58 | |
59 static int write_buffer(stream_t *s, char* buffer, int len) { | |
60 int r = write(s->fd,buffer,len); | |
61 return (r <= 0) ? -1 : r; | |
62 } | |
63 | |
64 static int seek(stream_t *s,off_t newpos) { | |
65 s->pos = newpos; | |
66 if(lseek(s->fd,s->pos,SEEK_SET)<0) { | |
67 s->eof=1; | |
68 return 0; | |
69 } | |
70 return 1; | |
71 } | |
72 | |
73 static int seek_forward(stream_t *s,off_t newpos) { | |
74 if(newpos<s->pos){ | |
75 mp_msg(MSGT_STREAM,MSGL_INFO,"Cannot seek backward in linear streams!\n"); | |
76 return 0; | |
77 } | |
78 while(s->pos<newpos){ | |
12018 | 79 int len=s->fill_buffer(s,s->buffer,STREAM_BUFFER_SIZE); |
80 if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; break; } // EOF | |
81 s->buf_pos=0; | |
82 s->buf_len=len; | |
83 s->pos+=len; | |
9794 | 84 } |
85 return 1; | |
86 } | |
87 | |
21658 | 88 static int control(stream_t *s, int cmd, void *arg) { |
89 switch(cmd) { | |
90 case STREAM_CTRL_GET_SIZE: { | |
91 off_t size; | |
92 | |
93 size = lseek(s->fd, 0, SEEK_END); | |
94 lseek(s->fd, s->pos, SEEK_SET); | |
95 if(size != (off_t)-1) { | |
96 *((off_t*)arg) = size; | |
97 return 1; | |
98 } | |
99 } | |
100 } | |
24257 | 101 return STREAM_UNSUPPORTED; |
21658 | 102 } |
103 | |
9794 | 104 static int open_f(stream_t *stream,int mode, void* opts, int* file_format) { |
105 int f; | |
106 mode_t m = 0; | |
107 off_t len; | |
15461
7c272bfba96f
fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents:
15452
diff
changeset
|
108 unsigned char *filename; |
9794 | 109 struct stream_priv_s* p = (struct stream_priv_s*)opts; |
110 | |
111 if(mode == STREAM_READ) | |
112 m = O_RDONLY; | |
113 else if(mode == STREAM_WRITE) | |
22451
86f7b9cab33b
truncate mencoder's output file if it exists, instead of overwriting just part of it.
lorenm
parents:
21705
diff
changeset
|
114 m = O_RDWR|O_CREAT|O_TRUNC; |
9794 | 115 else { |
10608 | 116 mp_msg(MSGT_OPEN,MSGL_ERR, "[file] Unknown open mode %d\n",mode); |
9794 | 117 m_struct_free(&stream_opts,opts); |
24257 | 118 return STREAM_UNSUPPORTED; |
9794 | 119 } |
120 | |
15461
7c272bfba96f
fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents:
15452
diff
changeset
|
121 if(p->filename) |
7c272bfba96f
fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents:
15452
diff
changeset
|
122 filename = p->filename; |
7c272bfba96f
fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents:
15452
diff
changeset
|
123 else if(p->filename2) |
7c272bfba96f
fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents:
15452
diff
changeset
|
124 filename = p->filename2; |
7c272bfba96f
fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents:
15452
diff
changeset
|
125 else |
7c272bfba96f
fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents:
15452
diff
changeset
|
126 filename = NULL; |
7c272bfba96f
fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents:
15452
diff
changeset
|
127 if(!filename) { |
9849 | 128 mp_msg(MSGT_OPEN,MSGL_ERR, "[file] No filename\n"); |
129 m_struct_free(&stream_opts,opts); | |
130 return STREAM_ERROR; | |
131 } | |
132 | |
30608
c05fbacce55f
Replace platform preprocessor check by HAVE_DOS_PATHS.
komh
parents:
30426
diff
changeset
|
133 #if HAVE_DOS_PATHS |
26005 | 134 // extract '/' from '/x:/path' |
135 if( filename[ 0 ] == '/' && filename[ 1 ] && filename[ 2 ] == ':' ) | |
136 filename++; | |
137 #endif | |
138 | |
29304 | 139 #if defined(__CYGWIN__)|| defined(__MINGW32__) || defined(__OS2__) |
9794 | 140 m |= O_BINARY; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
27727
diff
changeset
|
141 #endif |
9794 | 142 |
15461
7c272bfba96f
fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents:
15452
diff
changeset
|
143 if(!strcmp(filename,"-")){ |
9794 | 144 if(mode == STREAM_READ) { |
145 // read from stdin | |
146 mp_msg(MSGT_OPEN,MSGL_INFO,MSGTR_ReadSTDIN); | |
147 f=0; // 0=stdin | |
29304 | 148 #if defined(__MINGW32__) || defined(__OS2__) |
12921 | 149 setmode(fileno(stdin),O_BINARY); |
150 #endif | |
9794 | 151 } else { |
152 mp_msg(MSGT_OPEN,MSGL_INFO,"Writing to stdout\n"); | |
153 f=1; | |
29304 | 154 #if defined(__MINGW32__) || defined(__OS2__) |
12921 | 155 setmode(fileno(stdout),O_BINARY); |
156 #endif | |
9794 | 157 } |
158 } else { | |
21705
829b1f30a07f
fix compilation on the most delicious variant of unix (mingw) that lacks S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
nicodvb
parents:
21673
diff
changeset
|
159 mode_t openmode = S_IRUSR|S_IWUSR; |
829b1f30a07f
fix compilation on the most delicious variant of unix (mingw) that lacks S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
nicodvb
parents:
21673
diff
changeset
|
160 #ifndef __MINGW32__ |
829b1f30a07f
fix compilation on the most delicious variant of unix (mingw) that lacks S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
nicodvb
parents:
21673
diff
changeset
|
161 openmode |= S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; |
829b1f30a07f
fix compilation on the most delicious variant of unix (mingw) that lacks S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
nicodvb
parents:
21673
diff
changeset
|
162 #endif |
829b1f30a07f
fix compilation on the most delicious variant of unix (mingw) that lacks S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
nicodvb
parents:
21673
diff
changeset
|
163 f=open(filename,m, openmode); |
9794 | 164 if(f<0) { |
15461
7c272bfba96f
fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents:
15452
diff
changeset
|
165 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_FileNotFound,filename); |
9794 | 166 m_struct_free(&stream_opts,opts); |
9827 | 167 return STREAM_ERROR; |
9794 | 168 } |
169 } | |
170 | |
171 len=lseek(f,0,SEEK_END); lseek(f,0,SEEK_SET); | |
12921 | 172 #ifdef __MINGW32__ |
173 if(f==0 || len == -1) { | |
174 #else | |
9794 | 175 if(len == -1) { |
12921 | 176 #endif |
21656
32c50eb3ff18
in STREAM_WRITE mode open the stream with O_RDWR|O_CREAT, S_IRUSR|S_IWUSR and disable seek_forward for pipes
nicodvb
parents:
19271
diff
changeset
|
177 if(mode == STREAM_READ) stream->seek = seek_forward; |
9794 | 178 stream->type = STREAMTYPE_STREAM; // Must be move to STREAMTYPE_FILE |
29920
4f740437ed2b
Finally rename the STREAM_SEEK define to MP_STREAM_SEEK, there are just too many
reimar
parents:
29304
diff
changeset
|
179 stream->flags |= MP_STREAM_SEEK_FW; |
9794 | 180 } else if(len >= 0) { |
181 stream->seek = seek; | |
182 stream->end_pos = len; | |
183 stream->type = STREAMTYPE_FILE; | |
184 } | |
185 | |
16750
0a31740dd5e6
Use PRI?64 defines as format strings for 64 bit variables.
reimar
parents:
16748
diff
changeset
|
186 mp_msg(MSGT_OPEN,MSGL_V,"[file] File size is %"PRId64" bytes\n", (int64_t)len); |
9794 | 187 |
188 stream->fd = f; | |
189 stream->fill_buffer = fill_buffer; | |
190 stream->write_buffer = write_buffer; | |
21658 | 191 stream->control = control; |
9794 | 192 |
193 m_struct_free(&stream_opts,opts); | |
194 return STREAM_OK; | |
195 } | |
196 | |
25211 | 197 const stream_info_t stream_info_file = { |
9794 | 198 "File", |
199 "file", | |
200 "Albeu", | |
201 "based on the code from ??? (probably Arpi)", | |
202 open_f, | |
203 { "file", "", NULL }, | |
204 &stream_opts, | |
205 1 // Urls are an option string | |
206 }; |