3 * Copyright 2004,2006 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_sink.h>
28 #include <gr_io_signature.h>
30 #include <sys/types.h>
35 // win32 (mingw/msvc) specific
40 #define OUR_O_BINARY O_BINARY
42 #define OUR_O_BINARY 0
45 // should be handled via configure
47 #define OUR_O_LARGEFILE O_LARGEFILE
49 #define OUR_O_LARGEFILE 0
52 gr_file_sink::gr_file_sink(size_t itemsize, const char *filename)
53 : gr_sync_block ("file_sink",
54 gr_make_io_signature(1, 1, itemsize),
55 gr_make_io_signature(0, 0, 0)),
56 d_itemsize(itemsize), d_fp(0), d_new_fp(0), d_updated(false)
59 throw std::runtime_error ("can't open file");
63 gr_make_file_sink (size_t itemsize, const char *filename)
65 return gr_file_sink_sptr (new gr_file_sink (itemsize, filename));
68 gr_file_sink::~gr_file_sink ()
72 fclose((FILE *) d_fp);
78 gr_file_sink::open(const char *filename)
80 omni_mutex_lock l(d_mutex); // hold mutex for duration of this function
82 // we use the open system call to get access to the O_LARGEFILE flag.
84 if ((fd = ::open (filename,
85 O_WRONLY|O_CREAT|O_TRUNC|OUR_O_LARGEFILE|OUR_O_BINARY, 0664)) < 0){
90 if (d_new_fp){ // if we've already got a new one open, close it
91 fclose((FILE *) d_new_fp);
95 if ((d_new_fp = fdopen (fd, "wb")) == NULL){
97 ::close(fd); // don't leak file descriptor if fdopen fails.
101 return d_new_fp != 0;
105 gr_file_sink::close()
107 omni_mutex_lock l(d_mutex); // hold mutex for duration of this function
110 fclose((FILE *) d_new_fp);
117 gr_file_sink::work (int noutput_items,
118 gr_vector_const_void_star &input_items,
119 gr_vector_void_star &output_items)
121 char *inbuf = (char *) input_items[0];
125 omni_mutex_lock l(d_mutex); // hold mutex for duration of this block
127 fclose((FILE *)d_fp);
128 d_fp = d_new_fp; // install new file pointer
134 return noutput_items; // drop output on the floor
136 while (nwritten < noutput_items){
137 int count = fwrite (inbuf, d_itemsize, noutput_items - nwritten, (FILE *) d_fp);
138 if (count == 0) // FIXME add error handling
141 inbuf += count * d_itemsize;