GNU Radio's OsmoSDR 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 <boost/foreach.hpp>
33 #include <ciso646>
34 
35 typedef std::map< std::string, std::string > dict_t;
36 typedef std::pair< std::string, std::string > pair_t;
37 
38 inline std::string dict_to_args_string( const dict_t &d )
39 {
40  std::string out;
41  BOOST_FOREACH(const pair_t pair, d)
42  {
43  if (not out.empty()) out += ",";
44  out += pair.first + "='" + pair.second + "'";
45  }
46  return out;
47 }
48 
49 inline std::vector< std::string > args_to_vector( const std::string &args )
50 {
51  std::vector< std::string > result;
52 
53  boost::escaped_list_separator<char> separator("\\", " ", "'");
54  typedef boost::tokenizer< boost::escaped_list_separator<char> > tokenizer_t;
55  tokenizer_t tokens(args, separator);
56 
57  BOOST_FOREACH(std::string token, tokens)
58  result.push_back(token);
59 
60  return result;
61 }
62 
63 inline std::vector< std::string > params_to_vector( const std::string &params )
64 {
65  std::vector< std::string > result;
66 
67  boost::escaped_list_separator<char> separator("\\", ",", "'");
68  typedef boost::tokenizer< boost::escaped_list_separator<char> > tokenizer_t;
69  tokenizer_t tokens(params, separator);
70 
71  BOOST_FOREACH(std::string token, tokens)
72  result.push_back(token);
73 
74  return result;
75 }
76 
77 inline pair_t param_to_pair( const std::string &param )
78 {
79  pair_t result;
80 
81  std::size_t pos = param.find('=');
82  if(pos != std::string::npos)
83  {
84  result.first = param.substr(0, pos);
85  result.second = param.substr(pos + 1);
86  }
87  else
88  {
89  result.first = param;
90  result.second = "";
91  }
92 
93  return result;
94 }
95 
96 inline dict_t params_to_dict( const std::string &params )
97 {
98  dict_t result;
99 
100  std::vector< std::string > param_list = params_to_vector( params );
101  BOOST_FOREACH(std::string param, param_list)
102  {
103  pair_t pair = param_to_pair( param );
104  std::string value = pair.second;
105  if (value.length() && value[0] == '\'' && value[ value.length() - 1 ] == '\'')
106  value = value.substr(1, value.length() - 1);
107  result[ pair.first ] = value;
108  }
109 
110  return result;
111 }
112 
114 {
115  bool operator ()(const std::string &str)
116  {
117  return str.find("numchan=") == 0;
118  }
119 };
120 
121 inline gr::io_signature::sptr args_to_io_signature( const std::string &args )
122 {
123  size_t max_nchan = 0;
124  size_t dev_nchan = 0;
125  std::vector< std::string > arg_list = args_to_vector( args );
126 
127  BOOST_FOREACH( std::string arg, arg_list )
128  {
129  if ( arg.find( "numchan=" ) == 0 ) // try to parse global nchan value
130  {
131  pair_t pair = param_to_pair( arg );
132  max_nchan = boost::lexical_cast<size_t>( pair.second );
133  }
134  }
135 
136  arg_list.erase( std::remove_if( // remove any global nchan tokens
137  arg_list.begin(),
138  arg_list.end(),
139  is_nchan_argument() ),
140  arg_list.end() );
141 
142  // try to parse device specific nchan values, assume 1 channel if none given
143 
144  BOOST_FOREACH( std::string arg, arg_list )
145  {
146  dict_t dict = params_to_dict(arg);
147  if (dict.count("nchan"))
148  {
149  dev_nchan += boost::lexical_cast<size_t>( dict["nchan"] );
150  }
151  else // no channels given via args
152  {
153  dev_nchan++; // assume one channel
154  }
155  }
156 
157  // if at least one nchan was given, perform a sanity check
158  if ( max_nchan && dev_nchan && max_nchan != dev_nchan )
159  throw std::runtime_error("Wrong device arguments specified. Missing nchan?");
160 
161  const size_t nchan = std::max<size_t>(dev_nchan, 1); // assume at least one
162  return gr::io_signature::make(nchan, nchan, sizeof(gr_complex));
163 }
164 
165 #endif // OSMOSDR_ARG_HELPERS_H
pair_t param_to_pair(const std::string &param)
Definition: arg_helpers.h:77
bool operator()(const std::string &str)
Definition: arg_helpers.h:115
gr::io_signature::sptr args_to_io_signature(const std::string &args)
Definition: arg_helpers.h:121
std::vector< std::string > params_to_vector(const std::string &params)
Definition: arg_helpers.h:63
std::vector< std::string > args_to_vector(const std::string &args)
Definition: arg_helpers.h:49
Definition: arg_helpers.h:113
std::string dict_to_args_string(const dict_t &d)
Definition: arg_helpers.h:38
std::pair< std::string, std::string > pair_t
Definition: arg_helpers.h:36
std::map< std::string, std::string > dict_t
Definition: arg_helpers.h:35
dict_t params_to_dict(const std::string &params)
Definition: arg_helpers.h:96