VirtualBox

Changeset 67598 in vbox


Ignore:
Timestamp:
Jun 26, 2017 9:17:22 AM (8 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
116356
Message:

RTGetOptArgvFromString: Added a RTGETOPTARGV_CNV_MODIFY_INPUT flag for avoiding duplicating the input command line string.

Location:
trunk
Files:
3 edited

Legend:

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

    r64603 r67598  
    412412 *
    413413 * @param   ppapszArgv      Where to return the argument vector.  This must be
    414  *                          freed by calling RTGetOptArgvFree.
     414 *                          freed by calling RTGetOptArgvFreeEx or
     415 *                          RTGetOptArgvFree.
    415416 * @param   pcArgs          Where to return the argument count.
    416417 * @param   pszCmdLine      The string to parse.
     
    432433
    433434/**
     435 * Frees and argument vector returned by RTGetOptStringToArgv, taking
     436 * RTGETOPTARGV_CNV_MODIFY_INPUT into account.
     437 *
     438 * @param   papszArgv       Argument vector.  NULL is fine.
     439 * @param   fFlags          The flags passed to RTGetOptStringToArgv.
     440 */
     441RTDECL(void) RTGetOptArgvFreeEx(char **papszArgv, uint32_t fFlags);
     442
     443/**
    434444 * Turns an argv array into a command line string.
    435445 *
     
    448458RTDECL(int) RTGetOptArgvToString(char **ppszCmdLine, const char * const *papszArgv, uint32_t fFlags);
    449459
    450 /** @name RTGetOptArgvToString and RTGetOptArgvToUtf16String flags
     460/** @name RTGetOptArgvToString, RTGetOptArgvToUtf16String and
     461 *        RTGetOptArgvFromString flags
    451462 * @{ */
    452463/** Quote strings according to the Microsoft CRT rules. */
    453 #define RTGETOPTARGV_CNV_QUOTE_MS_CRT       UINT32_C(0)
     464#define RTGETOPTARGV_CNV_QUOTE_MS_CRT       UINT32_C(0x00000000)
    454465/** Quote strings according to the Unix Bourne Shell. */
    455 #define RTGETOPTARGV_CNV_QUOTE_BOURNE_SH    UINT32_C(1)
     466#define RTGETOPTARGV_CNV_QUOTE_BOURNE_SH    UINT32_C(0x00000001)
    456467/** Don't quote any strings at all. */
    457 #define RTGETOPTARGV_CNV_UNQUOTED           UINT32_C(2)
     468#define RTGETOPTARGV_CNV_UNQUOTED           UINT32_C(0x00000002)
    458469/** Mask for the quoting style. */
    459 #define RTGETOPTARGV_CNV_QUOTE_MASK         UINT32_C(3)
     470#define RTGETOPTARGV_CNV_QUOTE_MASK         UINT32_C(0x00000003)
     471/** Allow RTGetOptArgvFromString to modifying the command line input string.
     472 * @note Must use RTGetOptArgvFreeEx to free. */
     473#define RTGETOPTARGV_CNV_MODIFY_INPUT       UINT32_C(0x00000004)
     474/** Valid bits. */
     475#define RTGETOPTARGV_CNV_VALID_MASK         UINT32_C(0x00000007)
    460476/** @} */
    461477
  • trunk/include/iprt/mangling.h

    r67549 r67598  
    956956# define RTGetOpt                                       RT_MANGLER(RTGetOpt)
    957957# define RTGetOptArgvFree                               RT_MANGLER(RTGetOptArgvFree)
     958# define RTGetOptArgvFreeEx                             RT_MANGLER(RTGetOptArgvFreeEx)
    958959# define RTGetOptArgvFromString                         RT_MANGLER(RTGetOptArgvFromString)
    959960# define RTGetOptArgvToString                           RT_MANGLER(RTGetOptArgvToString)
  • trunk/src/VBox/Runtime/common/misc/getoptargv.cpp

    r66861 r67598  
    231231    AssertPtr(pcArgs);
    232232    AssertPtr(ppapszArgv);
    233     AssertReturn(   fFlags == RTGETOPTARGV_CNV_QUOTE_BOURNE_SH
    234                  || fFlags == RTGETOPTARGV_CNV_QUOTE_MS_CRT, VERR_INVALID_FLAGS);
     233    AssertReturn(   (fFlags & RTGETOPTARGV_CNV_QUOTE_MASK) == RTGETOPTARGV_CNV_QUOTE_BOURNE_SH
     234                 || (fFlags & RTGETOPTARGV_CNV_QUOTE_MASK) == RTGETOPTARGV_CNV_QUOTE_MS_CRT, VERR_INVALID_FLAGS);
     235    AssertReturn(~(fFlags & ~RTGETOPTARGV_CNV_VALID_MASK), VERR_INVALID_FLAGS);
     236
    235237    if (!pszSeparators)
    236238        pszSeparators = " \t\n\r";
     
    243245     * Parse the command line and chop off it into argv individual argv strings.
    244246     */
     247    const char *pszSrc    = pszCmdLine;
     248    char       *pszDup    = NULL;
     249    char       *pszDst;
     250    if (fFlags & RTGETOPTARGV_CNV_MODIFY_INPUT)
     251        pszDst = (char *)pszCmdLine;
     252    else
     253    {
     254        pszDst = pszDup = (char *)RTMemAlloc(strlen(pszSrc) + 1);
     255        if (!pszDup)
     256            return VERR_NO_STR_MEMORY;
     257    }
    245258    int         rc        = VINF_SUCCESS;
    246     const char *pszSrc    = pszCmdLine;
    247     char       *pszDup    = (char *)RTMemAlloc(strlen(pszSrc) + 1);
    248     char       *pszDst    = pszDup;
    249     if (!pszDup)
    250         return VERR_NO_STR_MEMORY;
    251259    char      **papszArgs = NULL;
    252260    unsigned    iArg      = 0;
     
    425433RTDECL(void) RTGetOptArgvFree(char **papszArgv)
    426434{
     435    RTGetOptArgvFreeEx(papszArgv, 0);
     436}
     437
     438
     439RTDECL(void) RTGetOptArgvFreeEx(char **papszArgv, uint32_t fFlags)
     440{
     441    Assert(~(fFlags & ~RTGETOPTARGV_CNV_VALID_MASK));
    427442    if (papszArgv)
    428443    {
     
    431446         * RTGetOptArgvFromString for the particulars.
    432447         */
    433         RTMemFree(papszArgv[0]);
     448        if (!(fFlags & RTGETOPTARGV_CNV_MODIFY_INPUT))
     449            RTMemFree(papszArgv[0]);
    434450        RTMemFree(papszArgv);
    435451    }
     
    502518RTDECL(int) RTGetOptArgvToString(char **ppszCmdLine, const char * const *papszArgv, uint32_t fFlags)
    503519{
    504     AssertReturn(fFlags <= RTGETOPTARGV_CNV_UNQUOTED, VERR_INVALID_PARAMETER);
     520    AssertReturn((fFlags & RTGETOPTARGV_CNV_QUOTE_MASK) <= RTGETOPTARGV_CNV_UNQUOTED, VERR_INVALID_FLAGS);
     521    AssertReturn(!(fFlags & (~RTGETOPTARGV_CNV_VALID_MASK | RTGETOPTARGV_CNV_MODIFY_INPUT)), VERR_INVALID_FLAGS);
    505522
    506523#define PUT_CH(ch) \
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