VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/path/comparepaths.cpp@ 21673

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

IPRT: Split up r3/path.cpp.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 5.8 KB
 
1/* $Id: comparepaths.cpp 21673 2009-07-17 12:10:10Z vboxsync $ */
2/** @file
3 * IPRT - Path Comparison.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include "internal/iprt.h"
36#include <iprt/path.h>
37#include <iprt/err.h>
38#include <iprt/string.h>
39
40
41/**
42 * Helper for RTPathCompare() and RTPathStartsWith().
43 *
44 * @returns similar to strcmp.
45 * @param pszPath1 Path to compare.
46 * @param pszPath2 Path to compare.
47 * @param fLimit Limit the comparison to the length of \a pszPath2
48 * @internal
49 */
50static int rtPathCompare(const char *pszPath1, const char *pszPath2, bool fLimit)
51{
52 if (pszPath1 == pszPath2)
53 return 0;
54 if (!pszPath1)
55 return -1;
56 if (!pszPath2)
57 return 1;
58
59#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
60 PRTUNICP puszPath1;
61 int rc = RTStrToUni(pszPath1, &puszPath1);
62 if (RT_FAILURE(rc))
63 return -1;
64 PRTUNICP puszPath2;
65 rc = RTStrToUni(pszPath2, &puszPath2);
66 if (RT_FAILURE(rc))
67 {
68 RTUniFree(puszPath1);
69 return 1;
70 }
71
72 int iDiff = 0;
73 PRTUNICP puszTmpPath1 = puszPath1;
74 PRTUNICP puszTmpPath2 = puszPath2;
75 for (;;)
76 {
77 register RTUNICP uc1 = *puszTmpPath1;
78 register RTUNICP uc2 = *puszTmpPath2;
79 if (uc1 != uc2)
80 {
81 if (uc1 == '\\')
82 uc1 = '/';
83 else
84 uc1 = RTUniCpToUpper(uc1);
85 if (uc2 == '\\')
86 uc2 = '/';
87 else
88 uc2 = RTUniCpToUpper(uc2);
89 if (uc1 != uc2)
90 {
91 iDiff = uc1 > uc2 ? 1 : -1; /* (overflow/underflow paranoia) */
92 if (fLimit && uc2 == '\0')
93 iDiff = 0;
94 break;
95 }
96 }
97 if (!uc1)
98 break;
99 puszTmpPath1++;
100 puszTmpPath2++;
101
102 }
103
104 RTUniFree(puszPath2);
105 RTUniFree(puszPath1);
106 return iDiff;
107
108#else
109 if (!fLimit)
110 return strcmp(pszPath1, pszPath2);
111 return strncmp(pszPath1, pszPath2, strlen(pszPath2));
112#endif
113}
114
115
116/**
117 * Compares two paths.
118 *
119 * The comparison takes platform-dependent details into account,
120 * such as:
121 * <ul>
122 * <li>On DOS-like platforms, both separator chars (|\| and |/|) are considered
123 * to be equal.
124 * <li>On platforms with case-insensitive file systems, mismatching characters
125 * are uppercased and compared again.
126 * </ul>
127 *
128 * @returns @< 0 if the first path less than the second path.
129 * @returns 0 if the first path identical to the second path.
130 * @returns @> 0 if the first path greater than the second path.
131 *
132 * @param pszPath1 Path to compare (must be an absolute path).
133 * @param pszPath2 Path to compare (must be an absolute path).
134 *
135 * @remarks File system details are currently ignored. This means that you won't
136 * get case-insentive compares on unix systems when a path goes into a
137 * case-insensitive filesystem like FAT, HPFS, HFS, NTFS, JFS, or
138 * similar. For NT, OS/2 and similar you'll won't get case-sensitve
139 * compares on a case-sensitive file system.
140 */
141RTDECL(int) RTPathCompare(const char *pszPath1, const char *pszPath2)
142{
143 return rtPathCompare(pszPath1, pszPath2, false /* full path lengths */);
144}
145
146
147/**
148 * Checks if a path starts with the given parent path.
149 *
150 * This means that either the path and the parent path matches completely, or
151 * that the path is to some file or directory residing in the tree given by the
152 * parent directory.
153 *
154 * The path comparison takes platform-dependent details into account,
155 * see RTPathCompare() for details.
156 *
157 * @returns |true| when \a pszPath starts with \a pszParentPath (or when they
158 * are identical), or |false| otherwise.
159 *
160 * @param pszPath Path to check, must be an absolute path.
161 * @param pszParentPath Parent path, must be an absolute path.
162 * No trailing directory slash!
163 *
164 * @remarks This API doesn't currently handle root directory compares in a
165 * manner consistant with the other APIs. RTPathStartsWith(pszSomePath,
166 * "/") will not work if pszSomePath isn't "/".
167 */
168RTDECL(bool) RTPathStartsWith(const char *pszPath, const char *pszParentPath)
169{
170 if (pszPath == pszParentPath)
171 return true;
172 if (!pszPath || !pszParentPath)
173 return false;
174
175 if (rtPathCompare(pszPath, pszParentPath, true /* limited by path 2 */) != 0)
176 return false;
177
178 const size_t cchParentPath = strlen(pszParentPath);
179 return RTPATH_IS_SLASH(pszPath[cchParentPath])
180 || pszPath[cchParentPath] == '\0';
181}
182
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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