(file) Return to getoopt.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / getoopt

  1 karl  1.32 //%2006////////////////////////////////////////////////////////////////////////
  2 mike  1.13 //
  3 karl  1.28 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
  4            // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
  5            // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
  6 karl  1.27 // IBM Corp.; EMC Corporation, The Open Group.
  7 karl  1.28 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
  8            // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
  9 karl  1.29 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 10            // EMC Corporation; VERITAS Software Corporation; The Open Group.
 11 karl  1.32 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 12            // EMC Corporation; Symantec Corporation; The Open Group.
 13 mike  1.13 //
 14            // Permission is hereby granted, free of charge, to any person obtaining a copy
 15 kumpf 1.19 // of this software and associated documentation files (the "Software"), to
 16            // deal in the Software without restriction, including without limitation the
 17            // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 18 mike  1.13 // sell copies of the Software, and to permit persons to whom the Software is
 19            // furnished to do so, subject to the following conditions:
 20 karl  1.32 // 
 21 kumpf 1.19 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 22 mike  1.13 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 23            // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 24 kumpf 1.19 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 25            // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 26            // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 27 mike  1.13 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 28            // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 29            //
 30            //==============================================================================
 31            //
 32            // Author: Bob Blair (bblair@bmc.com)
 33            //
 34 mike  1.14 // Modified By: Carol Ann Krug Graves, Hewlett-Packard Company
 35 david.dillard 1.31 //                  (carolann_graves@hp.com)
 36                    //              David Dillard, VERITAS Software Corp.
 37                    //                  (david.dillard@veritas.com)
 38 mike          1.13 //
 39                    //%/////////////////////////////////////////////////////////////////////////////
 40                    
 41                    
 42                    //
 43                    // implementation of getoopt
 44                    
 45 kumpf         1.15 #include <Pegasus/Common/PegasusVersion.h>
 46 humberto      1.26 #include <Pegasus/Common/MessageLoader.h> //l10n
 47 kumpf         1.15 
 48 mike          1.13 #include "getoopt.h"
 49                    #include <cctype>
 50                    #include <cstdlib>
 51                    
 52                    PEGASUS_USING_STD;
 53                    
 54                    //-----------------------------------------------------------------------
 55                    //              Implementation of class Optarg
 56                    // An Optarg is created for each parameter on the command line.
 57                    //-----------------------------------------------------------------------
 58                    
 59                    // Constructors
 60                    //   Default Constructor
 61                    Optarg::Optarg() : _name(""), _opttype(REGULAR), _value("") {
 62                    }
 63                    
 64                    //   All-in-one
 65                    Optarg::Optarg(const String &name, opttype type, const String &value) :
 66                      _name(name), _opttype(type), _value(value) {
 67                    }
 68                    
 69 mike          1.13 // Destructor
 70                    Optarg::~Optarg() {};
 71                    
 72                    //-----------------------------------------------------------------------
 73                    //        Set the class members
 74                    //-----------------------------------------------------------------------
 75                    
 76                    // Set the _name member
 77                    void
 78                    Optarg::setName(const String &name) {
 79                      _name = name;
 80                    }
 81                    
 82                    // Set the _opttype member
 83                    void
 84                    Optarg::setType(opttype type) {
 85                      _opttype = type;
 86                    }
 87                    
 88                    // Set the _value member
 89                    void
 90 mike          1.13 Optarg::setValue(const String &value) {
 91                      _value = value;
 92                    }
 93                    
 94                    //-----------------------------------------------------------------------
 95                    //      Retrieve class members
 96                    //-----------------------------------------------------------------------
 97                    
 98                    //  Get the _name member
 99                    const String &
