janwas
8667ea74c8
- directoryPosix: replace most methods with boost filesystem (but not all: the latter cannot efficiently enumerate files AND query their size/mtime) - AllocatorChecker: better name for member functions - file: move the File class here - trace: bugfix - io: move UnalignedWriter to write_buffer.cpp (basically the same thing) - vfs: remove unnecessary "vfs" warts from variable names - vfs_tree: VfsFile now stores single Name/Size/MTime fields instead of the FileInfo record (less clunky) - vfs_path: use boost filesystem's version of the basename/extension functions - lf_alloc: remove (no longer necessary, won't be finished - not worth the trouble) - path_util: remove path_foreach_component (replaced by better path traversal logic) and PathPackage (obsoleted by fs::path) ! resource loading code now receives VfsPath as its filename. there is also OsPath (native absolute path) and Path (relative to binaries/data) - tex is now independent of file loading code; it just en/decodes in-memory buffers - wdll_ver: clean up, use smart pointer to simplify bailout code - wsdl: remove nonexistent failure path from calc_gamma (cruised by here because SDL_SetGamme is failing once after a cold boot at work) - wsnd: simplify OpenAL DLL search, use boost::filesystem - wutil: Wow64 redirection is now packaged in a (RAII) class This was SVN commit r5525.
139 lines
4.0 KiB
C++
139 lines
4.0 KiB
C++
/**
|
|
* =========================================================================
|
|
* File : file_system_util.cpp
|
|
* Project : 0 A.D.
|
|
* Description : helper functions for directory access
|
|
* =========================================================================
|
|
*/
|
|
|
|
// license: GPL; see lib/license.txt
|
|
|
|
#include "precompiled.h"
|
|
#include "file_system_util.h"
|
|
|
|
#include <queue>
|
|
#include <cstring>
|
|
|
|
#include "lib/path_util.h"
|
|
#include "lib/regex.h"
|
|
|
|
LibError fs_GetPathnames(PIVFS fs, const VfsPath& path, const char* filter, VfsPaths& pathnames)
|
|
{
|
|
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++)
|
|
{
|
|
if(match_wildcard(files[i].Name().c_str(), filter))
|
|
pathnames.push_back(path/files[i].Name());
|
|
}
|
|
|
|
return INFO::OK;
|
|
}
|
|
|
|
|
|
struct FileInfoNameLess : public std::binary_function<const FileInfo, const FileInfo, bool>
|
|
{
|
|
bool operator()(const FileInfo& fileInfo1, const FileInfo& fileInfo2) const
|
|
{
|
|
return strcasecmp(fileInfo1.Name().c_str(), fileInfo2.Name().c_str()) < 0;
|
|
}
|
|
};
|
|
|
|
void fs_SortFiles(FileInfos& files)
|
|
{
|
|
std::sort(files.begin(), files.end(), FileInfoNameLess());
|
|
}
|
|
|
|
|
|
struct NameLess : public std::binary_function<const std::string, const std::string, bool>
|
|
{
|
|
bool operator()(const std::string& name1, const std::string& name2) const
|
|
{
|
|
return strcasecmp(name1.c_str(), name2.c_str()) < 0;
|
|
}
|
|
};
|
|
|
|
void fs_SortDirectories(DirectoryNames& directories)
|
|
{
|
|
std::sort(directories.begin(), directories.end(), NameLess());
|
|
}
|
|
|
|
|
|
LibError fs_ForEachFile(PIVFS fs, const VfsPath& path, FileCallback cb, uintptr_t cbData, const char* pattern, unsigned flags)
|
|
{
|
|
debug_assert(vfs_path_IsDirectory(path));
|
|
|
|
// (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;
|
|
pendingDirectories.push(path);
|
|
while(!pendingDirectories.empty())
|
|
{
|
|
const VfsPath& path = pendingDirectories.front();
|
|
|
|
RETURN_ERR(fs->GetDirectoryEntries(path/"/", &files, &subdirectoryNames));
|
|
|
|
for(size_t i = 0; i < files.size(); i++)
|
|
{
|
|
const FileInfo fileInfo = files[i];
|
|
if(!match_wildcard(fileInfo.Name().c_str(), pattern))
|
|
continue;
|
|
|
|
const VfsPath pathname(path/fileInfo.Name()); // (FileInfo only stores the name)
|
|
cb(pathname, fileInfo, cbData);
|
|
}
|
|
|
|
if(!(flags & DIR_RECURSIVE))
|
|
break;
|
|
|
|
for(size_t i = 0; i < subdirectoryNames.size(); i++)
|
|
pendingDirectories.push(path/subdirectoryNames[i]);
|
|
pendingDirectories.pop();
|
|
}
|
|
|
|
return INFO::OK;
|
|
}
|
|
|
|
|
|
void fs_NextNumberedFilename(PIVFS fs, const char* pathnameFmt, unsigned& nextNumber, char* nextPathname)
|
|
{
|
|
// (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)
|
|
{
|
|
char path[PATH_MAX]; char nameFmt[PATH_MAX];
|
|
path_split(pathnameFmt, path, nameFmt);
|
|
|
|
unsigned maxNumber = 0;
|
|
FileInfos files;
|
|
fs->GetDirectoryEntries(path, &files, 0);
|
|
for(size_t i = 0; i < files.size(); i++)
|
|
{
|
|
unsigned number;
|
|
if(sscanf(files[i].Name().c_str(), nameFmt, &number) == 1)
|
|
maxNumber = std::max(number, maxNumber);
|
|
}
|
|
|
|
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
|
|
snprintf(nextPathname, PATH_MAX, pathnameFmt, nextNumber++);
|
|
while(fs->GetFileInfo(nextPathname, 0) == INFO::OK);
|
|
}
|