VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/path/RTPathParsedReassemble.cpp@ 45400

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

IPRT: A Study in Paths - Chapter 3: Reassembling parsed and split paths.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 3.9 KB
 
1/* $Id: RTPathParsedReassemble.cpp 45400 2013-04-08 12:08:00Z vboxsync $ */
2/** @file
3 * IPRT - RTPathParsedReassemble.
4 */
5
6/*
7 * Copyright (C) 2013 Oracle Corporation
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/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include "internal/iprt.h"
32#include <iprt/path.h>
33
34#include <iprt/assert.h>
35#include <iprt/err.h>
36#include <iprt/string.h>
37
38
39RTDECL(int) RTPathParsedReassemble(const char *pszSrcPath, PRTPATHPARSED pParsed, uint32_t fFlags,
40 char *pszDstPath, size_t cbDstPath)
41{
42 /*
43 * Input validation.
44 */
45 AssertPtrReturn(pszSrcPath, VERR_INVALID_POINTER);
46 AssertPtrReturn(pParsed, VERR_INVALID_POINTER);
47 AssertReturn(pParsed->cComps > 0, VERR_INVALID_PARAMETER);
48 AssertReturn(RTPATH_STR_F_IS_VALID(fFlags, 0) && !(fFlags & RTPATH_STR_F_MIDDLE), VERR_INVALID_FLAGS);
49 AssertPtrReturn(pszDstPath, VERR_INVALID_POINTER);
50 AssertReturn(cbDstPath > pParsed->cchPath, VERR_BUFFER_OVERFLOW);
51
52 /*
53 * Figure which slash to use.
54 */
55 char chSlash;
56 switch (fFlags & RTPATH_STR_F_STYLE_MASK)
57 {
58#if PATH_STYLE == RTPATH_STR_F_STYLE_DOS
59 case RTPATH_STR_F_STYLE_HOST:
60#endif
61 case RTPATH_STR_F_STYLE_DOS:
62 chSlash = '\\';
63 break;
64
65#if PATH_STYLE == RTPATH_STR_F_STYLE_UNIX
66 case RTPATH_STR_F_STYLE_HOST:
67#endif
68 case RTPATH_STR_F_STYLE_UNIX:
69 chSlash = '/';
70 break;
71
72 default:
73 AssertFailedReturn(VERR_INVALID_FLAGS); /* impossible */
74 }
75
76 /*
77 * Do the joining.
78 */
79 uint32_t const cchOrgPath = pParsed->cchPath;
80 uint32_t cchDstPath = 0;
81 uint32_t const cComps = pParsed->cComps;
82 uint32_t idxComp = 0;
83 char *pszDst = pszDstPath;
84 uint32_t cchComp;
85
86 if (RTPATH_PROP_HAS_ROOT_SPEC(pParsed->fProps))
87 {
88 cchComp = pParsed->aComps[0].cch;
89 cchDstPath += cchComp;
90 AssertReturn(cchDstPath <= cchOrgPath, VERR_INVALID_PARAMETER);
91 memcpy(pszDst, &pszSrcPath[pParsed->aComps[0].off], cchComp);
92
93 /* fix the slashes */
94 char chOtherSlash = chSlash == '\\' ? '/' : '\\';
95 while (cchComp-- > 0)
96 {
97 if (*pszDst == chOtherSlash)
98 *pszDst = chSlash;
99 pszDst++;
100 }
101 idxComp = 1;
102 }
103
104 while (idxComp < cComps)
105 {
106 cchComp = pParsed->aComps[idxComp].cch;
107 cchDstPath += cchComp;
108 AssertReturn(cchDstPath <= cchOrgPath, VERR_INVALID_PARAMETER);
109 memcpy(pszDst, &pszSrcPath[pParsed->aComps[idxComp].off], cchComp);
110 pszDst += cchComp;
111 idxComp++;
112 if (idxComp != cComps || (pParsed->fProps & RTPATH_PROP_DIR_SLASH))
113 {
114 cchDstPath++;
115 AssertReturn(cchDstPath <= cchOrgPath, VERR_INVALID_PARAMETER);
116 *pszDst++ = chSlash;
117 }
118 }
119
120 *pszDst = '\0';
121
122 return VINF_SUCCESS;
123}
124
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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