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>
36 // win32 (mingw/msvc) specific
41 #define OUR_O_BINARY O_BINARY
43 #define OUR_O_BINARY 0
45 // should be handled via configure
47 #define OUR_O_LARGEFILE O_LARGEFILE
49 #define OUR_O_LARGEFILE 0
52 gr_file_source::gr_file_source (size_t itemsize, const char *filename, bool repeat)
53 : gr_sync_block ("file_source",
54 gr_make_io_signature (0, 0, 0),
55 gr_make_io_signature (1, 1, itemsize)),
56 d_itemsize (itemsize), d_fp (0), d_repeat (repeat)
58 // we use "open" to use to the O_LARGEFILE flag
61 if ((fd = open (filename, O_RDONLY | OUR_O_LARGEFILE | OUR_O_BINARY)) < 0){
63 throw std::runtime_error ("can't open file");
66 if ((d_fp = fdopen (fd, "rb")) == NULL){
68 throw std::runtime_error ("can't open file");
72 // public constructor that returns a shared_ptr
75 gr_make_file_source (size_t itemsize, const char *filename, bool repeat)
77 return gr_file_source_sptr (new gr_file_source (itemsize, filename, repeat));
80 gr_file_source::~gr_file_source ()
82 fclose ((FILE *) d_fp);
86 gr_file_source::work (int noutput_items,
87 gr_vector_const_void_star &input_items,
88 gr_vector_void_star &output_items)
90 char *o = (char *) output_items[0];
92 int size = noutput_items;
95 i = fread(o, d_itemsize, size, (FILE *) d_fp);
100 if (size == 0) // done
103 if (i > 0) // short read, try again
106 // We got a zero from fread. This is either EOF or error. In
107 // any event, if we're in repeat mode, seek back to the beginning
108 // of the file and try again, else break
113 if (fseek ((FILE *) d_fp, 0, SEEK_SET) == -1) {
114 fprintf(stderr, "[%s] fseek failed\n", __FILE__);
119 if (size > 0){ // EOF or error
120 if (size == noutput_items) // we didn't read anything; say we're done
122 return noutput_items - size; // else return partial result
125 return noutput_items;
129 gr_file_source::seek (long seek_point, int whence)
131 return fseek ((FILE *) d_fp, seek_point * d_itemsize, whence) == 0;