Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / io / gr_file_descriptor_source.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2005 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
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 2, or (at your option)
10  * any later version.
11  * 
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.
16  * 
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gr_file_descriptor_source.h>
28 #include <gr_io_signature.h>
29 #include <cstdio>
30 #include <errno.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <stdexcept>
35
36
37 gr_file_descriptor_source::gr_file_descriptor_source (size_t itemsize,
38                                                       int fd,
39                                                       bool repeat)
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)
45 {
46 }
47
48 // public constructor that returns a shared_ptr
49
50 gr_file_descriptor_source_sptr
51 gr_make_file_descriptor_source (size_t itemsize, int fd, bool repeat)
52 {
53   return gr_file_descriptor_source_sptr (
54                new gr_file_descriptor_source (itemsize, fd, repeat));
55 }
56
57 gr_file_descriptor_source::~gr_file_descriptor_source ()
58 {
59   close (d_fd);
60   delete [] d_residue;
61 }
62
63 int
64 gr_file_descriptor_source::work (int noutput_items,
65                                  gr_vector_const_void_star &input_items,
66                                  gr_vector_void_star &output_items)
67 {
68   assert (noutput_items > 0);
69
70   char *o = (char *) output_items[0];
71   int nread = 0;
72
73   while (1){
74     int r = read_items (o, noutput_items - nread);
75     if (r == -1){
76       if (errno == EINTR)
77         continue;
78       else {
79         perror ("file_descriptor_source[read]");
80         return -1;
81       }
82     }
83     else if (r == 0){     // end of file
84       if (!d_repeat)
85         break;
86       else {
87         flush_residue ();
88         if (lseek (d_fd, 0, SEEK_SET) == -1){
89           perror ("file_descriptor_source[lseek]");
90           return -1;
91         }
92       }
93     }
94     else {
95       o += r * d_itemsize;
96       nread += r;
97       break;
98     }
99   }
100
101   if (nread == 0)       // EOF
102     return -1;
103
104   return nread;
105 }
106
107 int
108 gr_file_descriptor_source::read_items (char *buf, int nitems)
109 {
110   assert (nitems > 0);
111   assert (d_residue_len < d_itemsize);
112
113   int nbytes_read = 0;
114
115   if (d_residue_len > 0){
116     memcpy (buf, d_residue, d_residue_len);
117     nbytes_read = d_residue_len;
118     d_residue_len = 0;
119   }
120
121   int r = read (d_fd, buf + nbytes_read, nitems * d_itemsize - nbytes_read);
122   if (r <= 0){
123     handle_residue (buf, nbytes_read);
124     return r;
125   }
126
127   r = handle_residue (buf, r + nbytes_read);
128
129   if (r == 0)                   // block until we get something
130     return read_items (buf, nitems);
131   
132   return r;
133 }  
134
135 int
136 gr_file_descriptor_source::handle_residue (char *buf, int nbytes_read)
137 {
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);
144   }
145   return nitems_read;
146 }