VirtualBox

Changeset 8147 in vbox for trunk/include


Ignore:
Timestamp:
Apr 18, 2008 1:49:01 PM (17 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
29853
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/getopt.h

    r6000 r8147  
    3838 */
    3939
    40 /** @name RTOPTIONDEF::f
     40/** @name RTOPTIONDEF::fFlags
     41 *
     42 * @remarks When neither of the RTGETOPT_FLAG_HEX, RTGETOPT_FLAG_OCT and RTGETOPT_FLAG_DEC
     43 *          flags are specified with a integer value format, RTGetOpt will default to
     44 *          decimal but recognize the 0x prefix when present. RTGetOpt will not look for
     45 *          for the octal prefix (0).
    4146 * @{ */
    42 
    43 /** Requires no extra argument.
     47/** Requires no extra argument.
    4448 * (Can be assumed to be 0 for ever.) */
    4549#define RTGETOPT_REQ_NOTHING                    0
    4650/** A value is required or error will be returned. */
    4751#define RTGETOPT_REQ_STRING                     1
     52/** The value must be a valid signed 8-bit integer or an error will be returned. */
     53#define RTGETOPT_REQ_INT8                       2
     54/** The value must be a valid unsigned 8-bit integer or an error will be returned. */
     55#define RTGETOPT_REQ_UINT8                      3
     56/** The value must be a valid signed 16-bit integer or an error will be returned. */
     57#define RTGETOPT_REQ_INT16                      4
     58/** The value must be a valid unsigned 16-bit integer or an error will be returned. */
     59#define RTGETOPT_REQ_UINT16                     5
    4860/** The value must be a valid signed 32-bit integer or an error will be returned. */
    49 #define RTGETOPT_REQ_INT32                      2
    50 /** The value must be a valid signed 32-bit integer or an error will be returned. */
    51 #define RTGETOPT_REQ_UINT32                     3
     61#define RTGETOPT_REQ_INT32                      6
     62/** The value must be a valid unsigned 32-bit integer or an error will be returned. */
     63#define RTGETOPT_REQ_UINT32                     7
     64/** The value must be a valid signed 64-bit integer or an error will be returned. */
     65#define RTGETOPT_REQ_INT64                      8
     66/** The value must be a valid unsigned 64-bit integer or an error will be returned. */
     67#define RTGETOPT_REQ_UINT64                     9
    5268/** The mask of the valid required types. */
    53 #define RTGETOPT_REQ_MASK                       3
     69#define RTGETOPT_REQ_MASK                       15
     70/** Treat the value as hexadecimal - only applicable with the RTGETOPT_REQ_*INT*. */
     71#define RTGETOPT_FLAG_HEX                       RT_BIT(16)
     72/** Treat the value as octal - only applicable with the RTGETOPT_REQ_*INT*. */
     73#define RTGETOPT_FLAG_OCT                       RT_BIT(17)
     74/** Treat the value as decimal - only applicable with the RTGETOPT_REQ_*INT*. */
     75#define RTGETOPT_FLAG_DEC                       RT_BIT(18)
     76/** Mask of valid bits - for validation. */
     77#define RTGETOPT_VALID_MASK                     ( RTGETOPT_REQ_MASK | RTGETOPT_FLAG_HEX | RTGETOPT_FLAG_OCT | RTGETOPT_FLAG_DEC )
    5478/** @} */
    5579
     
    6286     * This is optional */
    6387    const char     *pszLong;
    64     /** The short option character. 
    65      * This doesn't have to be a character, it may also be a \#define or enum value if 
     88    /** The short option character.
     89     * This doesn't have to be a character, it may also be a \#define or enum value if
    6690     * there isn't any short version of this option. */
    6791    int             iShort;
     
    76100/**
    77101 * Option argument union.
    78  * 
     102 *
    79103 * What ends up here depends on argument format in the option definition.
     104 *
     105 * @remarks Integers will bet put in the \a i and \a u members and sign/zero extended
     106 *          according to the signedness indicated by the \a fFlags. So, you can choose
     107 *          use which ever of the integer members for accessing the value regardless
     108 *          of restrictions indicated in the \a fFlags.
    80109 */
    81110typedef union RTOPTIONUNION
     
    86115    /** A RTGETOPT_ARG_FORMAT_STRING option argument. */
    87116    const char     *psz;
    88     /** A RTGETOPT_ARG_FORMAT_INT32 option argument. */
     117
     118#if !defined(RT_ARCH_AMD64) && !defined(RT_ARCH_X86)
     119# error "PORTME: big-endian systems will need to fix the layout here to get the next two fields working right"
     120#endif
     121
     122    /** A RTGETOPT_ARG_FORMAT_INT8 option argument. */
     123    int8_t          i8;
     124    /** A RTGETOPT_ARG_FORMAT_UINT8 option argument . */
     125    uint8_t         u8;
     126    /** A RTGETOPT_ARG_FORMAT_INT16 option argument. */
     127    int16_t         i16;
     128    /** A RTGETOPT_ARG_FORMAT_UINT16 option argument . */
     129    uint16_t        u16;
     130    /** A RTGETOPT_ARG_FORMAT_INT16 option argument. */
    89131    int32_t         i32;
    90132    /** A RTGETOPT_ARG_FORMAT_UINT32 option argument . */
    91133    uint32_t        u32;
     134    /** A RTGETOPT_ARG_FORMAT_INT64 option argument. */
     135    int64_t         i64;
     136    /** A RTGETOPT_ARG_FORMAT_UINT64 option argument. */
     137    uint64_t        u64;
     138    /** A signed integer value. */
     139    int64_t         i;
     140    /** An unsigned integer value. */
     141    uint64_t        u;
    92142} RTOPTIONUNION;
    93143/** Pointer to an option argument union. */
    94144typedef RTOPTIONUNION *PRTOPTIONUNION;
     145/** Pointer to a const option argument union. */
     146typedef RTOPTIONUNION const *PCRTOPTIONUNION;
    95147
    96148
     
    100152 *
    101153 * This is to be called in a loop until it returns 0 (meaning that all options
    102  * were parsed) or a negative value (meaning that an error occured). The passed in 
     154 * were parsed) or a negative value (meaning that an error occured). The passed in
    103155 * argument vector is sorted into options and non-option arguments, such that when
    104156 * returning 0 the *piThis is the index of the first non-option argument.
     
    115167 * int main(int argc, char *argv[])
    116168 * {
    117  *      static const RTOPTIONDEF g_aOptions[] = 
     169 *      static const RTOPTIONDEF g_aOptions[] =
    118170 *      {
    119171 *          { "--optwithstring",    's', RTGETOPT_REQ_STRING },
     
    145197 *          }
    146198 *      }
    147  *     
     199 *
    148200 *      while (i < argc)
    149201 *      {
    150202 *          //do stuff to argv[i].
    151203 *      }
    152  * 
     204 *
    153205 *      return 0;
    154206 * }
     
    164216 *                      that require an argument, this contains the value of that argument, depending on the type that is required.
    165217 */
    166 RTDECL(int) RTGetOpt(int argc, char *argv[], PCRTOPTIONDEF paOptions, size_t cOptions, int *piThis, PRTOPTIONUNION pValueUnion);
     218RTDECL(int) RTGetOpt(int argc, char **argv, PCRTOPTIONDEF paOptions, size_t cOptions, int *piThis, PRTOPTIONUNION pValueUnion);
    167219
    168220/** @} */
Note: See TracChangeset for help on using the changeset viewer.

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