$search
00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Willow Garage, Inc. nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 * 00034 * $Id: parse.cpp 30713 2010-07-09 20:00:51Z rusu $ 00035 * 00036 */ 00037 00038 #include <stdio.h> 00039 #include "terminal_tools/parse.h" 00040 #include "terminal_tools/print.h" 00041 00043 00050 int 00051 terminal_tools::parse_argument (int argc, char** argv, const char* str, std::string &val) 00052 { 00053 for (int i = 1; i < argc; ++i) 00054 { 00055 // Search for the string 00056 if ((strcmp (argv[i], str) == 0) && (++i < argc)) 00057 { 00058 val = std::string (argv[i]); 00059 return (i-1); 00060 } 00061 } 00062 return (-1); 00063 } 00064 00066 00073 int 00074 terminal_tools::parse_argument (int argc, char** argv, const char* str, bool &val) 00075 { 00076 for (int i = 1; i < argc; ++i) 00077 { 00078 // Search for the string 00079 if ((strcmp (argv[i], str) == 0) && (++i < argc)) 00080 { 00081 val = (bool)atoi (argv[i]); 00082 return (i-1); 00083 } 00084 } 00085 return (-1); 00086 } 00087 00089 00096 int 00097 terminal_tools::parse_argument (int argc, char** argv, const char* str, double &val) 00098 { 00099 for (int i = 1; i < argc; ++i) 00100 { 00101 // Search for the string 00102 if ((strcmp (argv[i], str) == 0) && (++i < argc)) 00103 { 00104 val = atof (argv[i]); 00105 return (i-1); 00106 } 00107 } 00108 return (-1); 00109 } 00110 00112 00119 int 00120 terminal_tools::parse_argument (int argc, char** argv, const char* str, int &val) 00121 { 00122 for (int i = 1; i < argc; ++i) 00123 { 00124 // Search for the string 00125 if ((strcmp (argv[i], str) == 0) && (++i < argc)) 00126 { 00127 val = atoi (argv[i]); 00128 return (i-1); 00129 } 00130 } 00131 return (-1); 00132 } 00133 00135 00142 int 00143 terminal_tools::parse_argument (int argc, char** argv, const char* str, unsigned int &val) 00144 { 00145 for (int i = 1; i < argc; ++i) 00146 { 00147 // Search for the string 00148 if ((strcmp (argv[i], str) == 0) && (++i < argc)) 00149 { 00150 val = atoi (argv[i]); 00151 return (i-1); 00152 } 00153 } 00154 return (-1); 00155 } 00156 00158 00164 std::vector<int> 00165 terminal_tools::parse_file_extension_argument (int argc, char** argv, const std::string &extension) 00166 { 00167 std::vector<int> indices; 00168 for (int i = 1; i < argc; ++i) 00169 { 00170 std::string fname = std::string (argv[i]); 00171 std::string ext = extension; 00172 00173 // Needs to be at least 4: .ext 00174 if (fname.size () <= 4) 00175 continue; 00176 00177 // For being case insensitive 00178 std::transform (fname.begin (), fname.end (), fname.begin (), tolower); 00179 std::transform (ext.begin (), ext.end (), ext.begin (), tolower); 00180 00181 // Check if found 00182 std::string::size_type it; 00183 if ((it = fname.find (ext)) != std::string::npos) 00184 { 00185 // Additional check: we want to be able to differentiate between .p and .png 00186 if ((ext.size () - (fname.size () - it)) == 0) 00187 indices.push_back (i); 00188 } 00189 } 00190 return (indices); 00191 } 00192 00194 00202 int 00203 terminal_tools::parse_2x_arguments (int argc, char** argv, const char* str, double &f, double &s, bool debug) 00204 { 00205 for (int i = 1; i < argc; ++i) 00206 { 00207 // Search for the string 00208 if ((strcmp (argv[i], str) == 0) && (++i < argc)) 00209 { 00210 // look for ',' as a separator 00211 std::vector<std::string> values; 00212 boost::split (values, argv[i], boost::is_any_of (","), boost::token_compress_on); 00213 if (values.size () != 2 && debug) 00214 { 00215 print_error ("[parse_2x_arguments] Number of values for %s (%d) different than 2!\n", str, (int)values.size ()); 00216 return (-2); 00217 } 00218 f = atof (values.at (0).c_str ()); 00219 s = atof (values.at (1).c_str ()); 00220 return (i-1); 00221 } 00222 } 00223 return (-1); 00224 } 00225 00227 00235 int 00236 terminal_tools::parse_2x_arguments (int argc, char** argv, const char* str, int &f, int &s, bool debug) 00237 { 00238 for (int i = 1; i < argc; ++i) 00239 { 00240 // Search for the string 00241 if ((strcmp (argv[i], str) == 0) && (++i < argc)) 00242 { 00243 // look for ',' as a separator 00244 std::vector<std::string> values; 00245 boost::split (values, argv[i], boost::is_any_of (","), boost::token_compress_on); 00246 if (values.size () != 2 && debug) 00247 { 00248 print_error ("[parse_2x_arguments] Number of values for %s (%d) different than 2!\n", str, (int)values.size ()); 00249 return (-2); 00250 } 00251 f = atoi (values.at (0).c_str ()); 00252 s = atoi (values.at (1).c_str ()); 00253 return (i-1); 00254 } 00255 } 00256 return (-1); 00257 } 00258 00260 00269 int 00270 terminal_tools::parse_3x_arguments (int argc, char** argv, const char* str, double &f, double &s, double &t, bool debug) 00271 { 00272 for (int i = 1; i < argc; ++i) 00273 { 00274 // Search for the string 00275 if ((strcmp (argv[i], str) == 0) && (++i < argc)) 00276 { 00277 // look for ',' as a separator 00278 std::vector<std::string> values; 00279 boost::split (values, argv[i], boost::is_any_of (","), boost::token_compress_on); 00280 if (values.size () != 3 && debug) 00281 { 00282 print_error ("[parse_3x_arguments] Number of values for %s (%d) different than 3!\n", str, (int)values.size ()); 00283 return (-2); 00284 } 00285 f = atof (values.at (0).c_str ()); 00286 s = atof (values.at (1).c_str ()); 00287 t = atof (values.at (2).c_str ()); 00288 return (i-1); 00289 } 00290 } 00291 return (-1); 00292 } 00293 00295 00304 int 00305 terminal_tools::parse_3x_arguments (int argc, char** argv, const char* str, int &f, int &s, int &t, bool debug) 00306 { 00307 for (int i = 1; i < argc; ++i) 00308 { 00309 // Search for the string 00310 if ((strcmp (argv[i], str) == 0) && (++i < argc)) 00311 { 00312 // look for ',' as a separator 00313 std::vector<std::string> values; 00314 boost::split (values, argv[i], boost::is_any_of (","), boost::token_compress_on); 00315 if (values.size () != 3 && debug) 00316 { 00317 print_error ("[parse_3x_arguments] Number of values for %s (%d) different than 3!\n", str, (int)values.size ()); 00318 return (-2); 00319 } 00320 f = atoi (values.at (0).c_str ()); 00321 s = atoi (values.at (1).c_str ()); 00322 t = atoi (values.at (2).c_str ()); 00323 return (i-1); 00324 } 00325 } 00326 return (-1); 00327 } 00328 00330 00337 bool 00338 terminal_tools::parse_multiple_arguments (int argc, char** argv, const char* str, std::vector<int> &values) 00339 { 00340 for (int i = 1; i < argc; ++i) 00341 { 00342 // Search for the string 00343 if ((strcmp (argv[i], str) == 0) && (++i < argc)) 00344 { 00345 int val = atoi (argv[i]); 00346 values.push_back (val); 00347 } 00348 } 00349 if (values.size () == 0) 00350 return (false); 00351 else 00352 return (true); 00353 } 00354 00356 00363 bool 00364 terminal_tools::parse_multiple_arguments (int argc, char** argv, const char* str, std::vector<double> &values) 00365 { 00366 for (int i = 1; i < argc; ++i) 00367 { 00368 // Search for the string 00369 if ((strcmp (argv[i], str) == 0) && (++i < argc)) 00370 { 00371 double val = atof (argv[i]); 00372 values.push_back (val); 00373 } 00374 } 00375 if (values.size () == 0) 00376 return (false); 00377 else 00378 return (true); 00379 } 00380 00382 00391 bool 00392 terminal_tools::parse_multiple_2x_arguments (int argc, char** argv, const char* str, std::vector<double> &values_f, std::vector<double> &values_s) 00393 { 00394 double f, s; 00395 for (int i = 1; i < argc; ++i) 00396 { 00397 // Search for the string 00398 if ((strcmp (argv[i], str) == 0) && (++i < argc)) 00399 { 00400 // look for ',' as a separator 00401 std::vector<std::string> values; 00402 boost::split (values, argv[i], boost::is_any_of (","), boost::token_compress_on); 00403 if (values.size () != 2) 00404 { 00405 print_error ("[parse_multiple_2x_arguments] Number of values for %s (%d) different than 2!\n", str, (int)values.size ()); 00406 return (false); 00407 } 00408 f = atof (values.at (0).c_str ()); 00409 s = atof (values.at (1).c_str ()); 00410 values_f.push_back (f); 00411 values_s.push_back (s); 00412 } 00413 } 00414 if (values_f.size () == 0) 00415 return (false); 00416 else 00417 return (true); 00418 } 00419 00421 00431 bool 00432 terminal_tools::parse_multiple_3x_arguments (int argc, char** argv, const char* str, 00433 std::vector<double> &values_f, 00434 std::vector<double> &values_s, 00435 std::vector<double> &values_t) 00436 { 00437 double f, s, t; 00438 for (int i = 1; i < argc; ++i) 00439 { 00440 // Search for the string 00441 if ((strcmp (argv[i], str) == 0) && (++i < argc)) 00442 { 00443 // look for ',' as a separator 00444 std::vector<std::string> values; 00445 boost::split (values, argv[i], boost::is_any_of (","), boost::token_compress_on); 00446 if (values.size () != 3) 00447 { 00448 print_error ("[parse_multiple_3x_arguments] Number of values for %s (%d) different than 3!\n", str, (int)values.size ()); 00449 return (false); 00450 } 00451 f = atof (values.at (0).c_str ()); 00452 s = atof (values.at (1).c_str ()); 00453 t = atof (values.at (2).c_str ()); 00454 values_f.push_back (f); 00455 values_s.push_back (s); 00456 values_t.push_back (t); 00457 } 00458 } 00459 if (values_f.size () == 0) 00460 return (false); 00461 else 00462 return (true); 00463 } 00464