2013-03-13 16:22:44 +04:00
|
|
|
#include "opencv2/contrib.hpp"
|
2013-08-06 13:56:49 +04:00
|
|
|
#include "cvconfig.h"
|
2012-10-17 11:12:04 +04:00
|
|
|
|
2013-05-29 01:14:01 -07:00
|
|
|
#if defined(WIN32) || defined(_WIN32)
|
2012-10-17 11:12:04 +04:00
|
|
|
#include <windows.h>
|
|
|
|
#include <tchar.h>
|
|
|
|
#else
|
|
|
|
#include <dirent.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace cv
|
|
|
|
{
|
2013-03-22 20:37:49 +04:00
|
|
|
std::vector<String> Directory::GetListFiles( const String& path, const String & exten, bool addPath )
|
2012-10-17 11:12:04 +04:00
|
|
|
{
|
2013-03-22 20:37:49 +04:00
|
|
|
std::vector<String> list;
|
2012-10-17 11:12:04 +04:00
|
|
|
list.clear();
|
2013-03-22 20:37:49 +04:00
|
|
|
String path_f = path + "/" + exten;
|
2012-10-17 11:12:04 +04:00
|
|
|
#ifdef WIN32
|
2013-07-25 07:22:14 -07:00
|
|
|
#ifdef HAVE_WINRT
|
2013-07-23 06:44:57 -07:00
|
|
|
WIN32_FIND_DATAW FindFileData;
|
|
|
|
#else
|
2013-07-29 04:38:18 -07:00
|
|
|
WIN32_FIND_DATAA FindFileData;
|
2013-07-23 06:44:57 -07:00
|
|
|
#endif
|
|
|
|
HANDLE hFind;
|
2012-10-17 11:12:04 +04:00
|
|
|
|
2013-07-23 06:44:57 -07:00
|
|
|
#ifdef HAVE_WINRT
|
|
|
|
size_t size = mbstowcs(NULL, path_f.c_str(), path_f.size());
|
2013-07-29 04:38:18 -07:00
|
|
|
Ptr<wchar_t> wpath = new wchar_t[size+1];
|
2013-07-23 06:44:57 -07:00
|
|
|
wpath[size] = 0;
|
|
|
|
mbstowcs(wpath, path_f.c_str(), path_f.size());
|
2013-07-25 07:22:14 -07:00
|
|
|
hFind = FindFirstFileExW(wpath, FindExInfoStandard, &FindFileData, FindExSearchNameMatch, NULL, 0);
|
2013-07-23 06:44:57 -07:00
|
|
|
#else
|
|
|
|
hFind = FindFirstFileA((LPCSTR)path_f.c_str(), &FindFileData);
|
|
|
|
#endif
|
2012-10-17 11:12:04 +04: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-07-29 04:38:18 -07:00
|
|
|
cv::Ptr<char> fname;
|
2013-07-23 06:44:57 -07:00
|
|
|
#ifdef HAVE_WINRT
|
|
|
|
size_t asize = wcstombs(NULL, FindFileData.cFileName, 0);
|
2013-07-29 04:38:18 -07:00
|
|
|
fname = new char[asize+1];
|
2013-07-23 06:44:57 -07:00
|
|
|
fname[asize] = 0;
|
|
|
|
wcstombs(fname, FindFileData.cFileName, asize);
|
|
|
|
#else
|
|
|
|
fname = FindFileData.cFileName;
|
|
|
|
#endif
|
2012-10-17 11:12:04 +04:00
|
|
|
if (addPath)
|
2013-07-29 04:38:18 -07:00
|
|
|
list.push_back(path + "/" + std::string(fname));
|
2012-10-17 11:12:04 +04:00
|
|
|
else
|
2013-07-29 04:38:18 -07:00
|
|
|
list.push_back(std::string(fname));
|
2012-10-17 11:12:04 +04:00
|
|
|
}
|
|
|
|
}
|
2013-07-23 06:44:57 -07:00
|
|
|
#ifdef HAVE_WINRT
|
|
|
|
while(FindNextFileW(hFind, &FindFileData));
|
|
|
|
#else
|
|
|
|
while(FindNextFileA(hFind, &FindFileData));
|
|
|
|
#endif
|
2012-10-17 11:12:04 +04: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)
|
2013-03-22 20:37:49 +04:00
|
|
|
list.push_back(static_cast<String>(dirp->d_name));
|
2012-10-17 11:12:04 +04:00
|
|
|
else
|
2013-03-22 20:37:49 +04:00
|
|
|
if (String(dirp->d_name).find(exten) != String::npos)
|
|
|
|
list.push_back(static_cast<String>(dirp->d_name));
|
2012-10-17 11:12:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dp);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2013-03-22 20:37:49 +04:00
|
|
|
std::vector<String> Directory::GetListFolders( const String& path, const String & exten, bool addPath )
|
2012-10-17 11:12:04 +04:00
|
|
|
{
|
2013-03-22 20:37:49 +04:00
|
|
|
std::vector<String> list;
|
|
|
|
String path_f = path + "/" + exten;
|
2012-10-17 11:12:04 +04:00
|
|
|
list.clear();
|
|
|
|
#ifdef WIN32
|
2013-07-25 07:22:14 -07:00
|
|
|
#ifdef HAVE_WINRT
|
2013-07-23 06:44:57 -07:00
|
|
|
WIN32_FIND_DATAW FindFileData;
|
|
|
|
#else
|
2013-07-29 04:38:18 -07:00
|
|
|
WIN32_FIND_DATAA FindFileData;
|
2013-07-23 06:44:57 -07:00
|
|
|
#endif
|
2012-10-17 11:12:04 +04:00
|
|
|
HANDLE hFind;
|
|
|
|
|
2013-07-23 06:44:57 -07:00
|
|
|
#ifdef HAVE_WINRT
|
|
|
|
size_t size = mbstowcs(NULL, path_f.c_str(), path_f.size());
|
2013-07-29 04:38:18 -07:00
|
|
|
Ptr<wchar_t> wpath = new wchar_t[size+1];
|
2013-07-23 06:44:57 -07:00
|
|
|
wpath[size] = 0;
|
|
|
|
mbstowcs(wpath, path_f.c_str(), path_f.size());
|
2013-07-25 07:22:14 -07:00
|
|
|
hFind = FindFirstFileExW(wpath, FindExInfoStandard, &FindFileData, FindExSearchNameMatch, NULL, 0);
|
2013-07-23 06:44:57 -07:00
|
|
|
#else
|
|
|
|
hFind = FindFirstFileA((LPCSTR)path_f.c_str(), &FindFileData);
|
|
|
|
#endif
|
2012-10-17 11:12:04 +04:00
|
|
|
if (hFind == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2013-07-29 04:38:18 -07:00
|
|
|
#ifdef HAVE_WINRT
|
2012-10-17 03:18:30 +04:00
|
|
|
if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&
|
2013-07-25 07:22:14 -07:00
|
|
|
wcscmp(FindFileData.cFileName, L".") != 0 &&
|
|
|
|
wcscmp(FindFileData.cFileName, L"..") != 0)
|
2013-07-29 04:38:18 -07:00
|
|
|
#else
|
2012-10-17 11:12:04 +04:00
|
|
|
if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&
|
|
|
|
strcmp(FindFileData.cFileName, ".") != 0 &&
|
|
|
|
strcmp(FindFileData.cFileName, "..") != 0)
|
2013-07-29 04:38:18 -07:00
|
|
|
#endif
|
2012-10-17 11:12:04 +04:00
|
|
|
{
|
2013-07-29 04:38:18 -07:00
|
|
|
cv::Ptr<char> fname;
|
2013-07-23 06:44:57 -07:00
|
|
|
#ifdef HAVE_WINRT
|
|
|
|
size_t asize = wcstombs(NULL, FindFileData.cFileName, 0);
|
2013-07-29 04:38:18 -07:00
|
|
|
fname = new char[asize+1];
|
2013-07-23 06:44:57 -07:00
|
|
|
fname[asize] = 0;
|
|
|
|
wcstombs(fname, FindFileData.cFileName, asize);
|
|
|
|
#else
|
|
|
|
fname = FindFileData.cFileName;
|
|
|
|
#endif
|
|
|
|
|
2012-10-17 11:12:04 +04:00
|
|
|
if (addPath)
|
2013-07-29 04:38:18 -07:00
|
|
|
list.push_back(path + "/" + std::string(fname));
|
2012-10-17 11:12:04 +04:00
|
|
|
else
|
2013-07-29 04:38:18 -07:00
|
|
|
list.push_back(std::string(fname));
|
2012-10-17 11:12:04 +04:00
|
|
|
}
|
|
|
|
}
|
2013-07-23 06:44:57 -07:00
|
|
|
#ifdef HAVE_WINRT
|
|
|
|
while(FindNextFileW(hFind, &FindFileData));
|
|
|
|
#else
|
|
|
|
while(FindNextFileA(hFind, &FindFileData));
|
|
|
|
#endif
|
2012-10-17 11:12:04 +04: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)
|
2013-03-22 20:37:49 +04:00
|
|
|
list.push_back(static_cast<String>(dirp->d_name));
|
2012-10-17 11:12:04 +04:00
|
|
|
else
|
2013-03-22 20:37:49 +04:00
|
|
|
if (String(dirp->d_name).find(exten) != String::npos)
|
|
|
|
list.push_back(static_cast<String>(dirp->d_name));
|
2012-10-17 11:12:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dp);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2013-03-22 20:37:49 +04:00
|
|
|
std::vector<String> Directory::GetListFilesR ( const String& path, const String & exten, bool addPath )
|
2012-10-17 11:12:04 +04:00
|
|
|
{
|
2013-03-22 20:37:49 +04:00
|
|
|
std::vector<String> list = Directory::GetListFiles(path, exten, addPath);
|
2012-10-17 11:12:04 +04:00
|
|
|
|
2013-03-22 20:37:49 +04:00
|
|
|
std::vector<String> dirs = Directory::GetListFolders(path, exten, addPath);
|
2012-10-17 11:12:04 +04:00
|
|
|
|
2013-03-22 20:37:49 +04:00
|
|
|
std::vector<String>::const_iterator it;
|
2012-10-17 11:12:04 +04:00
|
|
|
for (it = dirs.begin(); it != dirs.end(); ++it)
|
|
|
|
{
|
2013-03-22 20:37:49 +04:00
|
|
|
std::vector<String> cl = Directory::GetListFiles(*it, exten, addPath);
|
2012-10-17 11:12:04 +04:00
|
|
|
list.insert(list.end(), cl.begin(), cl.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|