VirtualBox

Changeset 26476 in vbox


Ignore:
Timestamp:
Feb 13, 2010 2:06:41 AM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
57643
Message:

iprt: Added RTPathCountComponents, RTPathCopyComponents, RTGetOptArgvFromString and RTGetOptArgvFree.

Location:
trunk
Files:
5 added
10 edited

Legend:

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

    r25351 r26476  
    358358RTDECL(int) RTGetOptPrintError(int ch, PCRTGETOPTUNION pValueUnion);
    359359
     360/**
     361 * Parses the @a pszCmdLine string into an argv array.
     362 *
     363 * This is useful for converting a response file or similar to an argument
     364 * vector that can be used with RTGetOptInit().
     365 *
     366 * This function aims at following the bourn shell string quoting rules.
     367 *
     368 * @returns IPRT status code.
     369 *
     370 * @param   ppapszArgv      Where to return the argument vector.  This must be
     371 *                          freed by calling RTGetOptArgvFree.
     372 * @param   pcArgs          Where to return the argument count.
     373 * @param   pszCmdLine      The string to parse.
     374 * @param   pszSeparators   String containing the argument separators. If NULL,
     375 *                          then space, tab, line feed (\\n) and return (\\r)
     376 *                          are used.
     377 */
     378RTDECL(int) RTGetOptArgvFromString(char ***ppapszArgv, int *pcArgs, const char *pszCmdLine, const char *pszSeparators);
     379
     380/**
     381 * Frees and argument vector returned by RTGetOptStringToArgv.
     382 *
     383 * @param   papszArgv       Argument vector.  NULL is fine.
     384 */
     385RTDECL(void) RTGetOptArgvFree(char **paArgv);
     386
    360387/** @} */
    361388
  • trunk/include/iprt/path.h

    r26133 r26476  
    320320
    321321/**
     322 * Counts the components in the specified path.
     323 *
     324 * An empty string has zero components.  A lone root slash is considered have
     325 * one.  The paths "/init" and "/bin/" are considered having two components.  An
     326 * UNC share specifier like "\\myserver\share" will be considered as one single
     327 * component.
     328 *
     329 * @returns The number of path components.
     330 * @param   pszPath     The path to parse.
     331 */
     332RTDECL(size_t) RTPathCountComponents(const char *pszPath);
     333
     334/**
     335 * Copies the specified number of path components from @a pszSrc and into @a
     336 * pszDst.
     337 *
     338 * @returns VINF_SUCCESS or VERR_BUFFER_OVERFLOW.  In the latter case the buffer
     339 *          is not touched.
     340 *
     341 * @param   pszDst      The destination buffer.
     342 * @param   cbDst       The size of the destination buffer.
     343 * @param   pszSrc      The source path.
     344 * @param   cComponents The number of components to copy from @a pszSrc.
     345 */
     346RTDECL(int) RTPathCopyComponents(char *pszDst, size_t cbDst, const char *pszSrc, size_t cComponents);
     347
     348/**
    322349 * Compares two paths.
    323350 *
  • trunk/src/VBox/Runtime/Makefile.kmk

    r26416 r26476  
    252252        common/misc/cidr.cpp \
    253253        common/misc/getopt.cpp \
     254        common/misc/getoptargv.cpp \
    254255        common/misc/handletable.cpp \
    255256        common/misc/handletablectx.cpp \
     
    266267        common/misc/term.cpp \
    267268        common/misc/tar.cpp \
     269        common/path/rtPathRootSpecLen.cpp \
    268270        common/path/rtPathVolumeSpecLen.cpp \
    269271        common/path/RTPathAbsDup.cpp \
     
    271273        common/path/RTPathAbsExDup.cpp \
    272274        common/path/RTPathAppend.cpp \
     275        common/path/RTPathCopyComponents.cpp \
     276        common/path/RTPathCountComponents.cpp \
    273277        common/path/RTPathExt.cpp \
    274278        common/path/RTPathFilename.cpp \
  • trunk/src/VBox/Runtime/common/path/RTPathAppend.cpp

    r21676 r26476  
    3535#include "internal/iprt.h"
    3636#include <iprt/path.h>
     37
    3738#include <iprt/assert.h>
    3839#include <iprt/ctype.h>
     
    5051 *
    5152 * @remarks Unnecessary root slashes will not be counted. The caller will have
    52  *          to deal with it where it matters.
     53 *          to deal with it where it matters.  (Unlike rtPathRootSpecLen which
     54 *          counts them.)
    5355 */
    54 static size_t rtPathRootSpecLen(const char *pszPath)
     56static size_t rtPathRootSpecLen2(const char *pszPath)
    5557{
    5658    /* fend of wildlife. */
     
    170172        /* In the leading path we can skip unnecessary trailing slashes, but
    171173           be sure to leave one. */
    172         size_t const cchRoot = rtPathRootSpecLen(pszPath);
     174        size_t const cchRoot = rtPathRootSpecLen2(pszPath);
    173175        while (     (size_t)(pszPathEnd - pszPath) > RT_MAX(1, cchRoot)
    174176               &&   RTPATH_IS_SLASH(pszPathEnd[-2]))
  • trunk/src/VBox/Runtime/common/path/rtPathVolumeSpecLen.cpp

    r21678 r26476  
    4444 * If no such specifier zero is returned.
    4545 */
    46 size_t rtPathVolumeSpecLen(const char *pszPath)
     46DECLHIDDEN(size_t) rtPathVolumeSpecLen(const char *pszPath)
    4747{
    4848#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
  • trunk/src/VBox/Runtime/include/internal/path.h

    r21675 r26476  
    4343
    4444
    45 size_t  rtPathVolumeSpecLen(const char *pszPath);
    46 int     rtPathPosixRename(const char *pszSrc, const char *pszDst, unsigned fRename, RTFMODE fFileType);
    47 int     rtPathWin32MoveRename(const char *pszSrc, const char *pszDst, uint32_t fFlags, RTFMODE fFileType);
     45DECLHIDDEN(size_t)  rtPathRootSpecLen(const char *pszPath);
     46DECLHIDDEN(size_t)  rtPathVolumeSpecLen(const char *pszPath);
     47DECLHIDDEN(int)     rtPathPosixRename(const char *pszSrc, const char *pszDst, unsigned fRename, RTFMODE fFileType);
     48DECLHIDDEN(int)     rtPathWin32MoveRename(const char *pszSrc, const char *pszDst, uint32_t fFlags, RTFMODE fFileType);
    4849
    4950
  • trunk/src/VBox/Runtime/r3/posix/path-posix.cpp

    r25898 r26476  
    662662 *                      not a directory (we are NOT checking whether it's a file).
    663663 */
    664 int rtPathPosixRename(const char *pszSrc, const char *pszDst, unsigned fRename, RTFMODE fFileType)
     664DECLHIDDEN(int) rtPathPosixRename(const char *pszSrc, const char *pszDst, unsigned fRename, RTFMODE fFileType)
    665665{
    666666    /*
  • trunk/src/VBox/Runtime/r3/win/path-win.cpp

    r23375 r26476  
    455455 *                      not a directory (we are NOT checking whether it's a file).
    456456 */
    457 int rtPathWin32MoveRename(const char *pszSrc, const char *pszDst, uint32_t fFlags, RTFMODE fFileType)
     457DECLHIDDEN(int) rtPathWin32MoveRename(const char *pszSrc, const char *pszDst, uint32_t fFlags, RTFMODE fFileType)
    458458{
    459459    /*
  • trunk/src/VBox/Runtime/testcase/Makefile.kmk

    r26419 r26476  
    6666        tstFork \
    6767        tstGetOpt \
     68        tstRTGetOptArgv \
    6869        tstHandleTable \
    6970        tstRTHeapOffset \
     
    202203tstGetOpt_SOURCES = tstGetOpt.cpp
    203204
     205tstRTGetOptArgv_TEMPLATE = VBOXR3TSTEXE
     206tstRTGetOptArgv_SOURCES = tstRTGetOptArgv.cpp
     207
    204208tstHandleTable_SOURCES = tstHandleTable.cpp
    205209
  • trunk/src/VBox/Runtime/testcase/tstRTPath.cpp

    r26133 r26476  
    414414
    415415    /*
     416     * RTPathCountComponents
     417     */
     418    RTTestSub(hTest, "RTPathCountComponents");
     419    RTTESTI_CHECK(RTPathCountComponents("") == 0);
     420    RTTESTI_CHECK(RTPathCountComponents("/") == 1);
     421    RTTESTI_CHECK(RTPathCountComponents("//") == 1);
     422    RTTESTI_CHECK(RTPathCountComponents("//////////////") == 1);
     423    RTTESTI_CHECK(RTPathCountComponents("//////////////bin") == 2);
     424    RTTESTI_CHECK(RTPathCountComponents("//////////////bin/") == 2);
     425    RTTESTI_CHECK(RTPathCountComponents("//////////////bin/////") == 2);
     426    RTTESTI_CHECK(RTPathCountComponents("..") == 1);
     427    RTTESTI_CHECK(RTPathCountComponents("../") == 1);
     428    RTTESTI_CHECK(RTPathCountComponents("../..") == 2);
     429    RTTESTI_CHECK(RTPathCountComponents("../../") == 2);
     430#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
     431    RTTESTI_CHECK(RTPathCountComponents("d:") == 1);
     432    RTTESTI_CHECK(RTPathCountComponents("d:/") == 1);
     433    RTTESTI_CHECK(RTPathCountComponents("d:/\\") == 1);
     434    RTTESTI_CHECK(RTPathCountComponents("d:\\") == 1);
     435    RTTESTI_CHECK(RTPathCountComponents("c:\\config.sys") == 2);
     436    RTTESTI_CHECK(RTPathCountComponents("c:\\windows") == 2);
     437    RTTESTI_CHECK(RTPathCountComponents("c:\\windows\\") == 2);
     438    RTTESTI_CHECK(RTPathCountComponents("c:\\windows\\system32") == 3);
     439    RTTESTI_CHECK(RTPathCountComponents("//./C$") == 1);
     440    RTTESTI_CHECK(RTPathCountComponents("\\\\.\\C$") == 1);
     441    RTTESTI_CHECK(RTPathCountComponents("/\\.\\C$") == 1);
     442    RTTESTI_CHECK(RTPathCountComponents("//myserver") == 1);
     443    RTTESTI_CHECK(RTPathCountComponents("//myserver/") == 1);
     444    RTTESTI_CHECK(RTPathCountComponents("//myserver/share") == 1);
     445    RTTESTI_CHECK(RTPathCountComponents("//myserver/share/") == 1);
     446    RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\") == 1);
     447    RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\x") == 2);
     448    RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\x\\y") == 3);
     449    RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\x\\y\\") == 3);
     450#endif
     451
     452    /*
     453     * RTPathCopyComponents
     454     */
     455    struct
     456    {
     457        const char *pszSrc;
     458        size_t      cComponents;
     459        const char *pszResult;
     460    } s_aCopyComponents[] =
     461    {
     462        { "",                           0, "" },
     463        { "",                           5, "" },
     464        { "/",                          0, "" },
     465        { "/",                          1, "/" },
     466        { "/",                          2, "/" },
     467        { "/usr/bin/sed",               0, "" },
     468        { "/usr/bin/sed",               1, "/" },
     469        { "/usr/bin/sed",               2, "/usr/" },
     470        { "/usr/bin/sed",               3, "/usr/bin/" },
     471        { "/usr/bin/sed",               4, "/usr/bin/sed" },
     472        { "/usr/bin/sed",               5, "/usr/bin/sed" },
     473        { "/usr/bin/sed",               6, "/usr/bin/sed" },
     474        { "/usr///bin/sed",             2, "/usr///" },
     475    };
     476    for (unsigned i = 0; i < RT_ELEMENTS(s_aCopyComponents); i++)
     477    {
     478        const char *pszInput    = s_aCopyComponents[i].pszSrc;
     479        size_t      cComponents = s_aCopyComponents[i].cComponents;
     480        const char *pszResult   = s_aCopyComponents[i].pszResult;
     481
     482        memset(szPath, 'a', sizeof(szPath));
     483        rc = RTPathCopyComponents(szPath, sizeof(szPath), pszInput, cComponents);
     484        RTTESTI_CHECK_RC(rc, VINF_SUCCESS);
     485        if (RT_SUCCESS(rc) && strcmp(szPath, pszResult))
     486            RTTestIFailed("Unexpected result\n"
     487                          "   input: '%s' cComponents=%u\n"
     488                          "  output: '%s'\n"
     489                          "expected: '%s'",
     490                          pszInput, cComponents, szPath, pszResult);
     491        else if (RT_SUCCESS(rc))
     492        {
     493            RTTESTI_CHECK_RC(RTPathCopyComponents(szPath, strlen(pszResult) + 1, pszInput, cComponents), VINF_SUCCESS);
     494            RTTESTI_CHECK_RC(RTPathCopyComponents(szPath, strlen(pszResult), pszInput, cComponents), VERR_BUFFER_OVERFLOW);
     495        }
     496    }
     497
     498    /*
    416499     * Summary.
    417500     */
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