1 /* -*- c++ -*- */
2 /*
3  * Copyright 2012 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 #ifndef INCLUDED_BLOCKS_FILE_META_SINK_IMPL_H
24 #define INCLUDED_BLOCKS_FILE_META_SINK_IMPL_H
25 
26 #include <gnuradio/blocks/file_meta_sink.h>
27 #include <pmt/pmt.h>
28 
29 using namespace pmt;
30 
31 namespace gr {
32 namespace blocks {
33 
34 class file_meta_sink_impl : public file_meta_sink
35 {
36 private:
37     enum meta_state_t { STATE_INLINE = 0, STATE_DETACHED };
38 
39     size_t d_itemsize;
40     double d_samp_rate;
41     double d_relative_rate;
42     size_t d_max_seg_size;
43     size_t d_total_seg_size;
44     pmt_t d_header;
45     pmt_t d_extra;
46     size_t d_extra_size;
47     bool d_updated;
48     bool d_unbuffered;
49 
50     FILE *d_new_fp, *d_new_hdr_fp;
51     FILE *d_fp, *d_hdr_fp;
52     meta_state_t d_state;
53 
54 protected:
55     void write_header(FILE* fp, pmt_t header, pmt_t extra);
56     void update_header(pmt_t key, pmt_t value);
57     void update_last_header();
58     void update_last_header_inline();
59     void update_last_header_detached();
60     void write_and_update();
61     void update_rx_time();
62 
63     bool _open(FILE** fp, const char* filename);
64 
65 public:
66     file_meta_sink_impl(size_t itemsize,
67                         const std::string& filename,
68                         double samp_rate = 1,
69                         double relative_rate = 1,
70                         gr_file_types type = GR_FILE_FLOAT,
71                         bool complex = true,
72                         size_t max_segment_size = 1000000,
73                         pmt::pmt_t extra_dict = pmt::make_dict(),
74                         bool detached_header = false);
75     ~file_meta_sink_impl();
76 
77     bool open(const std::string& filename);
78     void close();
79     void do_update();
80 
set_unbuffered(bool unbuffered)81     void set_unbuffered(bool unbuffered) { d_unbuffered = unbuffered; }
82 
83     int work(int noutput_items,
84              gr_vector_const_void_star& input_items,
85              gr_vector_void_star& output_items);
86 };
87 
88 } /* namespace blocks */
89 } /* namespace gr */
90 
91 #endif /* INCLUDED_BLOCKS_FILE_META_SINK_IMPL_H */
92