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>
39 gr_file_descriptor_source::gr_file_descriptor_source (size_t itemsize,
42 : gr_sync_block ("file_descriptor_source",
43 gr_make_io_signature (0, 0, 0),
44 gr_make_io_signature (1, 1, itemsize)),
45 d_itemsize (itemsize), d_fd (fd), d_repeat (repeat),
46 d_residue (new unsigned char[itemsize]), d_residue_len (0)
50 // public constructor that returns a shared_ptr
52 gr_file_descriptor_source_sptr
53 gr_make_file_descriptor_source (size_t itemsize, int fd, bool repeat)
55 return gr_file_descriptor_source_sptr (
56 new gr_file_descriptor_source (itemsize, fd, repeat));
59 gr_file_descriptor_source::~gr_file_descriptor_source ()
66 gr_file_descriptor_source::work (int noutput_items,
67 gr_vector_const_void_star &input_items,
68 gr_vector_void_star &output_items)
70 assert (noutput_items > 0);
72 char *o = (char *) output_items[0];
76 int r = read_items (o, noutput_items - nread);
81 perror ("file_descriptor_source[read]");
85 else if (r == 0){ // end of file
90 if (lseek (d_fd, 0, SEEK_SET) == -1){
91 perror ("file_descriptor_source[lseek]");
103 if (nread == 0) // EOF
110 gr_file_descriptor_source::read_items (char *buf, int nitems)
113 assert (d_residue_len < d_itemsize);
117 if (d_residue_len > 0){
118 memcpy (buf, d_residue, d_residue_len);
119 nbytes_read = d_residue_len;
123 int r = read (d_fd, buf + nbytes_read, nitems * d_itemsize - nbytes_read);
125 handle_residue (buf, nbytes_read);
129 r = handle_residue (buf, r + nbytes_read);
131 if (r == 0) // block until we get something
132 return read_items (buf, nitems);
138 gr_file_descriptor_source::handle_residue (char *buf, int nbytes_read)
140 assert (nbytes_read >= 0);
141 int nitems_read = nbytes_read / d_itemsize;
142 d_residue_len = nbytes_read % d_itemsize;
143 if (d_residue_len > 0){
144 // fprintf (stderr, "handle_residue: %d\n", d_residue_len);
145 memcpy (d_residue, buf + nbytes_read - d_residue_len, d_residue_len);