path.cpp revision 388725e4e90e6295275a0c3f55583bf6cf0a84f1
/* $Id$ */
/** @file
* IPRT - Path Manipulation.
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
/**
* Strips the filename from a path.
*
* @param pszPath Path which filename should be extracted from.
*
*/
{
char *pszLastSep = pszPath;
for (;; psz++)
{
switch (*psz)
{
/* handle separators. */
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
case ':':
break;
case '\\':
#endif
case '/':
pszLastSep = psz;
break;
/* the end */
case '\0':
if (pszLastSep == pszPath)
*pszLastSep++ = '.';
*pszLastSep = '\0';
return;
}
}
/* will never get here */
}
/**
* Strips the extension from a path.
*
* @param pszPath Path which extension should be stripped.
*/
{
for (;; pszPath++)
{
switch (*pszPath)
{
/* handle separators. */
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
case ':':
case '\\':
#endif
case '/':
break;
case '.':
break;
/* the end */
case '\0':
if (pszDot)
*pszDot = '\0';
return;
}
}
/* will never get here */
}
/**
* Parses a path.
*
* It figures the length of the directory component, the offset of
* the file name and the location of the suffix dot.
*
* @returns The path length.
*
* @param pszPath Path to find filename in.
* @param pcbDir Where to put the length of the directory component.
* If no directory, this will be 0. Optional.
* @param poffName Where to store the filename offset.
* If empty string or if it's ending with a slash this
* will be set to -1. Optional.
* @param poffSuff Where to store the suffix offset (the last dot).
* If empty string or if it's ending with a slash this
* will be set to -1. Optional.
* @param pfFlags Where to set flags returning more information about
* the path. For the future. Optional.
*/
RTDECL(size_t) RTPathParse(const char *pszPath, size_t *pcchDir, ssize_t *poffName, ssize_t *poffSuff)
{
const char *pszLastDot = NULL;
for (;; psz++)
{
switch (*psz)
{
/* handle separators. */
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
case ':':
break;
case '\\':
#endif
case '/':
break;
case '.':
pszLastDot = psz;
break;
/*
* The end. Complete the results.
*/
case '\0':
{
if (poffName)
if (poffSuff)
{
if (pszLastDot)
{
offSuff = -1;
}
}
if (pcchDir)
{
off--;
}
}
}
}
/* will never get here */
return 0;
}
/**
* Finds the filename in a path.
*
* @returns Pointer to filename within pszPath.
* @returns NULL if no filename (i.e. empty string or ends with a slash).
* @param pszPath Path to find filename in.
*/
{
for (;; psz++)
{
switch (*psz)
{
/* handle separators. */
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
case ':':
break;
case '\\':
#endif
case '/':
break;
/* the end */
case '\0':
if (*pszName)
return (char *)(void *)pszName;
return NULL;
}
}
/* will never get here */
return NULL;
}
/**
* Strips the trailing slashes of a path name.
*
* @param pszPath Path to strip.
*/
{
{
switch (*pszEnd)
{
case '/':
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
case '\\':
#endif
*pszEnd = '\0';
break;
default:
return;
}
}
return;
}
/**
* Finds the extension part of in a path.
*
* @returns Pointer to extension within pszPath.
* @returns NULL if no extension.
* @param pszPath Path to find extension in.
*/
{
for (;; psz++)
{
switch (*psz)
{
/* handle separators. */
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
case ':':
break;
case '\\':
#endif
case '/':
break;
case '.':
break;
/* the end */
case '\0':
if (pszExt)
return (char *)(void *)pszExt;
return NULL;
}
}
/* will never get here */
return NULL;
}
/**
* Checks if a path have an extension.
*
* @returns true if extension present.
* @returns false if no extension.
* @param pszPath Path to check.
*/
{
}
/**
* Checks if a path includes more than a filename.
*
* @returns true if path present.
* @returns false if no path.
* @param pszPath Path to check.
*/
{
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
#else
#endif
}
/**
* Helper for RTPathCompare() and RTPathStartsWith().
*
* @returns similar to strcmp.
* @param pszPath1 Path to compare.
* @param pszPath2 Path to compare.
* @param fLimit Limit the comparison to the length of \a pszPath2
* @internal
*/
{
return 0;
if (!pszPath1)
return -1;
if (!pszPath2)
return 1;
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
if (RT_FAILURE(rc))
return -1;
if (RT_FAILURE(rc))
{
return 1;
}
int iDiff = 0;
for (;;)
{
{
if (uc1 == '\\')
uc1 = '/';
else
if (uc2 == '\\')
uc2 = '/';
else
{
iDiff = 0;
break;
}
}
if (!uc1)
break;
puszTmpPath1++;
puszTmpPath2++;
}
return iDiff;
#else
if (!fLimit)
#endif
}
/**
* Compares two paths.
*
* The comparison takes platform-dependent details into account,
* such as:
* <ul>
* <li>On DOS-like platforms, both |\| and |/| separator chars are considered
* to be equal.
* <li>On platforms with case-insensitive file systems, mismatching characters
* are uppercased and compared again.
* </ul>
*
* @remark
*
* File system details are currently ignored. This means that you won't get
* case-insentive compares on unix systems when a path goes into a case-insensitive
* filesystem like FAT, HPFS, HFS, NTFS, JFS, or similar. For NT, OS/2 and similar
* you'll won't get case-sensitve compares on a case-sensitive file system.
*
* @param pszPath1 Path to compare (must be an absolute path).
* @param pszPath2 Path to compare (must be an absolute path).
*
* @returns @< 0 if the first path less than the second path.
* @returns 0 if the first path identical to the second path.
* @returns @> 0 if the first path greater than the second path.
*/
{
}
/**
* Checks if a path starts with the given parent path.
*
* This means that either the path and the parent path matches completely, or that
* the path is to some file or directory residing in the tree given by the parent
* directory.
*
* The path comparison takes platform-dependent details into account,
* see RTPathCompare() for details.
*
* @param pszPath Path to check, must be an absolute path.
* @param pszParentPath Parent path, must be an absolute path.
* No trailing directory slash!
*
* @returns |true| when \a pszPath starts with \a pszParentPath (or when they
* are identical), or |false| otherwise.
*
* @remark This API doesn't currently handle root directory compares in a manner
* consistant with the other APIs. RTPathStartsWith(pszSomePath, "/") will
* not work if pszSomePath isn't "/".
*/
{
if (pszPath == pszParentPath)
return true;
if (!pszPath || !pszParentPath)
return false;
return false;
}
/**
* Same as RTPathReal only the result is RTStrDup()'ed.
*
* @returns Pointer to real path. Use RTStrFree() to free this string.
* @returns NULL if RTPathReal() or RTStrDup() fails.
* @param pszPath
*/
{
char szPath[RTPATH_MAX];
if (RT_SUCCESS(rc))
return NULL;
}
/**
* Same as RTPathAbs only the result is RTStrDup()'ed.
*
* @returns Pointer to real path. Use RTStrFree() to free this string.
* @returns NULL if RTPathAbs() or RTStrDup() fails.
* @param pszPath The path to resolve.
*/
{
char szPath[RTPATH_MAX];
if (RT_SUCCESS(rc))
return NULL;
}
/**
* Returns the length of the volume name specifier of the given path.
* If no such specifier zero is returned.
*/
{
#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
{
/* UTC path. */
/* Drive letter. */
return 2;
}
return 0;
#else
/* This isn't quite right when looking at the above stuff, but it works assuming that '//' does not mean UNC. */
/// @todo (dmik) well, it's better to consider there's no volume name
// at all on *nix systems
return 0;
// return pszPath && pszPath[0] == '/';
#endif
}
/**
* Get the absolute path (no symlinks, no . or .. components), assuming the
* given base path as the current directory. The resulting path doesn't have
* to exist.
*
* @returns iprt status code.
* @param pszBase The base path to act like a current directory.
* When NULL, the actual cwd is used (i.e. the call
* is equivalent to RTPathAbs(pszPath, ...).
* @param pszPath The path to resolve.
* @param pszAbsPath Where to store the absolute path.
* @param cchAbsPath Size of the buffer.
*/
RTDECL(int) RTPathAbsEx(const char *pszBase, const char *pszPath, char *pszAbsPath, unsigned cchAbsPath)
{
{
#if defined(RT_OS_WINDOWS)
/* The format for very long paths is not supported. */
return VERR_INVALID_NAME;
#endif
/** @todo there are a couple of things which isn't 100% correct, although the
* current code will have to work for now - I don't have time to fix it right now.
*
* 1) On Windows & OS/2 we confuse '/' with an abspath spec and will
* not necessarily resolve it on the right drive.
* 2) A trailing slash in the base might cause UNC names to be created.
* 3) The lengths total doesn't have to be less than max length
* if the pszPath starts with a slash.
*/
return VERR_FILENAME_TOO_LONG;
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|| pszPath[0] == '\\'
#endif
;
char szPath[RTPATH_MAX];
if (fRootSpec)
{
/* join the disk name from base and the path */
}
else
{
/* join the base path and the path */
}
}
/* Fallback to the non *Ex version */
}
/**
* Same as RTPathAbsEx only the result is RTStrDup()'ed.
*
* @returns Pointer to the absolute path. Use RTStrFree() to free this string.
* @returns NULL if RTPathAbsEx() or RTStrDup() fails.
* @param pszBase The base path to act like a current directory.
* When NULL, the actual cwd is used (i.e. the call
* is equivalent to RTPathAbs(pszPath, ...).
* @param pszPath The path to resolve.
*/
{
char szPath[RTPATH_MAX];
if (RT_SUCCESS(rc))
return NULL;
}
#ifndef RT_MINI
{
/*
* Calc the length and check if there is space before copying.
*/
{
return VINF_SUCCESS;
}
return VERR_BUFFER_OVERFLOW;
}
/**
* Gets the directory for architecture-independent application data, for
* example NLS files, module sources, ...
*
* Windows: @<program files directory@>/@<application@>
* Old path: same as RTPathProgram()
*
*/
{
#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_PRIVATE)
char *pszUtf8Path;
int rc;
if (RT_SUCCESS(rc))
{
if (cchPathPrivateNoArch < cchPath)
else
}
return rc;
#else
#endif
}
/**
* Gets the directory for architecture-dependent application data, for
* example modules which can be loaded at runtime.
*
* Windows: @<program files directory@>/@<application@>
* Old path: same as RTPathProgram()
*
* @returns iprt status code.
* @param pszPath Buffer where to store the path.
* @param cchPath Buffer size in bytes.
*/
{
#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_PRIVATE_ARCH)
char *pszUtf8Path;
int rc;
if (RT_SUCCESS(rc))
{
if (cchPathPrivateArch < cchPath)
else
}
return rc;
#else
#endif
}
/**
* Gets the directory of shared libraries. This is not the same as
* RTPathAppPrivateArch() as Linux depends all shared libraries in
* a common global directory where ld.so can found them.
*
* Windows: @<program files directory@>/@<application@>
* Old path: same as RTPathProgram()
*
* @returns iprt status code.
* @param pszPath Buffer where to store the path.
* @param cchPath Buffer size in bytes.
*/
{
#if !defined(RT_OS_WINDOWS) && defined(RTPATH_SHARED_LIBS)
char *pszUtf8Path;
int rc;
if (RT_SUCCESS(rc))
{
if (cchPathSharedLibs < cchPath)
else
}
return rc;
#else
#endif
}
/**
* Gets the directory for documentation.
*
* Windows: @<program files directory@>/@<application@>
* Old path: same as RTPathProgram()
*
* @returns iprt status code.
* @param pszPath Buffer where to store the path.
* @param cchPath Buffer size in bytes.
*/
{
#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_DOCS)
char *pszUtf8Path;
int rc;
if (RT_SUCCESS(rc))
{
if (cchPathAppDocs < cchPath)
else
}
return rc;
#else
#endif
}
#endif /* !RT_MINI */