448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync/* $Id$ */
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync/** @file
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync * IPRT - RTPathParsedReassemble.
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync */
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync/*
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync * Copyright (C) 2013 Oracle Corporation
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync *
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync * available from http://www.virtualbox.org. This file is free software;
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync * you can redistribute it and/or modify it under the terms of the GNU
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync * General Public License (GPL) as published by the Free Software
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync *
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync * The contents of this file may alternatively be used under the terms
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync * of the Common Development and Distribution License Version 1.0
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync * VirtualBox OSE distribution, in which case the provisions of the
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync * CDDL are applicable instead of those of the GPL.
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync *
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync * You may elect to license modified versions of this file under the
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync * terms and conditions of either the GPL or the CDDL or both.
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync */
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync/*******************************************************************************
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync* Header Files *
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync*******************************************************************************/
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync#include "internal/iprt.h"
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync#include <iprt/path.h>
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync#include <iprt/assert.h>
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync#include <iprt/err.h>
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync#include <iprt/string.h>
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsyncRTDECL(int) RTPathParsedReassemble(const char *pszSrcPath, PRTPATHPARSED pParsed, uint32_t fFlags,
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync char *pszDstPath, size_t cbDstPath)
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync{
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync /*
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync * Input validation.
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync */
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync AssertPtrReturn(pszSrcPath, VERR_INVALID_POINTER);
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync AssertPtrReturn(pParsed, VERR_INVALID_POINTER);
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync AssertReturn(pParsed->cComps > 0, VERR_INVALID_PARAMETER);
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync AssertReturn(RTPATH_STR_F_IS_VALID(fFlags, 0) && !(fFlags & RTPATH_STR_F_MIDDLE), VERR_INVALID_FLAGS);
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync AssertPtrReturn(pszDstPath, VERR_INVALID_POINTER);
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync AssertReturn(cbDstPath > pParsed->cchPath, VERR_BUFFER_OVERFLOW);
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync /*
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync * Figure which slash to use.
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync */
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync char chSlash;
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync switch (fFlags & RTPATH_STR_F_STYLE_MASK)
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync {
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync case RTPATH_STR_F_STYLE_HOST:
1d2ce109a56eae707f58dae0d2386619bdb36955vboxsync chSlash = RTPATH_SLASH;
1d2ce109a56eae707f58dae0d2386619bdb36955vboxsync break;
1d2ce109a56eae707f58dae0d2386619bdb36955vboxsync
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync case RTPATH_STR_F_STYLE_DOS:
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync chSlash = '\\';
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync break;
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync case RTPATH_STR_F_STYLE_UNIX:
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync chSlash = '/';
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync break;
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync default:
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync AssertFailedReturn(VERR_INVALID_FLAGS); /* impossible */
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync }
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync /*
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync * Do the joining.
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync */
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync uint32_t const cchOrgPath = pParsed->cchPath;
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync uint32_t cchDstPath = 0;
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync uint32_t const cComps = pParsed->cComps;
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync uint32_t idxComp = 0;
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync char *pszDst = pszDstPath;
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync uint32_t cchComp;
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync if (RTPATH_PROP_HAS_ROOT_SPEC(pParsed->fProps))
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync {
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync cchComp = pParsed->aComps[0].cch;
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync cchDstPath += cchComp;
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync AssertReturn(cchDstPath <= cchOrgPath, VERR_INVALID_PARAMETER);
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync memcpy(pszDst, &pszSrcPath[pParsed->aComps[0].off], cchComp);
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync /* fix the slashes */
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync char chOtherSlash = chSlash == '\\' ? '/' : '\\';
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync while (cchComp-- > 0)
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync {
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync if (*pszDst == chOtherSlash)
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync *pszDst = chSlash;
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync pszDst++;
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync }
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync idxComp = 1;
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync }
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync while (idxComp < cComps)
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync {
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync cchComp = pParsed->aComps[idxComp].cch;
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync cchDstPath += cchComp;
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync AssertReturn(cchDstPath <= cchOrgPath, VERR_INVALID_PARAMETER);
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync memcpy(pszDst, &pszSrcPath[pParsed->aComps[idxComp].off], cchComp);
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync pszDst += cchComp;
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync idxComp++;
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync if (idxComp != cComps || (pParsed->fProps & RTPATH_PROP_DIR_SLASH))
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync {
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync cchDstPath++;
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync AssertReturn(cchDstPath <= cchOrgPath, VERR_INVALID_PARAMETER);
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync *pszDst++ = chSlash;
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync }
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync }
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync *pszDst = '\0';
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync return VINF_SUCCESS;
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync}
448991352053bf9c3ca8789439a4e1329cc34ba0vboxsync