VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/path/RTPathParse.cpp@ 21812

Last change on this file since 21812 was 21673, checked in by vboxsync, 15 years ago

IPRT: Split up r3/path.cpp.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1/* $Id: RTPathParse.cpp 21673 2009-07-17 12:10:10Z 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 pcbDir Where to put the length of the directory component.
49 * If 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 * @param pfFlags Where to set flags returning more information about
57 * the path. For the future. Optional.
58 */
59RTDECL(size_t) RTPathParse(const char *pszPath, size_t *pcchDir, ssize_t *poffName, ssize_t *poffSuff)
60{
61 const char *psz = pszPath;
62 ssize_t offRoot = 0;
63 const char *pszName = pszPath;
64 const char *pszLastDot = NULL;
65
66 for (;; psz++)
67 {
68 switch (*psz)
69 {
70 /* handle separators. */
71#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
72 case ':':
73 pszName = psz + 1;
74 offRoot = pszName - psz;
75 break;
76
77 case '\\':
78#endif
79 case '/':
80 pszName = psz + 1;
81 break;
82
83 case '.':
84 pszLastDot = psz;
85 break;
86
87 /*
88 * The end. Complete the results.
89 */
90 case '\0':
91 {
92 ssize_t offName = *pszName != '\0' ? pszName - pszPath : -1;
93 if (poffName)
94 *poffName = offName;
95
96 if (poffSuff)
97 {
98 ssize_t offSuff = -1;
99 if (pszLastDot)
100 {
101 offSuff = pszLastDot - pszPath;
102 if (offSuff <= offName)
103 offSuff = -1;
104 }
105 *poffSuff = offSuff;
106 }
107
108 if (pcchDir)
109 {
110 ssize_t off = offName - 1;
111 while (off >= offRoot && RTPATH_IS_SLASH(pszPath[off]))
112 off--;
113 *pcchDir = RT_MAX(off, offRoot) + 1;
114 }
115
116 return psz - pszPath;
117 }
118 }
119 }
120
121 /* will never get here */
122 return 0;
123}
124
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