VirtualBox

Changeset 5843 in vbox for trunk/src/VBox/Runtime/common


Ignore:
Timestamp:
Nov 26, 2007 6:45:33 PM (17 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
26302
Message:

r=bird: Adjusted the RTGetOpt API a little bit.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/misc/getopt.cpp

    r5766 r5843  
     1/* $Id$ */
    12/** @file
    23 * innotek Portable Runtime - Command Line Parsing
     
    2122#include <iprt/err.h>
    2223#include <iprt/string.h>
     24#include <iprt/assert.h>
    2325
    24 #include <string.h>
    2526
    26 /*******************************************************************************
    27 *   Code                                                                       *
    28 *******************************************************************************/
    29 int RTGetOpt(int argc,
    30              char *argv[],
    31              const RTOPTIONDEF *paOptions,
    32              int cOptions,
    33              int *piThis,
    34              RTOPTIONUNION *pValueUnion)
     27
     28RTDECL(int) RTGetOpt(int argc, char *argv[], PCRTOPTIONDEF paOptions, size_t cOptions, int *piThis, PRTOPTIONUNION pValueUnion)
    3529{
    36     if (    (!piThis)
    37          || (*piThis >= argc)
     30    pValueUnion->pDef = NULL;
     31
     32    if (    !piThis
     33         || *piThis >= argc
    3834       )
    3935        return 0;
    4036
    4137    int iThis = (*piThis)++;
    42     const char *pcszArgThis = argv[iThis];
     38    const char *pszArgThis = argv[iThis];
    4339
    44     if (*pcszArgThis == '-')
     40    if (*pszArgThis == '-')
    4541    {
    46         int i;
    47         for (i = 0;
    48              i < cOptions;
    49              ++i)
     42        for (size_t i = 0; i < cOptions; i++)
    5043        {
    5144            bool fShort = false;
    52             if (    (!strcmp(pcszArgThis, paOptions[i].pcszLong))
    53                  || (    ((fShort = (pcszArgThis[1] == paOptions[i].cShort)))
    54                       && (pcszArgThis[2] == '\0')
     45            if (    (   paOptions[i].pszLong
     46                     && !strcmp(pszArgThis, paOptions[i].pszLong))
     47                ||  (   (fShort = (pszArgThis[1] == paOptions[i].iShort))
     48                     && pszArgThis[2] == '\0'
    5549                    )
    5650               )
    5751            {
    58                 if (paOptions[i].fl & RTGETOPT_REQUIRES_ARGUMENT)
     52                Assert(!(paOptions[i].fFlags & ~RTGETOPT_REQ_MASK));
     53                pValueUnion->pDef = &paOptions[i];
     54
     55                if ((paOptions[i].fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
    5956                {
    6057                    if (iThis >= argc - 1)
     58                        return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
     59
     60                    int iNext = (*piThis)++;
     61                    switch (paOptions[i].fFlags & RTGETOPT_REQ_MASK)
    6162                    {
    62                         pValueUnion->pcsz = paOptions[i].pcszLong;
    63                         return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
    64                     }
    65                     else
    66                     {
    67                         int iNext = (*piThis)++;
    68                         if (paOptions[i].fl & RTGETOPT_ARG_FORMAT_INT32)
     63                        case RTGETOPT_REQ_STRING:
     64                            pValueUnion->psz = argv[iNext];
     65                            break;
     66
     67                        case RTGETOPT_REQ_INT32:
    6968                        {
    70                             int32_t i;
    71                             if (RTStrToInt32Full(argv[iNext], 10, &i))
    72                             {
    73                                 pValueUnion->pcsz = paOptions[i].pcszLong;
     69                            int32_t i32;
     70                            if (RTStrToInt32Full(argv[iNext], 10, &i32))
    7471                                return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
    75                             }
    7672
    77                             pValueUnion->i = i;
     73                            pValueUnion->i32 = i32;
     74                            break;
    7875                        }
    79                         else if (paOptions[i].fl & RTGETOPT_ARG_FORMAT_UINT32)
     76
     77                        case RTGETOPT_REQ_UINT32:
    8078                        {
    81                             uint32_t u;
    82                             if (RTStrToUInt32Full(argv[iNext], 10, &u))
    83                             {
    84                                 pValueUnion->pcsz = paOptions[i].pcszLong;
     79                            uint32_t u32;
     80                            if (RTStrToUInt32Full(argv[iNext], 10, &u32))
    8581                                return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
    86                             }
     82   
     83                            pValueUnion->u32 = u32;
     84                            break;
     85                        }
    8786
    88                             pValueUnion->u = u;
    89                         }
    90                         else
    91                             pValueUnion->pcsz = argv[iNext];
     87                        default:
     88                            AssertMsgFailed(("i=%d f=%#x\n", i, paOptions[i].fFlags));
     89                            return VERR_INTERNAL_ERROR;
    9290                    }
    9391                }
    9492
    95                 return paOptions[i].cShort;
     93                return paOptions[i].iShort;
    9694            }
    9795        }
    9896    }
    9997
     98    /** @todo Sort options and arguments (i.e. stuff that doesn't start with '-'), stop when
     99     * encountering the first argument. */
     100
    100101    return VERR_GETOPT_UNKNOWN_OPTION;
    101102}
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette