VirtualBox

Changeset 5843 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Nov 26, 2007 6:45:33 PM (17 years ago)
Author:
vboxsync
Message:

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

Location:
trunk/src/VBox/Runtime
Files:
3 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/Makefile.kmk

    r5760 r5843  
    208208        r3/tcp.cpp
    209209
    210 #if1of ($(BUILD_TARGET_ARCH),amd64 x86)
    211 # RuntimeR3_SOURCES += common/time/timesupA.asm
    212 #else
     210if1of ($(BUILD_TARGET_ARCH),amd64 x86)
     211 RuntimeR3_SOURCES += common/time/timesupA.asm
     212else
    213213 RuntimeR3_SOURCES += common/time/timesupref.cpp
    214 #endif
     214endif
    215215
    216216ifdef IPRT_WITH_KSTUFF
     
    666666        VBox/strformat-vbox.cpp
    667667
    668 #if1of ($(BUILD_TARGET_ARCH),amd64 x86)
    669 # RuntimeR0_SOURCES += common/time/timesupA.asm
    670 #else
     668if1of ($(BUILD_TARGET_ARCH),amd64 x86)
     669 RuntimeR0_SOURCES += common/time/timesupA.asm
     670else
    671671 RuntimeR0_SOURCES += common/time/timesupref.cpp
    672 #endif
     672endif
    673673
    674674RuntimeR0_SOURCES.win.amd64 = $(RuntimeWin64ASM_SOURCES)
     
    997997        VBox/strformat-vbox.cpp \
    998998
    999 #if1of ($(BUILD_TARGET_ARCH),amd64 x86)
    1000 # RuntimeGC_SOURCES += common/time/timesupA.asm
    1001 #else
     999if1of ($(BUILD_TARGET_ARCH),amd64 x86)
     1000 RuntimeGC_SOURCES += common/time/timesupA.asm
     1001else
    10021002 RuntimeGC_SOURCES += common/time/timesupref.cpp
    1003 #endif
     1003endif
    10041004
    10051005RuntimeGC_SOURCES.win.x86 = $(RuntimeWin32ASM_SOURCES)
  • 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}
  • trunk/src/VBox/Runtime/testcase/Makefile.kmk

    r4546 r5843  
    3030        tstFile \
    3131        tstFileLock \
     32        tstGetOpt \
    3233        tstHeapSimple \
    3334        tstInlineAsm \
     
    234235tstErrUnique.cpp_DEPS = $(PATH_TARGET)/../errmsgdata.h
    235236
     237tstGetOpt_SOURCES = tstGetOpt.cpp
     238
    236239tstHeapSimple_SOURCES = tstHeapSimple.cpp
    237240
  • trunk/src/VBox/Runtime/testcase/tstGetOpt.cpp

    r5832 r5843  
    11/* $Id$ */
    22/** @file
    3  * innotek Portable Runtime Testcase - Native Loader.
     3 * innotek Portable Runtime Testcase - RTGetOpt
    44 */
    55
    66/*
    7  * Copyright (C) 2006-2007 innotek GmbH
     7 * Copyright (C) 2007 innotek GmbH
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    1717
    1818
    19 #include <iprt/ldr.h>
     19#include <iprt/getopt.h>
    2020#include <iprt/stream.h>
    21 #include <iprt/runtime.h>
     21#include <iprt/initterm.h>
     22#include <iprt/string.h>
    2223#include <iprt/err.h>
    2324
    24 int main(int argc, const char * const *argv)
     25
     26int main()
    2527{
    26     int rcRet = 0;
     28    int cErrors = 0;
    2729    RTR3Init();
    2830
    29     /*
    30      * If no args, display usage.
    31      */
    32     if (argc <= 1)
    33     {
    34         RTPrintf("Syntax: %s [so/dll [so/dll [..]]\n", argv[0]);
    35         return 1;
    36     }
     31    int i;
     32    RTOPTIONUNION Val;
     33#define CHECK(expr)  do { if (!(expr)) { RTPrintf("tstGetOpt: error line %d (i=%d): %s\n", __LINE__, i, #expr); cErrors++; } } while (0)
     34
     35#define CHECK_GETOPT(expr, chRet, iNext) \
     36    do { \
     37        CHECK((expr) == (chRet)); \
     38        CHECK(i == (iNext)); \
     39        i = (iNext); \
     40    } while (0)
    3741
    3842    /*
    39      * Iterate the arguments and treat all of them as so/dll paths.
     43     * Simple.
    4044     */
    41     for (int i = 1; i < argc; i++)
     45    static const RTOPTIONDEF s_aOpts2[] =
    4246    {
    43         RTLDRMOD hLdrMod = (RTLDRMOD)0xbaadffaa;
    44         int rc = RTLdrLoad(argv[i], &hLdrMod);
    45         if (RT_SUCCESS(rc))
    46         {
    47             RTPrintf("tstLdrLoad: %d - %s\n", i, argv[i]);
    48             rc = RTLdrClose(hLdrMod);
    49             if (RT_FAILURE(rc))
    50             {
    51                 RTPrintf("tstLdrLoad: rc=%Rrc RTLdrClose()\n", rc);
    52                 rcRet++;
    53             }
    54         }
    55         else
    56         {
    57             RTPrintf("tstLdrLoad: rc=%Rrc RTLdrOpen('%s')\n", rc, argv[i]);
    58             rcRet++;
    59         }
    60     }
     47        { "--optwithstring",    's', RTGETOPT_REQ_STRING },
     48        { "--optwithint",       'i', RTGETOPT_REQ_INT32 },
     49        { "--verbose",          'v', RTGETOPT_REQ_NOTHING },
     50        { NULL,                 'q', RTGETOPT_REQ_NOTHING },
     51        { "--quiet",            384, RTGETOPT_REQ_NOTHING },
     52    };
     53
     54    char *argv2[] =
     55    {
     56        "-s",               "string1",
     57        "--optwithstring",  "string2",
     58        "-i",               "-42",
     59        "--optwithint",     "42",
     60        "-v",
     61        "--verbose",
     62        "-q",
     63        "--quiet",
     64        /* "filename1", */
     65        /* "filename2", */
     66        NULL
     67    };
     68    int argc2 = (int)RT_ELEMENTS(argv2) - 1;
     69
     70    i = 0;
     71    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 's', 2);
     72    CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string1"));
     73    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 's', 4);
     74    CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string2"));
     75    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 6);
     76    CHECK(Val.i32 == -42);
     77    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 8);
     78    CHECK(Val.i32 == 42);
     79    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'v', 9);
     80    CHECK(Val.pDef == &s_aOpts2[2]);
     81    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'v', 10);
     82    CHECK(Val.pDef == &s_aOpts2[2]);
     83    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'q', 11);
     84    CHECK(Val.pDef == &s_aOpts2[3]);
     85    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 384, 12);
     86    CHECK(Val.pDef == &s_aOpts2[4]);
     87    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 0, 12);
     88    CHECK(Val.pDef == NULL);
     89    CHECK(argc2 == i);
     90
    6191
    6292    /*
    6393     * Summary.
    6494     */
    65     if (!rcRet)
    66         RTPrintf("tstLdrLoad: SUCCESS\n");
     95    if (!cErrors)
     96        RTPrintf("tstGetOpt: SUCCESS\n");
    6797    else
    68         RTPrintf("tstLdrLoad: FAILURE - %d errors\n", rcRet);
     98        RTPrintf("tstGetOpt: FAILURE - %d errors\n", cErrors);
    6999
    70     return !!rcRet;
     100    return !!cErrors;
    71101}
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