Updated license from GPL version 2 or later to GPL version 3 or later.
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_skiphead.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2005,2007 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 #include <gr_skiphead.h>
27 #include <gr_io_signature.h>
28
29 gr_skiphead::gr_skiphead (size_t itemsize, size_t nitems_to_skip)
30   : gr_block ("skiphead",
31               gr_make_io_signature(1, 1, itemsize),
32               gr_make_io_signature(1, 1, itemsize)),
33     d_nitems_to_skip(nitems_to_skip), d_nitems(0)
34 {
35 }
36
37 gr_skiphead_sptr
38 gr_make_skiphead (size_t itemsize, size_t nitems_to_skip)
39 {
40   return gr_skiphead_sptr (new gr_skiphead (itemsize, nitems_to_skip));
41 }
42
43 int
44 gr_skiphead::general_work(int noutput_items,
45                           gr_vector_int &ninput_items_ignored,
46                           gr_vector_const_void_star &input_items,
47                           gr_vector_void_star &output_items)
48 {
49   const char *in = (const char *) input_items[0];
50   char *out = (char *) output_items[0];
51
52   int ninput_items = noutput_items;     // we've got at least this many input items
53   int ii = 0;                           // input index
54
55   while (ii < ninput_items){
56
57     long long ni_total = ii + d_nitems;         // total items processed so far
58     if (ni_total < d_nitems_to_skip){           // need to skip some more
59
60       int n_to_skip = (int) std::min(d_nitems_to_skip - ni_total,
61                                      (long long)(ninput_items - ii));
62       ii += n_to_skip;
63     }
64
65     else {              // nothing left to skip.  copy away
66
67       int n_to_copy = ninput_items - ii;
68       if (n_to_copy > 0){
69         size_t itemsize = output_signature()->sizeof_stream_item(0);
70         memcpy(out, in + (ii*itemsize), n_to_copy*itemsize);
71       }
72
73       d_nitems += ninput_items;
74       consume_each(ninput_items);
75       return n_to_copy;
76     }
77   }
78
79   d_nitems += ninput_items;
80   consume_each(ninput_items);
81   return 0;
82 }