mirror of
https://codeberg.org/comaps/comaps
synced 2026-01-05 04:03:46 +00:00
[core] Regex refactoring
Signed-off-by: x7z4w <x7z4w@noreply.codeberg.org>
This commit is contained in:
@@ -17,10 +17,11 @@
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <memory>
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
#include <unordered_set>
|
||||
|
||||
#include <boost/regex.hpp>
|
||||
|
||||
#include "defines.hpp"
|
||||
|
||||
namespace platform
|
||||
@@ -42,8 +43,8 @@ bool IsSpecialName(string const & name) { return name == "." || name == ".."; }
|
||||
*/
|
||||
bool IsDownloaderFile(string const & name)
|
||||
{
|
||||
static std::regex const filter(".*\\.(downloading|resume|ready)[0-9]?$");
|
||||
return std::regex_match(name.begin(), name.end(), filter);
|
||||
static boost::regex const filter(".*\\.(downloading|resume|ready)[0-9]?$");
|
||||
return boost::regex_match(name.begin(), name.end(), filter);
|
||||
}
|
||||
|
||||
bool IsDiffFile(string const & name)
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
#include <algorithm>
|
||||
#include <thread>
|
||||
|
||||
#include <boost/regex.hpp>
|
||||
|
||||
#include "private.h"
|
||||
|
||||
#include <cerrno>
|
||||
@@ -30,7 +32,7 @@ std::string RandomString(size_t length)
|
||||
return str;
|
||||
}
|
||||
|
||||
bool IsSpecialDirName(std::string const & dirName)
|
||||
inline bool IsSpecialDirName(std::string const & dirName)
|
||||
{
|
||||
return dirName == "." || dirName == "..";
|
||||
}
|
||||
@@ -73,7 +75,7 @@ bool Platform::RmDirRecursively(std::string const & dirName)
|
||||
bool res = true;
|
||||
|
||||
FilesList allFiles;
|
||||
GetFilesByRegExp(dirName, ".*", allFiles);
|
||||
GetAllFiles(dirName, allFiles);
|
||||
for (std::string const & file : allFiles)
|
||||
{
|
||||
std::string const path = base::JoinPath(dirName, file);
|
||||
@@ -207,15 +209,14 @@ void Platform::GetFilesByExt(std::string const & directory, std::string_view ext
|
||||
// Transform extension mask to regexp (.mwm -> \.mwm$)
|
||||
ASSERT(!ext.empty(), ());
|
||||
ASSERT_EQUAL(ext[0], '.', ());
|
||||
std::string regexp = "\\";
|
||||
GetFilesByRegExp(directory, regexp.append(ext).append("$"), outFiles);
|
||||
GetFilesByRegExp(directory, boost::regex(std::string("\\").append(ext).append("$")), outFiles);
|
||||
}
|
||||
|
||||
// static
|
||||
void Platform::GetFilesByType(std::string const & directory, unsigned typeMask, TFilesWithType & outFiles)
|
||||
{
|
||||
FilesList allFiles;
|
||||
GetFilesByRegExp(directory, ".*", allFiles);
|
||||
GetAllFiles(directory, allFiles);
|
||||
for (auto const & file : allFiles)
|
||||
{
|
||||
EFileType type;
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <boost/regex.hpp>
|
||||
|
||||
#include "defines.hpp"
|
||||
|
||||
DECLARE_EXCEPTION(FileAbsentException, RootException);
|
||||
@@ -209,7 +211,8 @@ public:
|
||||
//@{
|
||||
/// @param ext files extension to find, like ".mwm".
|
||||
static void GetFilesByExt(std::string const & directory, std::string_view ext, FilesList & outFiles);
|
||||
static void GetFilesByRegExp(std::string const & directory, std::string const & regexp, FilesList & outFiles);
|
||||
static void GetFilesByRegExp(std::string const & directory, boost::regex const & regexp, FilesList & outFiles);
|
||||
static void GetAllFiles(std::string const & directory, FilesList & outFiles);
|
||||
//@}
|
||||
|
||||
static void GetFilesByType(std::string const & directory, unsigned typeMask, TFilesWithType & outFiles);
|
||||
|
||||
@@ -9,12 +9,12 @@
|
||||
#include "base/file_name_utils.hpp"
|
||||
#include "base/logging.hpp"
|
||||
#include "base/string_utils.hpp"
|
||||
#include "base/thread.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
|
||||
#include <boost/regex.hpp>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h> // for sysconf
|
||||
|
||||
@@ -125,7 +125,7 @@ std::unique_ptr<ModelReader> Platform::GetReader(std::string const & file, std::
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void Platform::GetFilesByRegExp(std::string const & directory, std::string const & regexp, FilesList & res)
|
||||
void Platform::GetFilesByRegExp(std::string const & directory, boost::regex const & regexp, FilesList & res)
|
||||
{
|
||||
if (ZipFileReader::IsZip(directory))
|
||||
{
|
||||
@@ -134,12 +134,10 @@ void Platform::GetFilesByRegExp(std::string const & directory, std::string const
|
||||
FilesT fList;
|
||||
ZipFileReader::FilesList(directory, fList);
|
||||
|
||||
std::regex exp(regexp);
|
||||
|
||||
for (FilesT::iterator it = fList.begin(); it != fList.end(); ++it)
|
||||
{
|
||||
std::string & name = it->first;
|
||||
if (std::regex_search(name.begin(), name.end(), exp))
|
||||
if (boost::regex_search(name.begin(), name.end(), regexp))
|
||||
{
|
||||
// Remove assets/ prefix - clean files are needed for fonts white/blacklisting logic
|
||||
size_t const ASSETS_LENGTH = 7;
|
||||
@@ -154,6 +152,30 @@ void Platform::GetFilesByRegExp(std::string const & directory, std::string const
|
||||
pl::EnumerateFilesByRegExp(directory, regexp, res);
|
||||
}
|
||||
|
||||
void Platform::GetAllFiles(std::string const & directory, FilesList & res)
|
||||
{
|
||||
if (ZipFileReader::IsZip(directory))
|
||||
{
|
||||
// Get files list inside zip file
|
||||
typedef ZipFileReader::FileList FilesT;
|
||||
FilesT fList;
|
||||
ZipFileReader::FilesList(directory, fList);
|
||||
|
||||
for (FilesT::iterator it = fList.begin(); it != fList.end(); ++it)
|
||||
{
|
||||
std::string & name = it->first;
|
||||
// Remove assets/ prefix - clean files are needed for fonts white/blacklisting logic
|
||||
size_t const ASSETS_LENGTH = 7;
|
||||
if (name.find("assets/") == 0)
|
||||
name.erase(0, ASSETS_LENGTH);
|
||||
|
||||
res.push_back(name);
|
||||
}
|
||||
}
|
||||
else
|
||||
pl::EnumerateFiles(directory, res);
|
||||
}
|
||||
|
||||
int Platform::VideoMemoryLimit() const
|
||||
{
|
||||
return 10 * 1024 * 1024;
|
||||
|
||||
@@ -97,11 +97,16 @@ Platform::EError Platform::MkDir(std::string const & dirName)
|
||||
return Platform::ERR_OK;
|
||||
}
|
||||
|
||||
void Platform::GetFilesByRegExp(std::string const & directory, std::string const & regexp, FilesList & res)
|
||||
void Platform::GetFilesByRegExp(std::string const & directory, boost::regex const & regexp, FilesList & res)
|
||||
{
|
||||
pl::EnumerateFilesByRegExp(directory, regexp, res);
|
||||
}
|
||||
|
||||
void Platform::GetAllFiles(std::string const & directory, FilesList & res)
|
||||
{
|
||||
pl::EnumerateFiles(directory, res);
|
||||
}
|
||||
|
||||
bool Platform::GetFileSizeByName(std::string const & fileName, uint64_t & size) const
|
||||
{
|
||||
try
|
||||
|
||||
@@ -7,9 +7,9 @@
|
||||
|
||||
#include "base/logging.hpp"
|
||||
|
||||
#include <future>
|
||||
#include <memory>
|
||||
#include <regex>
|
||||
|
||||
#include <boost/regex.hpp>
|
||||
|
||||
#include <QtCore/QCoreApplication>
|
||||
#include <QtCore/QDir>
|
||||
@@ -33,21 +33,27 @@ bool Platform::GetFileSizeByName(std::string const & fileName, uint64_t & size)
|
||||
}
|
||||
}
|
||||
|
||||
void Platform::GetFilesByRegExp(std::string const & directory, std::string const & regexp, FilesList & outFiles)
|
||||
void Platform::GetFilesByRegExp(std::string const & directory, boost::regex const & regexp, FilesList & outFiles)
|
||||
{
|
||||
std::regex exp(regexp);
|
||||
|
||||
QDir dir(QString::fromUtf8(directory.c_str()));
|
||||
int const count = dir.count();
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
std::string name = dir[i].toStdString();
|
||||
if (std::regex_search(name.begin(), name.end(), exp))
|
||||
if (boost::regex_search(name.begin(), name.end(), regexp))
|
||||
outFiles.push_back(std::move(name));
|
||||
}
|
||||
}
|
||||
|
||||
void Platform::GetAllFiles(std::string const & directory, FilesList & outFiles)
|
||||
{
|
||||
QDir dir(QString::fromUtf8(directory.c_str()));
|
||||
|
||||
for (int i = 0; i < dir.count(); ++i)
|
||||
outFiles.push_back(dir[i].toStdString());
|
||||
}
|
||||
|
||||
int Platform::PreCachingDepth() const
|
||||
{
|
||||
return 3;
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include <boost/regex.hpp>
|
||||
|
||||
#include "defines.hpp"
|
||||
|
||||
namespace
|
||||
@@ -100,7 +102,7 @@ UNIT_TEST(GetFilesInDir_Smoke)
|
||||
|
||||
TEST(base::IsExist(files1, "minsk-pass.mwm"), ());
|
||||
|
||||
pl.GetFilesByRegExp(dir, ".*\\" DATA_FILE_EXTENSION "$", files2);
|
||||
pl.GetFilesByRegExp(dir, boost::regex(".*\\" + DATA_FILE_EXTENSION + "$"), files2);
|
||||
TEST_EQUAL(files1, files2, ());
|
||||
|
||||
files1.clear();
|
||||
|
||||
@@ -8,7 +8,8 @@
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <regex>
|
||||
|
||||
#include <boost/regex.hpp>
|
||||
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
@@ -143,15 +144,13 @@ void EnumerateFiles(std::string const & directory, std::function<void(char const
|
||||
fn(entry->d_name);
|
||||
}
|
||||
|
||||
void EnumerateFilesByRegExp(std::string const & directory, std::string const & regexp, std::vector<std::string> & res)
|
||||
void EnumerateFilesByRegExp(std::string const & directory, boost::regex const & regexp, std::vector<std::string> & res)
|
||||
{
|
||||
std::regex exp(regexp);
|
||||
EnumerateFiles(directory, [&](char const * entry)
|
||||
{
|
||||
std::string const name(entry);
|
||||
if (std::regex_search(name.begin(), name.end(), exp))
|
||||
if (boost::regex_search(name.begin(), name.end(), regexp))
|
||||
res.push_back(name);
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace pl
|
||||
|
||||
@@ -4,9 +4,19 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <boost/regex.hpp>
|
||||
|
||||
namespace pl
|
||||
{
|
||||
void EnumerateFiles(std::string const & directory, std::function<void(char const *)> const & fn);
|
||||
|
||||
void EnumerateFilesByRegExp(std::string const & directory, std::string const & regexp, std::vector<std::string> & res);
|
||||
void EnumerateFilesByRegExp(std::string const & directory, boost::regex const & regexp, std::vector<std::string> & res);
|
||||
|
||||
inline void EnumerateFiles(std::string const & directory, std::vector<std::string> & res)
|
||||
{
|
||||
EnumerateFiles(directory, [&](char const * entry)
|
||||
{
|
||||
res.push_back(std::string(entry));
|
||||
});
|
||||
}
|
||||
} // namespace pl
|
||||
|
||||
Reference in New Issue
Block a user