Fawkes API  Fawkes Development Version
camargp.cpp
1 
2 /***************************************************************************
3  * camargp.cpp - Camera argument parser
4  *
5  * Created: Wed Apr 11 15:47:34 2007
6  * Copyright 2005-2007 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <fvutils/system/camargp.h>
25 #include <core/exceptions/software.h>
26 
27 #include <cstdlib>
28 
29 using namespace std;
30 using namespace fawkes;
31 
32 namespace firevision {
33 #if 0 /* just to make Emacs auto-indent happy */
34 }
35 #endif
36 
37 /** @class CameraArgumentParser <fvutils/system/camargp.h>
38  * Camera argument parser.
39  * Simple parser that will parse a camera parameter string that defines
40  * the camera type and options specific to this camera.
41  *
42  * In general a string is of the form
43  * @code
44  * camera-type:id-substring:param1=value1:param2=value2:arg1:arg2
45  * @endcode
46  * The string is a colon-separated (:) list of elements.
47  *
48  * The first element (camera in the example) denotes the camera type.
49  * See the CameraFactory documentation for allowed values. It can be queried
50  * with the cam_type() method.
51  *
52  * There is one special parameter that is used for all kinds of cameras, the
53  * identifier string (second element). This special value is meant to be used to recognize
54  * the very same camera even if it has different parameters and to distinguish multiple
55  * cameras of the same type (for instance to distinguish two different firewire
56  * cameras). The ID can be queried with cam_id().
57  *
58  * The rest is a list of parameters and arguments. Parameters are key/value
59  * pairs separated by an equals sign. The are then queried with the has(),
60  * get() and parameters() methods. Arguments are simple strings that do not contain
61  * an equals sign and are given as-is via the arguments() method. These could
62  * for example be a list of files etc..
63  *
64  * @see CameraFactory
65  * @author Tim Niemueller
66  */
67 
68 /** Constructor.
69  * @param as camera argument string
70  */
71 CameraArgumentParser::CameraArgumentParser(const char *as)
72 {
73  values.clear();
74 
75  std::string s = as;
76  s += ":";
77 
78  _cam_type = s;
79  string::size_type start = 0;
80  string::size_type end;
81  if ( (end = s.find(":", start)) != string::npos ) {
82  _cam_type = s.substr(start, end);
83  } else {
84  _cam_type = "";
85  }
86  start = end + 1;
87  if ( (end = s.find(":", start)) != string::npos ) {
88  _cam_id = s.substr(start, end - start);
89  start = end + 1;
90  } else {
91  _cam_id = "";
92  }
93 
94  while ( (end = s.find(":", start)) != string::npos ) {
95  string t = s.substr(start, (end - start));
96  string::size_type e;
97  if ( (e = t.find("=", 0)) != string::npos ) {
98  if ( (e > 0 ) && (e < t.length() - 1) ) {
99  string key = t.substr(0, e);
100  string value = t.substr(e+1);
101  values[key] = value;
102  }
103  } else {
104  if ( t != "" ) {
105  args.push_back(t);
106  }
107  }
108  start = end + 1;
109  }
110 }
111 
112 
113 /** Destructor. */
114 CameraArgumentParser::~CameraArgumentParser()
115 {
116  values.clear();
117  args.clear();
118 }
119 
120 
121 /** Get camera type.
122  * Get the camera type. This is the very first element before
123  * the first colon.
124  * @return camera type
125  */
126 std::string
127 CameraArgumentParser::cam_type() const
128 {
129  return _cam_type;
130 }
131 
132 
133 /** Get camera ID.
134  * Get the camera ID. This is the very first element before
135  * the first colon.
136  * @return camera ID string
137  */
138 std::string
139 CameraArgumentParser::cam_id() const
140 {
141  return _cam_id;
142 }
143 
144 
145 /** Check if an parameter was given.
146  * Checks if the given parameter s was given in the argument
147  * string.
148  * @param s parameter key to check for
149  * @return true, if the parameter has been supplied, false otherwise
150  */
151 bool
152 CameraArgumentParser::has(std::string s) const
153 {
154  return (values.find(s) != values.end());
155 }
156 
157 
158 /** Get the value of the given parameter.
159  * @param s key of the parameter to retrieve
160  * @return the value of the given parameter or an empty string if the
161  * parameter was not supplied.
162  */
163 std::string
164 CameraArgumentParser::get(std::string s) const
165 {
166  if ( values.find(s) != values.end() ) {
167  // this is needed to be able to make this method const
168  return (*(values.find(s))).second;
169  } else {
170  return string();
171  }
172 }
173 
174 
175 /** Get the value of the given parameter as integer.
176  * This method assumes that the value is an integer and converts it.
177  * @param s key of the parameter to retrieve
178  * @return the value of the given parameter as integer
179  * @exception IllegalArgumentException thrown if the value cannot be properly
180  * converted to an integer
181  * @exception Exception thrown if the argument has not been supplied
182  */
183 long int
184 CameraArgumentParser::get_int(std::string s) const
185 {
186  if ( values.find(s) != values.end() ) {
187  char *endptr;
188  long int rv = strtol((*(values.find(s))).second.c_str(), &endptr, 10);
189  if ( endptr[0] != 0 ) {
190  throw IllegalArgumentException("Supplied argument is not of type int");
191  }
192  return rv;
193  } else {
194  throw Exception("Value for '%s' not available", s.c_str());
195  }
196 }
197 
198 
199 /** Get the value of the given parameter as integer.
200  * This method assumes that the value is an integer and converts it.
201  * @param s key of the parameter to retrieve
202  * @return the value of the given parameter as integer
203  * @exception IllegalArgumentException thrown if the value cannot be properly
204  * converted to an integer
205  * @exception Exception thrown if the argument has not been supplied
206  */
207 double
208 CameraArgumentParser::get_float(std::string s) const
209 {
210  if ( values.find(s) != values.end() ) {
211  char *endptr;
212  double rv = strtod((*(values.find(s))).second.c_str(), &endptr);
213  if ( endptr[0] != 0 ) {
214  throw IllegalArgumentException("Supplied argument is not of type double");
215  }
216  return rv;
217  } else {
218  throw Exception("Value for '%s' not available", s.c_str());
219  }
220 }
221 
222 
223 /** Get the arguments.
224  * Returns a vector of arguments supplied in the argument string.
225  * @return vector of arguments
226  */
227 std::vector<std::string>
228 CameraArgumentParser::arguments() const
229 {
230  return args;
231 }
232 
233 
234 /** Get a map of parameters.
235  * @returns map of key/value pairs of parameters supplied in the argument string.
236  */
237 std::map<std::string, std::string>
238 CameraArgumentParser::parameters() const
239 {
240  return values;
241 }
242 
243 } // end namespace firevision
Fawkes library namespace.
STL namespace.
Base class for exceptions in Fawkes.
Definition: exception.h:36
Expected parameter is missing.
Definition: software.h:82