add:添加文件通配。
This commit is contained in:
parent
cf634e27da
commit
42aef813da
14
.vscode/settings.json
vendored
14
.vscode/settings.json
vendored
@ -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"
|
||||
}
|
||||
}
|
@ -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,
|
||||
|
@ -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
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user