3cff2fd5ac3bfa4db6452027a2c923abebd72e8a
[debian/gnuradio] / gnuradio-core / src / lib / gengen / gr_vector_source_b.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2008 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 // WARNING: this file is machine generated.  Edits will be over written
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 #include <gr_vector_source_b.h>
29 #include <algorithm>
30 #include <gr_io_signature.h>
31 #include <stdexcept>
32
33
34 gr_vector_source_b::gr_vector_source_b (const std::vector<unsigned char> &data, bool repeat, int vlen)
35   : gr_sync_block ("vector_source_b",
36                gr_make_io_signature (0, 0, 0),
37                gr_make_io_signature (1, 1, sizeof (unsigned char) * vlen)),
38     d_data (data),
39     d_repeat (repeat),
40     d_offset (0),
41     d_vlen (vlen)
42 {
43   if ((data.size() % vlen) != 0)
44     throw std::invalid_argument("data length must be a multiple of vlen");
45 }
46
47 int
48 gr_vector_source_b::work (int noutput_items,
49                     gr_vector_const_void_star &input_items,
50                     gr_vector_void_star &output_items)
51 {
52   unsigned char *optr = (unsigned char *) output_items[0];
53
54   if (d_repeat){
55     unsigned int size = d_data.size ();
56     unsigned int offset = d_offset;
57     
58     if (size == 0)
59       return -1;
60     
61     for (int i = 0; i < noutput_items*d_vlen; i++){
62       optr[i] = d_data[offset++];
63       if (offset >= size)
64         offset = 0;
65     }
66     d_offset = offset;
67     return noutput_items;
68   }
69
70   else {
71     if (d_offset >= d_data.size ())
72       return -1;                        // Done!
73
74     unsigned n = std::min ((unsigned) d_data.size () - d_offset,
75                            (unsigned) noutput_items*d_vlen);
76     for (unsigned i = 0; i < n; i++)
77       optr[i] = d_data[d_offset + i];
78
79     d_offset += n;
80     return n/d_vlen;
81   }
82 }
83
84 gr_vector_source_b_sptr
85 gr_make_vector_source_b (const std::vector<unsigned char> &data, bool repeat, int vlen)
86 {
87   return gr_vector_source_b_sptr (new gr_vector_source_b (data, repeat, vlen));
88 }
89