2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
#include "opencv2/contrib/contrib.hpp"
|
2013-07-25 22:22:14 +08:00
|
|
|
#include <cvconfig.h>
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2013-05-29 16:14:01 +08:00
|
|
|
#if defined(WIN32) || defined(_WIN32)
|
2012-10-17 07:18:30 +08:00
|
|
|
#include <windows.h>
|
|
|
|
#include <tchar.h>
|
|
|
|
#else
|
|
|
|
#include <dirent.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace cv
|
|
|
|
{
|
2013-08-19 16:25:53 +08:00
|
|
|
std::vector<std::string> Directory::GetListFiles( const std::string& path, const std::string & exten, bool addPath )
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
|
|
|
std::vector<std::string> list;
|
|
|
|
list.clear();
|
|
|
|
std::string path_f = path + "/" + exten;
|
|
|
|
#ifdef WIN32
|
2013-07-25 22:22:14 +08:00
|
|
|
#ifdef HAVE_WINRT
|
2013-07-23 21:44:57 +08:00
|
|
|
WIN32_FIND_DATAW FindFileData;
|
|
|
|
#else
|
2013-07-29 19:38:18 +08:00
|
|
|
WIN32_FIND_DATAA FindFileData;
|
2013-07-23 21:44:57 +08:00
|
|
|
#endif
|
|
|
|
HANDLE hFind;
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2013-07-23 21:44:57 +08:00
|
|
|
#ifdef HAVE_WINRT
|
2013-08-19 16:25:53 +08:00
|
|
|
wchar_t wpath[MAX_PATH];
|
|
|
|
size_t copied = mbstowcs(wpath, path_f.c_str(), MAX_PATH);
|
|
|
|
CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
|
2013-07-25 22:22:14 +08:00
|
|
|
hFind = FindFirstFileExW(wpath, FindExInfoStandard, &FindFileData, FindExSearchNameMatch, NULL, 0);
|
2013-07-23 21:44:57 +08:00
|
|
|
#else
|
|
|
|
hFind = FindFirstFileA((LPCSTR)path_f.c_str(), &FindFileData);
|
|
|
|
#endif
|
2012-10-17 07:18:30 +08:00
|
|
|
if (hFind == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL ||
|
|
|
|
FindFileData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE ||
|
|
|
|
FindFileData.dwFileAttributes == FILE_ATTRIBUTE_HIDDEN ||
|
|
|
|
FindFileData.dwFileAttributes == FILE_ATTRIBUTE_SYSTEM ||
|
|
|
|
FindFileData.dwFileAttributes == FILE_ATTRIBUTE_READONLY)
|
|
|
|
{
|
2013-08-19 16:25:53 +08:00
|
|
|
char* fname;
|
2013-07-23 21:44:57 +08:00
|
|
|
#ifdef HAVE_WINRT
|
2013-08-19 16:25:53 +08:00
|
|
|
char fname_tmp[MAX_PATH] = {0};
|
|
|
|
size_t copied = wcstombs(fname_tmp, FindFileData.cFileName, MAX_PATH);
|
|
|
|
CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
|
|
|
|
fname = fname_tmp;
|
2013-07-23 21:44:57 +08:00
|
|
|
#else
|
|
|
|
fname = FindFileData.cFileName;
|
|
|
|
#endif
|
2012-10-17 07:18:30 +08:00
|
|
|
if (addPath)
|
2013-07-29 19:38:18 +08:00
|
|
|
list.push_back(path + "/" + std::string(fname));
|
2012-10-17 07:18:30 +08:00
|
|
|
else
|
2013-07-29 19:38:18 +08:00
|
|
|
list.push_back(std::string(fname));
|
2012-10-17 07:18:30 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-23 21:44:57 +08:00
|
|
|
#ifdef HAVE_WINRT
|
|
|
|
while(FindNextFileW(hFind, &FindFileData));
|
|
|
|
#else
|
|
|
|
while(FindNextFileA(hFind, &FindFileData));
|
|
|
|
#endif
|
2012-10-17 07:18:30 +08:00
|
|
|
FindClose(hFind);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
(void)addPath;
|
|
|
|
DIR *dp;
|
|
|
|
struct dirent *dirp;
|
|
|
|
if((dp = opendir(path.c_str())) == NULL)
|
|
|
|
{
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((dirp = readdir(dp)) != NULL)
|
|
|
|
{
|
|
|
|
if (dirp->d_type == DT_REG)
|
|
|
|
{
|
|
|
|
if (exten.compare("*") == 0)
|
|
|
|
list.push_back(static_cast<std::string>(dirp->d_name));
|
|
|
|
else
|
|
|
|
if (std::string(dirp->d_name).find(exten) != std::string::npos)
|
|
|
|
list.push_back(static_cast<std::string>(dirp->d_name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dp);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> Directory::GetListFolders( const std::string& path, const std::string & exten, bool addPath )
|
|
|
|
{
|
|
|
|
std::vector<std::string> list;
|
|
|
|
std::string path_f = path + "/" + exten;
|
|
|
|
list.clear();
|
|
|
|
#ifdef WIN32
|
2013-07-25 22:22:14 +08:00
|
|
|
#ifdef HAVE_WINRT
|
2013-07-23 21:44:57 +08:00
|
|
|
WIN32_FIND_DATAW FindFileData;
|
|
|
|
#else
|
2013-07-29 19:38:18 +08:00
|
|
|
WIN32_FIND_DATAA FindFileData;
|
2013-07-23 21:44:57 +08:00
|
|
|
#endif
|
2012-10-17 07:18:30 +08:00
|
|
|
HANDLE hFind;
|
|
|
|
|
2013-07-23 21:44:57 +08:00
|
|
|
#ifdef HAVE_WINRT
|
2013-08-19 16:25:53 +08:00
|
|
|
wchar_t wpath [MAX_PATH];
|
|
|
|
size_t copied = mbstowcs(wpath, path_f.c_str(), path_f.size());
|
|
|
|
CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
|
|
|
|
|
2013-07-25 22:22:14 +08:00
|
|
|
hFind = FindFirstFileExW(wpath, FindExInfoStandard, &FindFileData, FindExSearchNameMatch, NULL, 0);
|
2013-07-23 21:44:57 +08:00
|
|
|
#else
|
|
|
|
hFind = FindFirstFileA((LPCSTR)path_f.c_str(), &FindFileData);
|
|
|
|
#endif
|
2012-10-17 07:18:30 +08:00
|
|
|
if (hFind == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2013-07-29 19:38:18 +08:00
|
|
|
#ifdef HAVE_WINRT
|
2012-10-17 07:18:30 +08:00
|
|
|
if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&
|
2013-07-25 22:22:14 +08:00
|
|
|
wcscmp(FindFileData.cFileName, L".") != 0 &&
|
|
|
|
wcscmp(FindFileData.cFileName, L"..") != 0)
|
2013-07-29 19:38:18 +08:00
|
|
|
#else
|
|
|
|
if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&
|
|
|
|
strcmp(FindFileData.cFileName, ".") != 0 &&
|
|
|
|
strcmp(FindFileData.cFileName, "..") != 0)
|
|
|
|
#endif
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
2013-08-19 16:25:53 +08:00
|
|
|
char* fname;
|
2013-07-23 21:44:57 +08:00
|
|
|
#ifdef HAVE_WINRT
|
2013-08-19 16:25:53 +08:00
|
|
|
char fname_tmp[MAX_PATH];
|
2013-08-22 14:59:27 +08:00
|
|
|
size_t copied = wcstombs(fname_tmp, FindFileData.cFileName, MAX_PATH);
|
2013-08-19 16:25:53 +08:00
|
|
|
CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
|
|
|
|
fname = fname_tmp;
|
2013-07-23 21:44:57 +08:00
|
|
|
#else
|
|
|
|
fname = FindFileData.cFileName;
|
|
|
|
#endif
|
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
if (addPath)
|
2013-07-29 19:38:18 +08:00
|
|
|
list.push_back(path + "/" + std::string(fname));
|
2012-10-17 07:18:30 +08:00
|
|
|
else
|
2013-07-29 19:38:18 +08:00
|
|
|
list.push_back(std::string(fname));
|
2012-10-17 07:18:30 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-23 21:44:57 +08:00
|
|
|
#ifdef HAVE_WINRT
|
|
|
|
while(FindNextFileW(hFind, &FindFileData));
|
|
|
|
#else
|
|
|
|
while(FindNextFileA(hFind, &FindFileData));
|
|
|
|
#endif
|
2012-10-17 07:18:30 +08:00
|
|
|
FindClose(hFind);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
(void)addPath;
|
|
|
|
DIR *dp;
|
|
|
|
struct dirent *dirp;
|
|
|
|
if((dp = opendir(path_f.c_str())) == NULL)
|
|
|
|
{
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((dirp = readdir(dp)) != NULL)
|
|
|
|
{
|
|
|
|
if (dirp->d_type == DT_DIR &&
|
|
|
|
strcmp(dirp->d_name, ".") != 0 &&
|
|
|
|
strcmp(dirp->d_name, "..") != 0 )
|
|
|
|
{
|
|
|
|
if (exten.compare("*") == 0)
|
|
|
|
list.push_back(static_cast<std::string>(dirp->d_name));
|
|
|
|
else
|
|
|
|
if (std::string(dirp->d_name).find(exten) != std::string::npos)
|
|
|
|
list.push_back(static_cast<std::string>(dirp->d_name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dp);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> Directory::GetListFilesR ( const std::string& path, const std::string & exten, bool addPath )
|
|
|
|
{
|
|
|
|
std::vector<std::string> list = Directory::GetListFiles(path, exten, addPath);
|
|
|
|
|
|
|
|
std::vector<std::string> dirs = Directory::GetListFolders(path, exten, addPath);
|
|
|
|
|
|
|
|
std::vector<std::string>::const_iterator it;
|
|
|
|
for (it = dirs.begin(); it != dirs.end(); ++it)
|
|
|
|
{
|
|
|
|
std::vector<std::string> cl = Directory::GetListFiles(*it, exten, addPath);
|
|
|
|
list.insert(list.end(), cl.begin(), cl.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|