00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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
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
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
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
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
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
00174 if (fname.size () <= 4)
00175 continue;
00176
00177
00178 std::transform (fname.begin (), fname.end (), fname.begin (), tolower);
00179 std::transform (ext.begin (), ext.end (), ext.begin (), tolower);
00180
00181
00182 std::string::size_type it;
00183 if ((it = fname.find (ext)) != std::string::npos)
00184 {
00185
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
00208 if ((strcmp (argv[i], str) == 0) && (++i < argc))
00209 {
00210
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
00241 if ((strcmp (argv[i], str) == 0) && (++i < argc))
00242 {
00243
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
00275 if ((strcmp (argv[i], str) == 0) && (++i < argc))
00276 {
00277
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
00310 if ((strcmp (argv[i], str) == 0) && (++i < argc))
00311 {
00312
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
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
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
00398 if ((strcmp (argv[i], str) == 0) && (++i < argc))
00399 {
00400
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
00441 if ((strcmp (argv[i], str) == 0) && (++i < argc))
00442 {
00443
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