3 * Copyright 2004,2005 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_descriptor_source.h>
28 #include <gr_io_signature.h>
31 #include <sys/types.h>
37 gr_file_descriptor_source::gr_file_descriptor_source (size_t itemsize,
40 : gr_sync_block ("file_descriptor_source",
41 gr_make_io_signature (0, 0, 0),
42 gr_make_io_signature (1, 1, itemsize)),
43 d_itemsize (itemsize), d_fd (fd), d_repeat (repeat),
44 d_residue (new unsigned char[itemsize]), d_residue_len (0)
48 // public constructor that returns a shared_ptr
50 gr_file_descriptor_source_sptr
51 gr_make_file_descriptor_source (size_t itemsize, int fd, bool repeat)
53 return gr_file_descriptor_source_sptr (
54 new gr_file_descriptor_source (itemsize, fd, repeat));
57 gr_file_descriptor_source::~gr_file_descriptor_source ()
64 gr_file_descriptor_source::work (int noutput_items,
65 gr_vector_const_void_star &input_items,
66 gr_vector_void_star &output_items)
68 assert (noutput_items > 0);
70 char *o = (char *) output_items[0];
74 int r = read_items (o, noutput_items - nread);
79 perror ("file_descriptor_source[read]");
83 else if (r == 0){ // end of file
88 if (lseek (d_fd, 0, SEEK_SET) == -1){
89 perror ("file_descriptor_source[lseek]");
101 if (nread == 0) // EOF
108 gr_file_descriptor_source::read_items (char *buf, int nitems)
111 assert (d_residue_len < d_itemsize);
115 if (d_residue_len > 0){
116 memcpy (buf, d_residue, d_residue_len);
117 nbytes_read = d_residue_len;
121 int r = read (d_fd, buf + nbytes_read, nitems * d_itemsize - nbytes_read);
123 handle_residue (buf, nbytes_read);
127 r = handle_residue (buf, r + nbytes_read);
129 if (r == 0) // block until we get something
130 return read_items (buf, nitems);
136 gr_file_descriptor_source::handle_residue (char *buf, int nbytes_read)
138 assert (nbytes_read >= 0);
139 int nitems_read = nbytes_read / d_itemsize;
140 d_residue_len = nbytes_read % d_itemsize;
141 if (d_residue_len > 0){
142 // fprintf (stderr, "handle_residue: %d\n", d_residue_len);
143 memcpy (d_residue, buf + nbytes_read - d_residue_len, d_residue_len);