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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2