forked from 0ad/0ad
Trivial wrapper around vfs_next_dirent, to make some other code a little tidier
This was SVN commit r1040.
This commit is contained in:
parent
a684d27654
commit
49b5c5c2b8
42
source/ps/VFSUtil.cpp
Executable file
42
source/ps/VFSUtil.cpp
Executable file
@ -0,0 +1,42 @@
|
||||
#include "precompiled.h"
|
||||
|
||||
#include "VFSUtil.h"
|
||||
#include "lib/res/vfs.h"
|
||||
|
||||
#include "CLogger.h"
|
||||
#define LOG_CATEGORY "vfs"
|
||||
|
||||
using namespace VFSUtil;
|
||||
|
||||
// Because I'm lazy, and it saves a few lines of code in other places:
|
||||
bool VFSUtil::FindFiles (CStr dirname, const char* filter, FileList& files)
|
||||
{
|
||||
files.clear();
|
||||
|
||||
Handle dir = vfs_open_dir(dirname.c_str());
|
||||
if (dir <= 0)
|
||||
{
|
||||
LOG(ERROR, LOG_CATEGORY, "Error opening directory '%s' (%lld)", dirname.c_str(), dir);
|
||||
return false;
|
||||
}
|
||||
|
||||
int err;
|
||||
vfsDirEnt entry;
|
||||
while ((err = vfs_next_dirent(dir, &entry, filter)) == 0)
|
||||
{
|
||||
files.push_back(dirname+"/"+entry.name);
|
||||
}
|
||||
// Can't tell the difference between end-of-directory and invalid filter;
|
||||
// assume that err == -1 just means there are no files left (and err != -1
|
||||
// means some other error)
|
||||
if (err != -1)
|
||||
{
|
||||
LOG(ERROR, LOG_CATEGORY, "Error reading files from directory '%s' (%d)", dirname.c_str(), err);
|
||||
return false;
|
||||
}
|
||||
|
||||
vfs_close_dir(dir);
|
||||
|
||||
return true;
|
||||
|
||||
}
|
18
source/ps/VFSUtil.h
Executable file
18
source/ps/VFSUtil.h
Executable file
@ -0,0 +1,18 @@
|
||||
#include "ps/CStr.h"
|
||||
|
||||
namespace VFSUtil
|
||||
{
|
||||
typedef std::vector<CStr> FileList;
|
||||
|
||||
// Puts the list of files inside 'dirname' matching 'filter' into 'files'.
|
||||
// 'dirname' shouldn't end with a slash.
|
||||
// 'filter' is as in vfs_next_dirent:
|
||||
// - 0: any file;
|
||||
// - ".": any file without extension (filename doesn't contain '.');
|
||||
// - ".ext": any file with extension ".ext" (which must not contain '.');
|
||||
// - "/": any subdirectory
|
||||
// 'files' is initially cleared, and undefined on failure.
|
||||
// On failure, logs an error and returns false.
|
||||
bool FindFiles(CStr dirname, const char* filter, FileList& files);
|
||||
|
||||
};
|
Loading…
Reference in New Issue
Block a user