ADMB Documentation  11.1.2247
 All Classes Files Functions Variables Typedefs Friends Defines
optmatch.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: optmatch.cpp 1826 2014-04-03 18:28:32Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00007 
00012 #include <adstring.hpp>
00013 
00025 int option_match(int argc, char* argv[], const char* option)
00026 {
00027   int match = -1;
00028   if (argv)
00029   {
00030     for (int i = 0; i < argc; i++)
00031     {
00032       const char* argvi = argv[i];
00033       if (argvi && !strcmp(argvi, option))
00034       {
00035         return i;
00036       }
00037     }
00038   }
00039   return match;
00040 }
00045 int option_match(char* _s, const char* option)
00046 {
00047   adstring ss = _s;
00048   char* s = (char*)ss;
00049   int rval = -1;
00050   int i = 1;
00051   char* p = strtok(s," ");
00052   do {
00053     if (!strcmp(p, option))
00054     {
00055       rval = i;
00056       break;
00057     }
00058     i++;
00059     p = strtok(NULL, " ");
00060   }
00061   while (p);
00062 
00063   return rval;
00064 }
00069 int option_match(char* _s, const char* option, const int& _nopt)
00070 {
00071   adstring ss = _s;
00072   char* s = (char*)ss;
00073   int& nopt = (int&)_nopt;
00074   int rval = -1;
00075   int i = 1;
00076   nopt = 0;
00077   char* p = strtok(s," ");
00078   do {
00079     if (!strcmp(p, option))
00080     {
00081       rval = i;
00082       break;
00083     }
00084     p = strtok(NULL, " ");
00085     i++;
00086   }
00087   while (p);
00088 
00089   bool found = false;
00090   do
00091   {
00092     p = strtok(NULL, " ");
00093     if (!p) break;
00094     if (p[0] == '-')
00095       found = true;
00096     nopt++;
00097   }
00098   while(!found);
00099 
00100   return rval;
00101 }
00116 int option_match(int argc, char *argv[], const char *option, const int& _nopt)
00117 {
00118   int match = -1;
00119 
00120   int& nopt = (int&)_nopt;
00121   nopt=0;
00122 
00123   int i = option_match(argc, argv, option);
00124   if (i >= 0)
00125   {
00126     match = i;
00127 
00128     for (int j = i + 1; j < argc; j++)
00129     {
00130       if (argv[j][0] == '-') break;
00131       nopt++;
00132     }
00133   }
00134 
00135   return match;
00136 }