Fix oggplay-dump-first-frame
[liboggplay.git] / src / liboggplay / oggplay_file_reader.c
blobf15e2f691df031931225690a18b7bc94ad4f8263
1 /*
2 Copyright (C) 2003 Commonwealth Scientific and Industrial Research
3 Organisation (CSIRO) Australia
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 - Neither the name of CSIRO Australia nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ORGANISATION OR
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * oggplay_file_reader.c
36 * Shane Stephens <shane.stephens@annodex.net>
37 * Michael Martin
40 #include "oggplay_private.h"
41 #include "oggplay_file_reader.h"
43 #include <stdlib.h>
44 #include <string.h>
46 OggPlayErrorCode
47 oggplay_file_reader_initialise(OggPlayReader * opr, int block) {
49 OggPlayFileReader * me = (OggPlayFileReader *)opr;
50 (void)block; /* unused for file readers */
52 if (me == NULL) {
53 return E_OGGPLAY_BAD_READER;
56 me->file = fopen(me->file_name, "rb");
58 if (me->file == NULL) {
59 return E_OGGPLAY_BAD_INPUT;
62 fseek(me->file, 0L, SEEK_END);
63 me->size = ftell(me->file);
64 fseek(me->file, 0L, SEEK_SET);
66 me->current_position = 0;
68 return E_OGGPLAY_OK;
71 OggPlayErrorCode
72 oggplay_file_reader_destroy(OggPlayReader * opr) {
74 OggPlayFileReader * me;
76 if (opr == NULL) {
77 return E_OGGPLAY_BAD_READER;
80 me = (OggPlayFileReader *)opr;
82 if (me->file != NULL) {
83 fclose(me->file);
86 oggplay_free(me);
88 return E_OGGPLAY_OK;
91 int
92 oggplay_file_reader_available(OggPlayReader * opr, ogg_int64_t current_bytes,
93 ogg_int64_t current_time) {
95 OggPlayFileReader *me = (OggPlayFileReader *)opr;
97 if (me == NULL) {
98 return -1;
101 return me->size;
106 oggplay_file_reader_finished_retrieving(OggPlayReader *opr) {
108 return 1;
113 static size_t
114 oggplay_file_reader_io_read(void * user_handle, void * buf, size_t n) {
116 OggPlayFileReader *me = (OggPlayFileReader *)user_handle;
117 int r;
119 if (me == NULL) {
120 return -1;
123 r = fread(buf, 1, n, me->file);
124 if (r > 0) {
125 me->current_position += r;
128 return r;
131 static int
132 oggplay_file_reader_io_seek(void * user_handle, long offset, int whence) {
134 OggPlayFileReader * me = (OggPlayFileReader *)user_handle;
135 int r;
137 if (me == NULL) {
138 return -1;
141 r = fseek(me->file, offset, whence);
142 me->current_position = ftell(me->file);
143 return r;
147 static long
148 oggplay_file_reader_io_tell(void * user_handle) {
150 OggPlayFileReader * me = (OggPlayFileReader *)user_handle;
152 if (me == NULL) {
153 return -1;
156 return ftell(me->file);
160 OggPlayReader *
161 oggplay_file_reader_new(const char *file_name) {
163 OggPlayFileReader * me = oggplay_malloc (sizeof (OggPlayFileReader));
165 if (me == NULL)
166 return NULL;
168 me->current_position = 0;
169 me->file_name = file_name;
170 me->file = NULL;
172 me->functions.initialise = &oggplay_file_reader_initialise;
173 me->functions.destroy = &oggplay_file_reader_destroy;
174 me->functions.available = &oggplay_file_reader_available;
175 me->functions.finished_retrieving = &oggplay_file_reader_finished_retrieving;
176 me->functions.seek = NULL;
177 me->functions.duration = NULL;
178 me->functions.io_read = &oggplay_file_reader_io_read;
179 me->functions.io_seek = &oggplay_file_reader_io_seek;
180 me->functions.io_tell = &oggplay_file_reader_io_tell;
181 me->functions.duration = NULL;
183 return (OggPlayReader *)me;