视窗fnmatch替代品
Windows fnmatch substitute
我在 Linux 中有以下代码,用于查找与给定通配符匹配的文件:
std::vector<std::string> listFilenamesInPath(std::string wildcard = "*", std::string directory = "./")
{
std::vector<std::string> result;
DIR* dirp = opendir(directory.c_str());
if (dirp)
{
while (true)
{
struct dirent* de = readdir(dirp);
if (de == NULL)
break;
if (fnmatch(wildcard.c_str(), de->d_name, 0))
continue;
else
result.push_back (std::string(de->d_name));
}
closedir(dirp);
}
std::sort(result.begin(), result.end());
return result;
}
我正在将此代码移植到 Windows,发现fnmatch
不可用(dirent
也不可用,但我可以根据以下 SO 链接找到一个。
有没有做完全相同事情的 fnmatch 替代函数?
如何在不破坏逻辑的情况下使此代码在VS2012中编译和运行?
感谢谢尔盖的帮助。这是我的最终解决方案,以防将来有人需要......
#ifdef _WIN32
#include "dirent.h"
#include "windows.h"
#include "shlwapi.h"
#else
#include <dirent.h>
#include <fnmatch.h>
#endif
std::vector<std::string> listFilenamesInPath(std::string wildcard = "*", std::string directory = "./")
{
std::vector<std::string> result;
DIR* dirp = opendir(directory.c_str());
if (dirp)
{
while (true)
{
struct dirent* de = readdir(dirp);
if (de == NULL)
break;
#ifdef _WIN32
wchar_t wname[1024];
wchar_t wmask[1024];
size_t outsize;
mbstowcs_s(&outsize, wname, de->d_name, strlen(de->d_name) + 1);
mbstowcs_s(&outsize, wmask, wildcard.c_str(), strlen(wildcard.c_str()) + 1);
if (PathMatchSpecW(wname, wmask))
result.push_back (std::string(de->d_name));
else
continue;
#else
if (fnmatch(wildcard.c_str(), de->d_name, 0))
continue;
else
result.push_back (std::string(de->d_name));
#endif
}
closedir(dirp);
}
std::sort(result.begin(), result.end());
return result;
}
请评论是否可以改进...
看起来 PathMatchSpec 是你的人。
相关文章:
- C++中原子的替代品<variant>
- C++sscanf_s的替代品
- getline的替代品
- 什么是 std::function::argument_type 的替代品?
- 有DirectShow的替代品吗?
- 智能指针作为 QObject::d eleteLater() 的替代品
- 有没有一种方法可以只对斜杠的第一个实例应用fnmatch的FNM_PATHNAME标志
- C++中 mktime 的替代品
- 对于C++,是否有任何现代的 std::strchr() 替代品?
- shared_future是<void>condition_variable的合法替代品吗?
- 这是山塔的替代品
- STD :: Referent_wrapper/std :: std :: vector的selution_ptr替代品
- 更好的替代品?
- 排序向量上 std::find_if 和 std::bind2nd 的替代品
- 提升勒让德多项式的 gsl_sf_legendre_sphPlm_array() 替代品
- 标准::矢量的高性能替代品
- c++线程池:std::函数的替代品,用于将函数/lambdas传递给线程
- SHGetSpecialFolderLocation是否有新的替代品?
- cstdlib 的 rand() 的替代品
- 视窗fnmatch替代品