VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/misc/getopt.cpp@ 8147

最後變更 在這個檔案從8147是 8147,由 vboxsync 提交於 17 年 前

Extended RTGetOpt:

o Support 8-, 16- and 64-bit types too.
o Added flags to force hexadecimal, decimal or octal value baseing.
o When no hex/dec/oct flag is present, default to decimal but recognize the hex prefix (0x).
o Support the usual option value separators ([:= ]) and not just space.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 10.0 KB
 
1/* $Id: getopt.cpp 8147 2008-04-18 13:49:01Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Command Line Parsing
4 */
5
6/*
7 * Copyright (C) 2007 innotek GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#include <iprt/getopt.h>
31#include <iprt/err.h>
32#include <iprt/string.h>
33#include <iprt/assert.h>
34#include <iprt/ctype.h>
35
36
37
38RTDECL(int) RTGetOpt(int argc, char **argv, PCRTOPTIONDEF paOptions, size_t cOptions, int *piThis, PRTOPTIONUNION pValueUnion)
39{
40 pValueUnion->u64 = 0;
41 pValueUnion->pDef = NULL;
42
43 if ( !piThis
44 || *piThis >= argc
45 )
46 return 0;
47
48 int iThis = (*piThis)++;
49 const char *pszArgThis = argv[iThis];
50
51 if (*pszArgThis == '-')
52 {
53 for (size_t i = 0; i < cOptions; i++)
54 {
55 Assert(!(paOptions[i].fFlags & ~RTGETOPT_VALID_MASK));
56
57 if ((paOptions[i].fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
58 {
59 /*
60 * A value is required with the argument. We're trying to very
61 * understanding here and will permit any of the following:
62 * -svalue, -s:value, -s=value,
63 * -s value, -s: value, -s= value
64 * (Ditto for long options.)
65 */
66 bool fShort = false;
67 size_t cchLong = 2;
68 if ( ( paOptions[i].pszLong
69 && !strncmp(pszArgThis, paOptions[i].pszLong, (cchLong = strlen(paOptions[i].pszLong)))
70 && ( pszArgThis[cchLong] == '\0'
71 || pszArgThis[cchLong] == ':'
72 || pszArgThis[cchLong] == '=')
73 )
74 || (fShort = (pszArgThis[1] == paOptions[i].iShort))
75 )
76 {
77 pValueUnion->pDef = &paOptions[i]; /* in case of error. */
78
79 /*
80 * Find the argument value
81 */
82 const char *pszValue;
83 if ( fShort
84 ? pszArgThis[2] == '\0'
85 || ((pszArgThis[2] == ':' || pszArgThis[2] == '=') && pszArgThis[3] == '\0')
86 : pszArgThis[cchLong] == '\0' || pszArgThis[cchLong + 1] == '\0')
87 {
88 if (iThis + 1 >= argc)
89 return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
90 pszValue = argv[iThis + 1];
91 (*piThis)++;
92 }
93 else /* same argument. */
94 pszValue = fShort
95 ? &pszArgThis[2 + (pszArgThis[2] == ':' || pszArgThis[2] == '=')]
96 : &pszArgThis[cchLong + 1];
97
98 /*
99 * Transform into a option value as requested.
100 * If decimal conversion fails, we'll check for "0x<xdigit>" and
101 * try a 16 based conversion. We will not interpret any of the
102 * generic ints as octals.
103 */
104 switch (paOptions[i].fFlags & (RTGETOPT_REQ_MASK | RTGETOPT_FLAG_HEX | RTGETOPT_FLAG_OCT | RTGETOPT_FLAG_DEC))
105 {
106 case RTGETOPT_REQ_STRING:
107 pValueUnion->psz = pszValue;
108 break;
109
110#define MY_INT_CASE(req,type,memb,convfn) \
111 case req: \
112 { \
113 type Value; \
114 if ( convfn(pszValue, 10, &Value) != VINF_SUCCESS \
115 && ( pszValue[0] != '0' \
116 || (pszValue[1] != 'x' && pszValue[1] != 'X') \
117 || !RT_C_IS_XDIGIT(pszValue[2]) \
118 || convfn(pszValue, 16, &Value) != VINF_SUCCESS ) ) \
119 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; \
120 pValueUnion->memb = Value; \
121 break; \
122 }
123#define MY_BASE_INT_CASE(req,type,memb,convfn,base) \
124 case req: \
125 { \
126 type Value; \
127 if (convfn(pszValue, base, &Value) != VINF_SUCCESS) \
128 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; \
129 pValueUnion->memb = Value; \
130 break; \
131 }
132
133 MY_INT_CASE(RTGETOPT_REQ_INT8, int8_t, i, RTStrToInt8Full)
134 MY_INT_CASE(RTGETOPT_REQ_INT16, int16_t, i, RTStrToInt16Full)
135 MY_INT_CASE(RTGETOPT_REQ_INT32, int32_t, i, RTStrToInt32Full)
136 MY_INT_CASE(RTGETOPT_REQ_INT64, int64_t, i, RTStrToInt64Full)
137 MY_INT_CASE(RTGETOPT_REQ_UINT8, uint8_t, u, RTStrToUInt8Full)
138 MY_INT_CASE(RTGETOPT_REQ_UINT16, uint16_t, u, RTStrToUInt16Full)
139 MY_INT_CASE(RTGETOPT_REQ_UINT32, uint32_t, u, RTStrToUInt32Full)
140 MY_INT_CASE(RTGETOPT_REQ_UINT64, uint64_t, u, RTStrToUInt64Full)
141
142 MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_HEX, int8_t, i, RTStrToInt8Full, 16)
143 MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_HEX, int16_t, i, RTStrToInt16Full, 16)
144 MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_HEX, int32_t, i, RTStrToInt32Full, 16)
145 MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_HEX, int64_t, i, RTStrToInt64Full, 16)
146 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_HEX, uint8_t, u, RTStrToUInt8Full, 16)
147 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_HEX, uint16_t, u, RTStrToUInt16Full, 16)
148 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_HEX, uint32_t, u, RTStrToUInt32Full, 16)
149 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_HEX, uint64_t, u, RTStrToUInt64Full, 16)
150
151 MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_DEC, int8_t, i, RTStrToInt8Full, 10)
152 MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_DEC, int16_t, i, RTStrToInt16Full, 10)
153 MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_DEC, int32_t, i, RTStrToInt32Full, 10)
154 MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_DEC, int64_t, i, RTStrToInt64Full, 10)
155 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_DEC, uint8_t, u, RTStrToUInt8Full, 10)
156 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_DEC, uint16_t, u, RTStrToUInt16Full, 10)
157 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_DEC, uint32_t, u, RTStrToUInt32Full, 10)
158 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_DEC, uint64_t, u, RTStrToUInt64Full, 10)
159
160 MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_OCT, int8_t, i, RTStrToInt8Full, 8)
161 MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_OCT, int16_t, i, RTStrToInt16Full, 8)
162 MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_OCT, int32_t, i, RTStrToInt32Full, 8)
163 MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_OCT, int64_t, i, RTStrToInt64Full, 8)
164 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_OCT, uint8_t, u, RTStrToUInt8Full, 8)
165 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_OCT, uint16_t, u, RTStrToUInt16Full, 8)
166 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT, uint32_t, u, RTStrToUInt32Full, 8)
167 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_OCT, uint64_t, u, RTStrToUInt64Full, 8)
168#undef MY_INT_CASE
169#undef MY_BASE_INT_CASE
170
171 default:
172 AssertMsgFailed(("i=%d f=%#x\n", i, paOptions[i].fFlags));
173 return VERR_INTERNAL_ERROR;
174 }
175 return paOptions[i].iShort;
176 }
177 }
178 else if ( ( paOptions[i].pszLong
179 && !strcmp(pszArgThis, paOptions[i].pszLong))
180 || ( pszArgThis[1] == paOptions[i].iShort
181 && pszArgThis[2] == '\0') /** @todo implement support for ls -lsR like stuff? */
182 )
183 {
184 pValueUnion->pDef = &paOptions[i];
185 return paOptions[i].iShort;
186 }
187 }
188 }
189
190 /** @todo Sort options and arguments (i.e. stuff that doesn't start with '-'), stop when
191 * encountering the first argument. */
192
193 return VERR_GETOPT_UNKNOWN_OPTION;
194}
195
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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