儲存庫 vbox 的更動 17093
- 時間撮記:
- 2009-2-24 下午07:58:09 (16 年 以前)
- 位置:
- trunk/src/VBox/Runtime
- 檔案:
-
- 修改 4 筆資料
圖例:
- 未更動
- 新增
- 刪除
-
trunk/src/VBox/Runtime/common/misc/getopt.cpp
r17088 r17093 40 40 41 41 42 RTDECL(int) RTGetOpt(int argc, char **argv, PCRTOPTIONDEF paOptions, size_t cOptions, int *piThis, PRTOPTIONUNION pValueUnion) 42 RTDECL(int) RTGetOptInit(PRTGETOPTSTATE pState, int argc, char **argv, 43 PCRTGETOPTDEF paOptions, size_t cOptions, 44 int iFirst, uint32_t fFlags) 45 { 46 AssertReturn(!fFlags, VERR_INVALID_PARAMETER); 47 48 pState->argv = argv; 49 pState->argc = argc; 50 pState->paOptions = paOptions; 51 pState->cOptions = cOptions; 52 pState->iNext = iFirst; 53 54 return VINF_SUCCESS; 55 } 56 57 58 RTDECL(int) RTGetOpt(PRTGETOPTSTATE pState, PRTGETOPTUNION pValueUnion) 43 59 { 44 60 pValueUnion->u64 = 0; 45 61 pValueUnion->pDef = NULL; 46 62 47 if ( !piThis 48 || *piThis >= argc 49 ) 63 if (pState->iNext >= pState->argc) 50 64 return 0; 51 65 52 int iThis = (*piThis)++; 53 const char *pszArgThis = argv[iThis]; 66 int iThis = pState->iNext++; 67 const char *pszArgThis = pState->argv[iThis]; 68 size_t const cOptions = pState->cOptions; 69 PCRTGETOPTDEF paOptions = pState->paOptions; 54 70 55 71 for (size_t i = 0; i < cOptions; i++) … … 57 73 Assert(!(paOptions[i].fFlags & ~RTGETOPT_VALID_MASK)); 58 74 Assert(paOptions[i].iShort > 0); 75 Assert(paOptions[i].iShort != VINF_GETOPT_NOT_OPTION); 59 76 60 77 bool fShort = *pszArgThis == '-' 61 && (uint32_t)pszArgThis[1] == paOptions[i].iShort;78 && pszArgThis[1] == paOptions[i].iShort; 62 79 63 80 if ((paOptions[i].fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING) … … 91 108 : pszArgThis[cchLong] == '\0' || pszArgThis[cchLong + 1] == '\0') 92 109 { 93 if (iThis + 1 >= argc)110 if (iThis + 1 >= pState->argc) 94 111 return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING; 95 pszValue = argv[iThis + 1];96 (*piThis)++;112 pszValue = pState->argv[iThis + 1]; 113 pState->iNext++; 97 114 } 98 115 else /* same argument. */ … … 196 213 return VERR_GETOPT_UNKNOWN_OPTION; 197 214 215 /** @todo Handle '--' and possibly implement an RTGetOptInit that lets us 216 * optionally sort the stuff and set other policeis sorts the result. */ 217 198 218 /* 199 219 * Not an option. 200 220 */ 201 (*piThis)--; 202 /** @todo Sort options and arguments (i.e. stuff that doesn't start with '-'), stop when 203 * encountering the first argument. */ 204 205 return 0; 221 pValueUnion->psz = pszArgThis; 222 return VINF_GETOPT_NOT_OPTION; 206 223 } 207 224 -
trunk/src/VBox/Runtime/testcase/tstGetOpt.cpp
r8245 r17093 42 42 RTR3Init(); 43 43 44 int i;45 RT OPTIONUNION Val;46 #define CHECK(expr) do { if (!(expr)) { RTPrintf("tstGetOpt: error line %d (i =%d): %s\n", __LINE__, i, #expr); cErrors++; } } while (0)44 RTGETOPTSTATE GetState; 45 RTGETOPTUNION Val; 46 #define CHECK(expr) do { if (!(expr)) { RTPrintf("tstGetOpt: error line %d (iNext=%d): %s\n", __LINE__, GetState.iNext, #expr); cErrors++; } } while (0) 47 47 48 48 #define CHECK_GETOPT(expr, chRet, iInc) \ 49 49 do { \ 50 const int iPrev = i; \50 const int iPrev = GetState.iNext; \ 51 51 CHECK((expr) == (chRet)); \ 52 CHECK( i== (iInc) + iPrev); \53 i= (iInc) + iPrev; \52 CHECK(GetState.iNext == (iInc) + iPrev); \ 53 GetState.iNext = (iInc) + iPrev; \ 54 54 } while (0) 55 55 … … 57 57 * Simple. 58 58 */ 59 static const RT OPTIONDEF s_aOpts2[] =59 static const RTGETOPTDEF s_aOpts2[] = 60 60 { 61 61 { "--optwithstring", 's', RTGETOPT_REQ_STRING }, … … 64 64 { NULL, 'q', RTGETOPT_REQ_NOTHING }, 65 65 { "--quiet", 384, RTGETOPT_REQ_NOTHING }, 66 { "-quiet2", 385, RTGETOPT_REQ_NOTHING }, 66 67 }; 67 68 … … 84 85 "-q", 85 86 "--quiet", 87 "-quiet2", 86 88 /* "filename1", */ 87 89 /* "filename2", */ … … 90 92 int argc2 = (int)RT_ELEMENTS(argv2) - 1; 91 93 92 i = 0; 93 CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 's', 2); 94 CHECK(RT_SUCCESS(RTGetOptInit(&GetState, argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), 0, 0 /* fFlags */))); 95 96 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 's', 2); 94 97 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string1")); 95 CHECK_GETOPT(RTGetOpt( argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 's', 2);98 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 's', 2); 96 99 CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string2")); 97 100 98 101 /* -i */ 99 CHECK_GETOPT(RTGetOpt( argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 2);102 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2); 100 103 CHECK(Val.i32 == -42); 101 CHECK_GETOPT(RTGetOpt( argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 1);104 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1); 102 105 CHECK(Val.i32 == -42); 103 CHECK_GETOPT(RTGetOpt( argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 1);106 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1); 104 107 CHECK(Val.i32 == -42); 105 CHECK_GETOPT(RTGetOpt( argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 2);108 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2); 106 109 CHECK(Val.i32 == -42); 107 CHECK_GETOPT(RTGetOpt( argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 2);110 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2); 108 111 CHECK(Val.i32 == -42); 109 112 110 113 /* --optwithint */ 111 CHECK_GETOPT(RTGetOpt( argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 2);114 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2); 112 115 CHECK(Val.i32 == 42); 113 CHECK_GETOPT(RTGetOpt( argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 1);116 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1); 114 117 CHECK(Val.i32 == 42); 115 CHECK_GETOPT(RTGetOpt( argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 1);118 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1); 116 119 CHECK(Val.i32 == 42); 117 CHECK_GETOPT(RTGetOpt( argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 2);120 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2); 118 121 CHECK(Val.i32 == 42); 119 CHECK_GETOPT(RTGetOpt( argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 2);122 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2); 120 123 CHECK(Val.i32 == 42); 121 124 122 CHECK_GETOPT(RTGetOpt( argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'v', 1);125 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'v', 1); 123 126 CHECK(Val.pDef == &s_aOpts2[2]); 124 CHECK_GETOPT(RTGetOpt( argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'v', 1);127 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'v', 1); 125 128 CHECK(Val.pDef == &s_aOpts2[2]); 126 CHECK_GETOPT(RTGetOpt( argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'q', 1);129 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'q', 1); 127 130 CHECK(Val.pDef == &s_aOpts2[3]); 128 CHECK_GETOPT(RTGetOpt( argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 384, 1);131 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 384, 1); 129 132 CHECK(Val.pDef == &s_aOpts2[4]); 130 CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 0, 0); 133 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 385, 1); 134 CHECK(Val.pDef == &s_aOpts2[5]); 135 CHECK_GETOPT(RTGetOpt(&GetState, &Val), 0, 0); 131 136 CHECK(Val.pDef == NULL); 132 CHECK(argc2 == i);137 CHECK(argc2 == GetState.iNext); 133 138 134 139 -
trunk/src/VBox/Runtime/testcase/tstHandleTable.cpp
r11822 r17093 455 455 RTR3Init(); 456 456 457 static RT OPTIONDEF const s_aOptions[] =457 static RTGETOPTDEF const s_aOptions[] = 458 458 { 459 459 { "--base", 'b', RTGETOPT_REQ_UINT32 }, … … 469 469 470 470 int ch; 471 int iArg = 1; 472 RTOPTIONUNION Value; 473 while ((ch = RTGetOpt(argc,argv, &s_aOptions[0], RT_ELEMENTS(s_aOptions), &iArg, &Value))) 471 RTGETOPTUNION Value; 472 RTGETOPTSTATE GetState; 473 RTGetOptInit(&GetState, argc, argv, &s_aOptions[0], RT_ELEMENTS(s_aOptions), 1, 0 /* fFlags */); 474 while ((ch = RTGetOpt(&GetState, &Value))) 474 475 switch (ch) 475 476 { … … 492 493 RTPrintf("syntax: tstHandleTable [-b <base>] [-m <max>] [-t <threads>]\n"); 493 494 return 1; 495 496 case VINF_GETOPT_NOT_OPTION: 497 RTPrintf("tstHandleTable: unexpected non-option: %s\n", Value.psz); 498 break; 494 499 495 500 default: … … 500 505 return 1; 501 506 } 502 if (iArg < argc)503 {504 RTPrintf("tstHandleTable: invalid argument: %s\n", argv[iArg]);505 return 1;506 }507 507 508 508 /* -
trunk/src/VBox/Runtime/testcase/tstTSC.cpp
r12548 r17093 416 416 bool fCalcFrequency = false; 417 417 uint32_t cMsDuration = 1000; /* 1 sec */ 418 static const RT OPTIONDEF s_aOptions[] =418 static const RTGETOPTDEF s_aOptions[] = 419 419 { 420 420 { "--duration", 'd', RTGETOPT_REQ_UINT32 }, … … 424 424 int iArg = 1; 425 425 int ch; 426 RTOPTIONUNION Value; 427 while ((ch = RTGetOpt(argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), &iArg, &Value))) 426 RTGETOPTUNION Value; 427 RTGETOPTSTATE GetState; 428 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0 /* fFlags */); 429 while ((ch = RTGetOpt(&GetState, &Value))) 428 430 switch (ch) 429 431 { … … 434 436 " or: tstTSC <-f|--calc-frequency> [--duration|-d ms]\n"); 435 437 return 1; 438 439 case VINF_GETOPT_NOT_OPTION: 440 RTStrmPrintf(g_pStdErr, "tstTSC: too many arguments\n"); 441 break; 442 436 443 default: 437 444 RTStrmPrintf(g_pStdErr, "tstTSC: Unknown arg or error (ch=%d)\n", ch); 438 445 return 1; 439 446 } 440 if (iArg != argc)441 {442 RTStrmPrintf(g_pStdErr, "tstTSC: too many arguments\n");443 return 1;444 }445 447 446 448 if (fCalcFrequency)
注意:
瀏覽 TracChangeset
來幫助您使用更動檢視器