1 /* -*- c++ -*- */
2 /*
3  * Copyright 2005,2007,2010,2013 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 "skiphead_impl.h"
28 #include <gnuradio/io_signature.h>
29 #include <string.h>
30 
31 namespace gr {
32 namespace blocks {
33 
make(size_t itemsize,uint64_t nitems_to_skip)34 skiphead::sptr skiphead::make(size_t itemsize, uint64_t nitems_to_skip)
35 {
36     return gnuradio::get_initial_sptr(new skiphead_impl(itemsize, nitems_to_skip));
37 }
38 
skiphead_impl(size_t itemsize,uint64_t nitems_to_skip)39 skiphead_impl::skiphead_impl(size_t itemsize, uint64_t nitems_to_skip)
40     : block("skiphead",
41             io_signature::make(1, 1, itemsize),
42             io_signature::make(1, 1, itemsize)),
43       d_nitems_to_skip(nitems_to_skip),
44       d_nitems(0)
45 {
46     // Reserve space for a few tags to avoid constant re-allocation
47     // in the call to get_tags_in_window
48     d_tags.reserve(8);
49 
50     // We'll handle propagating tags our selves to handle shifting offsets of tags
51     set_tag_propagation_policy(TPP_DONT);
52 }
53 
~skiphead_impl()54 skiphead_impl::~skiphead_impl() {}
55 
general_work(int noutput_items,gr_vector_int & ninput_items_,gr_vector_const_void_star & input_items,gr_vector_void_star & output_items)56 int skiphead_impl::general_work(int noutput_items,
57                                 gr_vector_int& ninput_items_,
58                                 gr_vector_const_void_star& input_items,
59                                 gr_vector_void_star& output_items)
60 {
61     const char* in = (const char*)input_items[0];
62     char* out = (char*)output_items[0];
63 
64     int ninput_items = std::min(ninput_items_[0], noutput_items);
65     int ii = 0; // input index
66 
67     while (ii < ninput_items) {
68         uint64_t ni_total = ii + d_nitems; // total items processed so far
69         if (ni_total < d_nitems_to_skip) { // need to skip some more
70 
71             int n_to_skip =
72                 (int)std::min(d_nitems_to_skip - ni_total, (uint64_t)(ninput_items - ii));
73             ii += n_to_skip;
74         }
75 
76         else { // nothing left to skip. copy away
77             // Grab all tags in the window and shift their offsets appropriately
78             get_tags_in_window(d_tags, 0, ii, ninput_items);
79             for (std::vector<tag_t>::iterator it = d_tags.begin(); it != d_tags.end();
80                  it++) {
81                 (*it).offset -= d_nitems_to_skip;
82                 add_item_tag(0, *it);
83             }
84             int n_to_copy = ninput_items - ii;
85             if (n_to_copy > 0) {
86                 size_t itemsize = output_signature()->sizeof_stream_item(0);
87                 memcpy(out, in + (ii * itemsize), n_to_copy * itemsize);
88             }
89 
90             d_nitems += ninput_items;
91             consume_each(ninput_items);
92             return n_to_copy;
93         }
94     }
95 
96     d_nitems += ninput_items;
97     consume_each(ninput_items);
98     return 0;
99 }
100 
101 } /* namespace blocks */
102 } /* namespace gr */
103