You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
857 lines
24 KiB
857 lines
24 KiB
14 years ago
|
/*==LICENSE==*
|
||
|
|
||
|
CyanWorlds.com Engine - MMOG client, server and tools
|
||
|
Copyright (C) 2011 Cyan Worlds, Inc.
|
||
|
|
||
|
This program is free software: you can redistribute it and/or modify
|
||
|
it under the terms of the GNU General Public License as published by
|
||
|
the Free Software Foundation, either version 3 of the License, or
|
||
|
(at your option) any later version.
|
||
|
|
||
|
This program 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
|
||
|
GNU General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU General Public License
|
||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
|
||
13 years ago
|
Additional permissions under GNU GPL version 3 section 7
|
||
|
|
||
|
If you modify this Program, or any covered work, by linking or
|
||
|
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
|
||
|
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
|
||
|
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
|
||
|
(or a modified version of those libraries),
|
||
|
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
|
||
|
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
|
||
|
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
|
||
|
licensors of this Program grant you additional
|
||
|
permission to convey the resulting work. Corresponding Source for a
|
||
|
non-source form of such a combination shall include the source code for
|
||
|
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
|
||
|
work.
|
||
|
|
||
14 years ago
|
You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||
|
or by snail mail at:
|
||
|
Cyan Worlds, Inc.
|
||
|
14617 N Newport Hwy
|
||
|
Mead, WA 99021
|
||
|
|
||
|
*==LICENSE==*/
|
||
|
/*****************************************************************************
|
||
|
*
|
||
|
* $/Plasma20/Sources/Plasma/NucleusLib/pnUtils/Private/Win32/pnUtW32Path.cpp
|
||
|
*
|
||
|
***/
|
||
|
|
||
|
#include "../../Pch.h"
|
||
|
#pragma hdrstop
|
||
|
|
||
|
|
||
|
/*****************************************************************************
|
||
|
*
|
||
|
* Local functions
|
||
|
*
|
||
|
***/
|
||
|
|
||
|
// make sure our definition is at least as big as the compiler's definition
|
||
|
COMPILER_ASSERT(MAX_PATH >= _MAX_PATH);
|
||
|
|
||
|
|
||
|
//===========================================================================
|
||
13 years ago
|
static inline bool IsSlash (wchar_t c) {
|
||
14 years ago
|
return (c == L'\\') || (c == L'/');
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
13 years ago
|
static inline wchar_t ConvertSlash (wchar_t c) {
|
||
14 years ago
|
return c != L'/' ? c : L'\\';
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
13 years ago
|
static inline bool IsUncPath (const wchar_t path[]) {
|
||
14 years ago
|
return IsSlash(path[0]) && IsSlash(path[1]);
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
13 years ago
|
static const wchar_t * SkipUncDrive (const wchar_t path[]) {
|
||
14 years ago
|
// UNC drive: "//server/share"
|
||
|
|
||
|
// skip over leading "//"
|
||
|
path += 2;
|
||
|
|
||
|
// scan forward to end of server name
|
||
|
for (;; ++path) {
|
||
|
if (!*path)
|
||
|
return path;
|
||
|
if (IsSlash(*path))
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
// skip over '/'
|
||
|
++path;
|
||
|
|
||
|
// skip over share name
|
||
|
for (;; ++path) {
|
||
|
if (!*path)
|
||
|
return path;
|
||
|
if (IsSlash(*path))
|
||
|
return path;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
13 years ago
|
static wchar_t * PathSkipOverSeparator (wchar_t * path) {
|
||
14 years ago
|
for (; *path; ++path) {
|
||
|
if (IsSlash(*path))
|
||
|
return path + 1;
|
||
|
}
|
||
|
|
||
|
return path;
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
|
static unsigned CommonPrefixLength (
|
||
13 years ago
|
const wchar_t src1[],
|
||
|
const wchar_t src2[]
|
||
14 years ago
|
) {
|
||
|
ASSERT(src1);
|
||
|
ASSERT(src2);
|
||
|
|
||
13 years ago
|
wchar_t const * const base = src1;
|
||
|
const wchar_t * common = nil;
|
||
14 years ago
|
for (;;) {
|
||
|
// Are the next components equal in length?
|
||
13 years ago
|
const wchar_t * next1 = PathSkipOverSeparator(const_cast<wchar_t *>(src1));
|
||
|
const wchar_t * next2 = PathSkipOverSeparator(const_cast<wchar_t *>(src2));
|
||
14 years ago
|
const int componentLen = next1 - src1;
|
||
|
if (componentLen != (next2 - src2))
|
||
|
break;
|
||
|
|
||
|
// Are the next components equal in value?
|
||
|
if (!StrCmpI(src1, src2, componentLen))
|
||
|
common = next1;
|
||
|
else
|
||
|
break;
|
||
|
|
||
|
if (!*next1)
|
||
|
break;
|
||
|
src1 = next1 + 1;
|
||
|
|
||
|
if (!*next2)
|
||
|
break;
|
||
|
src2 = next2 + 1;
|
||
|
}
|
||
|
|
||
|
if (!common)
|
||
|
return 0;
|
||
|
|
||
|
// Compute length of common subchunk;
|
||
|
// if it is "C:" convert it to "C:\"
|
||
|
unsigned commonLen = common - base;
|
||
|
if ((commonLen == 2) && (base[1] == L':'))
|
||
|
++commonLen;
|
||
|
return commonLen;
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
|
static void GetProgramName (
|
||
|
void * instance,
|
||
13 years ago
|
wchar_t * dst,
|
||
14 years ago
|
unsigned dstChars
|
||
|
) {
|
||
|
ASSERT(dst);
|
||
|
ASSERT(dstChars);
|
||
|
|
||
|
if (!GetModuleFileNameW((HINSTANCE) instance, dst, dstChars)) {
|
||
|
ErrorAssert(__LINE__, __FILE__, "GetModuleName failed");
|
||
|
*dst = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/****************************************************************************
|
||
|
*
|
||
|
* Exports
|
||
|
*
|
||
|
***/
|
||
|
|
||
|
//===========================================================================
|
||
|
void PathGetModuleName (
|
||
13 years ago
|
wchar_t * dst,
|
||
14 years ago
|
unsigned dstChars
|
||
|
) {
|
||
|
GetProgramName(ModuleGetInstance(), dst, dstChars);
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
|
void PathGetProgramName (
|
||
13 years ago
|
wchar_t * dst,
|
||
14 years ago
|
unsigned dstChars
|
||
|
) {
|
||
|
GetProgramName(nil, dst, dstChars);
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
|
bool PathFromString (
|
||
13 years ago
|
wchar_t * dst,
|
||
|
const wchar_t src[],
|
||
14 years ago
|
unsigned dstChars
|
||
|
) {
|
||
|
ASSERT(dst);
|
||
|
ASSERT(src);
|
||
|
ASSERT(dstChars);
|
||
|
|
||
|
for (;;) {
|
||
|
// enable src and dst to be the same buffer
|
||
13 years ago
|
wchar_t temp[MAX_PATH];
|
||
14 years ago
|
if (dst == src) {
|
||
|
StrCopy(temp, src, arrsize(temp));
|
||
|
src = temp;
|
||
|
}
|
||
|
|
||
|
DWORD const result = GetFullPathNameW(src, dstChars, dst, 0);
|
||
|
if (!result)
|
||
|
break;
|
||
|
if (dstChars < result)
|
||
|
break;
|
||
|
if (!dst[0])
|
||
|
break;
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
*dst = 0;
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
|
bool PathFromString (
|
||
13 years ago
|
wchar_t * dst, // ASSERT(dst);
|
||
|
const wchar_t src[], // ASSERT(src);
|
||
14 years ago
|
unsigned dstChars, // ASSERT(dstChars);
|
||
13 years ago
|
const wchar_t baseDir[] // ASSERT(baseDir);
|
||
14 years ago
|
) {
|
||
|
ASSERT(baseDir);
|
||
|
ASSERT(dstChars);
|
||
|
|
||
|
// Save current directory
|
||
13 years ago
|
wchar_t curr[MAX_PATH];
|
||
14 years ago
|
PathGetCurrentDirectory(curr, arrsize(curr));
|
||
|
|
||
|
// Perform string conversion from specified directory
|
||
|
bool result;
|
||
|
if (0 != (result = PathSetCurrentDirectory(baseDir)))
|
||
|
result = PathFromString(dst, src, dstChars);
|
||
|
else
|
||
|
*dst = 0;
|
||
|
|
||
|
// Restore directory
|
||
|
PathSetCurrentDirectory(curr);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
|
// this function was originally derived from _tsplitpath in the MSVCRT library,
|
||
|
// but has been updated to support UNC paths and to avoid blasting off the end
|
||
|
// of the buffers.
|
||
|
void PathSplitPath (
|
||
13 years ago
|
const wchar_t path[],
|
||
|
wchar_t * drive,
|
||
|
wchar_t * dir,
|
||
|
wchar_t * fname,
|
||
|
wchar_t * ext
|
||
14 years ago
|
) {
|
||
|
ASSERT(path);
|
||
|
ASSERT(path != drive);
|
||
|
ASSERT(path != dir);
|
||
|
ASSERT(path != fname);
|
||
|
ASSERT(path != ext);
|
||
|
|
||
|
// check for UNC path
|
||
|
if (IsUncPath(path)) {
|
||
13 years ago
|
const wchar_t * pathStart = path;
|
||
14 years ago
|
path = SkipUncDrive(path);
|
||
|
|
||
|
if (drive)
|
||
|
StrCopy(drive, pathStart, min(MAX_DRIVE, path - pathStart + 1));
|
||
|
}
|
||
|
// regular DOS path
|
||
|
else if (path[0] && (path[1] == L':')) {
|
||
|
if (drive) {
|
||
|
ASSERT(MAX_DRIVE >= 3);
|
||
|
drive[0] = path[0];
|
||
|
drive[1] = L':';
|
||
|
drive[2] = L'\0';
|
||
|
}
|
||
|
|
||
|
path += 2; // skip over 'C' ':'
|
||
|
}
|
||
|
else if (drive) {
|
||
|
*drive = 0;
|
||
|
}
|
||
|
|
||
|
// extract path string, if any. Path now points to the first character
|
||
|
// of the path, if any, or the filename or extension, if no path was
|
||
|
// specified. Scan ahead for the last occurence, if any, of a '/' or
|
||
|
// '\' path separator character. If none is found, there is no path.
|
||
|
// We will also note the last '.' character found, if any, to aid in
|
||
|
// handling the extension.
|
||
13 years ago
|
const wchar_t *last_slash = nil, *last_dot = nil, *p = path;
|
||
14 years ago
|
for (; *p; p++) {
|
||
|
if (IsSlash(*p))
|
||
|
last_slash = p + 1; // point to one beyond for later copy
|
||
|
else if (*p == L'.')
|
||
|
last_dot = p;
|
||
|
}
|
||
|
|
||
|
if (last_slash) {
|
||
|
if (dir)
|
||
|
StrCopy(dir, path, min(MAX_DIR, last_slash - path + 1));
|
||
|
path = last_slash;
|
||
|
}
|
||
|
else if (dir) {
|
||
|
*dir = 0;
|
||
|
}
|
||
|
|
||
|
// extract file name and extension, if any. Path now points to the
|
||
|
// first character of the file name, if any, or the extension if no
|
||
|
// file name was given. Dot points to the '.' beginning the extension,
|
||
|
// if any.
|
||
|
if (last_dot && (last_dot >= path)) {
|
||
|
if (fname)
|
||
|
StrCopy(fname, path, min(MAX_FNAME, last_dot - path + 1));
|
||
|
if (ext)
|
||
|
StrCopy(ext, last_dot, MAX_EXT);
|
||
|
}
|
||
|
else {
|
||
|
if (fname)
|
||
|
StrCopy(fname, path, MAX_FNAME);
|
||
|
if (ext)
|
||
|
*ext = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
|
void PathMakePath (
|
||
13 years ago
|
wchar_t * path,
|
||
14 years ago
|
unsigned chars,
|
||
13 years ago
|
const wchar_t drive[],
|
||
|
const wchar_t dir[],
|
||
|
const wchar_t fname[],
|
||
|
const wchar_t ext[]
|
||
14 years ago
|
) {
|
||
|
ASSERT(path);
|
||
|
ASSERT(path != drive);
|
||
|
ASSERT(path != dir);
|
||
|
ASSERT(path != fname);
|
||
|
ASSERT(path != ext);
|
||
|
|
||
|
// save space for string terminator
|
||
|
if (!chars--)
|
||
|
return;
|
||
|
|
||
|
// copy drive
|
||
|
if (drive && *drive && chars) {
|
||
|
do {
|
||
|
*path++ = ConvertSlash(*drive++);
|
||
|
} while (--chars && *drive);
|
||
|
ASSERT(!IsSlash(path[-1]));
|
||
|
}
|
||
|
|
||
|
// copy directory
|
||
|
if (dir && *dir && chars) {
|
||
|
do {
|
||
|
*path++ = ConvertSlash(*dir++);
|
||
|
} while (--chars && *dir);
|
||
|
|
||
|
// add trailing backslash
|
||
|
if (chars && (path[-1] != '\\')) {
|
||
|
*path++ = L'\\';
|
||
|
chars--;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// copy filename
|
||
|
if (fname && *fname && chars) {
|
||
|
// skip leading backslash
|
||
|
if (IsSlash(*fname))
|
||
|
++fname;
|
||
|
|
||
|
do {
|
||
|
*path++ = ConvertSlash(*fname++);
|
||
|
} while (--chars && *fname);
|
||
|
}
|
||
|
|
||
|
// copy extension
|
||
|
if (ext && *ext && chars) {
|
||
|
if (*ext != L'.') {
|
||
|
*path++ = L'.';
|
||
|
chars--;
|
||
|
}
|
||
|
while (chars-- && *ext)
|
||
|
*path++ = ConvertSlash(*ext++);
|
||
|
}
|
||
|
|
||
|
// add string terminator
|
||
|
*path = L'\0';
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
|
bool PathMakeRelative (
|
||
13 years ago
|
wchar_t *dst,
|
||
14 years ago
|
unsigned fromFlags, // 0 or kPathFlagDirectory
|
||
13 years ago
|
const wchar_t from[],
|
||
14 years ago
|
unsigned toFlags, // 0 or kPathFlagDirectory
|
||
13 years ago
|
const wchar_t to[],
|
||
14 years ago
|
unsigned dstChars
|
||
|
) {
|
||
|
ASSERT(dst);
|
||
|
ASSERT(from);
|
||
|
ASSERT(to);
|
||
|
ASSERT(dstChars);
|
||
|
*dst = 0;
|
||
|
|
||
|
unsigned prefixLength = CommonPrefixLength(from, to);
|
||
|
if (!prefixLength)
|
||
|
return false;
|
||
|
|
||
13 years ago
|
wchar_t fromBuf[MAX_PATH];
|
||
14 years ago
|
if (fromFlags & kPathFlagDirectory)
|
||
|
StrCopy(fromBuf, from, arrsize(fromBuf));
|
||
|
else
|
||
|
PathRemoveFilename(fromBuf, from, arrsize(fromBuf));
|
||
|
|
||
13 years ago
|
wchar_t toBuf[MAX_PATH];
|
||
14 years ago
|
if (toFlags & kPathFlagDirectory)
|
||
|
StrCopy(toBuf, to, arrsize(toBuf));
|
||
|
else
|
||
|
PathRemoveFilename(toBuf, to, arrsize(toBuf));
|
||
|
|
||
13 years ago
|
const wchar_t * curr = fromBuf + prefixLength;
|
||
14 years ago
|
if (*curr) {
|
||
|
// build ..\.. part of the path
|
||
|
if (IsSlash(*curr))
|
||
|
curr++; // skip slash
|
||
|
|
||
|
while (*curr) {
|
||
13 years ago
|
curr = PathSkipOverSeparator(const_cast<wchar_t *>(curr));
|
||
14 years ago
|
StrPack(dst, *curr ? L"..\\" : L"..", dstChars);
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
StrCopy(dst, L".", dstChars);
|
||
|
}
|
||
|
|
||
|
if (to[prefixLength]) {
|
||
|
// deal with root case
|
||
|
if (!IsSlash(to[prefixLength]))
|
||
|
--prefixLength;
|
||
|
|
||
|
ASSERT(IsSlash(to[prefixLength]));
|
||
|
StrPack(dst, to + prefixLength, dstChars);
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
|
bool PathIsRelative (
|
||
13 years ago
|
const wchar_t src[]
|
||
14 years ago
|
) {
|
||
|
ASSERT(src);
|
||
|
if (!src[0])
|
||
|
return true;
|
||
|
if (IsSlash(src[0]))
|
||
|
return false;
|
||
|
if (src[1] == L':')
|
||
|
return false;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
13 years ago
|
const wchar_t * PathFindFilename (
|
||
|
const wchar_t path[]
|
||
14 years ago
|
) {
|
||
|
ASSERT(path);
|
||
|
|
||
|
if (IsUncPath(path))
|
||
|
path = SkipUncDrive(path);
|
||
|
|
||
13 years ago
|
const wchar_t * last_slash = path;
|
||
|
for (const wchar_t * p = path; *p; p++) {
|
||
14 years ago
|
if ((*p == L'/') || (*p == L'\\') || (*p == L':'))
|
||
|
last_slash = p + 1;
|
||
|
}
|
||
|
|
||
|
return last_slash;
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
13 years ago
|
const wchar_t * PathFindExtension (
|
||
|
const wchar_t path[]
|
||
14 years ago
|
) {
|
||
|
ASSERT(path);
|
||
|
|
||
13 years ago
|
const wchar_t * last_dot = 0;
|
||
|
const wchar_t * p = PathFindFilename(path);
|
||
14 years ago
|
for ( ; *p; p++) {
|
||
|
if (*p == L'.')
|
||
|
last_dot = p;
|
||
|
}
|
||
|
|
||
|
return last_dot ? last_dot : p;
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
|
void PathGetCurrentDirectory (
|
||
13 years ago
|
wchar_t * dst,
|
||
14 years ago
|
unsigned dstChars
|
||
|
) {
|
||
|
ASSERT(dst);
|
||
|
ASSERT(dstChars);
|
||
|
|
||
|
DWORD result = GetCurrentDirectoryW(dstChars, dst);
|
||
|
if (!result || (result >= dstChars)) {
|
||
|
ErrorAssert(__LINE__, __FILE__, "GetDir failed");
|
||
|
*dst = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
|
void PathGetTempDirectory (
|
||
13 years ago
|
wchar_t * dst,
|
||
14 years ago
|
unsigned dstChars
|
||
|
) {
|
||
|
ASSERT(dst);
|
||
|
ASSERT(dstChars);
|
||
|
|
||
|
DWORD result = GetTempPathW(dstChars, dst);
|
||
|
if (!result || (result >= dstChars))
|
||
|
StrCopy(dst, L"C:\\temp\\", dstChars);
|
||
|
}
|
||
|
|
||
|
//============================================================================
|
||
|
void PathGetUserDirectory (
|
||
13 years ago
|
wchar_t * dst,
|
||
14 years ago
|
unsigned dstChars
|
||
|
) {
|
||
|
ASSERT(dst);
|
||
|
ASSERT(dstChars);
|
||
|
|
||
13 years ago
|
wchar_t temp[MAX_PATH]; // GetSpecialFolder path requires a buffer of MAX_PATH size or larger
|
||
14 years ago
|
if (SHGetSpecialFolderPathW(NULL, temp, CSIDL_LOCAL_APPDATA, TRUE) == FALSE)
|
||
14 years ago
|
StrCopy(temp, L"C:\\", arrsize(temp));
|
||
|
|
||
|
// append the product name
|
||
|
PathAddFilename(dst, temp, ProductLongName(), dstChars);
|
||
|
|
||
|
// ensure it exists
|
||
|
if (!PathDoesDirectoryExist(dst))
|
||
|
PathCreateDirectory(dst, kPathCreateDirFlagEntireTree);
|
||
|
}
|
||
|
|
||
|
//============================================================================
|
||
|
void PathGetLogDirectory (
|
||
13 years ago
|
wchar_t * dst,
|
||
14 years ago
|
unsigned dstChars
|
||
|
) {
|
||
|
ASSERT(dst);
|
||
|
ASSERT(dstChars);
|
||
|
PathGetUserDirectory(dst, dstChars);
|
||
|
PathAddFilename(dst, dst, L"Log", dstChars);
|
||
|
if (!PathDoesDirectoryExist(dst))
|
||
|
PathCreateDirectory(dst, kPathCreateDirFlagEntireTree);
|
||
|
}
|
||
|
|
||
|
//============================================================================
|
||
|
void PathGetInitDirectory (
|
||
13 years ago
|
wchar_t * dst,
|
||
14 years ago
|
unsigned dstChars
|
||
|
) {
|
||
|
ASSERT(dst);
|
||
|
ASSERT(dstChars);
|
||
|
PathGetUserDirectory(dst, dstChars);
|
||
|
PathAddFilename(dst, dst, L"Init", dstChars);
|
||
|
if (!PathDoesDirectoryExist(dst))
|
||
|
PathCreateDirectory(dst, kPathCreateDirFlagEntireTree);
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
|
bool PathSetCurrentDirectory (
|
||
13 years ago
|
const wchar_t path[]
|
||
14 years ago
|
) {
|
||
|
ASSERT(path);
|
||
|
return SetCurrentDirectoryW(path) != 0;
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
|
void PathSetProgramDirectory () {
|
||
13 years ago
|
wchar_t dir[MAX_PATH];
|
||
14 years ago
|
PathGetProgramDirectory(dir, arrsize(dir));
|
||
|
PathSetCurrentDirectory(dir);
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
|
void PathFindFiles (
|
||
|
ARRAY(PathFind) * paths,
|
||
13 years ago
|
const wchar_t fileSpec[],
|
||
14 years ago
|
unsigned pathFlags
|
||
|
) {
|
||
|
ASSERT(paths);
|
||
|
ASSERT(fileSpec);
|
||
|
|
||
|
HANDLE find;
|
||
|
WIN32_FIND_DATAW fd;
|
||
13 years ago
|
wchar_t directory[MAX_PATH];
|
||
14 years ago
|
PathRemoveFilename(directory, fileSpec, arrsize(directory));
|
||
|
if (INVALID_HANDLE_VALUE == (find = FindFirstFileW(fileSpec, &fd))) {
|
||
|
DWORD err = GetLastError();
|
||
|
if ((err != ERROR_FILE_NOT_FOUND) && (err != ERROR_PATH_NOT_FOUND))
|
||
|
ASSERTMSG(err, "PathFindFiles failed");
|
||
|
}
|
||
|
else {
|
||
|
// find all the items in the current directory
|
||
|
do {
|
||
|
unsigned fileFlags = 0;
|
||
|
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
||
|
if (! (pathFlags & kPathFlagDirectory))
|
||
|
continue;
|
||
|
|
||
|
// don't add "." and ".."
|
||
|
if (fd.cFileName[0] == L'.') {
|
||
|
if (!fd.cFileName[1])
|
||
|
continue;
|
||
|
if (fd.cFileName[1] == L'.' && !fd.cFileName[2])
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
fileFlags = kPathFlagDirectory;
|
||
|
}
|
||
|
else {
|
||
|
if (! (pathFlags & kPathFlagFile))
|
||
|
continue;
|
||
|
fileFlags = kPathFlagFile;
|
||
|
}
|
||
|
if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
|
||
|
if (! (pathFlags & kPathFlagHidden))
|
||
|
continue;
|
||
|
fileFlags |= kPathFlagHidden;
|
||
|
}
|
||
|
if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
|
||
|
if (! (pathFlags & kPathFlagSystem))
|
||
|
continue;
|
||
|
fileFlags |= kPathFlagSystem;
|
||
|
}
|
||
|
|
||
|
// add this one to the list of found files
|
||
|
PathFind * pf = paths->New();
|
||
|
pf->flags = fileFlags;
|
||
13 years ago
|
pf->fileLength = ((uint64_t) fd.nFileSizeHigh << 32) | fd.nFileSizeLow;
|
||
|
pf->lastWriteTime = * (const uint64_t *) &fd.ftLastWriteTime;
|
||
14 years ago
|
PathAddFilename(pf->name, directory, fd.cFileName, arrsize(pf->name));
|
||
|
} while (FindNextFileW(find, &fd));
|
||
|
FindClose(find);
|
||
|
}
|
||
|
|
||
|
// check for directory recursing
|
||
|
if ((pathFlags & kPathFlagRecurse) || StrStr(fileSpec, L"**")) {
|
||
|
// recurse directories
|
||
|
}
|
||
|
else {
|
||
|
return;
|
||
|
}
|
||
|
|
||
13 years ago
|
wchar_t dirSpec[MAX_PATH];
|
||
14 years ago
|
PathAddFilename(dirSpec, directory, L"*", arrsize(dirSpec));
|
||
|
if (INVALID_HANDLE_VALUE == (find = FindFirstFileW(dirSpec, &fd))) {
|
||
|
DWORD err = GetLastError();
|
||
|
if ((err != ERROR_FILE_NOT_FOUND) && (err != ERROR_PATH_NOT_FOUND))
|
||
|
ErrorAssert(__LINE__, __FILE__, "PathFindFiles failed");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// find all the directories in the current directory
|
||
13 years ago
|
const wchar_t * spec = PathFindFilename(fileSpec);
|
||
14 years ago
|
do {
|
||
|
if (! (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
|
||
|
continue;
|
||
|
}
|
||
|
if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
|
||
|
if (! (pathFlags & kPathFlagHidden))
|
||
|
continue;
|
||
|
}
|
||
|
if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
|
||
|
if (! (pathFlags & kPathFlagSystem))
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
// don't recurse "." and ".."
|
||
|
if (fd.cFileName[0] == L'.') {
|
||
|
if (!fd.cFileName[1])
|
||
|
continue;
|
||
|
if (fd.cFileName[1] == L'.' && !fd.cFileName[2])
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
// recursively search subdirectory
|
||
|
PathAddFilename(dirSpec, directory, fd.cFileName, arrsize(dirSpec));
|
||
|
PathAddFilename(dirSpec, dirSpec, spec, arrsize(dirSpec));
|
||
|
PathFindFiles(paths, dirSpec, pathFlags);
|
||
|
|
||
|
} while (FindNextFileW(find, &fd));
|
||
|
FindClose(find);
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
13 years ago
|
EPathCreateDirError PathCreateDirectory (const wchar_t path[], unsigned flags) {
|
||
14 years ago
|
ASSERT(path);
|
||
|
|
||
|
// convert from relative path to full path
|
||
13 years ago
|
wchar_t dir[MAX_PATH];
|
||
14 years ago
|
if (!PathFromString(dir, path, arrsize(dir))) {
|
||
|
return kPathCreateDirErrInvalidPath;
|
||
|
}
|
||
|
|
||
|
// are we going to build the entire directory tree?
|
||
13 years ago
|
wchar_t * dirEnd;
|
||
14 years ago
|
if (flags & kPathCreateDirFlagEntireTree) {
|
||
|
dirEnd = dir;
|
||
|
|
||
|
// skip over leading slashes in UNC paths
|
||
|
while (IsSlash(*dirEnd))
|
||
|
++dirEnd;
|
||
|
|
||
|
// skip forward to first directory
|
||
|
dirEnd = PathSkipOverSeparator(dirEnd);
|
||
|
}
|
||
|
// we're only creating the very last entry in the path
|
||
|
else {
|
||
|
dirEnd = dir + StrLen(dir);
|
||
|
}
|
||
|
|
||
|
bool result = true;
|
||
13 years ago
|
for (wchar_t saveChar = L' '; saveChar; *dirEnd++ = saveChar) {
|
||
14 years ago
|
// find the end of the current directory string and terminate it
|
||
|
dirEnd = PathSkipOverSeparator(dirEnd);
|
||
|
saveChar = *dirEnd;
|
||
|
*dirEnd = 0;
|
||
|
|
||
|
// create the directory and track the result from the last call
|
||
|
result = CreateDirectoryW(dir, (LPSECURITY_ATTRIBUTES) nil);
|
||
|
}
|
||
|
|
||
|
// if we successfully created the directory then we're done
|
||
|
if (result) {
|
||
|
// Avoid check for kPathCreateDirFlagOsError
|
||
|
COMPILER_ASSERT(kPathCreateDirSuccess == NO_ERROR);
|
||
|
return kPathCreateDirSuccess;
|
||
|
}
|
||
|
|
||
|
unsigned error = GetLastError();
|
||
|
switch (error) {
|
||
|
case ERROR_ACCESS_DENIED:
|
||
|
return kPathCreateDirErrAccessDenied;
|
||
|
|
||
|
case ERROR_ALREADY_EXISTS: {
|
||
|
DWORD attrib;
|
||
|
if (0xffffffff == (attrib = GetFileAttributesW(dir)))
|
||
|
return kPathCreateDirErrInvalidPath;
|
||
|
|
||
|
if (! (attrib & FILE_ATTRIBUTE_DIRECTORY))
|
||
|
return kPathCreateDirErrFileWithSameName;
|
||
|
|
||
|
if (flags & kPathCreateDirFlagCreateNew)
|
||
|
return kPathCreateDirErrDirExists;
|
||
|
}
|
||
|
return kPathCreateDirSuccess;
|
||
|
|
||
|
default:
|
||
|
return kPathCreateDirErrInvalidPath;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
13 years ago
|
void PathDeleteDirectory (const wchar_t path[], unsigned flags) {
|
||
14 years ago
|
ASSERT(path);
|
||
|
|
||
|
// convert from relative path to full path
|
||
13 years ago
|
wchar_t dir[MAX_PATH];
|
||
14 years ago
|
if (!PathFromString(dir, path, arrsize(dir)))
|
||
|
return;
|
||
|
|
||
|
for (;;) {
|
||
|
// Important: in order to ensure that we don't delete NTFS
|
||
|
// partition links, we must ensure that this is a directory!
|
||
13 years ago
|
uint32_t attributes = GetFileAttributesW(dir);
|
||
|
if (attributes == (uint32_t) -1)
|
||
14 years ago
|
break;
|
||
|
if ((attributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
|
||
|
break;
|
||
|
if (attributes & FILE_ATTRIBUTE_REPARSE_POINT)
|
||
|
break;
|
||
|
|
||
|
if (!RemoveDirectoryW(dir))
|
||
|
break;
|
||
|
|
||
|
if ((flags & kPathCreateDirFlagEntireTree) == 0)
|
||
|
break;
|
||
|
|
||
13 years ago
|
wchar_t * filename = PathFindFilename(dir);
|
||
14 years ago
|
if (!filename)
|
||
|
break;
|
||
|
|
||
|
// Move up one level in the directory hierarchy
|
||
|
unsigned oldLength = StrLen(dir);
|
||
|
while ((filename > dir) && IsSlash(filename[-1]))
|
||
|
--filename;
|
||
|
*filename = 0;
|
||
|
if (oldLength == StrLen(dir))
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
13 years ago
|
bool PathDoesFileExist (const wchar_t fileName[]) {
|
||
|
uint32_t attributes = GetFileAttributesW(fileName);
|
||
|
if (attributes == (uint32_t) -1)
|
||
14 years ago
|
return false;
|
||
|
if (attributes & FILE_ATTRIBUTE_DIRECTORY)
|
||
|
return false;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
//============================================================================
|
||
13 years ago
|
bool PathDoesDirectoryExist (const wchar_t directory[]) {
|
||
|
uint32_t attributes = GetFileAttributesW(directory);
|
||
|
if (attributes == (uint32_t) -1)
|
||
14 years ago
|
return false;
|
||
|
if (attributes & FILE_ATTRIBUTE_DIRECTORY)
|
||
|
return true;
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
|
bool PathDeleteFile (
|
||
13 years ago
|
const wchar_t file[]
|
||
14 years ago
|
) {
|
||
|
return DeleteFileW(file) != 0;
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
|
bool PathMoveFile (
|
||
13 years ago
|
const wchar_t src[],
|
||
|
const wchar_t dst[]
|
||
14 years ago
|
) {
|
||
|
return MoveFileW(src, dst) != 0;
|
||
|
}
|
||
|
|
||
|
//===========================================================================
|
||
|
bool PathCopyFile (
|
||
13 years ago
|
const wchar_t src[],
|
||
|
const wchar_t dst[]
|
||
14 years ago
|
) {
|
||
|
return CopyFileW(src, dst, FALSE) != 0;
|
||
|
}
|