3 * Copyright 2004 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
27 #include <gr_file_source.h>
28 #include <gr_io_signature.h>
30 #include <sys/types.h>
35 // win32 (mingw/msvc) specific
40 #define OUR_O_BINARY O_BINARY
42 #define OUR_O_BINARY 0
44 // should be handled via configure
46 #define OUR_O_LARGEFILE O_LARGEFILE
48 #define OUR_O_LARGEFILE 0
51 gr_file_source::gr_file_source (size_t itemsize, const char *filename, bool repeat)
52 : gr_sync_block ("file_source",
53 gr_make_io_signature (0, 0, 0),
54 gr_make_io_signature (1, 1, itemsize)),
55 d_itemsize (itemsize), d_fp (0), d_repeat (repeat)
57 // we use "open" to use to the O_LARGEFILE flag
60 if ((fd = open (filename, O_RDONLY | OUR_O_LARGEFILE | OUR_O_BINARY)) < 0){
62 throw std::runtime_error ("can't open file");
65 if ((d_fp = fdopen (fd, "rb")) == NULL){
67 throw std::runtime_error ("can't open file");
71 // public constructor that returns a shared_ptr
74 gr_make_file_source (size_t itemsize, const char *filename, bool repeat)
76 return gr_file_source_sptr (new gr_file_source (itemsize, filename, repeat));
79 gr_file_source::~gr_file_source ()
81 fclose ((FILE *) d_fp);
85 gr_file_source::work (int noutput_items,
86 gr_vector_const_void_star &input_items,
87 gr_vector_void_star &output_items)
89 char *o = (char *) output_items[0];
91 int size = noutput_items;
94 i = fread(o, d_itemsize, size, (FILE *) d_fp);
99 if (size == 0) // done
102 if (i > 0) // short read, try again
105 // We got a zero from fread. This is either EOF or error. In
106 // any event, if we're in repeat mode, seek back to the beginning
107 // of the file and try again, else break
112 if (fseek ((FILE *) d_fp, 0, SEEK_SET) == -1) {
113 fprintf(stderr, "[%s] fseek failed\n", __FILE__);
118 if (size > 0){ // EOF or error
119 if (size == noutput_items) // we didn't read anything; say we're done
121 return noutput_items - size; // else return partial result
124 return noutput_items;
128 gr_file_source::seek (long seek_point, int whence)
130 return fseek ((FILE *) d_fp, seek_point * d_itemsize, whence) == 0;