Adam Johnson
12 years ago
148 changed files with 1547 additions and 4227 deletions
@ -1,585 +0,0 @@
|
||||
/*==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/>.
|
||||
|
||||
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. |
||||
|
||||
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 "../pnUtils.h" |
||||
#include "plProduct.h" |
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* |
||||
* Local functions |
||||
* |
||||
***/ |
||||
|
||||
// make sure our definition is at least as big as the compiler's definition
|
||||
static_assert(MAX_PATH >= _MAX_PATH, "Windows and STDlib MAX_PATH constants differ"); |
||||
|
||||
|
||||
//===========================================================================
|
||||
static inline bool IsSlash (wchar_t c) { |
||||
return (c == L'\\') || (c == L'/'); |
||||
} |
||||
|
||||
//===========================================================================
|
||||
static inline wchar_t ConvertSlash (wchar_t c) { |
||||
return c != L'/' ? c : L'\\'; |
||||
} |
||||
|
||||
//===========================================================================
|
||||
static inline bool IsUncPath (const wchar_t path[]) { |
||||
return IsSlash(path[0]) && IsSlash(path[1]); |
||||
} |
||||
|
||||
//===========================================================================
|
||||
static const wchar_t * SkipUncDrive (const wchar_t path[]) { |
||||
// 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; |
||||
} |
||||
} |
||||
|
||||
//===========================================================================
|
||||
static wchar_t * PathSkipOverSeparator (wchar_t * path) { |
||||
for (; *path; ++path) { |
||||
if (IsSlash(*path)) |
||||
return path + 1; |
||||
} |
||||
|
||||
return path; |
||||
} |
||||
|
||||
//===========================================================================
|
||||
const wchar_t * PathFindFilename ( |
||||
const wchar_t path[] |
||||
) { |
||||
ASSERT(path); |
||||
|
||||
if (IsUncPath(path)) |
||||
path = SkipUncDrive(path); |
||||
|
||||
const wchar_t * last_slash = path; |
||||
for (const wchar_t * p = path; *p; p++) { |
||||
if ((*p == L'/') || (*p == L'\\') || (*p == L':')) |
||||
last_slash = p + 1; |
||||
} |
||||
|
||||
return last_slash; |
||||
} |
||||
|
||||
//===========================================================================
|
||||
static void GetProgramName ( |
||||
void * instance, |
||||
wchar_t * dst, |
||||
unsigned dstChars |
||||
) { |
||||
ASSERT(dst); |
||||
ASSERT(dstChars); |
||||
|
||||
if (!GetModuleFileNameW((HINSTANCE) instance, dst, dstChars)) { |
||||
ErrorAssert(__LINE__, __FILE__, "GetModuleName failed"); |
||||
*dst = 0; |
||||
} |
||||
} |
||||
|
||||
//============================================================================
|
||||
bool PathDoesDirectoryExist (const wchar_t directory[]) { |
||||
uint32_t attributes = GetFileAttributesW(directory); |
||||
if (attributes == (uint32_t) -1) |
||||
return false; |
||||
if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
||||
return true; |
||||
return false; |
||||
} |
||||
|
||||
|
||||
/****************************************************************************
|
||||
* |
||||
* Exports |
||||
* |
||||
***/ |
||||
|
||||
//===========================================================================
|
||||
void PathGetProgramName ( |
||||
wchar_t * dst, |
||||
unsigned dstChars |
||||
) { |
||||
GetProgramName(nil, dst, dstChars); |
||||
} |
||||
|
||||
//===========================================================================
|
||||
bool PathFromString ( |
||||
wchar_t * dst,
|
||||
const wchar_t src[],
|
||||
unsigned dstChars |
||||
) { |
||||
ASSERT(dst); |
||||
ASSERT(src); |
||||
ASSERT(dstChars); |
||||
|
||||
for (;;) { |
||||
// enable src and dst to be the same buffer
|
||||
wchar_t temp[MAX_PATH]; |
||||
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; |
||||
} |
||||
|
||||
//===========================================================================
|
||||
// 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 ( |
||||
const wchar_t path[], |
||||
wchar_t * drive, |
||||
wchar_t * dir, |
||||
wchar_t * fname, |
||||
wchar_t * ext |
||||
) { |
||||
ASSERT(path); |
||||
ASSERT(path != drive); |
||||
ASSERT(path != dir); |
||||
ASSERT(path != fname); |
||||
ASSERT(path != ext); |
||||
|
||||
// check for UNC path
|
||||
if (IsUncPath(path)) { |
||||
const wchar_t * pathStart = path; |
||||
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.
|
||||
const wchar_t *last_slash = nil, *last_dot = nil, *p = path; |
||||
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 ( |
||||
wchar_t * path, |
||||
unsigned chars, |
||||
const wchar_t drive[], |
||||
const wchar_t dir[], |
||||
const wchar_t fname[], |
||||
const wchar_t ext[] |
||||
) { |
||||
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'; |
||||
} |
||||
|
||||
//===========================================================================
|
||||
void PathGetUserDirectory ( |
||||
wchar_t * dst, |
||||
unsigned dstChars |
||||
) { |
||||
ASSERT(dst); |
||||
ASSERT(dstChars); |
||||
|
||||
wchar_t temp[MAX_PATH]; // GetSpecialFolder path requires a buffer of MAX_PATH size or larger
|
||||
if (SHGetSpecialFolderPathW(NULL, temp, CSIDL_LOCAL_APPDATA, TRUE) == FALSE) |
||||
StrCopy(temp, L"C:\\", arrsize(temp)); |
||||
|
||||
// append the product name
|
||||
PathAddFilename(dst, temp, plProduct::LongName().ToWchar(), dstChars); |
||||
|
||||
// ensure it exists
|
||||
if (!PathDoesDirectoryExist(dst)) |
||||
PathCreateDirectory(dst, kPathCreateDirFlagEntireTree); |
||||
} |
||||
|
||||
//============================================================================
|
||||
void PathGetLogDirectory ( |
||||
wchar_t * dst, |
||||
unsigned dstChars |
||||
) { |
||||
ASSERT(dst); |
||||
ASSERT(dstChars); |
||||
PathGetUserDirectory(dst, dstChars); |
||||
PathAddFilename(dst, dst, L"Log", dstChars); |
||||
if (!PathDoesDirectoryExist(dst)) |
||||
PathCreateDirectory(dst, kPathCreateDirFlagEntireTree); |
||||
} |
||||
|
||||
//============================================================================
|
||||
void PathGetInitDirectory ( |
||||
wchar_t * dst, |
||||
unsigned dstChars |
||||
) { |
||||
ASSERT(dst); |
||||
ASSERT(dstChars); |
||||
PathGetUserDirectory(dst, dstChars); |
||||
PathAddFilename(dst, dst, L"Init", dstChars); |
||||
if (!PathDoesDirectoryExist(dst)) |
||||
PathCreateDirectory(dst, kPathCreateDirFlagEntireTree); |
||||
} |
||||
|
||||
//===========================================================================
|
||||
void PathFindFiles ( |
||||
ARRAY(PathFind) * paths, |
||||
const wchar_t fileSpec[], |
||||
unsigned pathFlags |
||||
) { |
||||
ASSERT(paths); |
||||
ASSERT(fileSpec); |
||||
|
||||
HANDLE find; |
||||
WIN32_FIND_DATAW fd; |
||||
wchar_t directory[MAX_PATH]; |
||||
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; |
||||
pf->fileLength = ((uint64_t) fd.nFileSizeHigh << 32) | fd.nFileSizeLow; |
||||
pf->lastWriteTime = * (const uint64_t *) &fd.ftLastWriteTime; |
||||
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; |
||||
} |
||||
|
||||
wchar_t dirSpec[MAX_PATH]; |
||||
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
|
||||
const wchar_t * spec = PathFindFilename(fileSpec); |
||||
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); |
||||
} |
||||
|
||||
//===========================================================================
|
||||
EPathCreateDirError PathCreateDirectory (const wchar_t path[], unsigned flags) { |
||||
ASSERT(path); |
||||
|
||||
// convert from relative path to full path
|
||||
wchar_t dir[MAX_PATH]; |
||||
if (!PathFromString(dir, path, arrsize(dir))) { |
||||
return kPathCreateDirErrInvalidPath; |
||||
} |
||||
|
||||
// are we going to build the entire directory tree?
|
||||
wchar_t * dirEnd; |
||||
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; |
||||
for (wchar_t saveChar = L' '; saveChar; *dirEnd++ = saveChar) { |
||||
// 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
|
||||
static_assert(kPathCreateDirSuccess == NO_ERROR, "Path creation success and NO_ERROR constants differ"); |
||||
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; |
||||
} |
||||
} |
||||
|
||||
//===========================================================================
|
||||
bool PathDoesFileExist (const wchar_t fileName[]) { |
||||
uint32_t attributes = GetFileAttributesW(fileName); |
||||
if (attributes == (uint32_t) -1) |
||||
return false; |
||||
if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
||||
return false; |
||||
return true; |
||||
} |
||||
|
@ -1,214 +0,0 @@
|
||||
/*==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/>.
|
||||
|
||||
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. |
||||
|
||||
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/pnUtPath.cpp |
||||
*
|
||||
***/ |
||||
|
||||
#include "pnUtPath.h" |
||||
#include "pnUtStr.h" |
||||
|
||||
|
||||
/****************************************************************************
|
||||
* |
||||
* Exported functions |
||||
* |
||||
***/ |
||||
|
||||
//===========================================================================
|
||||
void PathGetProgramDirectory ( |
||||
wchar_t *dst, |
||||
unsigned dstChars |
||||
) { |
||||
ASSERT(dst); |
||||
ASSERT(dstChars); |
||||
|
||||
PathGetProgramName(dst, dstChars); |
||||
PathRemoveFilename(dst, dst, dstChars); |
||||
} |
||||
|
||||
//===========================================================================
|
||||
void PathAddFilename ( |
||||
wchar_t *dst,
|
||||
const wchar_t src[],
|
||||
const wchar_t fname[], |
||||
unsigned dstChars |
||||
) { |
||||
ASSERT(dst); |
||||
ASSERT(dstChars); |
||||
|
||||
wchar_t temp[MAX_PATH]; |
||||
if (dst == src) { |
||||
StrCopy(temp, src, arrsize(temp)); |
||||
src = temp; |
||||
} |
||||
else if (dst == fname) { |
||||
StrCopy(temp, fname, arrsize(temp)); |
||||
fname = temp; |
||||
} |
||||
|
||||
PathMakePath(dst, dstChars, 0, src, fname, 0); |
||||
} |
||||
|
||||
//===========================================================================
|
||||
void PathRemoveFilename ( |
||||
wchar_t *dst,
|
||||
const wchar_t src[],
|
||||
unsigned dstChars |
||||
) { |
||||
ASSERT(dst); |
||||
ASSERT(src); |
||||
ASSERT(dstChars); |
||||
|
||||
wchar_t drive[MAX_DRIVE]; |
||||
wchar_t dir[MAX_DIR]; |
||||
PathSplitPath(src, drive, dir, 0, 0); |
||||
PathMakePath(dst, dstChars, drive, dir, 0, 0); |
||||
} |
||||
|
||||
/*****************************************************************************
|
||||
* |
||||
* Email formatting functions |
||||
* |
||||
***/ |
||||
|
||||
//============================================================================
|
||||
void PathSplitEmail ( |
||||
const wchar_t emailAddr[], |
||||
wchar_t * user, |
||||
unsigned userChars, |
||||
wchar_t * domain, |
||||
unsigned domainChars, |
||||
wchar_t * tld, |
||||
unsigned tldChars, |
||||
wchar_t * subDomains, |
||||
unsigned subDomainChars, |
||||
unsigned subDomainCount |
||||
) { |
||||
ASSERT(emailAddr); |
||||
|
||||
#define SUB_DOMAIN(i) subDomains[(i) * subDomainChars] |
||||
|
||||
// null-terminate all output parameters
|
||||
if (userChars) { |
||||
ASSERT(user); |
||||
user[0] = 0; |
||||
} |
||||
if (domainChars) { |
||||
ASSERT(domain); |
||||
domain[0] = 0; |
||||
} |
||||
if (tldChars) { |
||||
ASSERT(tld); |
||||
tld[0] = 0; |
||||
} |
||||
if (subDomainChars || subDomainCount) { |
||||
ASSERT(subDomains); |
||||
for (unsigned i = 0; i < subDomainCount; ++i) |
||||
SUB_DOMAIN(i) = 0; |
||||
} |
||||
|
||||
// bail now if email address is zero-length
|
||||
unsigned len = StrLen(emailAddr); |
||||
if (!len) |
||||
return; |
||||
|
||||
// copy email address so we can tokenize it
|
||||
wchar_t * tmp = (wchar_t*)malloc(sizeof(wchar_t) * (len + 1)); |
||||
StrCopy(tmp, emailAddr, len + 1); |
||||
const wchar_t * work = tmp; |
||||
|
||||
// parse user
|
||||
wchar_t token[MAX_PATH]; |
||||
if (!StrTokenize(&work, token, arrsize(token), L"@")) |
||||
return; |
||||
|
||||
// copy user to output parameter
|
||||
if (userChars) |
||||
StrCopy(user, token, userChars); |
||||
|
||||
// skip past the '@' symbol
|
||||
if (!*work++) |
||||
return; |
||||
|
||||
// parse all domains
|
||||
ARRAY(wchar_t *) arr; |
||||
while (StrTokenize(&work, token, arrsize(token), L".")) { |
||||
unsigned toklen = StrLen(token); |
||||
wchar_t * str = (wchar_t*)malloc(sizeof(wchar_t) * (toklen + 1)); |
||||
StrCopy(str, token, toklen + 1); |
||||
arr.Add(str); |
||||
|
||||
free(str); |
||||
} |
||||
|
||||
// copy domains to output parameters
|
||||
unsigned index = 0; |
||||
if (arr.Count() > 2) { |
||||
// all domains except for the last two are sub-domains
|
||||
for (index = 0; index < arr.Count() - 2; ++index) { |
||||
if (index < subDomainCount) |
||||
if (subDomains) |
||||
StrCopy(&SUB_DOMAIN(index), arr[index], subDomainChars); |
||||
} |
||||
} |
||||
if (arr.Count() > 1) { |
||||
// second to last domain is the primary domain
|
||||
if (domain) |
||||
StrCopy(domain, arr[index], domainChars); |
||||
// last comes the top level domain
|
||||
++index; |
||||
if (tld) |
||||
StrCopy(tld, arr[index], tldChars); |
||||
} |
||||
else if (arr.Count() == 1) { |
||||
// if only one domain, return it as a sub-domain
|
||||
if (index < subDomainCount) |
||||
if (subDomains) |
||||
StrCopy(&SUB_DOMAIN(index), arr[index], subDomainChars); |
||||
} |
||||
|
||||
free(tmp); |
||||
|
||||
#undef SUB_DOMAIN |
||||
} |
@ -1,219 +0,0 @@
|
||||
/*==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/>.
|
||||
|
||||
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. |
||||
|
||||
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/pnUtPath.h |
||||
*
|
||||
***/ |
||||
|
||||
#ifndef PLASMA20_SOURCES_PLASMA_NUCLEUSLIB_PNUTILS_PRIVATE_PNUTPATH_H |
||||
#define PLASMA20_SOURCES_PLASMA_NUCLEUSLIB_PNUTILS_PRIVATE_PNUTPATH_H |
||||
|
||||
#include "Pch.h" |
||||
#include "pnUtArray.h" |
||||
|
||||
/*****************************************************************************
|
||||
* |
||||
* Path definitions |
||||
* |
||||
***/ |
||||
|
||||
#ifndef MAX_PATH |
||||
#define MAX_PATH 260 |
||||
#endif |
||||
|
||||
#define MAX_DRIVE 256 |
||||
#define MAX_DIR 256 |
||||
#define MAX_FNAME 256 |
||||
#define MAX_EXT 256 |
||||
|
||||
|
||||
|
||||
const unsigned kPathFlagFile = 1<<0; |
||||
const unsigned kPathFlagDirectory = 1<<1; |
||||
const unsigned kPathFlagHidden = 1<<2; |
||||
const unsigned kPathFlagSystem = 1<<3; |
||||
const unsigned kPathFlagRecurse = 1<<4; // also set if "**" used in filespec
|
||||
|
||||
struct PathFind { |
||||
unsigned flags; |
||||
uint64_t fileLength; |
||||
uint64_t lastWriteTime; |
||||
wchar_t name[MAX_PATH]; |
||||
}; |
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* |
||||
* Path "get" functions |
||||
* |
||||
***/ |
||||
|
||||
void PathFindFiles ( |
||||
ARRAY(PathFind) * paths, |
||||
const wchar_t fileSpec[], |
||||
unsigned pathFlags |
||||
); |
||||
void PathGetProgramName ( |
||||
wchar_t * dst, |
||||
unsigned dstChars |
||||
); |
||||
|
||||
bool PathDoesFileExist ( |
||||
const wchar_t fileName[] |
||||
); |
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* |
||||
* Path building functions |
||||
* |
||||
***/ |
||||
|
||||
void PathSplitPath ( |
||||
const wchar_t path[], |
||||
wchar_t * drive, |
||||
wchar_t * dir, |
||||
wchar_t * fname, |
||||
wchar_t * ext |
||||
); |
||||
|
||||
void PathMakePath ( |
||||
wchar_t * path, |
||||
unsigned chars, |
||||
const wchar_t drive[], |
||||
const wchar_t dir[], |
||||
const wchar_t fname[], |
||||
const wchar_t ext[] |
||||
); |
||||
|
||||
// c:\dir1 + dir2\file.txt => c:\dir1\dir2\file.txt
|
||||
void PathAddFilename ( |
||||
wchar_t * dst, |
||||
const wchar_t src[],
|
||||
const wchar_t fname[], |
||||
unsigned dstChars |
||||
); |
||||
|
||||
// c:\dir1\dir2\file.txt => c:\dir1\dir2\ * note trailing backslash
|
||||
void PathRemoveFilename ( |
||||
wchar_t * dst, |
||||
const wchar_t src[], |
||||
unsigned dstChars |
||||
); |
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* |
||||
* Directory functions |
||||
* |
||||
***/ |
||||
|
||||
// Create directory
|
||||
enum EPathCreateDirError { |
||||
kPathCreateDirSuccess, |
||||
kPathCreateDirErrInvalidPath, |
||||
kPathCreateDirErrAccessDenied, |
||||
kPathCreateDirErrFileWithSameName, |
||||
kPathCreateDirErrDirExists, // Directory exists and kPathCreateDirFlagCreateNew was specified
|
||||
}; |
||||
|
||||
// Setting this flag causes the function to create the entire directory
|
||||
// tree from top to bottom. Clearing this flag causes the function to
|
||||
// create only the last entry in the path.
|
||||
const unsigned kPathCreateDirFlagEntireTree = 1<<0; |
||||
|
||||
// Setting this flag causes the function to create the last entry in the path
|
||||
// ONLY if it doesn't already exist. If it does exist the function will return
|
||||
// kPathCreateDirErrDirExistes.
|
||||
const unsigned kPathCreateDirFlagCreateNew = 1<<1; |
||||
|
||||
|
||||
EPathCreateDirError PathCreateDirectory ( |
||||
const wchar_t path[], |
||||
unsigned flags |
||||
); |
||||
|
||||
|
||||
|
||||
// Get directory
|
||||
void PathGetProgramDirectory ( |
||||
wchar_t * dst, |
||||
unsigned dstChars |
||||
); |
||||
|
||||
|
||||
// Product and user-specific common directory locations
|
||||
void PathGetUserDirectory ( |
||||
wchar_t * dst, |
||||
unsigned dstChars |
||||
); |
||||
void PathGetLogDirectory ( |
||||
wchar_t * dst, |
||||
unsigned dstChars |
||||
); |
||||
void PathGetInitDirectory ( |
||||
wchar_t * dst, |
||||
unsigned dstChars |
||||
); |
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* |
||||
* Email formatting functions |
||||
* |
||||
***/ |
||||
|
||||
// you may send nil for any fields you don't care about
|
||||
void PathSplitEmail ( |
||||
const wchar_t emailAddr[], |
||||
wchar_t * user, |
||||
unsigned userChars, |
||||
wchar_t * domain, |
||||
unsigned domainChars, |
||||
wchar_t * tld, |
||||
unsigned tldChars, |
||||
wchar_t * subDomains, // (wchar_t *)&subs --> wchar_t subs[16][256];
|
||||
unsigned subDomainChars, // arrsize(subs[0]) --> 256
|
||||
unsigned subDomainCount // arrsize(subs) --> 16
|
||||
); |
||||
#endif |
@ -1,182 +0,0 @@
|
||||
/*==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/>.
|
||||
|
||||
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. |
||||
|
||||
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==*/ |
||||
#include "hsFiles.h" |
||||
#include <string.h> |
||||
|
||||
|
||||
#include "hsExceptions.h" |
||||
|
||||
#if HS_BUILD_FOR_WIN32 |
||||
#define kDirChar '\\' |
||||
#else |
||||
#define kDirChar '/' |
||||
#endif |
||||
|
||||
|
||||
static const char* FindNameInPath(const char path[]) |
||||
{ |
||||
const char* name = ::strrchr(path, kDirChar); |
||||
|
||||
if (name == nil) |
||||
name = path; |
||||
return name; |
||||
} |
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
|
||||
hsFile::hsFile() : fPathAndName(nil), fFILE(nil) |
||||
{ |
||||
} |
||||
|
||||
hsFile::hsFile(const char pathAndName[]) : fPathAndName(nil), fFILE(nil) |
||||
{ |
||||
if (pathAndName) |
||||
fPathAndName = hsStrcpy(pathAndName); |
||||
} |
||||
|
||||
hsFile::~hsFile() |
||||
{ |
||||
this->SetPathAndName(nil); |
||||
} |
||||
|
||||
const char* hsFile::GetPathAndName() |
||||
{ |
||||
return fPathAndName; |
||||
} |
||||
|
||||
void hsFile::SetPathAndName(const char pathAndName[]) |
||||
{ |
||||
this->Close(); |
||||
|
||||
if (fPathAndName) |
||||
{ delete[] fPathAndName; |
||||
fPathAndName = nil; |
||||
} |
||||
if (pathAndName) |
||||
fPathAndName = hsStrcpy(pathAndName); |
||||
} |
||||
|
||||
const char* hsFile::GetName() |
||||
{ |
||||
return FindNameInPath(this->GetPathAndName()); |
||||
} |
||||
|
||||
FILE* hsFile::OpenFILE(const char mode[], bool throwIfFailure) |
||||
{ |
||||
this->Close(); |
||||
|
||||
// We call the virtual method here rather than using
|
||||
// fPathAndName directly, allowing a subclass to construct
|
||||
// the name if necessary
|
||||
//
|
||||
const char* name = this->GetPathAndName(); |
||||
if (name) |
||||
fFILE = ::fopen(name, mode); |
||||
|
||||
hsThrowIfTrue(throwIfFailure && fFILE == nil); |
||||
return fFILE; |
||||
} |
||||
|
||||
hsStream* hsFile::OpenStream(const char mode[], bool throwIfFailure) |
||||
{ |
||||
FILE* file = this->OpenFILE(mode, throwIfFailure); |
||||
|
||||
if (file) |
||||
{ hsUNIXStream* stream = new hsUNIXStream; |
||||
stream->SetFILE(file); |
||||
return stream; |
||||
} |
||||
return nil; |
||||
} |
||||
|
||||
void hsFile::Close() |
||||
{ |
||||
if (fFILE) |
||||
{ int err = ::fflush(fFILE); |
||||
hsIfDebugMessage(err != 0, "fflush failed", err); |
||||
err = ::fclose(fFILE); |
||||
hsIfDebugMessage(err != 0, "fclose failed", err); |
||||
fFILE = nil; |
||||
} |
||||
} |
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
|
||||
bool hsFolderIterator::NextFileSuffix(const char suffix[]) |
||||
{ |
||||
while (this->NextFile()) |
||||
{ const char* fileSuffix = ::strrchr(this->GetFileName(), '.'); |
||||
if (fileSuffix != nil && stricmp(fileSuffix, suffix) == 0) |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
int hsFolderIterator::GetPathAndName(char pathandname[]) |
||||
{ |
||||
hsAssert(pathandname, "NULL path string"); |
||||
const char* name = this->GetFileName(); |
||||
int pathLen = strlen(fPath); |
||||
|
||||
// add 1 for null terminator
|
||||
int totalLen = pathLen + sizeof(kDirChar) + strlen(name) + 1; |
||||
hsAssert(totalLen <= kFolderIterator_MaxPath, "Overrun kFolderIterator_MaxPath"); |
||||
|
||||
if (pathandname) |
||||
{ |
||||
strcpy(pathandname, fPath); |
||||
if (pathLen > 0 && pathandname[pathLen - 1] != kDirChar) |
||||
pathandname[pathLen++] = kDirChar; |
||||
strcpy(pathandname + pathLen, name); |
||||
} |
||||
return totalLen; |
||||
} |
||||
|
||||
FILE* hsFolderIterator::OpenFILE(const char mode[]) |
||||
{ |
||||
char fileName[kFolderIterator_MaxPath]; |
||||
|
||||
(void)this->GetPathAndName(fileName); |
||||
|
||||
return ::fopen(fileName, mode); |
||||
} |
||||
|
@ -1,141 +0,0 @@
|
||||
/*==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/>.
|
||||
|
||||
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. |
||||
|
||||
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==*/ |
||||
#ifndef hsFiles_Defined |
||||
#define hsFiles_Defined |
||||
|
||||
#include "hsStream.h" |
||||
#include <stdio.h> |
||||
|
||||
#if HS_BUILD_FOR_UNIX |
||||
#include <limits.h> |
||||
#define kFolderIterator_MaxPath PATH_MAX |
||||
#define SetCurrentDirectory chdir |
||||
#else |
||||
#define kFolderIterator_MaxPath _MAX_PATH |
||||
#endif |
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
|
||||
class hsFile { |
||||
hsFile& operator=(const hsFile&); // disallow assignment
|
||||
protected: |
||||
char* fPathAndName; |
||||
FILE* fFILE; |
||||
public: |
||||
hsFile(); |
||||
hsFile(const char pathAndName[]); |
||||
virtual ~hsFile(); |
||||
|
||||
const char* GetName(); |
||||
virtual const char* GetPathAndName(); |
||||
virtual void SetPathAndName(const char pathAndName[]); |
||||
|
||||
virtual FILE* OpenFILE(const char mode[], bool throwIfFailure = false); |
||||
virtual hsStream* OpenStream(const char mode[], bool throwIfFailure = false); |
||||
|
||||
virtual void Close(); // called automatically in the destructor
|
||||
}; |
||||
typedef hsFile hsUnixFile; // for compatibility
|
||||
typedef hsFile hsOSFile; |
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
|
||||
class hsFolderIterator { |
||||
char fPath[kFolderIterator_MaxPath]; |
||||
struct hsFolderIterator_Data* fData; |
||||
bool fCustomFilter; |
||||
public: |
||||
#ifdef HS_BUILD_FOR_WIN32 |
||||
hsFolderIterator(const char path[] = nil, bool useCustomFilter=false); |
||||
#else |
||||
hsFolderIterator(const char path[] = nil, bool unused=true); |
||||
hsFolderIterator(const struct FSSpec* spec); // Alt constructor
|
||||
#endif |
||||
virtual ~hsFolderIterator(); |
||||
|
||||
const char* GetPath() const { return fPath; } |
||||
void SetPath(const char path[]); |
||||
|
||||
void Reset(); |
||||
bool NextFile(); |
||||
bool NextFileSuffix(const char suffix[]); |
||||
const char* GetFileName() const; |
||||
int GetPathAndName(char pathandname[] = nil); |
||||
bool IsDirectory( void ) const; |
||||
|
||||
FILE* OpenFILE(const char mode[]); |
||||
|
||||
#if HS_BUILD_FOR_WIN32 |
||||
void SetWinSystemDir(const char subdir[]); // e.g. "Fonts"
|
||||
void SetFileFilterStr(const char filterStr[]); // e.g. "*.*"
|
||||
#endif |
||||
}; |
||||
|
||||
#ifdef HS_BUILD_FOR_WIN32 |
||||
// only implemented on Win32 for now
|
||||
class hsWFolderIterator { |
||||
wchar_t fPath[kFolderIterator_MaxPath]; |
||||
struct hsWFolderIterator_Data* fData; |
||||
bool fCustomFilter; |
||||
public: |
||||
hsWFolderIterator(const wchar_t path[] = nil, bool useCustomFilter=false); |
||||
virtual ~hsWFolderIterator(); |
||||
|
||||
const wchar_t* GetPath() const { return fPath; } |
||||
void SetPath(const wchar_t path[]); |
||||
|
||||
void Reset(); |
||||
bool NextFile(); |
||||
bool NextFileSuffix(const wchar_t suffix[]); |
||||
const wchar_t* GetFileName() const; |
||||
int GetPathAndName(wchar_t pathandname[] = nil); |
||||
bool IsDirectory( void ) const; |
||||
|
||||
FILE* OpenFILE(const wchar_t mode[]); |
||||
|
||||
void SetWinSystemDir(const wchar_t subdir[]); // e.g. "Fonts"
|
||||
void SetFileFilterStr(const wchar_t filterStr[]); // e.g. "*.*"
|
||||
}; |
||||
#endif |
||||
|
||||
#endif |
@ -1,146 +0,0 @@
|
||||
/*==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/>.
|
||||
|
||||
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. |
||||
|
||||
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==*/ |
||||
#include "hsFiles.h" |
||||
|
||||
#if HS_BUILD_FOR_UNIX |
||||
|
||||
#include <errno.h> |
||||
#include <dirent.h> |
||||
#include <limits.h> |
||||
#include <sys/stat.h> |
||||
#include <sys/types.h> |
||||
#include <unistd.h> |
||||
#include <glob.h> |
||||
#include <string> |
||||
#include "hsTemplates.h" |
||||
#include "plFileUtils.h" |
||||
|
||||
struct hsFolderIterator_Data { |
||||
glob_t fGlobBuf; |
||||
bool fInited; |
||||
int fCnt; |
||||
hsFolderIterator_Data() : fInited(false), fCnt(0) {} |
||||
// ~hsFolderIterator_Data() { fInited=false; globfree(&fData->fGlobBuf); }
|
||||
}; |
||||
|
||||
hsFolderIterator::hsFolderIterator(const char path[], bool) |
||||
{ |
||||
fData = new hsFolderIterator_Data; |
||||
|
||||
this->SetPath(path); |
||||
} |
||||
|
||||
hsFolderIterator::~hsFolderIterator() |
||||
{ |
||||
this->Reset(); |
||||
delete fData; |
||||
} |
||||
|
||||
void hsFolderIterator::SetPath(const char path[]) |
||||
{ |
||||
fPath[0] = 0; |
||||
if (path) |
||||
{ |
||||
::strcpy(fPath, path); |
||||
} |
||||
this->Reset(); |
||||
} |
||||
|
||||
void hsFolderIterator::Reset() |
||||
{ |
||||
if (fData->fInited) |
||||
{ |
||||
globfree(&fData->fGlobBuf); |
||||
fData->fCnt = 0; |
||||
fData->fInited=false; |
||||
} |
||||
} |
||||
bool hsFolderIterator::NextFile() |
||||
{ |
||||
if (fData->fInited == false) |
||||
{
|
||||
std::string path=fPath; |
||||
if(!(strchr(fPath,'*') || strchr(fPath,'?') || strchr(fPath,'['))) |
||||
{ |
||||
if (fPath[strlen(fPath)-1] != PATH_SEPARATOR) |
||||
path = path + PATH_SEPARATOR_STR + "*"; |
||||
else |
||||
path = path + "*"; |
||||
} |
||||
|
||||
if(glob(path.c_str(), 0, NULL, &fData->fGlobBuf) != 0 ) { |
||||
return false; |
||||
} |
||||
fData->fInited=true; |
||||
fData->fCnt = 0; |
||||
} |
||||
|
||||
return fData->fCnt++ < fData->fGlobBuf.gl_pathc; |
||||
} |
||||
|
||||
const char* hsFolderIterator::GetFileName() const |
||||
{ |
||||
if (!fData->fInited || fData->fCnt > fData->fGlobBuf.gl_pathc) |
||||
throw "end of folder"; |
||||
|
||||
const char* fn=fData->fGlobBuf.gl_pathv[fData->fCnt-1]; |
||||
return plFileUtils::GetFileName(fn); |
||||
} |
||||
|
||||
bool hsFolderIterator::IsDirectory( void ) const |
||||
{ |
||||
// rob, please forgive me, this is my best attempt...
|
||||
if(fData->fCnt > fData->fGlobBuf.gl_pathc ) |
||||
return false; |
||||
|
||||
struct stat info; |
||||
const char* fn=fData->fGlobBuf.gl_pathv[fData->fCnt-1]; |
||||
if( stat( fn, &info ) ) |
||||
{ |
||||
printf("Error calling stat(): %s errno=%d\n", strerror(errno), errno); |
||||
return false; |
||||
} |
||||
return ( info.st_mode & S_IFDIR ) ? true : false; |
||||
} |
||||
|
||||
#endif |
||||
|
@ -1,320 +0,0 @@
|
||||
/*==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/>.
|
||||
|
||||
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. |
||||
|
||||
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==*/ |
||||
#include "hsFiles.h" |
||||
#include "HeadSpin.h" |
||||
#include "hsWindows.h" |
||||
|
||||
#if HS_BUILD_FOR_WIN32 |
||||
|
||||
#include "hsExceptions.h" |
||||
|
||||
struct hsFolderIterator_Data { |
||||
HANDLE fSearchHandle; |
||||
WIN32_FIND_DATA fFindData; |
||||
bool fValid; |
||||
}; |
||||
|
||||
hsFolderIterator::hsFolderIterator(const char path[], bool useCustomFilter) |
||||
{ |
||||
fCustomFilter = useCustomFilter; |
||||
|
||||
fData = new hsFolderIterator_Data; |
||||
fData->fSearchHandle = nil; |
||||
fData->fValid = true; |
||||
|
||||
if(useCustomFilter) |
||||
{ |
||||
this->SetFileFilterStr(path); |
||||
} |
||||
else |
||||
{ |
||||
this->SetPath(path); |
||||
} |
||||
} |
||||
|
||||
hsFolderIterator::~hsFolderIterator() |
||||
{ |
||||
delete fData; |
||||
} |
||||
|
||||
void hsFolderIterator::SetPath(const char path[]) |
||||
{ |
||||
fCustomFilter = false; |
||||
fPath[0] = 0; |
||||
if (path) |
||||
{ |
||||
::strcpy(fPath, path); |
||||
|
||||
// Make sure the dir ends with a slash
|
||||
char lastchar = fPath[strlen(fPath)-1]; |
||||
if (lastchar != '\\' && lastchar != '/') |
||||
strcat(fPath, "\\"); |
||||
} |
||||
|
||||
this->Reset(); |
||||
} |
||||
|
||||
void hsFolderIterator::SetWinSystemDir(const char subdir[]) |
||||
{ |
||||
int ret = GetWindowsDirectory(fPath, _MAX_PATH); |
||||
hsAssert(ret != 0, "Error getting windows directory in UseWindowsFontsPath"); |
||||
|
||||
if (subdir) |
||||
{ ::strcat(fPath, "\\"); |
||||
::strcat(fPath, subdir); |
||||
::strcat(fPath, "\\"); |
||||
} |
||||
this->Reset(); |
||||
} |
||||
|
||||
void hsFolderIterator::SetFileFilterStr(const char filterStr[]) |
||||
{ |
||||
fPath[0] = 0; |
||||
if (filterStr) |
||||
{ |
||||
fCustomFilter = true; |
||||
::strcpy(fPath, filterStr); |
||||
} |
||||
|
||||
this->Reset(); |
||||
} |
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void hsFolderIterator::Reset() |
||||
{ |
||||
if (fData->fSearchHandle) |
||||
{ FindClose(fData->fSearchHandle); |
||||
fData->fSearchHandle = nil; |
||||
} |
||||
fData->fValid = true; |
||||
} |
||||
|
||||
bool hsFolderIterator::NextFile() |
||||
{ |
||||
if (fData->fValid == false) |
||||
return false; |
||||
|
||||
if (fData->fSearchHandle == nil) |
||||
{ int len = ::strlen(fPath); |
||||
|
||||
if(fCustomFilter == false) |
||||
{ |
||||
fPath[len] = '*'; |
||||
fPath[len+1] = 0; |
||||
} |
||||
|
||||
fData->fSearchHandle = FindFirstFile(fPath, &fData->fFindData); |
||||
fPath[len] = 0; |
||||
|
||||
if (fData->fSearchHandle == INVALID_HANDLE_VALUE) |
||||
{ fData->fSearchHandle = nil; |
||||
fData->fValid = false; |
||||
return false; |
||||
} |
||||
} |
||||
else |
||||
{ if (FindNextFile(fData->fSearchHandle, &fData->fFindData) == false) |
||||
{ FindClose(fData->fSearchHandle); |
||||
fData->fSearchHandle = nil; |
||||
fData->fValid = false; |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
|
||||
bool hsFolderIterator::IsDirectory( void ) const |
||||
{ |
||||
if( fData->fValid && ( fData->fFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) ) |
||||
return true; |
||||
|
||||
return false; |
||||
} |
||||
|
||||
const char* hsFolderIterator::GetFileName() const |
||||
{ |
||||
if (fData->fValid == false) |
||||
hsThrow( "end of folder"); |
||||
|
||||
return fData->fFindData.cFileName; |
||||
} |
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct hsWFolderIterator_Data { |
||||
HANDLE fSearchHandle; |
||||
WIN32_FIND_DATAW fFindData; |
||||
bool fValid; |
||||
}; |
||||
|
||||
hsWFolderIterator::hsWFolderIterator(const wchar_t path[], bool useCustomFilter) |
||||
{ |
||||
fCustomFilter = useCustomFilter; |
||||
|
||||
fData = new hsWFolderIterator_Data; |
||||
fData->fSearchHandle = nil; |
||||
fData->fValid = true; |
||||
|
||||
if(useCustomFilter) |
||||
SetFileFilterStr(path); |
||||
else |
||||
SetPath(path); |
||||
} |
||||
|
||||
hsWFolderIterator::~hsWFolderIterator() |
||||
{ |
||||
delete fData; |
||||
} |
||||
|
||||
void hsWFolderIterator::SetPath(const wchar_t path[]) |
||||
{ |
||||
fCustomFilter = false; |
||||
fPath[0] = 0; |
||||
if (path) |
||||
{ |
||||
wcscpy(fPath, path); |
||||
|
||||
// Make sure the dir ends with a slash
|
||||
wchar_t lastchar = fPath[wcslen(fPath)-1]; |
||||
if (lastchar != L'\\' && lastchar != L'/') |
||||
wcscat(fPath, L"\\"); |
||||
} |
||||
|
||||
Reset(); |
||||
} |
||||
|
||||
void hsWFolderIterator::SetWinSystemDir(const wchar_t subdir[]) |
||||
{ |
||||
int ret = GetWindowsDirectoryW(fPath, _MAX_PATH); |
||||
hsAssert(ret != 0, "Error getting windows directory in UseWindowsFontsPath"); |
||||
|
||||
if (subdir) |
||||
{ |
||||
wcscat(fPath, L"\\"); |
||||
wcscat(fPath, subdir); |
||||
wcscat(fPath, L"\\"); |
||||
} |
||||
Reset(); |
||||
} |
||||
|
||||
void hsWFolderIterator::SetFileFilterStr(const wchar_t filterStr[]) |
||||
{ |
||||
fPath[0] = 0; |
||||
if (filterStr) |
||||
{ |
||||
fCustomFilter = true; |
||||
wcscpy(fPath, filterStr); |
||||
} |
||||
|
||||
Reset(); |
||||
} |
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void hsWFolderIterator::Reset() |
||||
{ |
||||
if (fData->fSearchHandle) |
||||
{ |
||||
FindClose(fData->fSearchHandle); |
||||
fData->fSearchHandle = nil; |
||||
} |
||||
fData->fValid = true; |
||||
} |
||||
|
||||
bool hsWFolderIterator::NextFile() |
||||
{ |
||||
if (fData->fValid == false) |
||||
return false; |
||||
|
||||
if (fData->fSearchHandle == nil) |
||||
{ |
||||
int len = wcslen(fPath); |
||||
|
||||
if(fCustomFilter == false) |
||||
{ |
||||
fPath[len] = L'*'; |
||||
fPath[len+1] = L'\0'; |
||||
} |
||||
|
||||
fData->fSearchHandle = FindFirstFileW(fPath, &fData->fFindData); |
||||
fPath[len] = 0; |
||||
|
||||
if (fData->fSearchHandle == INVALID_HANDLE_VALUE) |
||||
{ |
||||
fData->fSearchHandle = nil; |
||||
fData->fValid = false; |
||||
return false; |
||||
} |
||||
} |
||||
else |
||||
{ |
||||
if (FindNextFileW(fData->fSearchHandle, &fData->fFindData) == false) |
||||
{ |
||||
FindClose(fData->fSearchHandle); |
||||
fData->fSearchHandle = nil; |
||||
fData->fValid = false; |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
|
||||
bool hsWFolderIterator::IsDirectory( void ) const |
||||
{ |
||||
if( fData->fValid && ( fData->fFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) ) |
||||
return true; |
||||
|
||||
return false; |
||||
} |
||||
|
||||
const wchar_t* hsWFolderIterator::GetFileName() const |
||||
{ |
||||
if (fData->fValid == false) |
||||
hsThrow( "end of folder"); |
||||
|
||||
return fData->fFindData.cFileName; |
||||
} |
||||
|
||||
#endif // HS_BUILD_FOR_WIN32
|
@ -1,503 +0,0 @@
|
||||
/*==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/>.
|
||||
|
||||
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. |
||||
|
||||
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==*/ |
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// plFileUtils - Namespace of fun file utilities
|
||||
//
|
||||
//// History /////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 5.7.2002 mcn - Created
|
||||
// 4.8.2003 chip - added FileCopy and FileMove for Unix
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "HeadSpin.h" |
||||
#include "hsWindows.h" |
||||
#include "plFileUtils.h" |
||||
#include "hsFiles.h" |
||||
#include "hsStringTokenizer.h" |
||||
|
||||
|
||||
#include "plUnifiedTime/plUnifiedTime.h" |
||||
|
||||
#include <time.h> |
||||
#include <sys/types.h> |
||||
#include <sys/stat.h> |
||||
#include <errno.h> |
||||
|
||||
|
||||
#if HS_BUILD_FOR_WIN32 |
||||
#include <direct.h> |
||||
#include <io.h> |
||||
#define mkdir _mkdir |
||||
#define chdir _chdir |
||||
#define chmod _chmod |
||||
#define rmdir _rmdir |
||||
#define unlink _unlink |
||||
#endif |
||||
|
||||
#if HS_BUILD_FOR_UNIX |
||||
#include <unistd.h> |
||||
#include <utime.h> |
||||
#endif |
||||
|
||||
|
||||
//// CreateDir ///////////////////////////////////////////////////////////////
|
||||
// Creates the directory specified. Returns false if unsuccessful or
|
||||
// directory already exists
|
||||
|
||||
bool plFileUtils::CreateDir( const char *path ) |
||||
{ |
||||
// Create our directory
|
||||
#if HS_BUILD_FOR_WIN32 |
||||
return ( mkdir( path ) == 0 ) ? true : ( errno==EEXIST ); |
||||
#elif HS_BUILD_FOR_UNIX |
||||
return ( mkdir( path, 0777 ) == 0 ) ? true : ( errno==EEXIST ); |
||||
#endif |
||||
} |
||||
|
||||
bool plFileUtils::CreateDir( const wchar_t *path ) |
||||
{ |
||||
// Create our directory
|
||||
#if HS_BUILD_FOR_WIN32 |
||||
return ( _wmkdir( path ) == 0 ) ? true : ( errno==EEXIST ); |
||||
#elif HS_BUILD_FOR_UNIX |
||||
const char* cpath = hsWStringToString(path); |
||||
bool ret = CreateDir(cpath); |
||||
delete[] cpath; /* Free the string */ |
||||
|
||||
return ret; |
||||
#endif |
||||
} |
||||
|
||||
bool plFileUtils::RemoveDir(const char* path) |
||||
{ |
||||
return (rmdir(path) == 0); |
||||
} |
||||
|
||||
bool plFileUtils::RemoveDirTree(const char * path) |
||||
{ |
||||
hsFolderIterator it(path); |
||||
while (it.NextFile()) |
||||
{ |
||||
const char * fname = it.GetFileName(); |
||||
if ( fname[0]=='.' ) |
||||
continue; |
||||
char pathAndName[128]; |
||||
it.GetPathAndName(pathAndName); |
||||
if ( it.IsDirectory() ) |
||||
{ |
||||
RemoveDirTree( pathAndName ); |
||||
RemoveDir( pathAndName ); |
||||
} |
||||
else |
||||
{ |
||||
RemoveFile( pathAndName ); |
||||
} |
||||
} |
||||
RemoveDir( path ); |
||||
|
||||
return 1; |
||||
} |
||||
|
||||
//// RemoveFile ////////////////////////////////////////////////////////////
|
||||
|
||||
bool plFileUtils::RemoveFile(const char* filename, bool delReadOnly) |
||||
{ |
||||
if (delReadOnly) |
||||
chmod(filename, S_IWRITE); |
||||
return (unlink(filename) == 0); |
||||
} |
||||
|
||||
bool plFileUtils::RemoveFile(const wchar_t* filename, bool delReadOnly) |
||||
{ |
||||
#ifdef HS_BUILD_FOR_WIN32 |
||||
if (delReadOnly) |
||||
_wchmod(filename, S_IWRITE); |
||||
return (_wunlink(filename) == 0); |
||||
#elif HS_BUILD_FOR_UNIX |
||||
const char* cfilename = hsWStringToString(filename); |
||||
bool ret = RemoveFile(cfilename, delReadOnly); |
||||
delete[] cfilename; /* Free the string */ |
||||
|
||||
return ret; |
||||
#endif |
||||
} |
||||
|
||||
bool plFileUtils::FileCopy(const char* existingFile, const char* newFile) |
||||
{ |
||||
wchar_t* wExisting = hsStringToWString(existingFile); |
||||
wchar_t* wNew = hsStringToWString(newFile); |
||||
bool ret = FileCopy(wExisting, wNew); |
||||
delete [] wExisting; |
||||
delete [] wNew; |
||||
return ret; |
||||
} |
||||
|
||||
bool plFileUtils::FileCopy(const wchar_t* existingFile, const wchar_t* newFile) |
||||
{ |
||||
#if HS_BUILD_FOR_WIN32 |
||||
return (::CopyFileW(existingFile, newFile, FALSE) != 0); |
||||
#elif HS_BUILD_FOR_UNIX |
||||
char data[1500]; |
||||
const char* cexisting = hsWStringToString(existingFile); |
||||
const char* cnew = hsWStringToString(newFile); |
||||
FILE* fp = fopen(cexisting, "rb"); |
||||
FILE* fw = fopen(cnew, "w"); |
||||
delete[] cexisting; |
||||
delete[] cnew; |
||||
int num = 0; |
||||
bool retVal = true; |
||||
if (fp && fw){ |
||||
while(!feof(fp)){ |
||||
num = fread(data, sizeof( char ), 1500, fp); |
||||
if( ferror( fp ) ) { |
||||
retVal = false; |
||||
break; |
||||
} |
||||
fwrite(data, sizeof( char ), num, fw); |
||||
} |
||||
fclose(fp); |
||||
fclose(fw); |
||||
} else { |
||||
retVal = false; |
||||
} |
||||
return retVal; |
||||
#else |
||||
hsAssert(0, "Not implemented"); |
||||
return false; |
||||
#endif |
||||
} |
||||
|
||||
bool plFileUtils::FileMove(const char* existingFile, const char* newFile) |
||||
{ |
||||
#if HS_BUILD_FOR_WIN32 |
||||
return (::MoveFile(existingFile, newFile) != 0); |
||||
#elif HS_BUILD_FOR_UNIX |
||||
FileCopy(existingFile,newFile); |
||||
return( RemoveFile( existingFile )==0); |
||||
#else |
||||
hsAssert(0, "Not implemented"); |
||||
return false; |
||||
#endif |
||||
} |
||||
|
||||
bool plFileUtils::FileMove(const wchar_t* existingFile, const wchar_t* newFile) |
||||
{ |
||||
#if HS_BUILD_FOR_WIN32 |
||||
return (::MoveFileW(existingFile, newFile) != 0); |
||||
#elif HS_BUILD_FOR_UNIX |
||||
FileCopy(existingFile,newFile); |
||||
return( RemoveFile( existingFile )==0); |
||||
#else |
||||
hsAssert(0, "Not implemented"); |
||||
return false; |
||||
#endif |
||||
} |
||||
|
||||
bool plFileUtils::FileExists(const wchar_t* file) |
||||
{ |
||||
FILE* fp = hsWFopen(file, L"rb"); |
||||
bool retVal = (fp != nil); |
||||
if (fp) |
||||
fclose(fp); |
||||
return retVal; |
||||
} |
||||
|
||||
bool plFileUtils::FileExists(const char* file) |
||||
{ |
||||
FILE* fp = fopen(file, "rb"); |
||||
bool retVal = (fp != nil); |
||||
if (fp) |
||||
fclose(fp); |
||||
return retVal; |
||||
} |
||||
|
||||
//// EnsureFilePathExists ////////////////////////////////////////////////////
|
||||
// Given a filename with path, makes sure the file's path exists
|
||||
|
||||
bool plFileUtils::EnsureFilePathExists( const char *filename ) |
||||
{ |
||||
wchar_t* wFilename = hsStringToWString(filename); |
||||
bool ret = EnsureFilePathExists(wFilename); |
||||
delete [] wFilename; |
||||
return ret; |
||||
} |
||||
|
||||
bool plFileUtils::EnsureFilePathExists( const wchar_t *filename ) |
||||
{ |
||||
hsWStringTokenizer izer( filename, L"\\/" ); |
||||
|
||||
bool lastWorked = false; |
||||
wchar_t token[ kFolderIterator_MaxPath ]; |
||||
|
||||
|
||||
while( izer.Next( token, arrsize( token ) ) && izer.HasMoreTokens() ) |
||||
{ |
||||
// Want the full path from the start of the string
|
||||
lastWorked = CreateDir( izer.fString ); |
||||
izer.RestoreLastTerminator(); |
||||
} |
||||
|
||||
return lastWorked; |
||||
} |
||||
|
||||
//// GetFileTimes ////////////////////////////////////////////////////////////
|
||||
// Gets the creation and modification dates of the file specified. Returns
|
||||
// false if unsuccessful
|
||||
|
||||
bool plFileUtils::GetFileTimes( const char *path, plUnifiedTime *createTimeOut, plUnifiedTime *modifyTimeOut ) |
||||
{ |
||||
struct stat fileInfo; |
||||
|
||||
int result = stat( path, &fileInfo ); |
||||
if( result != 0 ) |
||||
return false; |
||||
|
||||
if( createTimeOut != nil ) |
||||
*createTimeOut = plUnifiedTime( fileInfo.st_ctime ); |
||||
if( modifyTimeOut != nil ) |
||||
*modifyTimeOut = plUnifiedTime( fileInfo.st_mtime ); |
||||
|
||||
return true; |
||||
} |
||||
|
||||
plFileUtils::Modify plFileUtils::CompareModifyTimes(const char* file1, const char* file2) |
||||
{ |
||||
plUnifiedTime modTime1, modTime2; |
||||
if (GetFileTimes(file1, nil, &modTime1) && |
||||
GetFileTimes(file2, nil, &modTime2)) |
||||
{ |
||||
double diff = plUnifiedTime::GetTimeDifference(modTime1, modTime2); |
||||
|
||||
if (hsABS(diff) <= 2) |
||||
return kFilesEqual; |
||||
else if (diff > 0) |
||||
return kFile1Newer; |
||||
else |
||||
return kFile2Newer; |
||||
} |
||||
|
||||
return kFileError; |
||||
} |
||||
|
||||
bool plFileUtils::SetModifyTime( const char * filename, const plUnifiedTime & timestamp ) |
||||
{ |
||||
#ifdef HS_BUILD_FOR_WIN32 |
||||
HANDLE hFile = CreateFile(filename, |
||||
GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE, |
||||
nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,nil); |
||||
if (hFile==INVALID_HANDLE_VALUE) |
||||
return false; |
||||
SYSTEMTIME systime; |
||||
systime.wDay = timestamp.GetDay(); |
||||
systime.wDayOfWeek = timestamp.GetDayOfWeek(); |
||||
systime.wHour = timestamp.GetHour(); |
||||
systime.wMilliseconds = 0; |
||||
systime.wMinute = timestamp.GetMinute(); |
||||
systime.wMonth = timestamp.GetMonth(); |
||||
systime.wSecond = timestamp.GetSecond(); |
||||
systime.wYear = timestamp.GetYear(); |
||||
FILETIME localFileTime, filetime; |
||||
SystemTimeToFileTime(&systime,&localFileTime); |
||||
LocalFileTimeToFileTime(&localFileTime,&filetime); |
||||
SetFileTime(hFile,nil,nil,&filetime); |
||||
CloseHandle(hFile); |
||||
return true; |
||||
|
||||
#elif HS_BUILD_FOR_UNIX |
||||
struct stat sbuf; |
||||
int result = stat( filename, &sbuf ); |
||||
if( result ) |
||||
return false; |
||||
struct utimbuf utb; |
||||
utb.actime = sbuf.st_atime; |
||||
utb.modtime = timestamp.GetSecs(); |
||||
result = utime( filename, &utb ); |
||||
if( result ) |
||||
return false; |
||||
return true; |
||||
|
||||
#endif |
||||
} |
||||
|
||||
//// StripPath ///////////////////////////////////////////////////////////////
|
||||
|
||||
const char* plFileUtils::GetFileName(const char* path) |
||||
{ |
||||
const char* c = strrchr(path, '/'); |
||||
if (c == nil) |
||||
c = strrchr(path, '\\'); |
||||
|
||||
if (c == nil) |
||||
c = path; |
||||
else |
||||
c++; |
||||
|
||||
return c; |
||||
} |
||||
|
||||
const wchar_t* plFileUtils::GetFileName(const wchar_t* path) |
||||
{ |
||||
const wchar_t* c = wcsrchr(path, L'/'); |
||||
if (c == nil) |
||||
c = wcsrchr(path, L'\\'); |
||||
|
||||
if (c == nil) |
||||
c = path; |
||||
else |
||||
c++; |
||||
|
||||
return c; |
||||
} |
||||
|
||||
void plFileUtils::StripFile(char* pathAndName) |
||||
{ |
||||
char* fileName = (char*)GetFileName(pathAndName); |
||||
if (fileName != pathAndName) |
||||
*fileName = '\0'; |
||||
} |
||||
|
||||
void plFileUtils::StripFile(wchar_t* pathAndName) |
||||
{ |
||||
wchar_t* fileName = (wchar_t*)GetFileName(pathAndName); |
||||
if (fileName != pathAndName) |
||||
*fileName = L'\0'; |
||||
} |
||||
|
||||
void plFileUtils::StripExt(char* fileName) |
||||
{ |
||||
char* ext = (char*)GetFileExt(fileName); |
||||
if (ext) |
||||
*(ext-1) = '\0'; |
||||
} |
||||
|
||||
void plFileUtils::StripExt(wchar_t* fileName) |
||||
{ |
||||
wchar_t* ext = (wchar_t*)GetFileExt(fileName); |
||||
if (ext) |
||||
*(ext-1) = L'\0'; |
||||
} |
||||
|
||||
const char* plFileUtils::GetFileExt(const char* pathAndName) |
||||
{ |
||||
const char* fileName = GetFileName(pathAndName); |
||||
if (fileName) |
||||
{ |
||||
const char* ext = strrchr(fileName, '.'); |
||||
if (ext) |
||||
return ext+1; |
||||
} |
||||
|
||||
return nil; |
||||
} |
||||
|
||||
const wchar_t* plFileUtils::GetFileExt(const wchar_t* pathAndName) |
||||
{ |
||||
const wchar_t* fileName = GetFileName(pathAndName); |
||||
if (fileName) |
||||
{ |
||||
const wchar_t* ext = wcsrchr(fileName, L'.'); |
||||
if (ext) |
||||
return ext+1; |
||||
} |
||||
|
||||
return nil; |
||||
} |
||||
|
||||
void plFileUtils::AddSlash(char* path) |
||||
{ |
||||
char lastChar = path[strlen(path)-1]; |
||||
if (lastChar != '\\' && lastChar != '/') |
||||
#if HS_BUILD_FOR_WIN32 |
||||
strcat(path, "\\"); |
||||
#else |
||||
strcat(path, "/"); |
||||
#endif |
||||
} |
||||
|
||||
void plFileUtils::AddSlash(wchar_t* path) |
||||
{ |
||||
wchar_t lastChar = path[wcslen(path)-1]; |
||||
if (lastChar != L'\\' && lastChar != L'/') |
||||
#if HS_BUILD_FOR_WIN32 |
||||
wcscat(path, L"\\"); |
||||
#else |
||||
wcscat(path, L"/"); |
||||
#endif |
||||
} |
||||
|
||||
void plFileUtils::ConcatFileName(char* path, const char* fileName) |
||||
{ |
||||
AddSlash(path); |
||||
strcat(path, fileName); |
||||
} |
||||
|
||||
void plFileUtils::ConcatFileName(wchar_t* path, const wchar_t* fileName) |
||||
{ |
||||
AddSlash(path); |
||||
wcscat(path, fileName); |
||||
} |
||||
|
||||
//// GetFileSize /////////////////////////////////////////////////////////////
|
||||
|
||||
uint32_t plFileUtils::GetFileSize( const char *path ) |
||||
{ |
||||
wchar_t* wPath = hsStringToWString(path); |
||||
uint32_t ret = GetFileSize(wPath); |
||||
delete [] wPath; |
||||
return ret; |
||||
} |
||||
|
||||
uint32_t plFileUtils::GetFileSize( const wchar_t *path ) |
||||
{ |
||||
uint32_t len = 0; |
||||
|
||||
hsUNIXStream str; |
||||
if (str.Open(plString::FromWchar(path), "rb")) |
||||
{ |
||||
len = str.GetEOF(); |
||||
str.Close(); |
||||
} |
||||
|
||||
return len; |
||||
} |
@ -1,116 +0,0 @@
|
||||
/*==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/>.
|
||||
|
||||
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. |
||||
|
||||
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==*/ |
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// plFileUtils - Namespace of fun file utilities
|
||||
//
|
||||
//// History /////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 5.7.2002 mcn - Created
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef _plFileUtils_h |
||||
#define _plFileUtils_h |
||||
|
||||
class plUnifiedTime; |
||||
|
||||
namespace plFileUtils |
||||
{ |
||||
// Creates the directory specified. Returns false if unsuccessful or directory already exists
|
||||
bool CreateDir( const char *path );
|
||||
bool CreateDir( const wchar_t *path );
|
||||
bool RemoveDir(const char* path); |
||||
bool RemoveDirTree(const char * path); |
||||
|
||||
// delete file from disk
|
||||
bool RemoveFile(const char* filename, bool delReadOnly=false); |
||||
bool RemoveFile(const wchar_t* filename, bool delReadOnly=false); |
||||
|
||||
bool FileCopy(const char* existingFile, const char* newFile); |
||||
bool FileCopy(const wchar_t* existingFile, const wchar_t* newFile); |
||||
bool FileMove(const char* existingFile, const char* newFile); |
||||
bool FileMove(const wchar_t* existingFile, const wchar_t* newFile); |
||||
|
||||
bool FileExists(const char* file); |
||||
bool FileExists(const wchar_t* file); |
||||
|
||||
// Given a filename with path, makes sure the file's path exists
|
||||
bool EnsureFilePathExists( const char *filename ); |
||||
bool EnsureFilePathExists( const wchar_t *filename ); |
||||
|
||||
// Gets the creation and modification dates of the file specified. Returns false if unsuccessful
|
||||
bool GetFileTimes( const char *path, plUnifiedTime *createTimeOut, plUnifiedTime *modifyTimeOut ); |
||||
// Compares file times, taking into account NTFS/FAT32 time issues
|
||||
enum Modify { kFileError, kFilesEqual, kFile1Newer, kFile2Newer }; |
||||
Modify CompareModifyTimes(const char* file1, const char* file2); |
||||
// Set file modify time
|
||||
bool SetModifyTime( const char * filename, const plUnifiedTime & time ); |
||||
|
||||
// Return a pointer into the given string at the start of the actual filename (i.e. past any path info)
|
||||
const char* GetFileName(const char* pathAndName); |
||||
const wchar_t* GetFileName(const wchar_t* pathAndName); |
||||
// Get the file extension (without the .), or nil if it doesn't have one
|
||||
const char* GetFileExt(const char* pathAndName); |
||||
const wchar_t* GetFileExt(const wchar_t* pathAndName); |
||||
|
||||
// Strips the filename off the given full path
|
||||
void StripFile(char* pathAndName); |
||||
void StripFile(wchar_t* pathAndName); |
||||
void StripExt(char* fileName); |
||||
void StripExt(wchar_t* fileName); |
||||
|
||||
// Get the size of the given file in bytes
|
||||
uint32_t GetFileSize( const char *path ); |
||||
uint32_t GetFileSize( const wchar_t *path ); |
||||
|
||||
// Adds a slash to the end of a filename (or does nothing if it's already there)
|
||||
void AddSlash(char* path); |
||||
void AddSlash(wchar_t* path); |
||||
|
||||
// Concatenates fileName onto path, making sure to add a slash if necessary
|
||||
void ConcatFileName(char* path, const char* fileName); |
||||
void ConcatFileName(wchar_t* path, const wchar_t* fileName); |
||||
}; |
||||
|
||||
|
||||
#endif // _plFileUtils_h
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue