(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 kumpf         1.33 #include <Pegasus/Common/MessageLoader.h>
 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 kumpf         1.33 Optarg::Optarg()
 62                        : _name(""),
 63                          _opttype(REGULAR),
 64                          _value("")
 65                    {
 66 mike          1.13 }
 67                    
 68                    //   All-in-one
 69 kumpf         1.33 Optarg::Optarg(
 70                        const String& name,
 71                        opttype type,
 72                        const String& value)
 73                        : _name(name),
 74                          _opttype(type),
 75                          _value(value)
 76                    {
 77 mike          1.13 }
 78                    
 79                    // Destructor
 80 kumpf         1.33 Optarg::~Optarg()
 81                    {
 82                    }
 83 mike          1.13 
 84                    //-----------------------------------------------------------------------
 85                    //        Set the class members
 86                    //-----------------------------------------------------------------------
 87                    
 88                    // Set the _name member
 89 kumpf         1.33 void Optarg::setName(const String& name)
 90                    {
 91                        _name = name;
 92 mike          1.13 }
 93                    
 94                    // Set the _opttype member
 95 kumpf         1.33 void Optarg::setType(opttype type)
 96                    {
 97                        _opttype = type;
 98 mike          1.13 }
 99                    
100                    // Set the _value member
101 kumpf         1.33 void Optarg::setValue(const String& value)
102                    {
103                        _value = value;
104 mike          1.13 }
105                    
106                    //-----------------------------------------------------------------------
107                    //      Retrieve class members
108                    //-----------------------------------------------------------------------
109                    
110                    //  Get the _name member
111 kumpf         1.33 const String&
112 mike          1.13 Optarg::getName() const { return _name; }
113                    
114                    //  Get the _name member using getopt() terminology
115 kumpf         1.33 const String& Optarg::getopt() const
116                    {
117                        return _name;
118                    }
119 mike          1.13 
120                    //  Get the _type member
121 kumpf         1.33 Optarg::opttype Optarg::getType() const
122                    {
123                        return _opttype;
124                    }
125 mike          1.13 
126                    //-------------------------------------------------------------------
127                    //             Ways to get the _value member
128                    //-------------------------------------------------------------------
129                    
130                    //  Return _value as const String ref
131 kumpf         1.33 const String& Optarg::Value() const
132                    {
133                        return _value;
134                    }
135 mike          1.13 
136                    //  Same thing as Value(), but using getopt() terminology
137 kumpf         1.33 const String& Optarg::optarg() const
138                    {
139                        return _value;
140                    }
141 mike          1.13 
142                    //  Fill in a caller-provided String
143 kumpf         1.33 void Optarg::Value(String& s) const
144                    {
145                        s = _value;
146                    }
147 mike          1.13 
148                    //  Fill in a caller-provided int with the integer conversion of the value.
149 kumpf         1.33 void Optarg::Value(int& i) const
150 mike          1.13 {
151 kumpf         1.23     CString cs = _value.getCString();
152 mike          1.14     const char* s = cs;
153                        Boolean valid = true;
154                        Uint32 j;
155                        for (j = 0; j < strlen (s); j++)
156                        {
157 david.dillard 1.31         if ((!isdigit (s [j])) && (!isspace (s [j])) && (s [j] != '+') &&
158 mike          1.14             (s [j] != '-'))
159                            {
160                                valid = false;
161                                break;
162                            }
163                        }
164                        if (valid)
165                        {
166 kumpf         1.25         Sint64 i64;
167                            if ( !(sscanf (s, "%" PEGASUS_64BIT_CONVERSION_WIDTH "d", &i64)) ||
168                                 (i64 != Sint64(Sint32(i64))) )
169 mike          1.14         {
170 kumpf         1.24             throw TypeMismatchException ();
171 mike          1.14         }
172 kumpf         1.25 
173                            i = Sint32(i64);
174 mike          1.14     }
175                        else
176                        {
177 kumpf         1.24         throw TypeMismatchException ();
178 mike          1.14     }
179 mike          1.13 }
180                    
181                    //  Fill in a caller-provided unsigned int
182 kumpf         1.33 void Optarg::Value(unsigned int& i) const
183 mike          1.14 {
184 kumpf         1.23     CString cs = _value.getCString();
185 mike          1.14     const char* s = cs;
186                        Boolean valid = true;
187                        Uint32 j;
188                    
189                        for (j = 0; j < strlen (s); j++)
190                        {
191                            if ((!isdigit (s [j])) && (!isspace (s [j])))
192                            {
193                                valid = false;
194                                break;
195                            }
196                        }
197                        if (valid)
198                        {
199 kumpf         1.25         Uint64 i64;
200                            if ( !(sscanf (s, "%" PEGASUS_64BIT_CONVERSION_WIDTH "u", &i64)) ||
201 david.dillard 1.30              (i64 > 0xFFFFFFFF))
202 mike          1.14         {
203 kumpf         1.24             throw TypeMismatchException ();
204 mike          1.14         }
205 kumpf         1.25 
206                            i = Uint32(i64);
207 mike          1.14     }
208                        else
209                        {
210 kumpf         1.33         throw TypeMismatchException();
211 mike          1.14     }
212 mike          1.13 }
213                    
214 kumpf         1.23 //  Fill in a caller-provided long
215 kumpf         1.33 void Optarg::Value(long& l) const
216                    {
217                        l = (long)atoi(_value.getCString());
218 mike          1.13 }
219                    
220 kumpf         1.23 //  Fill in a caller-provided unsigned long
221 kumpf         1.33 void Optarg::Value(unsigned long& l) const
222                    {
223                        l = (unsigned long)atoi(_value.getCString());
224 mike          1.13 }
225                    
226 kumpf         1.23 //  Fill in a caller-provided double
227 kumpf         1.33 void Optarg::Value(double& d) const
228                    {
229                        d = (double)atof(_value.getCString());
230 mike          1.13 }
231                    
232                    //--------------------------------------------------------------------
233                    //  Provide information about the flag, if any
234                    //--------------------------------------------------------------------
235                    
236                    // Is the option value is bound to a flag?
237 kumpf         1.33 Boolean Optarg::isFlag() const
238                    {
239                        return (_opttype == FLAG || _opttype == LONGFLAG);
240                    }
241 mike          1.13 
242                    // Is it bound to a long-named flag?
243 kumpf         1.33 Boolean Optarg::isLongFlag() const
244                    {
245                        return (_opttype == LONGFLAG);
246                    }
247 mike          1.13 
248                    //-----------------------------------------------------------------------
249                    //  print the members as a formatted String
250                    //-----------------------------------------------------------------------
251 kumpf         1.33 ostream& Optarg::print(ostream& os) const
252                    {
253                        os << "{name:(" << getName();
254                        os << ") type:(";
255                        switch (getType())
256                        {
257                        case FLAG: os << "FLAG"; break;
258                        case LONGFLAG: os << "LONGFLAG"; break;
259                        case REGULAR: os << "REGULAR"; break;
260                        }
261                        os << ") value:(" << Value() << ")}";
262                        return os;
263 mike          1.13 }
264                    
265                    
266                    
267                    //---------------------------------------------------------------------
268                    //                   Implementation of class getoopt
269                    //---------------------------------------------------------------------
270                    
271                    // Constructors and destructor
272                    
273                    // Default constructor.  The optional String is in the format of
274 david.dillard 1.31 // a getopt() optstring
275 kumpf         1.33 getoopt::getoopt(const char* optstring)
276 mike          1.13 {
277 kumpf         1.33     if (optstring)
278                        {
279                            addFlagspec(optstring);
280                        }
281 mike          1.13 }
282                    
283 kumpf         1.33 getoopt::~getoopt()
284                    {
285                    }
286 mike          1.13 
287                    //----------------------------------------------------------------------
288                    //  methods to register program-defined flags and their characteristics
289                    //  The flags are encapsulated in the struct flagspec, an array of which
290                    //  is a member of this class.
291                    //  There are also methods to deregister flags. This is done by resetting
292                    //  a flagspec's active flag.
293                    //----------------------------------------------------------------------
294                    
295                    // Parse through a getopt() optstring and create flagspecs from each
296                    // short flag.
297 kumpf         1.33 Boolean getoopt::addFlagspec(const String& opt)
298                    {
299                        unsigned int size = opt.size();
300                        if (size == 0)
301 david.dillard 1.31         return false;
302 kumpf         1.33     for (unsigned int i = 0; i < size; i++)
303                        {
304                            char c = static_cast<char>(opt[i]);
305                            if ( ((i + 1) < size) && (opt[i+1] == ':') )
306                            {
307                                if (!(addFlagspec(c, true)))
308                                {
309                                    return false;
310                                }
311                                ++i;
312                            }
313                            else
314                            {
315                                if (!(addFlagspec(c, false)))
316                                    return false;
317                            }
318 mike          1.13     }
319 kumpf         1.33     return true;
320 mike          1.13 }
321                    
322                    //  Create a filespec from a single short flag and push it onto the array
323 kumpf         1.33 Boolean getoopt::addFlagspec(char flag, Boolean hasarg)
324                    {
325                        if (flag == '*')
326                        {
327                            MessageLoaderParms parms(
328                                "getoopt.getoopt.CANT_NAME_FLAG",
329                                "You can't have a flag named '$0'",
330                                flag);
331                            addError(MessageLoader::getMessage(parms));
332                            return false;
333                        }
334                        flagspec fs;
335                        char c[2];
336                        c[0] = flag;
337                        c[1] = 0;
338                        fs.name = c;
339                        fs.argtype = hasarg ? 1 : 0;
340                        fs.islong = false;
341                        fs.active = true;
342                        _flagspecs.append(fs);
343                        return true;
344 mike          1.13 }
345                    
346                    // Create a flagspec from a single long flag and push it onto the array
347 kumpf         1.33 Boolean getoopt::addLongFlagspec(
348                        const String& name,
349                        argtype type)
350                    {
351                        flagspec fs;
352                    
353                        // Changing "fs.name = name" to the following line masks an ugly crash
354                        // which occurs when compiling with debug option on WIN32:
355                    
356                        fs.name = name;
357                    
358                        fs.argtype = type;
359                        fs.islong = true;
360                        fs.active = true;
361                        _flagspecs.append(fs);
362                        return true;
363 mike          1.13 }
364                    
365                    // Unregister a flagspec
366 kumpf         1.33 Boolean getoopt::removeFlagspec(char opt)
367                    {
368                        flagspec* fs = getFlagspecForUpdate(opt);
369                        if (fs)
370                        {
371                            fs->active = false;
372                            return true;
373                        }
374                        return false;
375 mike          1.13 }
376 mike          1.14 
377                    /**
378                        In the valid option definition string, following an option,
379                        indicates that the preceding option takes a required argument.
380                     */
381                    const char getoopt::GETOPT_ARGUMENT_DESIGNATOR = ':';
382 mike          1.13 
383                    //--------------------------------------------------------------------
384                    //      Routines for parsing the command line
385                    //--------------------------------------------------------------------
386                    
387                    //------------------------
388                    // Static functions
389                    //------------------------
390                    
391                    // Parse out the flagname and the value from a long flag option that
392                    // may be in the form
393                    //          --longflag=value
394 kumpf         1.33 static void partsFromLongOpt(
395                        const String& s,
396                        String& name,
397                        String& value)
398                    {
399                        for (unsigned int i = 0; i < s.size(); i++)
400                        {
401                            if (s[i] == '=')
402                            {
403                                name = s.subString(0, i);
404                                value = s.subString(i+1);
405                                return;
406                            }
407                        }
408                        name = s;
409                        value =  "";
410 mike          1.13 }
411                    
412                    // Create an Optarg instance from a long flag String like
413                    //          --longflag=value
414                    // (The =value is optional).
415 kumpf         1.33 static void optargFromLongOpt(
416                        Optarg& o,
417                        const String& arg)
418                    {
419                        String name;
420                        String value;
421                        partsFromLongOpt(arg, name, value);
422                        o.setName(name);
423                        o.setType(Optarg::LONGFLAG);
424                        o.setValue(value);
425 mike          1.13 }
426                    
427                    // Create an Optarg instance from a short flag String like
428                    //      -fValue
429                    // (The Value part is optional)
430 kumpf         1.33 static void optargFromShortOpt(
431                        Optarg& o,
432                        const char* arg)
433                    {
434                        char name[2];
435                        name[0] = arg[0];
436                        name[1] = 0;
437                        o.setName(name);
438                        o.setType(Optarg::FLAG);
439                        const char* p = arg + 1;
440                        o.setValue(p);
441 mike          1.13 }
442                    
443                    // Look at a command line option and determine whether it is a
444                    // long flag, a short flag or an unflagged option.
445 kumpf         1.33 static int catagorize(const char* s)
446                    {
447                        if (s[0] != '-')
448                            return 0;
449                        else
450                            if (s[1] == '-')
451                                return 2;
452                        return 1;
453 mike          1.13 }
454                    
455                    // Push an Optarg onto our array
456 kumpf         1.33 static void addarg(
457                        getoopt::Arg_List&list,
458                        const Optarg& o)
459                    {
460                        //o.print(cout);
461                        list.append(o);
462 mike          1.13 }
463                    
464                    // Create an Optarg from its members and push it onto the array
465 kumpf         1.33 static void addarg(
466                        getoopt::Arg_List& list,
467                        const String& name,
468                        Optarg::opttype type,
469                        const String& value)
470                    {
471                        Optarg* o = new Optarg(name, type, value);
472                        addarg(list, *o);
473                        delete o;
474 mike          1.13 }
475                    
476                    // Take an array of arguments and append it to another
477 kumpf         1.33 static void copyargs(
478                        getoopt::Arg_List& out,
479                        const getoopt::Arg_List& in)
480                    {
481                        Uint32 size = in.size();
482                        for (Uint32 i = 0; i < size; i++)
483                        {
484                            addarg(out, in[i]);
485                        }
486 mike          1.13 }
487                    
488                    //------------------------------------
489                    // The parse method:  Way too long.
490                    // Note that flag args are pushed
491                    // onto the stack, then the regular
492                    // args are appended, sorting them
493                    // to the rear the way getopt() does.
494                    //------------------------------------
495 kumpf         1.33 Boolean getoopt::parse(
496                        int argc,
497                        char** argv)
498                    {
499                        Optarg o;
500                        int cat;
501                        const flagspec* fs;
502                        Arg_List nonflagargs;
503                        enum states {START, ARGEXPECTED};
504                        states state = START;
505                        for (unsigned int i = 1; i < (unsigned int)argc; i++)
506                        {
507                            unsigned int endsize = static_cast<unsigned int>(strlen(argv[i]));
508                            switch (state)
509                            {
510                            case START:
511                                cat = catagorize(argv[i]);
512                                switch (cat)
513                                {
514                                case 0: // non-flag command line argument
515                                    addarg(nonflagargs, "", Optarg::REGULAR, argv[i]);
516 kumpf         1.33                 break;
517                                case 1: // short (1-character) flag
518                                {
519                                    unsigned int argpos = 1;
520                                    while (argpos < endsize)
521                                    {
522                                        char c = argv[i][argpos];
523                                        fs = getFlagspec(c);  // Short flag
524                                        String temp = argv[i];
525                                        String name = temp.subString(argpos, 1);
526                                        // See if we recognize it
527                                        if (!fs)
528                                        {
529                                            MessageLoaderParms parms(
530                                                "getoopt.getoopt.UNKNOWN_FLAG",
531                                                "Unknown flag $0$1",
532                                                "-",
533                                                name);
534                                            addError(MessageLoader::getMessage(parms));
535                                            argpos++;
536                                        }
537 kumpf         1.33                     else
538                                        {
539                                            // Should this flag be bound?
540                                            if (fs->argtype == NOARG)
541                                            {
542                                                // NO
543                                                addarg(_args, name, Optarg::FLAG,  "");
544                                                argpos++;
545                                            }
546                                            else
547                                            {
548                                                // YES -- the value is here or in the next arg
549                                                optargFromShortOpt(o, &argv[i][argpos]);
550                                                if (o.Value() == "")
551                                                {
552                                                    // No value yet
553                                                    state = ARGEXPECTED;
554                                                }
555                                                else
556                                                {
557                                                    addarg(_args, o);
558 kumpf         1.33                             }
559                                                argpos = endsize;
560                                            }
561                                        }
562                                    }
563                                    break;
564                                } // end subcase 1
565                                case 2:  // long (--xyz) flag
566                                {
567                                    String arg = &(argv[i][2]);
568                                    optargFromLongOpt(o, arg);
569                                    fs = getFlagspec(o.getName());
570                                    // see if we recognize this flag
571                                    if (!fs)
572                                    {
573                                        MessageLoaderParms parms(
574                                            "getoopt.getoopt.UNKNOWN_FLAG",
575                                            "Unknown flag $0$1",
576                                            "",
577                                            o.getName());
578                                        addError(MessageLoader::getMessage(parms));
579 kumpf         1.33                 }
580                                    else
581                                    {
582                                        // this is a long flag we know about
583                                        if (o.optarg() != ""  || fs->argtype != MUSTHAVEARG)
584                                        {
585                                            addarg(_args, o);
586                                            state = START;  // we have a completed long flag
587                                        }
588                                        else
589                                        {
590                                            // no value yet, and we expect one
591                                            if (fs->argtype == MUSTHAVEARG)
592                                            {
593                                                state = ARGEXPECTED;
594                                            }
595                                        }
596                                    }
597                                    break;
598                                } // end subcase 2
599                            } // end switch catagorize()
600 kumpf         1.33         break; // end of case START
601                    
602                            case ARGEXPECTED:
603                                if (argv[i][0] == '-')
604                                {
605                                    MessageLoaderParms parms(
606                                        "getoopt.getoopt.MISSING_VALUE_FOR_FLAG",
607                                        "Missing required value for flag $0",
608                                        o.getopt());
609                                    addError(MessageLoader::getMessage(parms));
610                                    i--;
611                                }
612                                else
613                                {
614                                    o.setValue(argv[i]);
615                                }
616                                addarg(_args, o);
617                                state = START;
618                                break;
619                            } // end switch
620                        } // end for
621 kumpf         1.33     if (state != START)
622                        {
623                            MessageLoaderParms parms(
624                                "getoopt.getoopt.MISSING_VALUE_FOR_FLAG",
625                                "Missing required value for flag $0",
626                                o.getName());
627                            addError(MessageLoader::getMessage(parms));
628                        }
629                        copyargs(_args, nonflagargs);
630                        return !_errorStrings.size();
631 mike          1.13 }
632                    
633                    //----------------------------------------------------------------------
634                    //         Methods to retrieve the command line arguments
635                    //----------------------------------------------------------------------
636                    
637                    //----------------------------------------------
638                    // Access the command line arguments by index
639                    //----------------------------------------------
640                    
641                    // Index operator
642 kumpf         1.33 const Optarg& getoopt::operator[](unsigned int n)
643                    {
644                        unsigned int lim = _args.size();
645                        if (n < lim)
646                            return _args[n];
647                        else
648                            return _emptyopt;
649 mike          1.13 }
650                    
651                    // Return first index
652 kumpf         1.33 unsigned int getoopt::first() const
653                    {
654                        return 0;
655                    }
656 mike          1.13 
657                    // Return one past last index
658 kumpf         1.33 unsigned int getoopt::last() const
659                    {
660                        return _args.size();
661                    }
662 mike          1.13 
663                    //-----------------------------------------------
664                    // Access the command line arguments ad-hoc
665                    //-----------------------------------------------
666                    
667                    // Return the number of times a short flag is set
668                    // on the command line
669 kumpf         1.33 unsigned int getoopt::isSet(char c) const
670                    {
671                        unsigned int cnt = 0;
672                        for (unsigned int i = 0; i < _args.size(); i++)
673                        {
674                            const Optarg& o = _args[i];
675                            if (o.getType() == Optarg::FLAG)
676                            {
677                                const String& s = o.getopt();
678                                if (s[0] == c)
679                                {
680                                    cnt++;
681                                }
682                            }
683 mike          1.13     }
684 kumpf         1.33     return cnt;
685 mike          1.13 }
686                    
687                    // Return the number of times any flag is set
688                    // on the command line
689 kumpf         1.33 unsigned int getoopt::isSet(const String& s) const
690                    {
691                        unsigned int cnt = 0;
692                        for (unsigned int i = 0; i < _args.size(); i++)
693                        {
694                            const Optarg& o = _args[i];
695                            if (o.getopt() == s)
696                            {
697                                cnt++;
698                            }
699 mike          1.13     }
700 kumpf         1.33     return cnt;
701 mike          1.13 }
702                    
703                    // Return the String value of the nth instance of
704                    // a particular short flag on the command line
705 kumpf         1.33 const String& getoopt::value(
706                        char opt,
707                        unsigned int idx) const
708                    {
709                        unsigned int cnt = 0;
710                        for (unsigned int i = 0; i < _args.size(); i++)
711                        {
712                            const Optarg& o = _args[i];
713                            if (o.getType() == Optarg::FLAG)
714                            {
715                                const String& s = o.getopt();
716                                if (s[0] == opt)
717                                {
718                                    if (cnt == idx)
719                                    {
720                                        return o.optarg();
721                                    }
722                                    else
723                                    {
724                                        cnt++;
725                                    }
726 kumpf         1.33             }
727                            }
728 mike          1.13     }
729 kumpf         1.33     return emptystring;
730 mike          1.13 }
731                    
732                    // Return the nth instance of any flag on the command line
733 kumpf         1.33 const String& getoopt::value(
734                        const String& opt,
735                         unsigned int idx) const
736                    {
737                        unsigned int cnt = 0;
738                        for (unsigned int i = 0; i < _args.size(); i++)
739                        {
740                            const Optarg& o = _args[i];
741                            if (o.optarg() == opt)
742                            {
743                                if (cnt == idx)
744                                {
745                                    return o.getopt();
746                                }
747                                else
748                                {
749                                    cnt++;
750                                }
751                            }
752 mike          1.13     }
753 kumpf         1.33     return emptystring;
754 mike          1.13 }
755                    
756                    // Of the command line arguments, how many are flags?
757 kumpf         1.33 unsigned int getoopt::flagcnt() const
758                    {
759                        unsigned int cnt = 0;
760                        for (Uint32 i = 0; i < _args.size(); i++)
761                        {
762                            if (_args[i].getType() != Optarg::REGULAR)
763                                cnt++;
764                        }
765                        return cnt;
766 mike          1.13 }
767                    
768                    // How many command line arguments were there?
769 kumpf         1.33 unsigned int getoopt::size() const
770                    {
771                        return _args.size();
772 mike          1.13 }
773                    
774                    // Return the list of command line arguments for use by
775                    // the program.
776 kumpf         1.33 const getoopt::Arg_List& getoopt::getArgs() const
777                    {
778                        return _args;
779                    }
780 mike          1.13 
781                    
782                    //-----------------------------------------------------------
783                    // Routines dealing with errors during parsing
784                    // FIXME:  This needs to be reworked so that the error text
785                    // is hidden and provided by the caller
786                    //----------------------------------------------------------
787                    
788                    // Add an error into the list
789 kumpf         1.33 void getoopt::addError(const String& s)
790 mike          1.13 {
791 kumpf         1.33     _errorStrings.append(s);
792 mike          1.13 }
793                    
794                    // Return a list of the errors
795 kumpf         1.33 const getoopt::Error_List& getoopt::getErrorStrings() const
796                    {
797                        return _errorStrings;
798 mike          1.13 }
799                    
800                    // Did any errors occur?
801 kumpf         1.33 Boolean getoopt::hasErrors() const
802                    {
803                        return _errorStrings.size() ? true : false;
804 mike          1.13 }
805                    
806                    
807                    
808 kumpf         1.33 flagspec* getoopt::getFlagspecForUpdate(const String& s)
809                    {
810                        for (unsigned int i = 0; i < _flagspecs.size(); i++)
811                        {
812                            flagspec& o = _flagspecs[i];
813                            if (o.islong && s == o.name)
814                                return &_flagspecs[i];
815                        }
816                        return 0;
817 mike          1.13 }
818                    
819 kumpf         1.33 const flagspec* getoopt::getFlagspec(const String& s)
820                    {
821                        return (const flagspec *)getFlagspecForUpdate(s);
822 mike          1.13 }
823                    
824 kumpf         1.33 ostream& getoopt::printErrors(ostream& os) const
825                    {
826                        for (Uint32 i = 0; i < _errorStrings.size(); i++)
827                        {
828                            os << "> " << _errorStrings[i] << endl;
829                        }
830                        return os;
831 mike          1.13 }
832                    
833 kumpf         1.33 void getoopt::printErrors(String& s) const
834                    {
835                        for (Uint32 i = 0; i < _errorStrings.size(); i++)
836                        {
837                            s.append("> " + _errorStrings[i] + "\n");
838                        }
839 mike          1.13 }
840                    
841                    //---------------------------------------------------------------
842                    //              Private methods
843                    //---------------------------------------------------------------
844 kumpf         1.33 flagspec* getoopt::getFlagspecForUpdate(char c)
845                    {
846                        for (unsigned int i = 0; i < _flagspecs.size(); i++)
847                        {
848                            flagspec& o = _flagspecs[i];
849                            if (!o.islong && c == o.name[0])
850                                return &_flagspecs[i];
851                        }
852                        return 0;
853 mike          1.13 }
854                    
855 kumpf         1.33 const flagspec* getoopt::getFlagspec(char c)
856                    {
857                        return (const flagspec *)getFlagspecForUpdate(c);
858 mike          1.13 }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2