Merge pull request #1263 from abidrahmank:pyCLAHE_24
[profile/ivi/opencv.git] / modules / contrib / src / inputoutput.cpp
1
2 #include "opencv2/contrib/contrib.hpp"
3 #include <cvconfig.h>
4
5 #if defined(WIN32) || defined(_WIN32)
6     #include <windows.h>
7     #include <tchar.h>
8 #else
9     #include <dirent.h>
10 #endif
11
12 namespace cv
13 {
14     std::vector<std::string> Directory::GetListFiles(  const std::string& path, const std::string & exten, bool addPath )
15     {
16         std::vector<std::string> list;
17         list.clear();
18         std::string path_f = path + "/" + exten;
19         #ifdef WIN32
20         #ifdef HAVE_WINRT
21             WIN32_FIND_DATAW FindFileData;
22         #else
23             WIN32_FIND_DATAA FindFileData;
24         #endif
25         HANDLE hFind;
26
27         #ifdef HAVE_WINRT
28             size_t size = mbstowcs(NULL, path_f.c_str(), path_f.size());
29             Ptr<wchar_t> wpath = new wchar_t[size+1];
30             wpath[size] = 0;
31             mbstowcs(wpath, path_f.c_str(), path_f.size());
32             hFind = FindFirstFileExW(wpath, FindExInfoStandard, &FindFileData, FindExSearchNameMatch, NULL, 0);
33         #else
34             hFind = FindFirstFileA((LPCSTR)path_f.c_str(), &FindFileData);
35         #endif
36             if (hFind == INVALID_HANDLE_VALUE)
37             {
38                 return list;
39             }
40             else
41             {
42                 do
43                 {
44                     if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL  ||
45                         FindFileData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE ||
46                         FindFileData.dwFileAttributes == FILE_ATTRIBUTE_HIDDEN  ||
47                         FindFileData.dwFileAttributes == FILE_ATTRIBUTE_SYSTEM  ||
48                         FindFileData.dwFileAttributes == FILE_ATTRIBUTE_READONLY)
49                     {
50                         cv::Ptr<char> fname;
51                     #ifdef HAVE_WINRT
52                         size_t asize = wcstombs(NULL, FindFileData.cFileName, 0);
53                         fname = new char[asize+1];
54                         fname[asize] = 0;
55                         wcstombs(fname, FindFileData.cFileName, asize);
56                     #else
57                         fname = FindFileData.cFileName;
58                     #endif
59                         if (addPath)
60                             list.push_back(path + "/" + std::string(fname));
61                         else
62                             list.push_back(std::string(fname));
63                     }
64                 }
65             #ifdef HAVE_WINRT
66                 while(FindNextFileW(hFind, &FindFileData));
67             #else
68                 while(FindNextFileA(hFind, &FindFileData));
69             #endif
70                 FindClose(hFind);
71             }
72         #else
73             (void)addPath;
74             DIR *dp;
75             struct dirent *dirp;
76             if((dp = opendir(path.c_str())) == NULL)
77             {
78                 return list;
79             }
80
81             while ((dirp = readdir(dp)) != NULL)
82             {
83                 if (dirp->d_type == DT_REG)
84                 {
85                     if (exten.compare("*") == 0)
86                         list.push_back(static_cast<std::string>(dirp->d_name));
87                     else
88                         if (std::string(dirp->d_name).find(exten) != std::string::npos)
89                             list.push_back(static_cast<std::string>(dirp->d_name));
90                 }
91             }
92             closedir(dp);
93         #endif
94
95         return list;
96     }
97
98     std::vector<std::string> Directory::GetListFolders( const std::string& path, const std::string & exten, bool addPath )
99     {
100         std::vector<std::string> list;
101         std::string path_f = path + "/" + exten;
102         list.clear();
103         #ifdef WIN32
104         #ifdef HAVE_WINRT
105             WIN32_FIND_DATAW FindFileData;
106         #else
107             WIN32_FIND_DATAA FindFileData;
108         #endif
109             HANDLE hFind;
110
111         #ifdef HAVE_WINRT
112             size_t size = mbstowcs(NULL, path_f.c_str(), path_f.size());
113             Ptr<wchar_t> wpath = new wchar_t[size+1];
114             wpath[size] = 0;
115             mbstowcs(wpath, path_f.c_str(), path_f.size());
116             hFind = FindFirstFileExW(wpath, FindExInfoStandard, &FindFileData, FindExSearchNameMatch, NULL, 0);
117         #else
118             hFind = FindFirstFileA((LPCSTR)path_f.c_str(), &FindFileData);
119         #endif
120             if (hFind == INVALID_HANDLE_VALUE)
121             {
122                 return list;
123             }
124             else
125             {
126                 do
127                 {
128 #ifdef HAVE_WINRT
129                     if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&
130                         wcscmp(FindFileData.cFileName, L".") != 0 &&
131                         wcscmp(FindFileData.cFileName, L"..") != 0)
132 #else
133                     if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&
134                         strcmp(FindFileData.cFileName, ".") != 0 &&
135                         strcmp(FindFileData.cFileName, "..") != 0)
136 #endif
137                     {
138                         cv::Ptr<char> fname;
139                     #ifdef HAVE_WINRT
140                         size_t asize = wcstombs(NULL, FindFileData.cFileName, 0);
141                         fname = new char[asize+1];
142                         fname[asize] = 0;
143                         wcstombs(fname, FindFileData.cFileName, asize);
144                     #else
145                         fname = FindFileData.cFileName;
146                     #endif
147
148                         if (addPath)
149                             list.push_back(path + "/" + std::string(fname));
150                         else
151                             list.push_back(std::string(fname));
152                     }
153                 }
154             #ifdef HAVE_WINRT
155                 while(FindNextFileW(hFind, &FindFileData));
156             #else
157                 while(FindNextFileA(hFind, &FindFileData));
158             #endif
159                 FindClose(hFind);
160             }
161
162         #else
163             (void)addPath;
164             DIR *dp;
165             struct dirent *dirp;
166             if((dp = opendir(path_f.c_str())) == NULL)
167             {
168                 return list;
169             }
170
171             while ((dirp = readdir(dp)) != NULL)
172             {
173                 if (dirp->d_type == DT_DIR &&
174                     strcmp(dirp->d_name, ".") != 0 &&
175                     strcmp(dirp->d_name, "..") != 0 )
176                 {
177                     if (exten.compare("*") == 0)
178                         list.push_back(static_cast<std::string>(dirp->d_name));
179                     else
180                         if (std::string(dirp->d_name).find(exten) != std::string::npos)
181                             list.push_back(static_cast<std::string>(dirp->d_name));
182                 }
183             }
184             closedir(dp);
185         #endif
186
187         return list;
188     }
189
190     std::vector<std::string> Directory::GetListFilesR ( const std::string& path, const std::string & exten, bool addPath )
191     {
192         std::vector<std::string> list = Directory::GetListFiles(path, exten, addPath);
193
194         std::vector<std::string> dirs = Directory::GetListFolders(path, exten, addPath);
195
196         std::vector<std::string>::const_iterator it;
197         for (it = dirs.begin(); it != dirs.end(); ++it)
198         {
199             std::vector<std::string> cl = Directory::GetListFiles(*it, exten, addPath);
200             list.insert(list.end(), cl.begin(), cl.end());
201         }
202
203         return list;
204     }
205
206 }