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