1 | /* $Id: RTPathParse.cpp 25645 2010-01-05 09:29:31Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTPathParse
|
---|
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.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 | * 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 |
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Parses a path.
|
---|
41 | *
|
---|
42 | * It figures the length of the directory component, the offset of
|
---|
43 | * the file name and the location of the suffix dot.
|
---|
44 | *
|
---|
45 | * @returns The path length.
|
---|
46 | *
|
---|
47 | * @param pszPath Path to find filename in.
|
---|
48 | * @param pcchDir Where to put the length of the directory component. If
|
---|
49 | * no directory, this will be 0. Optional.
|
---|
50 | * @param poffName Where to store the filename offset.
|
---|
51 | * If empty string or if it's ending with a slash this
|
---|
52 | * will be set to -1. Optional.
|
---|
53 | * @param poffSuff Where to store the suffix offset (the last dot).
|
---|
54 | * If empty string or if it's ending with a slash this
|
---|
55 | * will be set to -1. Optional.
|
---|
56 | */
|
---|
57 | RTDECL(size_t) RTPathParse(const char *pszPath, size_t *pcchDir, ssize_t *poffName, ssize_t *poffSuff)
|
---|
58 | {
|
---|
59 | const char *psz = pszPath;
|
---|
60 | ssize_t offRoot = 0;
|
---|
61 | const char *pszName = pszPath;
|
---|
62 | const char *pszLastDot = NULL;
|
---|
63 |
|
---|
64 | for (;; psz++)
|
---|
65 | {
|
---|
66 | switch (*psz)
|
---|
67 | {
|
---|
68 | /* handle separators. */
|
---|
69 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
70 | case ':':
|
---|
71 | pszName = psz + 1;
|
---|
72 | offRoot = pszName - psz;
|
---|
73 | break;
|
---|
74 |
|
---|
75 | case '\\':
|
---|
76 | #endif
|
---|
77 | case '/':
|
---|
78 | pszName = psz + 1;
|
---|
79 | break;
|
---|
80 |
|
---|
81 | case '.':
|
---|
82 | pszLastDot = psz;
|
---|
83 | break;
|
---|
84 |
|
---|
85 | /*
|
---|
86 | * The end. Complete the results.
|
---|
87 | */
|
---|
88 | case '\0':
|
---|
89 | {
|
---|
90 | ssize_t offName = *pszName != '\0' ? pszName - pszPath : -1;
|
---|
91 | if (poffName)
|
---|
92 | *poffName = offName;
|
---|
93 |
|
---|
94 | if (poffSuff)
|
---|
95 | {
|
---|
96 | ssize_t offSuff = -1;
|
---|
97 | if (pszLastDot)
|
---|
98 | {
|
---|
99 | offSuff = pszLastDot - pszPath;
|
---|
100 | if (offSuff <= offName)
|
---|
101 | offSuff = -1;
|
---|
102 | }
|
---|
103 | *poffSuff = offSuff;
|
---|
104 | }
|
---|
105 |
|
---|
106 | if (pcchDir)
|
---|
107 | {
|
---|
108 | ssize_t off = offName - 1;
|
---|
109 | while (off >= offRoot && RTPATH_IS_SLASH(pszPath[off]))
|
---|
110 | off--;
|
---|
111 | *pcchDir = RT_MAX(off, offRoot) + 1;
|
---|
112 | }
|
---|
113 |
|
---|
114 | return psz - pszPath;
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | /* will never get here */
|
---|
120 | return 0;
|
---|
121 | }
|
---|
122 |
|
---|