camargp.cpp
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 00026 using namespace std; 00027 00028 /** @class CameraArgumentParser <fvutils/system/camargp.h> 00029 * Camera argument parser. 00030 * Simple parser that will parse a camera parameter string that defines 00031 * the camera type and options specific to this camera. 00032 * 00033 * In general a string is of the form 00034 * @code 00035 * camera-type:id-substring:param1=value1:param2=value2:arg1:arg2 00036 * @endcode 00037 * The string is a colon-separated (:) list of elements. 00038 * 00039 * The first element (camera in the example) denotes the camera type. 00040 * See the CameraFactory documentation for allowed values. It can be queried 00041 * with the cam_type() method. 00042 * 00043 * There is one special parameter that is used for all kinds of cameras, the 00044 * identifier string (second element). This special value is meant to be used to recognize 00045 * the very same camera even if it has different parameters and to distinguish multiple 00046 * cameras of the same type (for instance to distinguish two different firewire 00047 * cameras). The ID can be queried with cam_id(). 00048 * 00049 * The rest is a list of parameters and arguments. Parameters are key/value 00050 * pairs separated by an equals sign. The are then queried with the has(), 00051 * get() and parameters() methods. Arguments are simple strings that do not contain 00052 * an equals sign and are given as-is via the arguments() method. These could 00053 * for example be a list of files etc.. 00054 * 00055 * @see CameraFactory 00056 * @author Tim Niemueller 00057 */ 00058 00059 /** Constructor. 00060 * @param as camera argument string 00061 */ 00062 CameraArgumentParser::CameraArgumentParser(const char *as) 00063 { 00064 values.clear(); 00065 00066 std::string s = as; 00067 s += ":"; 00068 00069 _cam_type = s; 00070 string::size_type start = 0; 00071 string::size_type end; 00072 if ( (end = s.find(":", start)) != string::npos ) { 00073 _cam_type = s.substr(start, end); 00074 } else { 00075 _cam_type = ""; 00076 } 00077 start = end + 1; 00078 if ( (end = s.find(":", start)) != string::npos ) { 00079 _cam_id = s.substr(start, end - start); 00080 start = end + 1; 00081 } else { 00082 _cam_id = ""; 00083 } 00084 00085 while ( (end = s.find(":", start)) != string::npos ) { 00086 string t = s.substr(start, (end - start)); 00087 string::size_type e; 00088 if ( (e = t.find("=", 0)) != string::npos ) { 00089 if ( (e > 0 ) && (e < t.length() - 1) ) { 00090 string key = t.substr(0, e); 00091 string value = t.substr(e+1); 00092 values[key] = value; 00093 } 00094 } else { 00095 if ( t != "" ) { 00096 args.push_back(t); 00097 } 00098 } 00099 start = end + 1; 00100 } 00101 } 00102 00103 00104 /** Destructor. */ 00105 CameraArgumentParser::~CameraArgumentParser() 00106 { 00107 values.clear(); 00108 args.clear(); 00109 } 00110 00111 00112 /** Get camera type. 00113 * Get the camera type. This is the very first element before 00114 * the first colon. 00115 * @return camera type 00116 */ 00117 std::string 00118 CameraArgumentParser::cam_type() const 00119 { 00120 return _cam_type; 00121 } 00122 00123 00124 /** Get camera ID. 00125 * Get the camera ID. This is the very first element before 00126 * the first colon. 00127 * @return camera ID string 00128 */ 00129 std::string 00130 CameraArgumentParser::cam_id() const 00131 { 00132 return _cam_id; 00133 } 00134 00135 00136 /** Check if an parameter was given. 00137 * Checks if the given parameter s was given in the argument 00138 * string. 00139 * @param s parameter key to check for 00140 * @return true, if the parameter has been supplied, false otherwise 00141 */ 00142 bool 00143 CameraArgumentParser::has(std::string s) const 00144 { 00145 return (values.find(s) != values.end()); 00146 } 00147 00148 00149 /** Get the value of the given parameter. 00150 * @param s key of the parameter to retrieve 00151 * @return the value of the given parameter or an empty string if the 00152 * parameter was not supplied. 00153 */ 00154 std::string 00155 CameraArgumentParser::get(std::string s) const 00156 { 00157 if ( values.find(s) != values.end() ) { 00158 // this is needed to be able to make this method const 00159 return (*(values.find(s))).second; 00160 } else { 00161 return string(); 00162 } 00163 } 00164 00165 00166 /** Get the arguments. 00167 * Returns a vector of arguments supplied in the argument string. 00168 * @return vector of arguments 00169 */ 00170 std::vector<std::string> 00171 CameraArgumentParser::arguments() const 00172 { 00173 return args; 00174 } 00175 00176 00177 /** Get a map of parameters. 00178 * @returns map of key/value pairs of parameters supplied in the argument string. 00179 */ 00180 std::map<std::string, std::string> 00181 CameraArgumentParser::parameters() const 00182 { 00183 return values; 00184 }

