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 <string> +#include <vector> #ifdef UNICODE_OFSTR using ofString = std::wstring; @@ -19,6 +20,7 @@ using ofChar = char; #define ofT(text) text #endif +using ofStrVec = std::vector<ofString>; 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 <fstream> +#include <regex> #ifdef USE_BOOST_FILESYSTEM #include <boost/filesystem.hpp> @@ -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