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