Spliced out the FS wildcard stuff.

This commit is contained in:
gibbed 2017-01-19 17:06:12 -06:00
parent 5b8b339c38
commit 17529b063f
5 changed files with 159 additions and 114 deletions

View File

@ -106,85 +106,5 @@ bool CreateParentFolder(const std::wstring& path) {
}
}
WildcardFlags WildcardFlags::FIRST(true, false);
WildcardFlags WildcardFlags::LAST(false, true);
WildcardFlags::WildcardFlags() : FromStart(false), ToEnd(false) {}
WildcardFlags::WildcardFlags(bool start, bool end)
: FromStart(start), ToEnd(end) {}
WildcardRule::WildcardRule(const std::string& str_match,
const WildcardFlags& flags)
: match(str_match), rules(flags) {
std::transform(match.begin(), match.end(), match.begin(), tolower);
}
bool WildcardRule::Check(const std::string& str_lower,
std::string::size_type* offset) const {
if (match.empty()) {
return true;
}
if ((str_lower.size() - *offset) < match.size()) {
return false;
}
std::string::size_type result(str_lower.find(match, *offset));
if (result != std::string::npos) {
if (rules.FromStart && result != *offset) {
return false;
}
if (rules.ToEnd && result != (str_lower.size() - match.size())) {
return false;
}
*offset = (result + match.size());
return true;
}
return false;
}
void WildcardEngine::PreparePattern(const std::string& pattern) {
rules.clear();
WildcardFlags flags(WildcardFlags::FIRST);
size_t n = 0;
size_t last = 0;
while ((n = pattern.find_first_of('*', last)) != pattern.npos) {
if (last != n) {
std::string str_str(pattern.substr(last, n - last));
rules.push_back(WildcardRule(str_str, flags));
}
last = n + 1;
flags = WildcardFlags();
}
if (last != pattern.size()) {
std::string str_str(pattern.substr(last));
rules.push_back(WildcardRule(str_str, WildcardFlags::LAST));
}
}
void WildcardEngine::SetRule(const std::string& pattern) {
PreparePattern(pattern);
}
bool WildcardEngine::Match(const std::string& str) const {
std::string str_lc;
std::transform(str.begin(), str.end(), std::back_inserter(str_lc), tolower);
std::string::size_type offset(0);
for (const auto& rule : rules) {
if (!(rule.Check(str_lc, &offset))) {
return false;
}
}
return true;
}
} // namespace filesystem
} // namespace xe

View File

@ -112,40 +112,6 @@ struct FileInfo {
bool GetInfo(const std::wstring& path, FileInfo* out_info);
std::vector<FileInfo> ListFiles(const std::wstring& path);
class WildcardFlags {
public:
bool FromStart : 1, ToEnd : 1;
WildcardFlags();
WildcardFlags(bool start, bool end);
static WildcardFlags FIRST;
static WildcardFlags LAST;
};
class WildcardRule {
public:
WildcardRule(const std::string& str_match, const WildcardFlags& flags);
bool Check(const std::string& str_lower,
std::string::size_type* offset) const;
private:
std::string match;
WildcardFlags rules;
};
class WildcardEngine {
public:
void SetRule(const std::string& pattern);
// Always ignoring case
bool Match(const std::string& str) const;
private:
std::vector<WildcardRule> rules;
void PreparePattern(const std::string& pattern);
};
} // namespace filesystem
} // namespace xe

View File

@ -0,0 +1,98 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2015 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include "xenia/base/filesystem_wildcard.h"
#include <algorithm>
namespace xe {
namespace filesystem {
WildcardFlags WildcardFlags::FIRST(true, false);
WildcardFlags WildcardFlags::LAST(false, true);
WildcardFlags::WildcardFlags() : FromStart(false), ToEnd(false) {}
WildcardFlags::WildcardFlags(bool start, bool end)
: FromStart(start), ToEnd(end) {}
WildcardRule::WildcardRule(const std::string& str_match,
const WildcardFlags& flags)
: match(str_match), rules(flags) {
std::transform(match.begin(), match.end(), match.begin(), tolower);
}
bool WildcardRule::Check(const std::string& str_lower,
std::string::size_type* offset) const {
if (match.empty()) {
return true;
}
if ((str_lower.size() - *offset) < match.size()) {
return false;
}
std::string::size_type result(str_lower.find(match, *offset));
if (result != std::string::npos) {
if (rules.FromStart && result != *offset) {
return false;
}
if (rules.ToEnd && result != (str_lower.size() - match.size())) {
return false;
}
*offset = (result + match.size());
return true;
}
return false;
}
void WildcardEngine::PreparePattern(const std::string& pattern) {
rules.clear();
WildcardFlags flags(WildcardFlags::FIRST);
size_t n = 0;
size_t last = 0;
while ((n = pattern.find_first_of('*', last)) != pattern.npos) {
if (last != n) {
std::string str_str(pattern.substr(last, n - last));
rules.push_back(WildcardRule(str_str, flags));
}
last = n + 1;
flags = WildcardFlags();
}
if (last != pattern.size()) {
std::string str_str(pattern.substr(last));
rules.push_back(WildcardRule(str_str, WildcardFlags::LAST));
}
}
void WildcardEngine::SetRule(const std::string& pattern) {
PreparePattern(pattern);
}
bool WildcardEngine::Match(const std::string& str) const {
std::string str_lc;
std::transform(str.begin(), str.end(), std::back_inserter(str_lc), tolower);
std::string::size_type offset(0);
for (const auto& rule : rules) {
if (!(rule.Check(str_lc, &offset))) {
return false;
}
}
return true;
}
} // namespace filesystem
} // namespace xe

View File

@ -0,0 +1,60 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2015 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#ifndef XENIA_BASE_FILESYSTEM_WILDCARD_H_
#define XENIA_BASE_FILESYSTEM_WILDCARD_H_
#include <iterator>
#include <memory>
#include <string>
#include <vector>
#include "xenia/base/string.h"
namespace xe {
namespace filesystem {
class WildcardFlags {
public:
bool FromStart : 1, ToEnd : 1;
WildcardFlags();
WildcardFlags(bool start, bool end);
static WildcardFlags FIRST;
static WildcardFlags LAST;
};
class WildcardRule {
public:
WildcardRule(const std::string& str_match, const WildcardFlags& flags);
bool Check(const std::string& str_lower,
std::string::size_type* offset) const;
private:
std::string match;
WildcardFlags rules;
};
class WildcardEngine {
public:
void SetRule(const std::string& pattern);
// Always ignoring case
bool Match(const std::string& str) const;
private:
std::vector<WildcardRule> rules;
void PreparePattern(const std::string& pattern);
};
} // namespace filesystem
} // namespace xe
#endif // XENIA_BASE_FILESYSTEM_WILDCARD_H_

View File

@ -15,6 +15,7 @@
#include <vector>
#include "xenia/base/filesystem.h"
#include "xenia/base/filesystem_wildcard.h"
#include "xenia/base/mapped_memory.h"
#include "xenia/base/mutex.h"
#include "xenia/base/string_buffer.h"