Changeset 5843 in vbox for trunk/src/VBox/Runtime/common
- Timestamp:
- Nov 26, 2007 6:45:33 PM (17 years ago)
- svn:sync-xref-src-repo-rev:
- 26302
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/misc/getopt.cpp
r5766 r5843 1 /* $Id$ */ 1 2 /** @file 2 3 * innotek Portable Runtime - Command Line Parsing … … 21 22 #include <iprt/err.h> 22 23 #include <iprt/string.h> 24 #include <iprt/assert.h> 23 25 24 #include <string.h>25 26 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 28 RTDECL(int) RTGetOpt(int argc, char *argv[], PCRTOPTIONDEF paOptions, size_t cOptions, int *piThis, PRTOPTIONUNION pValueUnion) 35 29 { 36 if ( (!piThis) 37 || (*piThis >= argc) 30 pValueUnion->pDef = NULL; 31 32 if ( !piThis 33 || *piThis >= argc 38 34 ) 39 35 return 0; 40 36 41 37 int iThis = (*piThis)++; 42 const char *p cszArgThis = argv[iThis];38 const char *pszArgThis = argv[iThis]; 43 39 44 if (*p cszArgThis == '-')40 if (*pszArgThis == '-') 45 41 { 46 int i; 47 for (i = 0; 48 i < cOptions; 49 ++i) 42 for (size_t i = 0; i < cOptions; i++) 50 43 { 51 44 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' 55 49 ) 56 50 ) 57 51 { 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) 59 56 { 60 57 if (iThis >= argc - 1) 58 return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING; 59 60 int iNext = (*piThis)++; 61 switch (paOptions[i].fFlags & RTGETOPT_REQ_MASK) 61 62 { 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: 69 68 { 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)) 74 71 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; 75 }76 72 77 pValueUnion->i = i; 73 pValueUnion->i32 = i32; 74 break; 78 75 } 79 else if (paOptions[i].fl & RTGETOPT_ARG_FORMAT_UINT32) 76 77 case RTGETOPT_REQ_UINT32: 80 78 { 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)) 85 81 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; 86 } 82 83 pValueUnion->u32 = u32; 84 break; 85 } 87 86 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; 92 90 } 93 91 } 94 92 95 return paOptions[i]. cShort;93 return paOptions[i].iShort; 96 94 } 97 95 } 98 96 } 99 97 98 /** @todo Sort options and arguments (i.e. stuff that doesn't start with '-'), stop when 99 * encountering the first argument. */ 100 100 101 return VERR_GETOPT_UNKNOWN_OPTION; 101 102 }
Note:
See TracChangeset
for help on using the changeset viewer.