VirtualBox

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

Last change on this file since 100031 was 99758, checked in by vboxsync, 19 months ago

IPRT: Make doxygen 1.9.6 happy. Mostly removing duplicate docs (iprt is documented in the header files). bugref:10442

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 4.5 KB
Line 
1/* $Id: comparepaths.cpp 99758 2023-05-11 21:37:59Z vboxsync $ */
2/** @file
3 * IPRT - Path Comparison.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include "internal/iprt.h"
42#include <iprt/path.h>
43#include <iprt/errcore.h>
44#include <iprt/string.h>
45#include <iprt/uni.h>
46
47
48/**
49 * Helper for RTPathCompare() and RTPathStartsWith().
50 *
51 * @returns similar to strcmp.
52 * @param pszPath1 Path to compare.
53 * @param pszPath2 Path to compare.
54 * @param fLimit Limit the comparison to the length of \a pszPath2
55 * @internal
56 */
57static int rtPathCompare(const char *pszPath1, const char *pszPath2, bool fLimit)
58{
59 if (pszPath1 == pszPath2)
60 return 0;
61 if (!pszPath1)
62 return -1;
63 if (!pszPath2)
64 return 1;
65
66#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
67 for (;;)
68 {
69 RTUNICP uc1;
70 int rc = RTStrGetCpEx(&pszPath1, &uc1);
71 if (RT_SUCCESS(rc))
72 {
73 RTUNICP uc2;
74 rc = RTStrGetCpEx(&pszPath2, &uc2);
75 if (RT_SUCCESS(rc))
76 {
77 if (uc1 == uc2)
78 {
79 if (uc1)
80 { /* likely */ }
81 else
82 return 0;
83 }
84 else
85 {
86 if (uc1 == '\\')
87 uc1 = '/';
88 else
89 uc1 = RTUniCpToUpper(uc1);
90 if (uc2 == '\\')
91 uc2 = '/';
92 else
93 uc2 = RTUniCpToUpper(uc2);
94 if (uc1 != uc2)
95 {
96 if (fLimit && uc2 == '\0')
97 return 0;
98 return uc1 > uc2 ? 1 : -1; /* (overflow/underflow paranoia) */
99 }
100 }
101 }
102 else
103 return 1;
104 }
105 else
106 return -1;
107 }
108#else
109 if (!fLimit)
110 return strcmp(pszPath1, pszPath2);
111 return strncmp(pszPath1, pszPath2, strlen(pszPath2));
112#endif
113}
114
115
116RTDECL(int) RTPathCompare(const char *pszPath1, const char *pszPath2)
117{
118 return rtPathCompare(pszPath1, pszPath2, false /* full path lengths */);
119}
120
121
122RTDECL(bool) RTPathStartsWith(const char *pszPath, const char *pszParentPath)
123{
124 if (pszPath == pszParentPath)
125 return true;
126 if (!pszPath || !pszParentPath)
127 return false;
128
129 if (rtPathCompare(pszPath, pszParentPath, true /* limited by path 2 */) != 0)
130 return false;
131
132 const size_t cchParentPath = strlen(pszParentPath);
133 if (RTPATH_IS_SLASH(pszPath[cchParentPath]))
134 return true;
135 if (pszPath[cchParentPath] == '\0')
136 return true;
137
138 /* Deal with pszParentPath = root (or having a trailing slash). */
139 if ( cchParentPath > 0
140 && RTPATH_IS_SLASH(pszParentPath[cchParentPath - 1])
141 && RTPATH_IS_SLASH(pszPath[cchParentPath - 1]))
142 return true;
143
144 return false;
145}
146
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