Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / io / gr_file_source.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004 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_source.h>
28 #include <gr_io_signature.h>
29 #include <cstdio>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <stdexcept>
34 #include <stdio.h>
35
36 // win32 (mingw/msvc) specific
37 #ifdef HAVE_IO_H
38 #include <io.h>
39 #endif
40 #ifdef O_BINARY
41 #define OUR_O_BINARY O_BINARY
42 #else
43 #define OUR_O_BINARY 0
44 #endif
45 // should be handled via configure
46 #ifdef O_LARGEFILE
47 #define OUR_O_LARGEFILE O_LARGEFILE
48 #else
49 #define OUR_O_LARGEFILE 0
50 #endif
51
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)
57 {
58   // we use "open" to use to the O_LARGEFILE flag
59   
60   int fd;
61   if ((fd = open (filename, O_RDONLY | OUR_O_LARGEFILE | OUR_O_BINARY)) < 0){
62     perror (filename);
63     throw std::runtime_error ("can't open file");
64   }
65
66   if ((d_fp = fdopen (fd, "rb")) == NULL){
67     perror (filename);
68     throw std::runtime_error ("can't open file");
69   }
70 }
71
72 // public constructor that returns a shared_ptr
73
74 gr_file_source_sptr
75 gr_make_file_source (size_t itemsize, const char *filename, bool repeat)
76 {
77   return gr_file_source_sptr (new gr_file_source (itemsize, filename, repeat));
78 }
79
80 gr_file_source::~gr_file_source ()
81 {
82   fclose ((FILE *) d_fp);
83 }
84
85 int 
86 gr_file_source::work (int noutput_items,
87                       gr_vector_const_void_star &input_items,
88                       gr_vector_void_star &output_items)
89 {
90   char *o = (char *) output_items[0];
91   int i;
92   int size = noutput_items;
93
94   while (size) {
95     i = fread(o, d_itemsize, size, (FILE *) d_fp);
96     
97     size -= i;
98     o += i * d_itemsize;
99
100     if (size == 0)              // done
101       break;
102
103     if (i > 0)                  // short read, try again
104       continue;
105
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
109
110     if (!d_repeat)
111       break;
112
113     if (fseek ((FILE *) d_fp, 0, SEEK_SET) == -1) {
114       fprintf(stderr, "[%s] fseek failed\n", __FILE__);
115       exit(-1);
116     }
117   }
118
119   if (size > 0){                        // EOF or error
120     if (size == noutput_items)          // we didn't read anything; say we're done
121       return -1;
122     return noutput_items - size;        // else return partial result
123   }
124
125   return noutput_items;
126 }
127
128 bool
129 gr_file_source::seek (long seek_point, int whence)
130 {
131    return fseek ((FILE *) d_fp, seek_point * d_itemsize, whence) == 0;
132 }