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