VirtualBox

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

Last change on this file since 35708 was 33540, checked in by vboxsync, 14 years ago

*: spelling fixes, thanks Timeless!

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

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