Imported Upstream version 3.0
[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 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_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
35 // win32 (mingw/msvc) specific
36 #ifdef HAVE_IO_H
37 #include <io.h>
38 #endif
39 #ifdef O_BINARY
40 #define OUR_O_BINARY O_BINARY
41 #else
42 #define OUR_O_BINARY 0
43 #endif
44 // should be handled via configure
45 #ifdef O_LARGEFILE
46 #define OUR_O_LARGEFILE O_LARGEFILE
47 #else
48 #define OUR_O_LARGEFILE 0
49 #endif
50
51 gr_file_source::gr_file_source (size_t itemsize, const char *filename, bool repeat)
52   : gr_sync_block ("file_source",
53                    gr_make_io_signature (0, 0, 0),
54                    gr_make_io_signature (1, 1, itemsize)),
55     d_itemsize (itemsize), d_fp (0), d_repeat (repeat)
56 {
57   // we use "open" to use to the O_LARGEFILE flag
58   
59   int fd;
60   if ((fd = open (filename, O_RDONLY | OUR_O_LARGEFILE | OUR_O_BINARY)) < 0){
61     perror (filename);
62     throw std::runtime_error ("can't open file");
63   }
64
65   if ((d_fp = fdopen (fd, "rb")) == NULL){
66     perror (filename);
67     throw std::runtime_error ("can't open file");
68   }
69 }
70
71 // public constructor that returns a shared_ptr
72
73 gr_file_source_sptr
74 gr_make_file_source (size_t itemsize, const char *filename, bool repeat)
75 {
76   return gr_file_source_sptr (new gr_file_source (itemsize, filename, repeat));
77 }
78
79 gr_file_source::~gr_file_source ()
80 {
81   fclose ((FILE *) d_fp);
82 }
83
84 int 
85 gr_file_source::work (int noutput_items,
86                       gr_vector_const_void_star &input_items,
87                       gr_vector_void_star &output_items)
88 {
89   char *o = (char *) output_items[0];
90   int i;
91   int size = noutput_items;
92
93   while (size) {
94     i = fread(o, d_itemsize, size, (FILE *) d_fp);
95     
96     size -= i;
97     o += i * d_itemsize;
98
99     if (size == 0)              // done
100       break;
101
102     if (i > 0)                  // short read, try again
103       continue;
104
105     // We got a zero from fread.  This is either EOF or error.  In
106     // any event, if we're in repeat mode, seek back to the beginning
107     // of the file and try again, else break
108
109     if (!d_repeat)
110       break;
111
112     if (fseek ((FILE *) d_fp, 0, SEEK_SET) == -1) {
113       fprintf(stderr, "[%s] fseek failed\n", __FILE__);
114       exit(-1);
115     }
116   }
117
118   if (size > 0){                        // EOF or error
119     if (size == noutput_items)          // we didn't read anything; say we're done
120       return -1;
121     return noutput_items - size;        // else return partial result
122   }
123
124   return noutput_items;
125 }
126
127 bool
128 gr_file_source::seek (long seek_point, int whence)
129 {
130    return fseek ((FILE *) d_fp, seek_point * d_itemsize, whence) == 0;
131 }