parse.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2010-2012, Willow Garage, Inc.
00006  *  Copyright (c) 2012-, Open Perception, Inc.
00007  *
00008  *  All rights reserved.
00009  *
00010  *  Redistribution and use in source and binary forms, with or without
00011  *  modification, are permitted provided that the following conditions
00012  *  are met:
00013  *
00014  *   * Redistributions of source code must retain the above copyright
00015  *     notice, this list of conditions and the following disclaimer.
00016  *   * Redistributions in binary form must reproduce the above
00017  *     copyright notice, this list of conditions and the following
00018  *     disclaimer in the documentation and/or other materials provided
00019  *     with the distribution.
00020  *   * Neither the name of the copyright holder(s) nor the names of its
00021  *     contributors may be used to endorse or promote products derived
00022  *     from this software without specific prior written permission.
00023  *
00024  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00026  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00027  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00028  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00029  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00030  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00031  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00033  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00034  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00035  *  POSSIBILITY OF SUCH DAMAGE.
00036  *
00037  */
00038 
00039 #include <ctype.h>
00040 #include <stdio.h>
00041 #include <pcl/console/parse.h>
00042 #include <pcl/console/print.h>
00043 #include <boost/algorithm/string.hpp>
00044 
00046 bool
00047 pcl::console::find_switch (int argc, char** argv, const char* argument_name)
00048 {
00049   return (find_argument (argc, argv, argument_name) != -1);
00050 }
00051 
00053 int
00054 pcl::console::find_argument (int argc, char** argv, const char* argument_name)
00055 {
00056   for (int i = 1; i < argc; ++i)
00057   {
00058     // Search for the string
00059     if (strcmp (argv[i], argument_name) == 0)
00060     {
00061       return (i);
00062     }
00063   }
00064   return (-1);
00065 }
00066 
00068 int
00069 pcl::console::parse_argument (int argc, char** argv, const char* str, std::string &val)
00070 {
00071   int index = find_argument (argc, argv, str) + 1;
00072   if (index > 0 && index < argc )
00073     val = argv[index];
00074 
00075   return index - 1;
00076 }
00077 
00079 int
00080 pcl::console::parse_argument (int argc, char** argv, const char* str, bool &val)
00081 {
00082   int index = find_argument (argc, argv, str) + 1;
00083 
00084   if (index > 0 && index < argc )
00085     val = atoi (argv[index]) == 1;
00086 
00087   return (index - 1);
00088 }
00089 
00091 int
00092 pcl::console::parse_argument (int argc, char** argv, const char* str, double &val)
00093 {
00094   int index = find_argument (argc, argv, str) + 1;
00095 
00096   if (index > 0 && index < argc )
00097     val = atof (argv[index]);
00098 
00099   return (index - 1);
00100 }
00101 
00103 int
00104 pcl::console::parse_argument (int argc, char** argv, const char* str, float &val)
00105 {
00106   int index = find_argument (argc, argv, str) + 1;
00107 
00108   if (index > 0 && index < argc )
00109     val = static_cast<float> (atof (argv[index]));
00110 
00111   return (index - 1);
00112 }
00113 
00115 int
00116 pcl::console::parse_argument (int argc, char** argv, const char* str, int &val)
00117 {
00118   int index = find_argument (argc, argv, str) + 1;
00119 
00120   if (index > 0 && index < argc )
00121     val = atoi (argv[index]);
00122 
00123   return (index - 1);
00124 }
00125 
00127 int
00128 pcl::console::parse_argument (int argc, char** argv, const char* str, unsigned int &val)
00129 {
00130   int index = find_argument (argc, argv, str) + 1;
00131 
00132   if (index > 0 && index < argc )
00133     val = atoi (argv[index]);
00134 
00135   return (index - 1);
00136 }
00137 
00139 int
00140 pcl::console::parse_argument (int argc, char** argv, const char* str, char &val)
00141 {
00142   int index = find_argument (argc, argv, str) + 1;
00143 
00144   if (index > 0 && index < argc )
00145     val = argv[index][0];
00146 
00147   return (index - 1);
00148 }
00149 
00151 std::vector<int>
00152 pcl::console::parse_file_extension_argument (int argc, char** argv, const std::string &extension)
00153 {
00154   std::vector<int> indices;
00155   for (int i = 1; i < argc; ++i)
00156   {
00157     std::string fname = std::string (argv[i]);
00158     std::string ext = extension;
00159 
00160     // Needs to be at least 4: .ext
00161     if (fname.size () <= 4)
00162       continue;
00163 
00164     // For being case insensitive
00165     std::transform (fname.begin (), fname.end (), fname.begin (), tolower);
00166     std::transform (ext.begin (), ext.end (), ext.begin (), tolower);
00167 
00168     // Check if found
00169     std::string::size_type it;
00170     if ((it = fname.rfind (ext)) != std::string::npos)
00171     {
00172       // Additional check: we want to be able to differentiate between .p and .png
00173       if ((ext.size () - (fname.size () - it)) == 0)
00174         indices.push_back (i);
00175     }
00176   }
00177   return (indices);
00178 }
00179 
00181 int
00182 pcl::console::parse_2x_arguments (int argc, char** argv, const char* str, float &f, float &s, bool debug)
00183 {
00184   for (int i = 1; i < argc; ++i)
00185   {
00186     // Search for the string
00187     if ((strcmp (argv[i], str) == 0) && (++i < argc))
00188     {
00189       // look for ',' as a separator
00190       std::vector<std::string> values;
00191       boost::split (values, argv[i], boost::is_any_of (","), boost::token_compress_on);
00192       if (values.size () != 2 && debug)
00193       {
00194         print_error ("[parse_2x_arguments] Number of values for %s (%zu) different than 2!\n", str, values.size ());
00195         return (-2);
00196       }
00197       f = static_cast<float> (atof (values.at (0).c_str ()));
00198       s = static_cast<float> (atof (values.at (1).c_str ()));
00199       return (i - 1);
00200     }
00201   }
00202   return (-1);
00203 }
00204 
00206 int
00207 pcl::console::parse_2x_arguments (int argc, char** argv, const char* str, double &f, double &s, bool debug)
00208 {
00209   for (int i = 1; i < argc; ++i)
00210   {
00211     // Search for the string
00212     if ((strcmp (argv[i], str) == 0) && (++i < argc))
00213     {
00214       // look for ',' as a separator
00215       std::vector<std::string> values;
00216       boost::split (values, argv[i], boost::is_any_of (","), boost::token_compress_on);
00217       if (values.size () != 2 && debug)
00218       {
00219         print_error ("[parse_2x_arguments] Number of values for %s (%zu) different than 2!\n", str, values.size ());
00220         return (-2);
00221       }
00222       f = atof (values.at (0).c_str ());
00223       s = atof (values.at (1).c_str ());
00224       return (i - 1);
00225     }
00226   }
00227   return (-1);
00228 }
00229 
00231 int
00232 pcl::console::parse_2x_arguments (int argc, char** argv, const char* str, int &f, int &s, bool debug)
00233 {
00234   for (int i = 1; i < argc; ++i)
00235   {
00236     // Search for the string
00237     if ((strcmp (argv[i], str) == 0) && (++i < argc))
00238     {
00239       // look for ',' as a separator
00240       std::vector<std::string> values;
00241       boost::split (values, argv[i], boost::is_any_of (","), boost::token_compress_on);
00242       if (values.size () != 2 && debug)
00243       {
00244         print_error ("[parse_2x_arguments] Number of values for %s (%zu) different than 2!\n", str, values.size ());
00245         return (-2);
00246       }
00247       f = atoi (values.at (0).c_str ());
00248       s = atoi (values.at (1).c_str ());
00249       return (i - 1);
00250     }
00251   }
00252   return (-1);
00253 }
00254 
00256 int
00257 pcl::console::parse_3x_arguments (int argc, char** argv, const char* str, float &f, float &s, float &t, bool debug)
00258 {
00259   for (int i = 1; i < argc; ++i)
00260   {
00261     // Search for the string
00262     if ((strcmp (argv[i], str) == 0) && (++i < argc))
00263     {
00264       // look for ',' as a separator
00265       std::vector<std::string> values;
00266       boost::split (values, argv[i], boost::is_any_of (","), boost::token_compress_on);
00267       if (values.size () != 3 && debug)
00268       {
00269         print_error ("[parse_3x_arguments] Number of values for %s (%zu) different than 3!\n", str, values.size ());
00270         return (-2);
00271       }
00272       f = static_cast<float> (atof (values.at (0).c_str ()));
00273       s = static_cast<float> (atof (values.at (1).c_str ()));
00274       t = static_cast<float> (atof (values.at (2).c_str ()));
00275       return (i - 1);
00276     }
00277   }
00278   return (-1);
00279 }
00280 
00282 int
00283 pcl::console::parse_3x_arguments (int argc, char** argv, const char* str, double &f, double &s, double &t, bool debug)
00284 {
00285   for (int i = 1; i < argc; ++i)
00286   {
00287     // Search for the string
00288     if ((strcmp (argv[i], str) == 0) && (++i < argc))
00289     {
00290       // look for ',' as a separator
00291       std::vector<std::string> values;
00292       boost::split (values, argv[i], boost::is_any_of (","), boost::token_compress_on);
00293       if (values.size () != 3 && debug)
00294       {
00295         print_error ("[parse_3x_arguments] Number of values for %s (%zu) different than 3!\n", str, values.size ());
00296         return (-2);
00297       }
00298       f = atof (values.at (0).c_str ());
00299       s = atof (values.at (1).c_str ());
00300       t = atof (values.at (2).c_str ());
00301       return (i - 1);
00302     }
00303   }
00304   return (-1);
00305 }
00306 
00308 int
00309 pcl::console::parse_3x_arguments (int argc, char** argv, const char* str, int &f, int &s, int &t, bool debug)
00310 {
00311   for (int i = 1; i < argc; ++i)
00312   {
00313     // Search for the string
00314     if ((strcmp (argv[i], str) == 0) && (++i < argc))
00315     {
00316       // look for ',' as a separator
00317       std::vector<std::string> values;
00318       boost::split (values, argv[i], boost::is_any_of (","), boost::token_compress_on);
00319       if (values.size () != 3 && debug)
00320       {
00321         print_error ("[parse_3x_arguments] Number of values for %s (%zu) different than 3!\n", str, values.size ());
00322         return (-2);
00323       }
00324       f = atoi (values.at (0).c_str ());
00325       s = atoi (values.at (1).c_str ());
00326       t = atoi (values.at (2).c_str ());
00327       return (i - 1);
00328     }
00329   }
00330   return (-1);
00331 }
00332 
00334 int
00335 pcl::console::parse_x_arguments (int argc, char** argv, const char* str, std::vector<double>& v)
00336 {
00337   for (int i = 1; i < argc; ++i)
00338   {
00339     // Search for the string
00340     if ((strcmp (argv[i], str) == 0) && (++i < argc))
00341     {
00342       // look for ',' as a separator
00343       std::vector<std::string> values;
00344       boost::split (values, argv[i], boost::is_any_of (","), boost::token_compress_on);
00345 
00346       v.resize (values.size ());
00347       for (size_t j = 0; j < v.size (); ++j)
00348         v[j] = atof (values.at (j).c_str ());
00349 
00350       return (i - 1);
00351     }
00352   }
00353   return (-1);
00354 }
00355 
00357 int
00358 pcl::console::parse_x_arguments (int argc, char** argv, const char* str, std::vector<float>& v)
00359 {
00360   for (int i = 1; i < argc; ++i)
00361   {
00362     // Search for the string
00363     if ((strcmp (argv[i], str) == 0) && (++i < argc))
00364     {
00365       // look for ',' as a separator
00366       std::vector<std::string> values;
00367       boost::split (values, argv[i], boost::is_any_of (","), boost::token_compress_on);
00368 
00369       v.resize (values.size ());
00370       for (size_t j = 0; j < v.size (); ++j)
00371         v[j] = static_cast<float> (atof (values.at (j).c_str ()));
00372 
00373       return (i - 1);
00374     }
00375   }
00376   return (-1);
00377 }
00378 
00380 int
00381 pcl::console::parse_x_arguments (int argc, char** argv, const char* str, std::vector<int>& v)
00382 {
00383   for (int i = 1; i < argc; ++i)
00384   {
00385     // Search for the string
00386     if ((strcmp (argv[i], str) == 0) && (++i < argc))
00387     {
00388       // look for ',' as a separator
00389       std::vector<std::string> values;
00390       boost::split (values, argv[i], boost::is_any_of (","), boost::token_compress_on);
00391 
00392       v.resize (values.size ());
00393       for (size_t j = 0; j < v.size (); ++j)
00394         v[j] = atoi (values.at (j).c_str ());
00395 
00396       return (i - 1);
00397     }
00398   }
00399   return (-1);
00400 }
00401 
00403 bool
00404 pcl::console::parse_multiple_arguments (int argc, char** argv, const char* str, std::vector<int> &values)
00405 {
00406   for (int i = 1; i < argc; ++i)
00407   {
00408     // Search for the string
00409     if ((strcmp (argv[i], str) == 0) && (++i < argc))
00410     {
00411       int val = atoi (argv[i]);
00412       values.push_back (val);
00413     }
00414   }
00415   if (values.size () == 0)
00416     return (false);
00417   else
00418     return (true);
00419 }
00420 
00422 bool
00423 pcl::console::parse_multiple_arguments (int argc, char** argv, const char* str, std::vector<double> &values)
00424 {
00425   for (int i = 1; i < argc; ++i)
00426   {
00427     // Search for the string
00428     if ((strcmp (argv[i], str) == 0) && (++i < argc))
00429     {
00430       double val = atof (argv[i]);
00431       values.push_back (val);
00432     }
00433   }
00434   if (values.size () == 0)
00435     return (false);
00436   else
00437     return (true);
00438 }
00439 
00441 bool
00442 pcl::console::parse_multiple_arguments (int argc, char** argv, const char* str, std::vector<float> &values)
00443 {
00444   for (int i = 1; i < argc; ++i)
00445   {
00446     // Search for the string
00447     if ((strcmp (argv[i], str) == 0) && (++i < argc))
00448     {
00449       float val = static_cast<float> (atof (argv[i]));
00450       values.push_back (val);
00451     }
00452   }
00453   if (values.size () == 0)
00454     return (false);
00455   else
00456     return (true);
00457 }
00458 
00460 bool
00461 pcl::console::parse_multiple_arguments (int argc, char** argv, const char* str, std::vector<std::string> &values)
00462 {
00463   for (int i = 1; i < argc; ++i)
00464   {
00465     // Search for the string
00466     if ((strcmp (argv[i], str) == 0) && (++i < argc))
00467     {
00468       values.push_back (std::string (argv[i]));
00469     }
00470   }
00471   if (values.size () == 0)
00472     return (false);
00473   else
00474     return (true);
00475 }
00476 
00478 bool
00479 pcl::console::parse_multiple_2x_arguments (int argc, char** argv, const char* str, std::vector<double> &values_f, std::vector<double> &values_s)
00480 {
00481   double f, s;
00482   for (int i = 1; i < argc; ++i)
00483   {
00484     // Search for the string
00485     if ((strcmp (argv[i], str) == 0) && (++i < argc))
00486     {
00487       // look for ',' as a separator
00488       std::vector<std::string> values;
00489       boost::split (values, argv[i], boost::is_any_of (","), boost::token_compress_on);
00490       if (values.size () != 2)
00491       {
00492         print_error ("[parse_multiple_2x_arguments] Number of values for %s (%zu) different than 2!\n", str, values.size ());
00493         return (false);
00494       }
00495       f = atof (values.at (0).c_str ());
00496       s = atof (values.at (1).c_str ());
00497       values_f.push_back (f);
00498       values_s.push_back (s);
00499     }
00500   }
00501   if (values_f.size () == 0)
00502     return (false);
00503   else
00504     return (true);
00505 }
00506 
00508 bool
00509 pcl::console::parse_multiple_3x_arguments (int argc, char** argv, const char* str,
00510                                              std::vector<double> &values_f,
00511                                              std::vector<double> &values_s,
00512                                              std::vector<double> &values_t)
00513 {
00514   double f, s, t;
00515   for (int i = 1; i < argc; ++i)
00516   {
00517     // Search for the string
00518     if ((strcmp (argv[i], str) == 0) && (++i < argc))
00519     {
00520       // look for ',' as a separator
00521       std::vector<std::string> values;
00522       boost::split (values, argv[i], boost::is_any_of (","), boost::token_compress_on);
00523       if (values.size () != 3)
00524       {
00525         print_error ("[parse_multiple_3x_arguments] Number of values for %s (%zu) different than 3!\n", str, values.size ());
00526         return (false);
00527       }
00528       f = atof (values.at (0).c_str ());
00529       s = atof (values.at (1).c_str ());
00530       t = atof (values.at (2).c_str ());
00531       values_f.push_back (f);
00532       values_s.push_back (s);
00533       values_t.push_back (t);
00534     }
00535   }
00536   if (values_f.size () == 0)
00537     return (false);
00538   else
00539     return (true);
00540 }
00541 


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:27:33