1 | /* $Id: RTPathStripFilename.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTPathStripFilename
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2019 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/ctype.h>
|
---|
34 |
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Strips the filename from a path. Truncates the given string in-place by overwriting the
|
---|
38 | * last path separator character with a null byte in a platform-neutral way.
|
---|
39 | *
|
---|
40 | * @param pszPath Path from which filename should be extracted, will be truncated.
|
---|
41 | * If the string contains no path separator, it will be changed to a "." string.
|
---|
42 | */
|
---|
43 | RTDECL(void) RTPathStripFilename(char *pszPath)
|
---|
44 | {
|
---|
45 | char *psz = pszPath;
|
---|
46 | char *pszLastSep = NULL;
|
---|
47 |
|
---|
48 |
|
---|
49 | for (;; psz++)
|
---|
50 | {
|
---|
51 | switch (*psz)
|
---|
52 | {
|
---|
53 | /* handle separators. */
|
---|
54 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
55 | case ':':
|
---|
56 | pszLastSep = psz + 1;
|
---|
57 | if (RTPATH_IS_SLASH(psz[1]))
|
---|
58 | pszPath = psz + 1;
|
---|
59 | else
|
---|
60 | pszPath = psz;
|
---|
61 | break;
|
---|
62 |
|
---|
63 | case '\\':
|
---|
64 | #endif
|
---|
65 | case '/':
|
---|
66 | pszLastSep = psz;
|
---|
67 | break;
|
---|
68 |
|
---|
69 | /* the end */
|
---|
70 | case '\0':
|
---|
71 | if (!pszLastSep)
|
---|
72 | {
|
---|
73 | /* no directory component */
|
---|
74 | pszPath[0] = '.';
|
---|
75 | pszPath[1] = '\0';
|
---|
76 | }
|
---|
77 | else if (pszLastSep == pszPath)
|
---|
78 | {
|
---|
79 | /* only root. */
|
---|
80 | pszLastSep[1] = '\0';
|
---|
81 | }
|
---|
82 | else
|
---|
83 | pszLastSep[0] = '\0';
|
---|
84 | return;
|
---|
85 | }
|
---|
86 | }
|
---|
87 | /* will never get here */
|
---|
88 | }
|
---|
89 |
|
---|