2010-02-08 17:23:39 +01:00
|
|
|
/* Copyright (c) 2010 Wildfire Games
|
2009-04-18 19:00:33 +02:00
|
|
|
*
|
2010-02-08 17:23:39 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2009-04-18 19:00:33 +02:00
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* helper functions for directory access
|
2009-04-18 18:14:48 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
2010-03-01 15:52:58 +01:00
|
|
|
#include "lib/file/file_system_util.h"
|
2009-04-18 18:14:48 +02:00
|
|
|
|
|
|
|
#include <queue>
|
|
|
|
#include <cstring>
|
2009-08-07 19:22:05 +02:00
|
|
|
#include <cstdio>
|
2009-04-18 18:14:48 +02:00
|
|
|
|
2011-03-23 14:36:20 +01:00
|
|
|
#include "lib/sysdep/filesystem.h"
|
2009-04-18 18:14:48 +02:00
|
|
|
#include "lib/regex.h"
|
|
|
|
|
2009-08-04 21:57:53 +02:00
|
|
|
|
|
|
|
namespace fs_util {
|
|
|
|
|
2011-03-23 14:36:20 +01:00
|
|
|
bool DirectoryExists(const OsPath& path)
|
2009-04-18 18:14:48 +02:00
|
|
|
{
|
2011-03-23 14:36:20 +01:00
|
|
|
WDIR* dir = wopendir(path);
|
|
|
|
if(dir)
|
2009-04-18 18:14:48 +02:00
|
|
|
{
|
2011-03-23 14:36:20 +01:00
|
|
|
wclosedir(dir);
|
|
|
|
return true;
|
2009-04-18 18:14:48 +02:00
|
|
|
}
|
2011-03-23 14:36:20 +01:00
|
|
|
return false;
|
2009-04-18 18:14:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-23 14:36:20 +01:00
|
|
|
bool FileExists(const OsPath& pathname)
|
2009-04-18 18:14:48 +02:00
|
|
|
{
|
2011-03-23 14:36:20 +01:00
|
|
|
struct stat s;
|
|
|
|
const bool exists = wstat(pathname, &s) == 0;
|
|
|
|
return exists;
|
|
|
|
}
|
|
|
|
|
2009-04-18 18:14:48 +02:00
|
|
|
|
2011-03-23 14:36:20 +01:00
|
|
|
u64 FileSize(const OsPath& pathname)
|
2009-04-18 18:14:48 +02:00
|
|
|
{
|
2011-03-23 14:36:20 +01:00
|
|
|
struct stat s;
|
|
|
|
debug_assert(wstat(pathname, &s) == 0);
|
|
|
|
return s.st_size;
|
2009-04-18 18:14:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-23 14:36:20 +01:00
|
|
|
LibError GetPathnames(const PIVFS& fs, const VfsPath& path, const wchar_t* filter, VfsPaths& pathnames)
|
2009-04-18 18:14:48 +02:00
|
|
|
{
|
2011-03-23 14:36:20 +01:00
|
|
|
std::vector<FileInfo> files;
|
|
|
|
RETURN_ERR(fs->GetDirectoryEntries(path, &files, 0));
|
|
|
|
|
|
|
|
pathnames.clear();
|
|
|
|
pathnames.reserve(files.size());
|
|
|
|
|
|
|
|
for(size_t i = 0; i < files.size(); i++)
|
2009-04-18 18:14:48 +02:00
|
|
|
{
|
2011-03-23 14:36:20 +01:00
|
|
|
if(match_wildcard(files[i].Name().string().c_str(), filter))
|
|
|
|
pathnames.push_back(path / files[i].Name());
|
2009-04-18 18:14:48 +02:00
|
|
|
}
|
|
|
|
|
2011-03-23 14:36:20 +01:00
|
|
|
return INFO::OK;
|
2009-04-18 18:14:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
LibError ForEachFile(const PIVFS& fs, const VfsPath& startPath, FileCallback cb, uintptr_t cbData, const wchar_t* pattern, size_t flags)
|
2009-04-18 18:14:48 +02:00
|
|
|
{
|
|
|
|
// (declare here to avoid reallocations)
|
|
|
|
FileInfos files; DirectoryNames subdirectoryNames;
|
|
|
|
|
|
|
|
// (a FIFO queue is more efficient than recursion because it uses less
|
|
|
|
// stack space and avoids seeks due to breadth-first traversal.)
|
|
|
|
std::queue<VfsPath> pendingDirectories;
|
2011-03-23 14:36:20 +01:00
|
|
|
pendingDirectories.push(startPath/"");
|
2009-04-18 18:14:48 +02:00
|
|
|
while(!pendingDirectories.empty())
|
|
|
|
{
|
|
|
|
const VfsPath& path = pendingDirectories.front();
|
|
|
|
|
2009-11-05 21:46:15 +01:00
|
|
|
RETURN_ERR(fs->GetDirectoryEntries(path, &files, &subdirectoryNames));
|
2009-04-18 18:14:48 +02:00
|
|
|
|
|
|
|
for(size_t i = 0; i < files.size(); i++)
|
|
|
|
{
|
|
|
|
const FileInfo fileInfo = files[i];
|
2011-03-23 14:36:20 +01:00
|
|
|
if(!match_wildcard(fileInfo.Name().string().c_str(), pattern))
|
2009-04-18 18:14:48 +02:00
|
|
|
continue;
|
|
|
|
|
2011-03-23 14:36:20 +01:00
|
|
|
const VfsPath pathname(path / fileInfo.Name()); // (FileInfo only stores the name)
|
2009-04-18 18:14:48 +02:00
|
|
|
cb(pathname, fileInfo, cbData);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!(flags & DIR_RECURSIVE))
|
|
|
|
break;
|
|
|
|
|
|
|
|
for(size_t i = 0; i < subdirectoryNames.size(); i++)
|
2011-03-23 14:36:20 +01:00
|
|
|
pendingDirectories.push(path / subdirectoryNames[i]/"");
|
2009-04-18 18:14:48 +02:00
|
|
|
pendingDirectories.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
return INFO::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-04 21:57:53 +02:00
|
|
|
void NextNumberedFilename(const PIVFS& fs, const VfsPath& pathnameFormat, size_t& nextNumber, VfsPath& nextPathname)
|
2009-04-18 18:14:48 +02:00
|
|
|
{
|
|
|
|
// (first call only:) scan directory and set nextNumber according to
|
|
|
|
// highest matching filename found. this avoids filling "holes" in
|
|
|
|
// the number series due to deleted files, which could be confusing.
|
|
|
|
// example: add 1st and 2nd; [exit] delete 1st; [restart]
|
|
|
|
// add 3rd -> without this measure it would get number 1, not 3.
|
|
|
|
if(nextNumber == 0)
|
|
|
|
{
|
2011-03-23 14:36:20 +01:00
|
|
|
const VfsPath nameFormat = pathnameFormat.Filename();
|
|
|
|
const VfsPath path = pathnameFormat.Parent()/"";
|
2009-04-18 18:14:48 +02:00
|
|
|
|
|
|
|
size_t maxNumber = 0;
|
|
|
|
FileInfos files;
|
|
|
|
fs->GetDirectoryEntries(path, &files, 0);
|
|
|
|
for(size_t i = 0; i < files.size(); i++)
|
|
|
|
{
|
2009-05-03 13:53:05 +02:00
|
|
|
int number;
|
2011-03-23 14:36:20 +01:00
|
|
|
if(swscanf_s(files[i].Name().string().c_str(), nameFormat.string().c_str(), &number) == 1)
|
2009-05-03 13:53:05 +02:00
|
|
|
maxNumber = std::max(size_t(number), maxNumber);
|
2009-04-18 18:14:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
nextNumber = maxNumber+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now increment number until that file doesn't yet exist.
|
|
|
|
// this is fairly slow, but typically only happens once due
|
|
|
|
// to scan loop above. (we still need to provide for looping since
|
|
|
|
// someone may have added files in the meantime)
|
|
|
|
// we don't bother with binary search - this isn't a bottleneck.
|
|
|
|
do
|
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
wchar_t pathnameBuf[PATH_MAX];
|
2011-03-23 14:36:20 +01:00
|
|
|
swprintf_s(pathnameBuf, ARRAY_SIZE(pathnameBuf), pathnameFormat.string().c_str(), nextNumber++);
|
2009-11-03 23:27:25 +01:00
|
|
|
nextPathname = pathnameBuf;
|
2009-04-18 18:14:48 +02:00
|
|
|
}
|
|
|
|
while(fs->GetFileInfo(nextPathname, 0) == INFO::OK);
|
|
|
|
}
|
2009-08-04 21:57:53 +02:00
|
|
|
|
|
|
|
} // namespace fs_util
|