ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
optmatch.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id$
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 }
00046 int option_match(char* _s, const char* option)
00047 {
00048   adstring ss = _s;
00049   char* s = (char*)ss;
00050   int rval = -1;
00051   int i = 1;
00052   char* p = strtok(s," ");
00053   while (p != NULL)
00054   {
00055     if (!strcmp(p, option))
00056     {
00057       rval = i;
00058       break;
00059     }
00060     i++;
00061     p = strtok(NULL, " ");
00062   }
00063 
00064   return rval;
00065 }
00072 int option_match(char* _s, const char* option, int& nopt)
00073 {
00074   adstring ss = _s;
00075   char* s = (char*)ss;
00076   int rval = -1;
00077   int i = 1;
00078   nopt = 0;
00079   char* p = strtok(s," ");
00080   while (p)
00081   {
00082     if (!strcmp(p, option))
00083     {
00084       rval = i;
00085       break;
00086     }
00087     p = strtok(NULL, " ");
00088     i++;
00089   }
00090 
00091   bool found = false;
00092   do
00093   {
00094     p = strtok(NULL, " ");
00095     if (!p) break;
00096     if (p[0] == '-')
00097       found = true;
00098     nopt++;
00099   }
00100   while(!found);
00101 
00102   return rval;
00103 }
00118 int option_match(int argc, char *argv[], const char *option, int& nopt)
00119 {
00120   int match = -1;
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 }