GNU Radio's TEST Package
arg_helpers.h
Go to the documentation of this file.
1/* -*- c++ -*- */
2/*
3 * Copyright 2012 Dimitri Stolnikov <horiz0n@gmx.net>
4 *
5 * GNU Radio is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3, or (at your option)
8 * any later version.
9 *
10 * GNU Radio is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with GNU Radio; see the file COPYING. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#ifndef OSMOSDR_ARG_HELPERS_H
22#define OSMOSDR_ARG_HELPERS_H
23
24#include <iostream>
25#include <vector>
26#include <map>
27
28#include <gnuradio/io_signature.h>
29
30#include <boost/lexical_cast.hpp>
31#include <boost/tokenizer.hpp>
32#include <ciso646>
33
34typedef std::map< std::string, std::string > dict_t;
35typedef std::pair< std::string, std::string > pair_t;
36
37inline std::string dict_to_args_string( const dict_t &d )
38{
39 std::string out;
40 for (const pair_t pair : d)
41 {
42 if (not out.empty()) out += ",";
43 out += pair.first + "='" + pair.second + "'";
44 }
45 return out;
46}
47
48inline std::vector< std::string > args_to_vector( const std::string &args )
49{
50 std::vector< std::string > result;
51
52 boost::escaped_list_separator<char> separator("\\", " ", "'");
53 typedef boost::tokenizer< boost::escaped_list_separator<char> > tokenizer_t;
54 tokenizer_t tokens(args, separator);
55
56 for (std::string token : tokens)
57 result.push_back(token);
58
59 return result;
60}
61
62inline std::vector< std::string > params_to_vector( const std::string &params )
63{
64 std::vector< std::string > result;
65
66 boost::escaped_list_separator<char> separator("\\", ",", "'");
67 typedef boost::tokenizer< boost::escaped_list_separator<char> > tokenizer_t;
68 tokenizer_t tokens(params, separator);
69
70 for (std::string token : tokens)
71 result.push_back(token);
72
73 return result;
74}
75
76inline pair_t param_to_pair( const std::string &param )
77{
78 pair_t result;
79
80 std::size_t pos = param.find('=');
81 if(pos != std::string::npos)
82 {
83 result.first = param.substr(0, pos);
84 result.second = param.substr(pos + 1);
85 }
86 else
87 {
88 result.first = param;
89 result.second = "";
90 }
91
92 return result;
93}
94
95inline dict_t params_to_dict( const std::string &params )
96{
97 dict_t result;
98
99 std::vector< std::string > param_list = params_to_vector( params );
100 for (std::string param : param_list)
101 {
102 pair_t pair = param_to_pair( param );
103 std::string value = pair.second;
104 if (value.length() && value[0] == '\'' && value[ value.length() - 1 ] == '\'')
105 value = value.substr(1, value.length() - 1);
106 result[ pair.first ] = value;
107 }
108
109 return result;
110}
111
113{
114 bool operator ()(const std::string &str)
115 {
116 return str.find("numchan=") == 0;
117 }
118};
119
120inline gr::io_signature::sptr args_to_io_signature( const std::string &args )
121{
122 size_t max_nchan = 0;
123 size_t dev_nchan = 0;
124 std::vector< std::string > arg_list = args_to_vector( args );
125
126 for (std::string arg : arg_list)
127 {
128 if ( arg.find( "numchan=" ) == 0 ) // try to parse global nchan value
129 {
130 pair_t pair = param_to_pair( arg );
131 max_nchan = boost::lexical_cast<size_t>( pair.second );
132 }
133 }
134
135 arg_list.erase( std::remove_if( // remove any global nchan tokens
136 arg_list.begin(),
137 arg_list.end(),
139 arg_list.end() );
140
141 // try to parse device specific nchan values, assume 1 channel if none given
142
143 for (std::string arg : arg_list)
144 {
145 dict_t dict = params_to_dict(arg);
146 if (dict.count("nchan"))
147 {
148 dev_nchan += boost::lexical_cast<size_t>( dict["nchan"] );
149 }
150 else // no channels given via args
151 {
152 dev_nchan++; // assume one channel
153 }
154 }
155
156 // if at least one nchan was given, perform a sanity check
157 if ( max_nchan && dev_nchan && max_nchan != dev_nchan )
158 throw std::runtime_error("Wrong device arguments specified. Missing nchan?");
159
160 const size_t nchan = std::max<size_t>(dev_nchan, 1); // assume at least one
161 return gr::io_signature::make(nchan, nchan, sizeof(gr_complex));
162}
163
164#endif // OSMOSDR_ARG_HELPERS_H
std::vector< std::string > args_to_vector(const std::string &args)
Definition: arg_helpers.h:48
std::vector< std::string > params_to_vector(const std::string &params)
Definition: arg_helpers.h:62
std::map< std::string, std::string > dict_t
Definition: arg_helpers.h:34
dict_t params_to_dict(const std::string &params)
Definition: arg_helpers.h:95
std::pair< std::string, std::string > pair_t
Definition: arg_helpers.h:35
std::string dict_to_args_string(const dict_t &d)
Definition: arg_helpers.h:37
pair_t param_to_pair(const std::string &param)
Definition: arg_helpers.h:76
gr::io_signature::sptr args_to_io_signature(const std::string &args)
Definition: arg_helpers.h:120
Definition: arg_helpers.h:113
bool operator()(const std::string &str)
Definition: arg_helpers.h:114