Fix compiler warnings across the tree. Adds --enable-warnings-as-errors configure...
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_stream_mux.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006 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 <gr_stream_mux.h>
28 #include <gr_io_signature.h>
29 #include <string.h>
30
31 #define VERBOSE 0
32
33 gr_stream_mux_sptr
34 gr_make_stream_mux (size_t itemsize, const std::vector<int> &lengths)
35 {
36   return gr_stream_mux_sptr (new gr_stream_mux (itemsize, lengths));
37 }
38
39 gr_stream_mux::gr_stream_mux (size_t itemsize, const std::vector<int> &lengths)
40   : gr_block ("stream_mux",
41               gr_make_io_signature (1, -1, itemsize),
42               gr_make_io_signature (1, 1, itemsize)),
43     d_itemsize(itemsize),
44     d_stream(0),
45     d_residual(0),
46     d_lengths(lengths)
47 {
48   if(d_lengths[d_stream] == 0) {
49     increment_stream();
50   }
51   d_residual = d_lengths[d_stream];
52 }    
53
54 gr_stream_mux::~gr_stream_mux(void)
55 {
56 }
57
58 void 
59 gr_stream_mux::forecast (int noutput_items, gr_vector_int &ninput_items_required)
60 {
61   unsigned ninputs = ninput_items_required.size ();
62   for (unsigned i = 0; i < ninputs; i++)
63     ninput_items_required[i] = (d_lengths[i] == 0 ? 0 : 1);
64 }
65
66 void gr_stream_mux::increment_stream()
67 {
68   do {
69     d_stream = (d_stream+1) % d_lengths.size();
70   } while(d_lengths[d_stream] == 0);
71   
72   d_residual = d_lengths[d_stream];
73 }
74
75 int
76 gr_stream_mux::general_work(int noutput_items,
77                             gr_vector_int &ninput_items,
78                             gr_vector_const_void_star &input_items,
79                             gr_vector_void_star &output_items)
80 {
81     char *out = (char *) output_items[0];
82     const char *in;
83     int out_index = 0;
84     std::vector<int> input_index(d_lengths.size(), 0);
85
86     if(VERBOSE) {
87       printf("mux: nouput_items: %d   d_stream: %d\n", noutput_items, d_stream);
88       for(size_t i = 0; i < d_lengths.size(); i++)
89         printf("\tninput_items[%zu]: %d\n", i, ninput_items[i]);
90     }
91
92     while (1) {
93       int r = std::min(noutput_items - out_index,
94                        std::min(d_residual,
95                                 ninput_items[d_stream] - input_index[d_stream]));
96       if(VERBOSE) {
97         printf("mux: r=%d\n", r);
98         printf("\tnoutput_items - out_index: %d\n", 
99                noutput_items - out_index);
100         printf("\td_residual: %d\n", 
101                d_residual);
102         printf("\tninput_items[d_stream] - input_index[d_stream]: %d\n", 
103                ninput_items[d_stream] - input_index[d_stream]);
104       }
105
106       if(r <= 0) {
107         return out_index;
108       }
109
110       in = (const char *) input_items[d_stream] + input_index[d_stream]*d_itemsize;
111       
112       memcpy(&out[out_index*d_itemsize], in, r*d_itemsize);
113       out_index += r;
114       input_index[d_stream] += r;
115       d_residual -= r;
116
117       consume(d_stream, r);
118       
119       if(d_residual == 0) {
120         increment_stream();
121       }
122     }
123 }