1 | /* $Id: RTPathFindCommon.cpp 85382 2020-07-18 11:33:58Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTPathFindCommon implementations.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2020 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/assert.h>
|
---|
33 | #include <iprt/errcore.h>
|
---|
34 | #include <iprt/path.h>
|
---|
35 |
|
---|
36 |
|
---|
37 | RTDECL(size_t) RTPathFindCommonEx(size_t cPaths, const char * const *papszPaths, uint32_t fFlags)
|
---|
38 | {
|
---|
39 | AssertReturn(cPaths > 0, 0);
|
---|
40 | AssertPtrReturn(papszPaths, 0);
|
---|
41 | AssertReturn(RTPATH_STR_F_IS_VALID(fFlags, 0), 0);
|
---|
42 |
|
---|
43 | /** @todo r=bird: Extremely naive code.
|
---|
44 | * - The original idea of taking either '/' or '\\' as separators is very out of
|
---|
45 | * touch with the rest of path.h. On DOS based systems we need to handle both
|
---|
46 | * of those as well as ':'.
|
---|
47 | * - Why compare pszRef with itself?
|
---|
48 | * - Why derefernece pszRef[cch] for each other path.
|
---|
49 | * - Why perform NULL checks for each outer iteration.
|
---|
50 | * - Why perform '\0' check before comparing with pszRef[cch]?
|
---|
51 | * It's sufficient to check if pszRef[cch] is '\0'.
|
---|
52 | * - Why backtrack to the last path separator? It won't return the expected
|
---|
53 | * result for cPaths=1, unless the path ends with a separator.
|
---|
54 | * - Multiple consequtive path separators must be treated as a single one (most
|
---|
55 | * of the time anyways - UNC crap).
|
---|
56 | */
|
---|
57 | const char *pszRef = papszPaths[0]; /* The reference we're comparing with. */
|
---|
58 | const char chNaiveSep = (fFlags & RTPATH_STR_F_STYLE_MASK) == RTPATH_STR_F_STYLE_HOST
|
---|
59 | ? RTPATH_SLASH
|
---|
60 | : (fFlags & RTPATH_STR_F_STYLE_MASK) == RTPATH_STR_F_STYLE_DOS ? '\\' : '/';
|
---|
61 |
|
---|
62 | size_t cch = 0;
|
---|
63 | do
|
---|
64 | {
|
---|
65 | for (size_t i = 0; i < cPaths; ++i)
|
---|
66 | {
|
---|
67 | const char *pcszPath = papszPaths[i];
|
---|
68 | if ( pcszPath
|
---|
69 | && pcszPath[cch]
|
---|
70 | && pcszPath[cch] == pszRef[cch])
|
---|
71 | continue;
|
---|
72 |
|
---|
73 | while ( cch
|
---|
74 | && pszRef[--cch] != chNaiveSep) { }
|
---|
75 |
|
---|
76 | return cch ? cch + 1 : 0;
|
---|
77 | }
|
---|
78 | } while (++cch);
|
---|
79 |
|
---|
80 | return 0;
|
---|
81 | }
|
---|
82 | RT_EXPORT_SYMBOL(RTPathFindCommonEx);
|
---|
83 |
|
---|
84 |
|
---|
85 | RTDECL(size_t) RTPathFindCommon(size_t cPaths, const char * const *papszPaths)
|
---|
86 | {
|
---|
87 | return RTPathFindCommonEx(cPaths, papszPaths, RTPATH_STR_F_STYLE_HOST);
|
---|
88 | }
|
---|
89 | RT_EXPORT_SYMBOL(RTPathFindCommon);
|
---|
90 |
|
---|