|
/************************************************************************
*
* Description : Path related utility functions.
*
* (c) Copyright 2000-2008 by Anna-Jayne Metcalfe (anna@riverblade.co.uk)
* and Beth Mackenzie (beth@riverblade.co.uk) / Riverblade Limited
*
* Licence Terms:
*
* This code may be freely reused, subject to the licence terms below.
* Please do let us know of any bugs you find or improvements you make,
* so that we can pass them on to the rest of the development community.
*
* This code is free software; you can redistribute it and/or
* modify it under the terms of the Code Project Open License (CPOL)
* version 1.0 (http://www.codeproject.com/info/cpol10.aspx).
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Code Project Open Licence for further details.
*
************************************************************************
* $Archive: /Projects/Applications/LintProject/Development/Shared/PathUtils.cpp $
* $Revision: 5 $
* $Date: 22/02/08 20:50 $
* $Author: Anna $
*
* $Nokeywords: $
************************************************************************/
/// \file
/// \brief Path handling utility functions implementation.
#include "StdAfx.h"
#ifndef __ATLPATH_H__
#pragma message(__FILE__" : For faster compilation, #include <atlpath.h> in stdafx.h")
#include <atlpath.h>
#endif
#ifndef _SHLOBJ_H_
#pragma message(__FILE__" : For faster compilation, #include <shlobj.h> in stdafx.h")
#include <shlobj.h>
#endif
#include "StringUtils.h"
#include "PathUtils.h"
#pragma comment(lib, "shlwapi.lib")
namespace Riverblade
{
namespace Libraries
{
/////////////////////////////////////////////////////////////////////////////
// Helper functions
/// \brief Determine whether the given path is valid.
///
/// \param sPathName The pathname to check.
/// \return \em true if the path is valid; \em false otherwise.
///
bool Utils::IsPathValid(const CString& sPathName)
{
// Pathnames are limited to _MAX_PATH characters
bool bValid = !sPathName.IsEmpty() && (sPathName.GetLength() <= _MAX_PATH);
if (bValid)
{
// Check for ':' characters. These are ONLY valid in drive specifiers
const int nPos = sPathName.Find( _T(':') );
if (0 == nPos)
{
bValid = false;
}
else if ( (nPos > 1) || (-1 != sPathName.Find( _T(':'), 2) ) )
{
bValid = false;
}
else
{
bValid = (sPathName.FindOneOf( _T("*?\"<>|") ) < 0); // These are never valid characters in a pathname
}
if (sPathName.Mid(1).Find( _T("\\\\") ) >= 0)
{
// Two consequtive backslashes are illegal, except at the start of a UNC pathname
bValid = false;
}
}
#ifdef _DEBUG
if (!bValid)
{
ATLTRACE( _T("IsPathValid(): %s is NOT a valid pathname\n"), sPathName);
}
#endif
return bValid;
}
/// \brief Determine whether the given path is relative.
///
/// \param sPathName The pathname to check.
/// \return \em true if the path is relative; \em false otherwise.
///
bool Utils::IsPathRelative(const CString& sPathName)
{
if ( (sPathName.GetLength() >= 2) && (_T('\\') == sPathName.GetAt(0) ) && (_T('\\') != sPathName.GetAt(1) ) )
{
// sPathName is a root path with no drive letter - e.g. \folder\file.txt.
// We should treat these as relative paths since they can be combined with another filespec.
return true;
}
return (FALSE != ::PathIsRelative(sPathName) );
}
/// \brief Determine whether the given pathnames are equal.
///
/// \param sPathName1 The first pathname.
/// \param sPathName2 The second pathname.
/// \return \em true if the pathnames are equivalent; \em false otherwise.
///
bool Utils::PathNamesEqual(const CString& sPathName1, const CString& sPathName2)
{
return (0 == sPathName1.CompareNoCase(sPathName2) );
}
/// \brief Quote the given path (but only if any spaces are found within it).
///
/// \param sPath The pathname to check.
/// \return The adjusted path.
///
CString Utils::PathAddQuotes(const CString& sPath)
{
CString sQuotedPath = sPath;
::PathQuoteSpaces(sQuotedPath.GetBuffer(sQuotedPath.GetLength() + 2) );
sQuotedPath.ReleaseBuffer();
return sQuotedPath;
}
/// \brief A simple wrapper around PathCombine() in Shlwapi.dll but using CStrings
///
/// \param sFolder The pathname of the folder.
/// \param sRelativePath A relative path to combine with sFolder.
/// \return The combined pathname.
///
CString Utils::CombinePath(const CString& sFolder, const CString& sRelativePath)
{
CString sResult;
LPTSTR const pszBuffer = sResult.GetBuffer(_MAX_PATH);
(void)::PathCombine(pszBuffer,
sFolder,
sRelativePath);
sResult.ReleaseBuffer();
return sResult;
}
/// \brief Create a relative path from the given pathnames.
///
/// \param sFolderPathFrom The pathname of the reference folder. The returned pathname will be relative to this.A const CString & object
/// \param sFilePathNameTo The pathname from which the relative path should be created.
/// \return The contents of sFilePathNameTom relative to sFolderPathFrom.
///
CString Utils::CreateRelativePath(const CString& sFolderPathFrom, const CString& sFilePathNameTo)
{
// Fix for root output paths suggested by coghlans@technologist.com on the
// LintProject CP forum [Anna 19.11.2004]
if (::PathIsRoot(sFolderPathFrom) )
{
return ::PathSkipRoot(sFilePathNameTo);
}
CString sRelativePathName;
CString sSafeFolderPathFrom = sFolderPathFrom;
LPTSTR const pszPath = sRelativePathName.GetBuffer(_MAX_PATH);
CPath pathFrom(sSafeFolderPathFrom);
const CString sPathFromExt = pathFrom.GetExtension();
CPath pathTo(sFilePathNameTo);
const CString sPathToExt = pathTo.GetExtension();
const bool bFilePathNameFromIsFolder = sPathFromExt.IsEmpty();
const bool bFilePathNameToIsFolder = sPathToExt.IsEmpty();
(void)sSafeFolderPathFrom.TrimRight( _T("\\") );
const BOOL bRelativePathOK = ::PathRelativePathTo( pszPath,
sSafeFolderPathFrom,
bFilePathNameFromIsFolder ? FILE_ATTRIBUTE_DIRECTORY : FILE_ATTRIBUTE_NORMAL,
sFilePathNameTo,
bFilePathNameToIsFolder ? FILE_ATTRIBUTE_DIRECTORY : FILE_ATTRIBUTE_NORMAL);
sRelativePathName.ReleaseBuffer();
if (bRelativePathOK)
{
if (0 == sRelativePathName.Find( _T(".\\") ) )
{
// If the filename is of the form ".\name.ext", strip off the leading ".\"
sRelativePathName = Utils::After(sRelativePathName, _T(".\\") );
}
}
else
{
// If the call failed, return the second pathname
sRelativePathName = sFilePathNameTo;
}
return sRelativePathName;
}
/// \brief Resolve the given pathname.
///
/// \param sPath A string that contains the path to resolve.
/// \param ppszarrayDirs A pointer to an optional null-terminated array of directories to be
/// searched first in the case that the path cannot be resolved from pszPath.
/// \param uFlags Flags that specify how the function operates:
/// - PRF_VERIFYEXISTS: Not supported.
/// - PRF_TRYPROGRAMEXTENSIONS: Look for the specified path with the
/// following extensions appended: .pif, .com, .bat, .cmd, .lnk, and .exe.
/// - PRF_FIRSTDIRDEF: Look first in the directory or directories specified by dirs.
/// - PRF_DONTFINDLNK: Ignore .lnk files.
/// \return The corresponding fully-qualified pathname.
///
CString Utils::ResolvePath(const CString& sPath, LPCWSTR* ppszarrayDirs /*= NULL*/, UINT uFlags /*= 0*/)
{
CStringW sResolvedPathW(sPath);
LPWSTR const pszPathW = sResolvedPathW.GetBuffer(_MAX_PATH + 1);
(void)PathResolve(pszPathW, ppszarrayDirs, uFlags);
sResolvedPathW.ReleaseBuffer();
return CString(sResolvedPathW);
}
/// \brief Append the given filename to the given path.
///
/// \param sFolder The path of the folder to which the filename should be appended.
/// \param sFileName The filename which should be appended to the folder path.
/// \return The combined pathname.
///
CString Utils::PathAppendFileName(const CString& sFolder, const CString& sFileName)
{
CPath path(sFolder);
ATLVERIFY(path.Append(sFileName) );
return CString(path); //lint !e1037 (Error -- ambiguous reference to conversion function)
}
}; // namespace Libraries
}; // namespace Riverblade
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I haven't always written software for a living. When I graduated from Surrey University in 1989, it was with an Electronic Engineering degree, but unfortunately that never really gave me the opportunity to do anything particularly interesting (with the possible exception of designing
Darth Vader's Codpiece * for the UK Army in 1990).
* Also known as the Standard Army Bootswitch. But that's another story...
Since the opportunity arose to lead a software team developing C++ software for
Avionic Test Systems in 1996, I've not looked back. More recently I've been involved in the development of subsea acoustic navigation systems, digital TV broadcast systems, port security/tracking systems, and most recently software development tools with my own company,
Riverblade Ltd.
One of my personal specialities is IDE plug-in development.
ResOrg was my first attempt at a plug-in, but my day to day work is with
Visual Lint, an interactive code analysis tool environment with works within the Visual Studio and Eclipse IDEs or on build servers.
I love lots of things, but particularly music, photography and anything connected with history or engineering. I
despise ignorant, intolerant and obstructive people - and it shows...I can be a bolshy cow if you wind me up the wrong way...
I'm currently based 15 minutes walk from the beach in Bournemouth on the south coast of England. Since I moved here I've grown to love the place - even if it is full of grockles in Summer!
I'm a software developer and/or tester with Riverblade Ltd (www.riverblade.co.uk) developing our core product range including our Visual Lint integration product and Lint Project Professional.
I incorporate a number of technologies into a daily basis including Windows API, C++ (VS2008), Managed C++, CLI, Databases, Java, JNI, Eclipse Framework, CDT and of course Visual Studio Extensibility (VSIP VSX).
In my spare time I enjoy cooking (prepping ingredients from scratch!), running, cycling, swimming, reading, interested in experimental electronic music (such as ClockDVA), movies, volunteering my IT skills where I can.