Skip to content

Commit

Permalink
Refactoring and adding some documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Sep 12, 2017
1 parent 4245727 commit ab33f88
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
15 changes: 7 additions & 8 deletions plugins/vfspk3/Doom3FileSystem.cpp
Expand Up @@ -230,11 +230,10 @@ void Doom3FileSystem::forEachFile(const std::string& basedir,
const VisitorFunc& visitorFunc,
std::size_t depth)
{
// Set of visited files, to avoid name conflicts
std::set<std::string> visitedFiles;
// Construct our FileVisitor filtering out the right elements
FileVisitor fileVisitor(visitorFunc, basedir, extension);

// Wrap around the passed visitor
FileVisitor fileVisitor(visitorFunc, basedir, extension, visitedFiles);
// Construct an ArchiveVisitor filtering out the files only, and watching the recursion depth
ArchiveVisitor functor(std::bind(&FileVisitor::visit, fileVisitor, std::placeholders::_1), Archive::eFiles, depth);

// Visit each Archive, applying the FileVisitor to each one (which in
Expand All @@ -250,13 +249,13 @@ void Doom3FileSystem::forEachFileInAbsolutePath(const std::string& path,
const VisitorFunc& visitorFunc,
std::size_t depth)
{
std::set<std::string> visitedFiles;

// Construct a temporary DirectoryArchive from the given path
DirectoryArchive tempArchive(os::standardPathWithSlash(path));

// Wrap around the passed visitor
FileVisitor fileVisitor(visitorFunc, "", extension, visitedFiles);
// Construct our FileVisitor filtering out the right elements
FileVisitor fileVisitor(visitorFunc, "", extension);

// Construct an ArchiveVisitor filtering out the files only, and watching the recursion depth
ArchiveVisitor functor(std::bind(&FileVisitor::visit, fileVisitor, std::placeholders::_1), Archive::eFiles, depth);

tempArchive.traverse(functor, "/");
Expand Down
33 changes: 19 additions & 14 deletions plugins/vfspk3/FileVisitor.h
@@ -1,20 +1,26 @@
#pragma once

#include "ifilesystem.h"
#include "iarchive.h"

#include "os/path.h"

#include <set>
#include <boost/algorithm/string/case_conv.hpp>

/**
* Adaptor class used in GlobalFileSystem().foreachFile().
* It's filtering out the files matching the defined extension only.
* Passes the filename to the VisitorFunc given in its constructor.
* The directory part is cut off the filename before it's passed to the VisitorFunc.
* On top of that, this class maintains a list of visited files to avoid
* hitting the same file twice (it might be present in more than one Archive).
*/
class FileVisitor
{
private:
// The VirtualFileSystem::Visitor to call for each located file
VirtualFileSystem::VisitorFunc _visitorFunc;

// Set of already-visited files
std::set<std::string>& _visitedFiles;
// Set of already-visited files to avoid visiting the same file twice
std::set<std::string> _visitedFiles;

// Directory to search within
std::string _directory;
Expand All @@ -31,13 +37,11 @@ class FileVisitor

public:

// Constructor
// Constructor. Pass "*" as extension to have it visit all files.
FileVisitor(const VirtualFileSystem::VisitorFunc& visitorFunc,
const std::string& dir,
const std::string& ext,
std::set<std::string>& visitedFiles)
const std::string& ext)
: _visitorFunc(visitorFunc),
_visitedFiles(visitedFiles),
_directory(dir),
_extension(ext),
_dirPrefixLength(_directory.length()),
Expand All @@ -63,8 +67,7 @@ class FileVisitor
if (!_visitAll)
{
// The dot must be at the right position
if (subname.length() <= _extLength ||
subname[subname.length() - _extLength - 1] != '.')
if (subname.length() <= _extLength || subname[subname.length() - _extLength - 1] != '.')
{
return;
}
Expand All @@ -74,15 +77,17 @@ class FileVisitor

#ifdef OS_CASE_INSENSITIVE
// Treat extensions case-insensitively in Windows
boost::to_lower(ext);
boost::algorithm::to_lower(ext);
#endif

if (ext != _extension) {
if (ext != _extension)
{
return; // extension mismatch
}
}

if (_visitedFiles.find(subname) != _visitedFiles.end()) {
if (_visitedFiles.find(subname) != _visitedFiles.end())
{
return; // already visited
}

Expand Down

0 comments on commit ab33f88

Please sign in to comment.