fmt: Added fmt::match

fs::dir: Allow usage in range-based for
This commit is contained in:
DHrpcs3
2015-12-21 13:35:23 +02:00
parent 2a5a06098b
commit ab610deda9
4 changed files with 149 additions and 7 deletions

View File

@@ -293,6 +293,78 @@ namespace fs
// Get next directory entry (UTF-8 name and file stat)
bool read(std::string& name, stat_t& info);
bool first(std::string& name, stat_t& info);
struct entry
{
std::string name;
stat_t info;
};
class iterator
{
entry m_entry;
dir* m_parent;
public:
enum class mode
{
from_first,
from_current
};
iterator(dir* parent, mode mode_ = mode::from_first)
: m_parent(parent)
{
if (!m_parent)
{
return;
}
bool is_ok;
if (mode_ == mode::from_first)
{
is_ok = m_parent->first(m_entry.name, m_entry.info);
}
else
{
is_ok = m_parent->read(m_entry.name, m_entry.info);
}
if (!is_ok)
{
m_parent = nullptr;
}
}
entry& operator *()
{
return m_entry;
}
iterator& operator++()
{
*this = { m_parent, mode::from_current };
return *this;
}
bool operator !=(const iterator& rhs) const
{
return m_parent != rhs.m_parent;
}
};
iterator begin()
{
return{ this };
}
iterator end()
{
return{ nullptr };
}
};
// Get configuration directory

View File

@@ -351,4 +351,5 @@ namespace fmt
std::string tolower(std::string source);
std::string toupper(std::string source);
std::string escape(std::string source);
bool match(const std::string &source, const std::string &mask);
}

View File

@@ -701,7 +701,11 @@ bool fs::dir::open(const std::string& dirname)
{
this->close();
g_tls_error = fse::ok;
#ifdef _WIN32
m_dd = -1;
#else
m_dd = 0;
#endif
if (!is_dir(dirname))
{
@@ -711,12 +715,6 @@ bool fs::dir::open(const std::string& dirname)
m_path.reset(new char[dirname.size() + 1]);
std::memcpy(m_path.get(), dirname.c_str(), dirname.size() + 1);
#ifdef _WIN32
m_dd = -1;
#else
m_dd = (std::intptr_t)::opendir(m_path.get());
#endif
return true;
}
@@ -733,8 +731,10 @@ bool fs::dir::close()
#ifdef _WIN32
CHECK_ASSERTION(m_dd == -1 || FindClose((HANDLE)m_dd));
m_dd = -1;
#else
CHECK_ASSERTION(!::closedir((DIR*)m_dd));
m_dd = 0;
#endif
return true;
@@ -775,6 +775,16 @@ bool fs::dir::read(std::string& name, stat_t& info)
info.mtime = to_time(found.ftLastWriteTime);
info.ctime = to_time(found.ftCreationTime);
#else
if (m_dd == 0)
{
m_dd = (std::intptr_t)::opendir(m_path.get());
if (m_dd == 0)
{
return false;
}
}
const auto found = ::readdir((DIR*)m_dd);
struct stat file_info;
@@ -796,6 +806,26 @@ bool fs::dir::read(std::string& name, stat_t& info)
return true;
}
bool fs::dir::first(std::string& name, stat_t& info)
{
#ifdef _WIN32
if (m_dd != -1)
{
CHECK_ASSERTION(FindClose((HANDLE)m_dd));
m_dd = -1;
}
#else
if (m_dd != 0)
{
CHECK_ASSERTION(!::closedir((DIR*)m_dd));
m_dd = 0;
}
#endif
return read(name, info);
}
std::string fs::get_config_dir()
{
// Use magic static for dir initialization

View File

@@ -237,3 +237,42 @@ std::string fmt::escape(std::string source)
return source;
}
bool fmt::match(const std::string &source, const std::string &mask)
{
std::size_t source_position = 0, mask_position = 0;
for (; source_position < source.size() && mask_position < mask.size(); ++mask_position, ++source_position)
{
switch (mask[mask_position])
{
case '?': break;
case '*':
for (std::size_t test_source_position = source_position; test_source_position < source.size(); ++test_source_position)
{
if (match(source.substr(test_source_position), mask.substr(mask_position + 1)))
{
return true;
}
}
return false;
default:
if (source[source_position] != mask[mask_position])
{
return false;
}
break;
}
}
if (source_position != source.size())
return false;
if (mask_position != mask.size())
return false;
return true;
}