100                    Optarg::getName() const { return _name; }
101                    
102                    //  Get the _name member using getopt() terminology
103                    const String &
104                    Optarg::getopt()  const { return _name; }
105                    
106                    //  Get the _type member
107 david.dillard 1.31 Optarg::opttype
108 mike          1.13 Optarg::getType() const { return _opttype; }
109                    
110                    //-------------------------------------------------------------------
111                    //             Ways to get the _value member
112                    //-------------------------------------------------------------------
113                    
114                    //  Return _value as const String ref
115                    const String &
116                    Optarg::Value() const { return _value; }
117                    
118                    //  Same thing as Value(), but using getopt() terminology
119                    const String &
120                    Optarg::optarg() const { return _value; }
121                    
122                    //  Fill in a caller-provided String
123                    void
124                    Optarg::Value(String &s) const { s = _value; }
125                    
126                    //  Fill in a caller-provided int with the integer conversion of the value.
127 david.dillard 1.31 void Optarg::Value (int &i) const
128 mike          1.13 {
129 kumpf         1.23     CString cs = _value.getCString();
130 mike          1.14     const char* s = cs;
131                        Boolean valid = true;
132                        Uint32 j;
133                        for (j = 0; j < strlen (s); j++)
134                        {
135 david.dillard 1.31         if ((!isdigit (s [j])) && (!isspace (s [j])) && (s [j] != '+') &&
136 mike          1.14             (s [j] != '-'))
137                            {
138                                valid = false;
139                                break;
140                            }
141                        }
142                        if (valid)
143                        {
144 kumpf         1.25         Sint64 i64;
145                            if ( !(sscanf (s, "%" PEGASUS_64BIT_CONVERSION_WIDTH "d", &i64)) ||
146                                 (i64 != Sint64(Sint32(i64))) )
147 mike          1.14         {
148 kumpf         1.24             throw TypeMismatchException ();
149 mike          1.14         }
150 kumpf         1.25 
151                            i = Sint32(i64);
152 mike          1.14     }
153                        else
154                        {
155 kumpf         1.24         throw TypeMismatchException ();
156 mike          1.14     }
157 mike          1.13 }
158                    
159                    //  Fill in a caller-provided unsigned int
160 david.dillard 1.31 void Optarg::Value (unsigned int &i) const
161 mike          1.14 {
162 kumpf         1.23     CString cs = _value.getCString();
163 mike          1.14     const char* s = cs;
164                        Boolean valid = true;
165                        Uint32 j;
166                    
167                        for (j = 0; j < strlen (s); j++)
168                        {
169                            if ((!isdigit (s [j])) && (!isspace (s [j])))
170                            {
171                                valid = false;
172                                break;
173                            }
174                        }
175                        if (valid)
176                        {
177 kumpf         1.25         Uint64 i64;
178                            if ( !(sscanf (s, "%" PEGASUS_64BIT_CONVERSION_WIDTH "u", &i64)) ||
179 david.dillard 1.30              (i64 > 0xFFFFFFFF))
180 mike          1.14         {
181 kumpf         1.24             throw TypeMismatchException ();
182 mike          1.14         }
183 kumpf         1.25 
184                            i = Uint32(i64);
185 mike          1.14     }
186                        else
187                        {
188 kumpf         1.24         throw TypeMismatchException ();
189 mike          1.14     }
190 mike          1.13 }
191                    
192 kumpf         1.23 //  Fill in a caller-provided long
193 mike          1.13 void
194                    Optarg::Value(long &l) const {
195 kumpf         1.23   l = (long)atoi(_value.getCString());
196 mike          1.13 }
197                    
198 kumpf         1.23 //  Fill in a caller-provided unsigned long
199 mike          1.13 void
200                    Optarg::Value(unsigned long &l) const {
201 kumpf         1.23   l = (unsigned long)atoi(_value.getCString());
202 mike          1.13 }
203                    
204 kumpf         1.23 //  Fill in a caller-provided double
205 mike          1.13 void
206                    Optarg::Value(double &d) const {
207 kumpf         1.23   d = (double)atof(_value.getCString());
208 mike          1.13 }
209                    
210                    //--------------------------------------------------------------------
211                    //  Provide information about the flag, if any
212                    //--------------------------------------------------------------------
213                    
214                    // Is the option value is bound to a flag?
215                    Boolean
216                    Optarg::isFlag() const { return (_opttype == FLAG || _opttype == LONGFLAG); }
217                    
218                    // Is it bound to a long-named flag?
219                    Boolean
220                    Optarg::isLongFlag() const { return (_opttype == LONGFLAG); }
221                    
222                    //-----------------------------------------------------------------------
223                    //  print the members as a formatted String
224                    //-----------------------------------------------------------------------
225                    ostream &
226                    Optarg::print(ostream &os) const {
227                      os << "{name:(" << getName();
228                      os << ") type:(";
229 mike          1.13   switch (getType()) {
230                      case FLAG: os << "FLAG"; break;
231                      case LONGFLAG: os << "LONGFLAG"; break;
232                      case REGULAR: os << "REGULAR"; break;
233                      }
234                      os << ") value:(" << Value() << ")}";
235                      return os;
236                    }
237                    
238                    
239                    
240                    //---------------------------------------------------------------------
241                    //                   Implementation of class getoopt
242                    //---------------------------------------------------------------------
243                    
244                    // Constructors and destructor
245                    
246                    // Default constructor.  The optional String is in the format of
247 david.dillard 1.31 // a getopt() optstring
248 mike          1.13 getoopt::getoopt(const char *optstring)
249                    {
250                      if (optstring) {
251                        addFlagspec(optstring);
252                      }
253                    }
254                    
255                    getoopt::~getoopt() {;}
256                    
257                    //----------------------------------------------------------------------
258                    //  methods to register program-defined flags and their characteristics
259                    //  The flags are encapsulated in the struct flagspec, an array of which
260                    //  is a member of this class.
261                    //  There are also methods to deregister flags. This is done by resetting
262                    //  a flagspec's active flag.
263                    //----------------------------------------------------------------------
264                    
265                    // Parse through a getopt() optstring and create flagspecs from each
266                    // short flag.
267                    Boolean
268                    getoopt::addFlagspec(const String &opt) {
269 mike          1.13   unsigned int size = opt.size();
270                      if (size == 0)
271                        return false;
272                      for (unsigned int i = 0; i < size; i++) {
273 david.dillard 1.31     char c = static_cast<char>(opt[i]);
274 mike          1.13     if ( ((i + 1) < size) && (opt[i+1] == ':') ) {
275                          if (!(addFlagspec(c, true))) {
276 david.dillard 1.31         return false;
277 mike          1.13       }
278                          ++i;
279                        } else {
280                          if (!(addFlagspec(c, false)))
281 david.dillard 1.31         return false;
282 mike          1.13     }
283                      }
284                      return true;
285                    }
286                    
287                    //  Create a filespec from a single short flag and push it onto the array
288                    Boolean
289                    getoopt::addFlagspec(char flag, Boolean hasarg) {
290                      if (flag == '*') {
291 humberto      1.26   	//l10n
292                      	MessageLoaderParms parms("getoopt.getoopt.CANT_NAME_FLAG",
293                      							 "You can't have a flag named '$0'",
294                      							 flag);
295                      	addError(MessageLoader::getMessage(parms));
296                        //addError("You can't have a flag named '*'");
297                        //l10n end
298 mike          1.13     return false;
299                      }
300                      flagspec fs;
301                      char c[2];
302                      c[0] = flag;
303                      c[1] = 0;
304                      fs.name = c;
305                      fs.argtype = hasarg ? 1 : 0;
306                      fs.islong = false;
307                      fs.active = true;
308                      _flagspecs.append(fs);
309                      return true;
310                    }
311                    
312                    // Create a flagspec from a single long flag and push it onto the array
313                    Boolean
314                    getoopt::addLongFlagspec(const String &name, argtype type) {
315                      flagspec fs;
316                    
317                      // Changing "fs.name = name" to the following line masks an ugly crash
318                      // which occurs when compiling with debug option on WIN32:
319 mike          1.13 
320                      fs.name = name;
321 david.dillard 1.31 
322 mike          1.13   fs.argtype = type;
323                      fs.islong = true;
324                      fs.active = true;
325                      _flagspecs.append(fs);
326                      return true;
327                    }
328                    
329                    // Unregister a flagspec
330                    Boolean
331                    getoopt::removeFlagspec(char opt) {
332                      flagspec *fs = getFlagspecForUpdate(opt);
333                      if (fs) {
334                        fs->active = false;
335                        return true;
336                      }
337                      return false;
338                    }
339 mike          1.14 
340                    /**
341                        In the valid option definition string, following an option,
342                        indicates that the preceding option takes a required argument.
343                     */
344                    const char getoopt::GETOPT_ARGUMENT_DESIGNATOR = ':';
345 mike          1.13 
346                    //--------------------------------------------------------------------
347                    //      Routines for parsing the command line
348                    //--------------------------------------------------------------------
349                    
350                    //------------------------
351                    // Static functions
352                    //------------------------
353                    
354                    // Parse out the flagname and the value from a long flag option that
355                    // may be in the form
356                    //          --longflag=value
357                    static void
358                    partsFromLongOpt (const String &s, String &name, String &value) {
359                      for (unsigned int i = 0; i < s.size(); i++) {
360                        if (s[i] == '=') {
361                          name = s.subString(0, i);
362                          value = s.subString(i+1);
363                          return;
364                        }
365                      }
366 mike          1.13   name = s;
367                      value =  "";
368                    }
369                    
370                    // Create an Optarg instance from a long flag String like
371                    //          --longflag=value
372                    // (The =value is optional).
373 david.dillard 1.31 static void
374 mike          1.13 optargFromLongOpt(Optarg &o, const String &arg) {
375                      String name;
376                      String value;
377                      partsFromLongOpt(arg, name, value);
378                      o.setName(name);
379                      o.setType(Optarg::LONGFLAG);
380                      o.setValue(value);
381                    }
382                    
383                    // Create an Optarg instance from a short flag String like
384                    //      -fValue
385                    // (The Value part is optional)
386                    static void
387                    optargFromShortOpt(Optarg &o, const char *arg) {
388                      char name[2];
389                      name[0] = arg[0];
390                      name[1] = 0;
391                      o.setName(name);
392                      o.setType(Optarg::FLAG);
393                      const char *p = arg + 1;
394                      o.setValue(p);
395 mike          1.13 }
396                    
397                    // Look at a command line option and determine whether it is a
398                    // long flag, a short flag or an unflagged option.
399                    static int
400                    catagorize(const char *s) {
401                      if (s[0] != '-')
402                        return 0;
403                      else
404                        if (s[1] == '-')
405                          return 2;
406                      return 1;
407                    }
408                    
409                    // Push an Optarg onto our array
410                    static void
411                    addarg(getoopt::Arg_List&list, const Optarg &o) {
412                      //o.print(cout);
413                      list.append(o);
414                    }
415                    
416 mike          1.13 // Create an Optarg from its members and push it onto the array
417                    static void
418                    addarg(getoopt::Arg_List&list, const String &name, Optarg::opttype type,
419                    	      const String &value) {
420                      Optarg *o = new Optarg(name, type, value);
421                      addarg(list, *o);
422                      delete o;
423                    }
424                    
425                    // Take an array of arguments and append it to another
426                    static void
427                    copyargs(getoopt::Arg_List &out, const getoopt::Arg_List &in) {
428 kumpf         1.21   Uint32 size = in.size();
429                      for (Uint32 i = 0; i < size; i++) {
430                        addarg(out, in[i]);
431 mike          1.13   }
432                    }
433                    
434                    //------------------------------------
435                    // The parse method:  Way too long.
436                    // Note that flag args are pushed
437                    // onto the stack, then the regular
438                    // args are appended, sorting them
439                    // to the rear the way getopt() does.
440                    //------------------------------------
441                    Boolean
442                    getoopt::parse(int argc, char **argv) {
443                      Optarg o;
444                      int cat;
445                      const flagspec *fs;
446                      Arg_List nonflagargs;
447                      enum states {START, ARGEXPECTED};
448                      states state = START;
449                      for (unsigned int i = 1; i < (unsigned int)argc; i++) {
450 david.dillard 1.31     unsigned int endsize = static_cast<unsigned int>(strlen(argv[i]));
451 mike          1.13       switch (state) {
452                          case START:
453                            cat = catagorize(argv[i]);
454                    	switch (cat) {
455                    	case 0: // non-flag command line argument
456                    	  addarg(nonflagargs, "", Optarg::REGULAR, argv[i]);
457                    	  break;
458                    	case 1: // short (1-character) flag
459                    	  {
460                    	    unsigned int argpos = 1;
461                    	    while (argpos < endsize) {
462                    	      char c = argv[i][argpos];
463                    	      fs = getFlagspec(c);  // Short flag
464                    	      String temp = argv[i];
465                    	      String name = temp.subString(argpos, 1);
466                    	      if (!fs) {  // See if we recognize it
467 humberto      1.26 	      	//l10n
468                    	      	MessageLoaderParms parms("getoopt.getoopt.UNKNOWN_FLAG",
469                    	      							 "Unknown flag $0$1",
470                    	      							 "-",
471                    	      							 name);
472                    	      	addError(MessageLoader::getMessage(parms));
473                    			//addError("Unknown flag -" + name);
474                    			//l10n end
475                    			argpos++;
476 mike          1.13 	      } else {
477                    		if (fs->argtype == NOARG) {  // Should this flag be bound
478                    		  addarg(_args, name, Optarg::FLAG,  "");  // NO
479                    		  argpos++;
480                    		} else { // YES -- the value is here or in the next arg
481                    		  optargFromShortOpt(o, &argv[i][argpos]);
482                    		  if (o.Value() == "") { // No value yet
483                    		    state = ARGEXPECTED;
484                    		  } else {
485                    		    addarg(_args, o);
486                    		  }
487                    		  argpos = endsize;
488                    		}
489                    	      }
490                    	    }
491                    	  } // end subcase 1
492                    	  break;
493                    	case 2:  // long (--xyz) flag
494 david.dillard 1.31 	  {
495 mike          1.13 	    String arg = &(argv[i][2]);
496                    	    optargFromLongOpt(o, arg);
497                    	    fs = getFlagspec(o.getName());
498                    	    if (!fs) { // see if we recognize this flag
499 humberto      1.26 	    	//l10n
500                    	      //String temp = "Unknown flag ";
501                    	      //addError(temp + o.getName());
502                    	      MessageLoaderParms parms("getoopt.getoopt.UNKNOWN_FLAG",
503                    	      						   "Unknown flag $0$1",
504                    	      						   "",
505                    	      						   o.getName());
506                    	      addError(MessageLoader::getMessage(parms));
507                    	      //l10n end
508 mike          1.13 	    } else {
509                    	        // this is a long flag we know about
510 david.dillard 1.31 	      if (o.optarg() != ""  || fs->argtype != MUSTHAVEARG) {
511 mike          1.13 		addarg(_args, o);
512                    		state = START;  // we have a completed long flag
513                    	      } else {   // no value yet, and we expect one
514 kumpf         1.16 		if (fs->argtype == MUSTHAVEARG) {
515 mike          1.13 		  state = ARGEXPECTED;
516                    		}
517                    	      }
518                    	    }
519                    	    break;
520                    	  } // end subcase 2
521                    	} // end switch catagorize()
522                    	break; // end of case START
523                    
524                          case ARGEXPECTED:
525                    	if (argv[i][0] == '-') {
526 humberto      1.26 		//l10n
527                    	  //addError("Missing required value for flag " + o.getopt());
528                    	  MessageLoaderParms parms("getoopt.getoopt.MISSING_VALUE_FOR_FLAG",
529                    	      					   "Missing required value for flag $0",
530                    	      					   o.getopt());
531                    	  addError(MessageLoader::getMessage(parms));
532                    	  //l10n end
533 mike          1.13 	  i--;
534                    	} else {
535                    	  o.setValue(argv[i]);
536                    	}
537                    	addarg(_args, o);
538                    	state = START;
539                    	break;
540                          } // end switch
541                      } // end for
542                      if (state != START) {
543 humberto      1.26   	//l10n
544                        //addError("Missing required value for flag " + o.getName());
545                        MessageLoaderParms parms("getoopt.getoopt.MISSING_VALUE_FOR_FLAG",
546                    	      					   "Missing required value for flag $0",
547                    	      					   o.getName());
548                    	  addError(MessageLoader::getMessage(parms));
549                        //l10n end
550 mike          1.13   }
551                      copyargs(_args, nonflagargs);
552                      return !_errorStrings.size();
553                    }
554                    
555                    //----------------------------------------------------------------------
556                    //         Methods to retrieve the command line arguments
557                    //----------------------------------------------------------------------
558                    
559                    //----------------------------------------------
560                    // Access the command line arguments by index
561                    //----------------------------------------------
562                    
563                    // Index operator
564                    const Optarg &
565                    getoopt::operator[](unsigned int n) {
566                      unsigned int lim = _args.size();
567 david.dillard 1.31   if (n < lim)
568 mike          1.13     return _args[n];
569                      else
570                        return _emptyopt;
571                    }
572                    
573                    // Return first index
574                    unsigned int
575                    getoopt::first() const { return 0; }
576                    
577                    // Return one past last index
578                    unsigned int
579                    getoopt::last() const { return _args.size(); }
580                    
581                    //-----------------------------------------------
582                    // Access the command line arguments ad-hoc
583                    //-----------------------------------------------
584                    
585                    // Return the number of times a short flag is set
586                    // on the command line
587                    unsigned int
588                    getoopt::isSet(char c) const {
589 mike          1.13   unsigned int cnt = 0;
590                      for (unsigned int i = 0; i < _args.size(); i++) {
591                        const Optarg &o = _args[i];
592                        if (o.getType() == Optarg::FLAG) {
593                          const String &s = o.getopt();
594                          if (s[0] == c) {
595                    	cnt++;
596                          }
597                        }
598                      }
599                      return cnt;
600                    }
601                    
602                    // Return the number of times any flag is set
603                    // on the command line
604                    unsigned int
605                    getoopt::isSet(const String &s) const {
606                      unsigned int cnt = 0;
607                      for (unsigned int i = 0; i < _args.size(); i++) {
608                        const Optarg &o = _args[i];
609                        if (o.getopt() == s) {
610 mike          1.13       cnt++;
611                        }
612                      }
613                      return cnt;
614                    }
615                    
616                    // Return the String value of the nth instance of
617                    // a particular short flag on the command line
618                    const String &
619                    getoopt::value(char opt, unsigned int idx) const {
620                      unsigned int cnt = 0;
621                      for (unsigned int i = 0; i < _args.size(); i++) {
622                        const Optarg &o = _args[i];
623                        if (o.getType() == Optarg::FLAG) {
624                          const String &s = o.getopt();
625                          if (s[0] == opt) {
626                    	if (cnt == idx) {
627                    	  return o.optarg();
628                    	} else {
629                    	  cnt++;
630                    	}
631 mike          1.13       }
632                        }
633                      }
634                      return(emptystring);
635                    }
636                    
637                    // Return the nth instance of any flag on the command line
638                    const String &
639                    getoopt::value(const String &opt, unsigned int idx) const {
640                      unsigned int cnt = 0;
641                      for (unsigned int i = 0; i < _args.size(); i++) {
642                        const Optarg &o = _args[i];
643                        if (o.optarg() == opt) {
644                          if (cnt == idx) {
645                    	return o.getopt();
646                          } else {
647                    	cnt++;
648                          }
649                        }
650                      }
651                      return(emptystring);
652 mike          1.13 }
653                    
654                    // Of the command line arguments, how many are flags?
655                    unsigned int
656                    getoopt::flagcnt() const {
657                      unsigned int cnt = 0;
658 kumpf         1.21   for (Uint32 i = 0; i < _args.size(); i++) {
659                        if (_args[i].getType() != Optarg::REGULAR)
660 david.dillard 1.31       cnt++;
661 mike          1.13   }
662                      return cnt;
663                    }
664                    
665                    // How many command line arguments were there?
666                    unsigned int
667                    getoopt::size() const {
668                      return _args.size();
669                    }
670                    
671                    // Return the list of command line arguments for use by
672                    // the program.
673                    const getoopt::Arg_List &
674                    getoopt::getArgs() const { return _args; }
675                    
676                    
677                    //-----------------------------------------------------------
678                    // Routines dealing with errors during parsing
679                    // FIXME:  This needs to be reworked so that the error text
680                    // is hidden and provided by the caller
681                    //----------------------------------------------------------
682 mike          1.13 
683                    // Add an error into the list
684                    void
685                    getoopt::addError(const String &s)
686                    {
687                      _errorStrings.append(s);
688                    }
689                    
690                    // Return a list of the errors
691                    const getoopt::Error_List &
692                    getoopt::getErrorStrings() const {
693                      return _errorStrings;
694                    }
695                    
696                    // Did any errors occur?
697                    Boolean
698                    getoopt::hasErrors() const {
699                      return _errorStrings.size() ? true : false;
700                    }
701                    
702                    
703 mike          1.13 
704                    flagspec *
705                    getoopt::getFlagspecForUpdate(const String &s) {
706                      for (unsigned int i = 0; i < _flagspecs.size(); i++) {
707                        flagspec &o = _flagspecs[i];
708                        if (o.islong && s == o.name)
709                          return &_flagspecs[i];
710                      }
711                    return 0;
712                    }
713                    
714                    const flagspec *
715                    getoopt::getFlagspec(const String &s) {
716                      return (const flagspec *)getFlagspecForUpdate(s);
717                    }
718                    
719                    ostream &
720                    getoopt::printErrors(ostream &os) const {
721 kumpf         1.21   for (Uint32 i = 0; i < _errorStrings.size(); i++) {
722                        os << "> " << _errorStrings[i] << endl;
723 mike          1.13   }
724                      return os;
725                    }
726                    
727                    void
728                    getoopt::printErrors(String &s) const {
729 kumpf         1.21   for (Uint32 i = 0; i < _errorStrings.size(); i++) {
730 kumpf         1.22     s.append("> " + _errorStrings[i] + "\n");
731 mike          1.13   }
732                    }
733                    
734                    //---------------------------------------------------------------
735                    //              Private methods
736                    //---------------------------------------------------------------
737                    flagspec *
738                    getoopt::getFlagspecForUpdate(char c) {
739                      for (unsigned int i = 0; i < _flagspecs.size(); i++) {
740                        flagspec &o = _flagspecs[i];
741                        if (!o.islong && c == o.name[0])
742                          return &_flagspecs[i];
743                      }
744                      return 0;
745                    }
746                    
747                    const flagspec *
748                    getoopt::getFlagspec(char c) {
749                      return (const flagspec *)getFlagspecForUpdate(c);
750                    }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2