mirror of
https://github.com/RPCS3/common.git
synced 2026-01-31 01:25:19 +01:00
fmt: Added fmt::match
fs::dir: Allow usage in range-based for
This commit is contained in:
44
src/File.cpp
44
src/File.cpp
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user