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 <core/exceptions/software.h>
25#include <fvutils/system/camargp.h>
26
27#include <cstdlib>
28
29using namespace std;
30using namespace fawkes;
31
32namespace firevision {
33
34/** @class CameraArgumentParser <fvutils/system/camargp.h>
35 * Camera argument parser.
36 * Simple parser that will parse a camera parameter string that defines
37 * the camera type and options specific to this camera.
38 *
39 * In general a string is of the form
40 * @code
41 * camera-type:id-substring:param1=value1:param2=value2:arg1:arg2
42 * @endcode
43 * The string is a colon-separated (:) list of elements.
44 *
45 * The first element (camera in the example) denotes the camera type.
46 * See the CameraFactory documentation for allowed values. It can be queried
47 * with the cam_type() method.
48 *
49 * There is one special parameter that is used for all kinds of cameras, the
50 * identifier string (second element). This special value is meant to be used to recognize
51 * the very same camera even if it has different parameters and to distinguish multiple
52 * cameras of the same type (for instance to distinguish two different firewire
53 * cameras). The ID can be queried with cam_id().
54 *
55 * The rest is a list of parameters and arguments. Parameters are key/value
56 * pairs separated by an equals sign. The are then queried with the has(),
57 * get() and parameters() methods. Arguments are simple strings that do not contain
58 * an equals sign and are given as-is via the arguments() method. These could
59 * for example be a list of files etc..
60 *
61 * @see CameraFactory
62 * @author Tim Niemueller
63 */
64
65/** Constructor.
66 * @param as camera argument string
67 */
69{
70 values.clear();
71
72 std::string s = as;
73 s += ":";
74
75 _cam_type = s;
76 string::size_type start = 0;
77 string::size_type end;
78 if ((end = s.find(":", start)) != string::npos) {
79 _cam_type = s.substr(start, end);
80 } else {
81 _cam_type = "";
82 }
83 start = end + 1;
84 if ((end = s.find(":", start)) != string::npos) {
85 _cam_id = s.substr(start, end - start);
86 start = end + 1;
87 } else {
88 _cam_id = "";
89 }
90
91 while ((end = s.find(":", start)) != string::npos) {
92 string t = s.substr(start, (end - start));
93 string::size_type e;
94 if ((e = t.find("=", 0)) != string::npos) {
95 if ((e > 0) && (e < t.length() - 1)) {
96 string key = t.substr(0, e);
97 string value = t.substr(e + 1);
98 values[key] = value;
99 }
100 } else {
101 if (t != "") {
102 args.push_back(t);
103 }
104 }
105 start = end + 1;
106 }
107}
108
109/** Destructor. */
111{
112 values.clear();
113 args.clear();
114}
115
116/** Get camera type.
117 * Get the camera type. This is the very first element before
118 * the first colon.
119 * @return camera type
120 */
121std::string
123{
124 return _cam_type;
125}
126
127/** Get camera ID.
128 * Get the camera ID. This is the very first element before
129 * the first colon.
130 * @return camera ID string
131 */
132std::string
134{
135 return _cam_id;
136}
137
138/** Check if an parameter was given.
139 * Checks if the given parameter s was given in the argument
140 * string.
141 * @param s parameter key to check for
142 * @return true, if the parameter has been supplied, false otherwise
143 */
144bool
145CameraArgumentParser::has(std::string s) const
146{
147 return (values.find(s) != values.end());
148}
149
150/** Get the value of the given parameter.
151 * @param s key of the parameter to retrieve
152 * @return the value of the given parameter or an empty string if the
153 * parameter was not supplied.
154 */
155std::string
156CameraArgumentParser::get(std::string s) const
157{
158 if (values.find(s) != values.end()) {
159 // this is needed to be able to make this method const
160 return (*(values.find(s))).second;
161 } else {
162 return string();
163 }
164}
165
166/** Get the value of the given parameter as integer.
167 * This method assumes that the value is an integer and converts it.
168 * @param s key of the parameter to retrieve
169 * @return the value of the given parameter as integer
170 * @exception IllegalArgumentException thrown if the value cannot be properly
171 * converted to an integer
172 * @exception Exception thrown if the argument has not been supplied
173 */
174long int
176{
177 if (values.find(s) != values.end()) {
178 char * endptr;
179 long int rv = strtol((*(values.find(s))).second.c_str(), &endptr, 10);
180 if (endptr[0] != 0) {
181 throw IllegalArgumentException("Supplied argument is not of type int");
182 }
183 return rv;
184 } else {
185 throw Exception("Value for '%s' not available", s.c_str());
186 }
187}
188
189/** Get the value of the given parameter as integer.
190 * This method assumes that the value is an integer and converts it.
191 * @param s key of the parameter to retrieve
192 * @return the value of the given parameter as integer
193 * @exception IllegalArgumentException thrown if the value cannot be properly
194 * converted to an integer
195 * @exception Exception thrown if the argument has not been supplied
196 */
197double
199{
200 if (values.find(s) != values.end()) {
201 char * endptr;
202 double rv = strtod((*(values.find(s))).second.c_str(), &endptr);
203 if (endptr[0] != 0) {
204 throw IllegalArgumentException("Supplied argument is not of type double");
205 }
206 return rv;
207 } else {
208 throw Exception("Value for '%s' not available", s.c_str());
209 }
210}
211
212/** Get the arguments.
213 * Returns a vector of arguments supplied in the argument string.
214 * @return vector of arguments
215 */
216std::vector<std::string>
218{
219 return args;
220}
221
222/** Get a map of parameters.
223 * @returns map of key/value pairs of parameters supplied in the argument string.
224 */
225std::map<std::string, std::string>
227{
228 return values;
229}
230
231} // end namespace firevision
Base class for exceptions in Fawkes.
Definition: exception.h:36
Expected parameter is missing.
Definition: software.h:80
long int get_int(std::string s) const
Get the value of the given parameter as integer.
Definition: camargp.cpp:175
std::vector< std::string > arguments() const
Get the arguments.
Definition: camargp.cpp:217
std::string cam_id() const
Get camera ID.
Definition: camargp.cpp:133
std::string cam_type() const
Get camera type.
Definition: camargp.cpp:122
bool has(std::string s) const
Check if an parameter was given.
Definition: camargp.cpp:145
std::string get(std::string s) const
Get the value of the given parameter.
Definition: camargp.cpp:156
double get_float(std::string s) const
Get the value of the given parameter as integer.
Definition: camargp.cpp:198
CameraArgumentParser(const char *as)
Constructor.
Definition: camargp.cpp:68
std::map< std::string, std::string > parameters() const
Get a map of parameters.
Definition: camargp.cpp:226
Fawkes library namespace.