From 42aef813dae5eaa8d14257b184d0e111cdf33423 Mon Sep 17 00:00:00 2001 From: taynpg Date: Thu, 13 Feb 2025 16:28:55 +0800 Subject: [PATCH] =?UTF-8?q?add=EF=BC=9A=E6=B7=BB=E5=8A=A0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E9=80=9A=E9=85=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 14 +++++++++++++- include/of_def.hpp | 2 ++ include/of_path.h | 5 +++++ src/of_path.cpp | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index e9e13c9..5f32842 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -87,6 +87,18 @@ "unordered_map": "cpp", "xhash": "cpp", "map": "cpp", - "xtree": "cpp" + "xtree": "cpp", + "fstream": "cpp", + "atomic": "cpp", + "condition_variable": "cpp", + "deque": "cpp", + "functional": "cpp", + "future": "cpp", + "iterator": "cpp", + "mutex": "cpp", + "queue": "cpp", + "regex": "cpp", + "sstream": "cpp", + "thread": "cpp" } } \ No newline at end of file diff --git a/include/of_def.hpp b/include/of_def.hpp index b557e12..a8c95a9 100644 --- a/include/of_def.hpp +++ b/include/of_def.hpp @@ -2,6 +2,7 @@ #define OFEN_DEFINE #include +#include #ifdef UNICODE_OFSTR using ofString = std::wstring; @@ -19,6 +20,7 @@ using ofChar = char; #define ofT(text) text #endif +using ofStrVec = std::vector; enum OfStatus { STA_SUCESS = 0, STA_ERROR, diff --git a/include/of_path.h b/include/of_path.h index a003148..d01a0ec 100644 --- a/include/of_path.h +++ b/include/of_path.h @@ -20,6 +20,11 @@ public: static bool exist(const ofString& path); static bool write(const ofString& path, const char* data, int len); static ofString to_full(const ofString& path); + + /// @brief 根据通配符获取内容,仅支持通配文件,仅支持 *? 两种通配符。 + /// @param path + /// @return + static ofStrVec match_files(const ofString& path); }; }; // namespace ofen #endif \ No newline at end of file diff --git a/src/of_path.cpp b/src/of_path.cpp index 00a556b..dfca1d1 100644 --- a/src/of_path.cpp +++ b/src/of_path.cpp @@ -1,6 +1,7 @@ #include "of_path.h" #include "of_str.h" #include +#include #ifdef USE_BOOST_FILESYSTEM #include @@ -201,4 +202,39 @@ ofString COfPath::to_full(const ofString& path) #endif } +ofStrVec COfPath::match_files(const ofString& path) +{ + ofStrVec ret; + + fs::path in(path); + fs::path parent_path = in.parent_path(); + std::string filename_pattern = in.filename().string(); + + if (parent_path.empty()) { + parent_path = fs::current_path(); + } + + std::string regex_pattern = std::regex_replace(filename_pattern, std::regex(R"(\*)"), ".*"); + regex_pattern = std::regex_replace(regex_pattern, std::regex(R"(\?)"), "."); + + try { + std::regex file_regex(regex_pattern); + for (const auto& entry : fs::directory_iterator(parent_path)) { +#ifdef UNICODE_OFSTR + if (std::regex_match(entry.path().filename().wstring(), file_regex)) { + ret.push_back(entry.path().wstring()); + } +#else + if (std::regex_match(entry.path().filename().string(), file_regex)) { + ret.push_back(entry.path().string()); + } +#endif + } + return ret; + } catch (const std::exception& e) { + ret.clear(); + return ret; + } +} + } // namespace ofen