00001 00002 /*************************************************************************** 00003 * camargp.cpp - Camera argument parser 00004 * 00005 * Created: Wed Apr 11 15:47:34 2007 00006 * Copyright 2005-2007 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00022 */ 00023 00024 #include <fvutils/system/camargp.h> 00025 #include <core/exceptions/software.h> 00026 00027 #include <cstdlib> 00028 00029 using namespace std; 00030 using namespace fawkes; 00031 00032 namespace firevision { 00033 #if 0 /* just to make Emacs auto-indent happy */ 00034 } 00035 #endif 00036 00037 /** @class CameraArgumentParser <fvutils/system/camargp.h> 00038 * Camera argument parser. 00039 * Simple parser that will parse a camera parameter string that defines 00040 * the camera type and options specific to this camera. 00041 * 00042 * In general a string is of the form 00043 * @code 00044 * camera-type:id-substring:param1=value1:param2=value2:arg1:arg2 00045 * @endcode 00046 * The string is a colon-separated (:) list of elements. 00047 * 00048 * The first element (camera in the example) denotes the camera type. 00049 * See the CameraFactory documentation for allowed values. It can be queried 00050 * with the cam_type() method. 00051 * 00052 * There is one special parameter that is used for all kinds of cameras, the 00053 * identifier string (second element). This special value is meant to be used to recognize 00054 * the very same camera even if it has different parameters and to distinguish multiple 00055 * cameras of the same type (for instance to distinguish two different firewire 00056 * cameras). The ID can be queried with cam_id(). 00057 * 00058 * The rest is a list of parameters and arguments. Parameters are key/value 00059 * pairs separated by an equals sign. The are then queried with the has(), 00060 * get() and parameters() methods. Arguments are simple strings that do not contain 00061 * an equals sign and are given as-is via the arguments() method. These could 00062 * for example be a list of files etc.. 00063 * 00064 * @see CameraFactory 00065 * @author Tim Niemueller 00066 */ 00067 00068 /** Constructor. 00069 * @param as camera argument string 00070 */ 00071 CameraArgumentParser::CameraArgumentParser(const char *as) 00072 { 00073 values.clear(); 00074 00075 std::string s = as; 00076 s += ":"; 00077 00078 _cam_type = s; 00079 string::size_type start = 0; 00080 string::size_type end; 00081 if ( (end = s.find(":", start)) != string::npos ) { 00082 _cam_type = s.substr(start, end); 00083 } else { 00084 _cam_type = ""; 00085 } 00086 start = end + 1; 00087 if ( (end = s.find(":", start)) != string::npos ) { 00088 _cam_id = s.substr(start, end - start); 00089 start = end + 1; 00090 } else { 00091 _cam_id = ""; 00092 } 00093 00094 while ( (end = s.find(":", start)) != string::npos ) { 00095 string t = s.substr(start, (end - start)); 00096 string::size_type e; 00097 if ( (e = t.find("=", 0)) != string::npos ) { 00098 if ( (e > 0 ) && (e < t.length() - 1) ) { 00099 string key = t.substr(0, e); 00100 string value = t.substr(e+1); 00101 values[key] = value; 00102 } 00103 } else { 00104 if ( t != "" ) { 00105 args.push_back(t); 00106 } 00107 } 00108 start = end + 1; 00109 } 00110 } 00111 00112 00113 /** Destructor. */ 00114 CameraArgumentParser::~CameraArgumentParser() 00115 { 00116 values.clear(); 00117 args.clear(); 00118 } 00119 00120 00121 /** Get camera type. 00122 * Get the camera type. This is the very first element before 00123 * the first colon. 00124 * @return camera type 00125 */ 00126 std::string 00127 CameraArgumentParser::cam_type() const 00128 { 00129 return _cam_type; 00130 } 00131 00132 00133 /** Get camera ID. 00134 * Get the camera ID. This is the very first element before 00135 * the first colon. 00136 * @return camera ID string 00137 */ 00138 std::string 00139 CameraArgumentParser::cam_id() const 00140 { 00141 return _cam_id; 00142 } 00143 00144 00145 /** Check if an parameter was given. 00146 * Checks if the given parameter s was given in the argument 00147 * string. 00148 * @param s parameter key to check for 00149 * @return true, if the parameter has been supplied, false otherwise 00150 */ 00151 bool 00152 CameraArgumentParser::has(std::string s) const 00153 { 00154 return (values.find(s) != values.end()); 00155 } 00156 00157 00158 /** Get the value of the given parameter. 00159 * @param s key of the parameter to retrieve 00160 * @return the value of the given parameter or an empty string if the 00161 * parameter was not supplied. 00162 */ 00163 std::string 00164 CameraArgumentParser::get(std::string s) const 00165 { 00166 if ( values.find(s) != values.end() ) { 00167 // this is needed to be able to make this method const 00168 return (*(values.find(s))).second; 00169 } else { 00170 return string(); 00171 } 00172 } 00173 00174 00175 /** Get the value of the given parameter as integer. 00176 * This method assumes that the value is an integer and converts it. 00177 * @param s key of the parameter to retrieve 00178 * @return the value of the given parameter as integer 00179 * @exception IllegalArgumentException thrown if the value cannot be properly 00180 * converted to an integer 00181 * @exception Exception thrown if the argument has not been supplied 00182 */ 00183 long int 00184 CameraArgumentParser::get_int(std::string s) const 00185 { 00186 if ( values.find(s) != values.end() ) { 00187 char *endptr; 00188 long int rv = strtol((*(values.find(s))).second.c_str(), &endptr, 10); 00189 if ( endptr[0] != 0 ) { 00190 throw IllegalArgumentException("Supplied argument is not of type int"); 00191 } 00192 return rv; 00193 } else { 00194 throw Exception("Value for '%s' not available", s.c_str()); 00195 } 00196 } 00197 00198 00199 /** Get the value of the given parameter as integer. 00200 * This method assumes that the value is an integer and converts it. 00201 * @param s key of the parameter to retrieve 00202 * @return the value of the given parameter as integer 00203 * @exception IllegalArgumentException thrown if the value cannot be properly 00204 * converted to an integer 00205 * @exception Exception thrown if the argument has not been supplied 00206 */ 00207 double 00208 CameraArgumentParser::get_float(std::string s) const 00209 { 00210 if ( values.find(s) != values.end() ) { 00211 char *endptr; 00212 double rv = strtod((*(values.find(s))).second.c_str(), &endptr); 00213 if ( endptr[0] != 0 ) { 00214 throw IllegalArgumentException("Supplied argument is not of type double"); 00215 } 00216 return rv; 00217 } else { 00218 throw Exception("Value for '%s' not available", s.c_str()); 00219 } 00220 } 00221 00222 00223 /** Get the arguments. 00224 * Returns a vector of arguments supplied in the argument string. 00225 * @return vector of arguments 00226 */ 00227 std::vector<std::string> 00228 CameraArgumentParser::arguments() const 00229 { 00230 return args; 00231 } 00232 00233 00234 /** Get a map of parameters. 00235 * @returns map of key/value pairs of parameters supplied in the argument string. 00236 */ 00237 std::map<std::string, std::string> 00238 CameraArgumentParser::parameters() const 00239 { 00240 return values; 00241 } 00242 00243 } // end namespace firevision