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

  1 martin 1.34 //%LICENSE////////////////////////////////////////////////////////////////
  2             // 
  3             // 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             // 
 10             // 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             // 
 17             // The above copyright notice and this permission notice shall be included
 18             // in all copies or substantial portions of the Software.
 19             // 
 20             // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 21             // 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 karl   1.32 // 
 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                        enum states {START, ARGEXPECTED};
502                        states state = START;
503                        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 kumpf         1.33                 break;
515                                case 1: // short (1-character) flag
516                                {
517                                    unsigned int argpos = 1;
518                                    while (argpos < endsize)
519                                    {
520                                        char c = argv[i][argpos];
521                                        fs = getFlagspec(c);  // Short flag
522                                        String temp = argv[i];
523                                        String name = temp.subString(argpos, 1);
524                                        // See if we recognize it
525                                        if (!fs)
526                                        {
527                                            MessageLoaderParms parms(
528                                                "getoopt.getoopt.UNKNOWN_FLAG",
529                                                "Unknown flag $0$1",
530                                                "-",
531                                                name);
532                                            addError(MessageLoader::getMessage(parms));
533                                            argpos++;
534                                        }
535 kumpf         1.33                     else
536                                        {
537                                            // Should this flag be bound?
538                                            if (fs->argtype == NOARG)
539                                            {
540                                                // NO
541                                                addarg(_args, name, Optarg::FLAG,  "");
542                                                argpos++;
543                                            }
544                                            else
545                                            {
546                                                // YES -- the value is here or in the next arg
547                                                optargFromShortOpt(o, &argv[i][argpos]);
548                                                if (o.Value() == "")
549                                                {
550                                                    // No value yet
551                                                    state = ARGEXPECTED;
552                                                }
553                                                else
554                                                {
555                                                    addarg(_args, o);
556 kumpf         1.33                             }
557                                                argpos = endsize;
558                                            }
559                                        }
560                                    }
561                                    break;
562                                } // end subcase 1
563                                case 2:  // long (--xyz) flag
564                                {
565                                    String arg = &(argv[i][2]);
566                                    optargFromLongOpt(o, arg);
567                                    fs = getFlagspec(o.getName());
568                                    // see if we recognize this flag
569                                    if (!fs)
570                                    {
571                                        MessageLoaderParms parms(
572                                            "getoopt.getoopt.UNKNOWN_FLAG",
573                                            "Unknown flag $0$1",
574                                            "",
575                                            o.getName());
576                                        addError(MessageLoader::getMessage(parms));
577 kumpf         1.33                 }
578                                    else
579                                    {
580                                        // this is a long flag we know about
581                                        if (o.optarg() != ""  || fs->argtype != MUSTHAVEARG)
582                                        {
583                                            addarg(_args, o);
584                                            state = START;  // we have a completed long flag
585                                        }
586                                        else
587                                        {
588                                            // no value yet, and we expect one
589                                            if (fs->argtype == MUSTHAVEARG)
590                                            {
591                                                state = ARGEXPECTED;
592                                            }
593                                        }
594                                    }
595                                    break;
596                                } // end subcase 2
597                            } // end switch catagorize()
598 kumpf         1.33         break; // end of case START
599                    
600                            case ARGEXPECTED:
601                                if (argv[i][0] == '-')
602                                {
603                                    MessageLoaderParms parms(
604                                        "getoopt.getoopt.MISSING_VALUE_FOR_FLAG",
605                                        "Missing required value for flag $0",
606                                        o.getopt());
607                                    addError(MessageLoader::getMessage(parms));
608                                    i--;
609                                }
610                                else
611                                {
612                                    o.setValue(argv[i]);
613                                }
614                                addarg(_args, o);
615                                state = START;
616                                break;
617                            } // end switch
618                        } // end for
619 kumpf         1.33     if (state != START)
620                        {
621                            MessageLoaderParms parms(
622                                "getoopt.getoopt.MISSING_VALUE_FOR_FLAG",
623                                "Missing required value for flag $0",
624                                o.getName());
625                            addError(MessageLoader::getMessage(parms));
626                        }
627                        copyargs(_args, nonflagargs);
628                        return !_errorStrings.size();
629 mike          1.13 }
630                    
631                    //----------------------------------------------------------------------
632                    //         Methods to retrieve the command line arguments
633                    //----------------------------------------------------------------------
634                    
635                    //----------------------------------------------
636                    // Access the command line arguments by index
637                    //----------------------------------------------
638                    
639                    // Index operator
640 kumpf         1.33 const Optarg& getoopt::operator[](unsigned int n)
641                    {
642                        unsigned int lim = _args.size();
643                        if (n < lim)
644                            return _args[n];
645                        else
646                            return _emptyopt;
647 mike          1.13 }
648                    
649                    // Return first index
650 kumpf         1.33 unsigned int getoopt::first() const
651                    {
652                        return 0;
653                    }
654 mike          1.13 
655                    // Return one past last index
656 kumpf         1.33 unsigned int getoopt::last() const
657                    {
658                        return _args.size();
659                    }
660 mike          1.13 
661                    //-----------------------------------------------
662                    // Access the command line arguments ad-hoc
663                    //-----------------------------------------------
664                    
665                    // Return the number of times a short flag is set
666                    // on the command line
667 kumpf         1.33 unsigned int getoopt::isSet(char c) const
668                    {
669                        unsigned int cnt = 0;
670                        for (unsigned int i = 0; i < _args.size(); i++)
671                        {
672                            const Optarg& o = _args[i];
673                            if (o.getType() == Optarg::FLAG)
674                            {
675                                const String& s = o.getopt();
676                                if (s[0] == c)
677                                {
678                                    cnt++;
679                                }
680                            }
681 mike          1.13     }
682 kumpf         1.33     return cnt;
683 mike          1.13 }
684                    
685                    // Return the number of times any flag is set
686                    // on the command line
687 kumpf         1.33 unsigned int getoopt::isSet(const String& s) const
688                    {
689                        unsigned int cnt = 0;
690                        for (unsigned int i = 0; i < _args.size(); i++)
691                        {
692                            const Optarg& o = _args[i];
693                            if (o.getopt() == s)
694                            {
695                                cnt++;
696                            }
697 mike          1.13     }
698 kumpf         1.33     return cnt;
699 mike          1.13 }
700                    
701                    // Return the String value of the nth instance of
702                    // a particular short flag on the command line
703 kumpf         1.33 const String& getoopt::value(
704                        char opt,
705                        unsigned int idx) const
706                    {
707                        unsigned int cnt = 0;
708                        for (unsigned int i = 0; i < _args.size(); i++)
709                        {
710                            const Optarg& o = _args[i];
711                            if (o.getType() == Optarg::FLAG)
712                            {
713                                const String& s = o.getopt();
714                                if (s[0] == opt)
715                                {
716                                    if (cnt == idx)
717                                    {
718                                        return o.optarg();
719                                    }
720                                    else
721                                    {
722                                        cnt++;
723                                    }
724 kumpf         1.33             }
725                            }
726 mike          1.13     }
727 kumpf         1.33     return emptystring;
728 mike          1.13 }
729                    
730                    // Return the nth instance of any flag on the command line
731 kumpf         1.33 const String& getoopt::value(
732                        const String& opt,
733                         unsigned int idx) const
734                    {
735                        unsigned int cnt = 0;
736                        for (unsigned int i = 0; i < _args.size(); i++)
737                        {
738                            const Optarg& o = _args[i];
739                            if (o.optarg() == opt)
740                            {
741                                if (cnt == idx)
742                                {
743                                    return o.getopt();
744                                }
745                                else
746                                {
747                                    cnt++;
748                                }
749                            }
750 mike          1.13     }
751 kumpf         1.33     return emptystring;
752 mike          1.13 }
753                    
754                    // Of the command line arguments, how many are flags?
755 kumpf         1.33 unsigned int getoopt::flagcnt() const
756                    {
757                        unsigned int cnt = 0;
758                        for (Uint32 i = 0; i < _args.size(); i++)
759                        {
760                            if (_args[i].getType() != Optarg::REGULAR)
761                                cnt++;
762                        }
763                        return cnt;
764 mike          1.13 }
765                    
766                    // How many command line arguments were there?
767 kumpf         1.33 unsigned int getoopt::size() const
768                    {
769                        return _args.size();
770 mike          1.13 }
771                    
772                    // Return the list of command line arguments for use by
773                    // the program.
774 kumpf         1.33 const getoopt::Arg_List& getoopt::getArgs() const
775                    {
776                        return _args;
777                    }
778 mike          1.13 
779                    
780                    //-----------------------------------------------------------
781                    // Routines dealing with errors during parsing
782                    // FIXME:  This needs to be reworked so that the error text
783                    // is hidden and provided by the caller
784                    //----------------------------------------------------------
785                    
786                    // Add an error into the list
787 kumpf         1.33 void getoopt::addError(const String& s)
788 mike          1.13 {
789 kumpf         1.33     _errorStrings.append(s);
790 mike          1.13 }
791                    
792                    // Return a list of the errors
793 kumpf         1.33 const getoopt::Error_List& getoopt::getErrorStrings() const
794                    {
795                        return _errorStrings;
796 mike          1.13 }
797                    
798                    // Did any errors occur?
799 kumpf         1.33 Boolean getoopt::hasErrors() const
800                    {
801                        return _errorStrings.size() ? true : false;
802 mike          1.13 }
803                    
804                    
805                    
806 kumpf         1.33 flagspec* getoopt::getFlagspecForUpdate(const String& s)
807                    {
808                        for (unsigned int i = 0; i < _flagspecs.size(); i++)
809                        {
810                            flagspec& o = _flagspecs[i];
811                            if (o.islong && s == o.name)
812                                return &_flagspecs[i];
813                        }
814                        return 0;
815 mike          1.13 }
816                    
817 kumpf         1.33 const flagspec* getoopt::getFlagspec(const String& s)
818                    {
819                        return (const flagspec *)getFlagspecForUpdate(s);
820 mike          1.13 }
821                    
822 kumpf         1.33 ostream& getoopt::printErrors(ostream& os) const
823                    {
824                        for (Uint32 i = 0; i < _errorStrings.size(); i++)
825                        {
826                            os << "> " << _errorStrings[i] << endl;
827                        }
828                        return os;
829 mike          1.13 }
830                    
831 kumpf         1.33 void getoopt::printErrors(String& s) const
832                    {
833                        for (Uint32 i = 0; i < _errorStrings.size(); i++)
834                        {
835                            s.append("> " + _errorStrings[i] + "\n");
836                        }
837 mike          1.13 }
838                    
839                    //---------------------------------------------------------------
840                    //              Private methods
841                    //---------------------------------------------------------------
842 kumpf         1.33 flagspec* getoopt::getFlagspecForUpdate(char c)
843                    {
844                        for (unsigned int i = 0; i < _flagspecs.size(); i++)
845                        {
846                            flagspec& o = _flagspecs[i];
847                            if (!o.islong && c == o.name[0])
848                                return &_flagspecs[i];
849                        }
850                        return 0;
851 mike          1.13 }
852                    
853 kumpf         1.33 const flagspec* getoopt::getFlagspec(char c)
854                    {
855                        return (const flagspec *)getFlagspecForUpdate(c);
856 mike          1.13 }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2