Imported Upstream version 3.2.2
[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 3, 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 #include <stdio.h>
36 #include <string.h>
37
38
39 gr_file_descriptor_source::gr_file_descriptor_source (size_t itemsize,
40                                                       int fd,
41                                                       bool repeat)
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)
47 {
48 }
49
50 // public constructor that returns a shared_ptr
51
52 gr_file_descriptor_source_sptr
53 gr_make_file_descriptor_source (size_t itemsize, int fd, bool repeat)
54 {
55   return gr_file_descriptor_source_sptr (
56                new gr_file_descriptor_source (itemsize, fd, repeat));
57 }
58
59 gr_file_descriptor_source::~gr_file_descriptor_source ()
60 {
61   close (d_fd);
62   delete [] d_residue;
63 }
64
65 int
66 gr_file_descriptor_source::work (int noutput_items,
67                                  gr_vector_const_void_star &input_items,
68                                  gr_vector_void_star &output_items)
69 {
70   assert (noutput_items > 0);
71
72   char *o = (char *) output_items[0];
73   int nread = 0;
74
75   while (1){
76     int r = read_items (o, noutput_items - nread);
77     if (r == -1){
78       if (errno == EINTR)
79         continue;
80       else {
81         perror ("file_descriptor_source[read]");
82         return -1;
83       }
84     }
85     else if (r == 0){     // end of file
86       if (!d_repeat)
87         break;
88       else {
89         flush_residue ();
90         if (lseek (d_fd, 0, SEEK_SET) == -1){
91           perror ("file_descriptor_source[lseek]");
92           return -1;
93         }
94       }
95     }
96     else {
97       o += r * d_itemsize;
98       nread += r;
99       break;
100     }
101   }
102
103   if (nread == 0)       // EOF
104     return -1;
105
106   return nread;
107 }
108
109 int
110 gr_file_descriptor_source::read_items (char *buf, int nitems)
111 {
112   assert (nitems > 0);
113   assert (d_residue_len < d_itemsize);
114
115   int nbytes_read = 0;
116
117   if (d_residue_len > 0){
118     memcpy (buf, d_residue, d_residue_len);
119     nbytes_read = d_residue_len;
120     d_residue_len = 0;
121   }
122
123   int r = read (d_fd, buf + nbytes_read, nitems * d_itemsize - nbytes_read);
124   if (r <= 0){
125     handle_residue (buf, nbytes_read);
126     return r;
127   }
128
129   r = handle_residue (buf, r + nbytes_read);
130
131   if (r == 0)                   // block until we get something
132     return read_items (buf, nitems);
133   
134   return r;
135 }  
136
137 int
138 gr_file_descriptor_source::handle_residue (char *buf, int nbytes_read)
139 {
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);
146   }
147   return nitems_read;
148 }