CLAHE Python bindings
[profile/ivi/opencv.git] / modules / contrib / src / inputoutput.cpp
1
2 #include "opencv2/contrib/contrib.hpp"
3
4 #ifdef WIN32
5     #include <windows.h>
6     #include <tchar.h>
7 #else
8     #include <dirent.h>
9 #endif
10
11 namespace cv
12 {
13     std::vector<std::string> Directory::GetListFiles(  const std::string& path, const std::string & exten, bool addPath )
14     {
15         std::vector<std::string> list;
16         list.clear();
17         std::string path_f = path + "/" + exten;
18         #ifdef WIN32
19             WIN32_FIND_DATA FindFileData;
20             HANDLE hFind;
21
22             hFind = FindFirstFile((LPCSTR)path_f.c_str(), &FindFileData);
23             if (hFind == INVALID_HANDLE_VALUE)
24             {
25                 return list;
26             }
27             else
28             {
29                 do
30                 {
31                     if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL  ||
32                         FindFileData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE ||
33                         FindFileData.dwFileAttributes == FILE_ATTRIBUTE_HIDDEN  ||
34                         FindFileData.dwFileAttributes == FILE_ATTRIBUTE_SYSTEM  ||
35                         FindFileData.dwFileAttributes == FILE_ATTRIBUTE_READONLY)
36                     {
37                         if (addPath)
38                             list.push_back(path + "/" + FindFileData.cFileName);
39                         else
40                             list.push_back(FindFileData.cFileName);
41                     }
42                 }
43                 while(FindNextFile(hFind, &FindFileData));
44                 FindClose(hFind);
45             }
46         #else
47             (void)addPath;
48             DIR *dp;
49             struct dirent *dirp;
50             if((dp = opendir(path.c_str())) == NULL)
51             {
52                 return list;
53             }
54
55             while ((dirp = readdir(dp)) != NULL)
56             {
57                 if (dirp->d_type == DT_REG)
58                 {
59                     if (exten.compare("*") == 0)
60                         list.push_back(static_cast<std::string>(dirp->d_name));
61                     else
62                         if (std::string(dirp->d_name).find(exten) != std::string::npos)
63                             list.push_back(static_cast<std::string>(dirp->d_name));
64                 }
65             }
66             closedir(dp);
67         #endif
68
69         return list;
70     }
71
72     std::vector<std::string> Directory::GetListFolders( const std::string& path, const std::string & exten, bool addPath )
73     {
74         std::vector<std::string> list;
75         std::string path_f = path + "/" + exten;
76         list.clear();
77         #ifdef WIN32
78             WIN32_FIND_DATA FindFileData;
79             HANDLE hFind;
80
81             hFind = FindFirstFile((LPCSTR)path_f.c_str(), &FindFileData);
82             if (hFind == INVALID_HANDLE_VALUE)
83             {
84                 return list;
85             }
86             else
87             {
88                 do
89                 {
90                     if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&
91                         strcmp(FindFileData.cFileName, ".") != 0 &&
92                         strcmp(FindFileData.cFileName, "..") != 0)
93                     {
94                         if (addPath)
95                             list.push_back(path + "/" + FindFileData.cFileName);
96                         else
97                             list.push_back(FindFileData.cFileName);
98                     }
99                 }
100                 while(FindNextFile(hFind, &FindFileData));
101                 FindClose(hFind);
102             }
103
104         #else
105             (void)addPath;
106             DIR *dp;
107             struct dirent *dirp;
108             if((dp = opendir(path_f.c_str())) == NULL)
109             {
110                 return list;
111             }
112
113             while ((dirp = readdir(dp)) != NULL)
114             {
115                 if (dirp->d_type == DT_DIR &&
116                     strcmp(dirp->d_name, ".") != 0 &&
117                     strcmp(dirp->d_name, "..") != 0 )
118                 {
119                     if (exten.compare("*") == 0)
120                         list.push_back(static_cast<std::string>(dirp->d_name));
121                     else
122                         if (std::string(dirp->d_name).find(exten) != std::string::npos)
123                             list.push_back(static_cast<std::string>(dirp->d_name));
124                 }
125             }
126             closedir(dp);
127         #endif
128
129         return list;
130     }
131
132     std::vector<std::string> Directory::GetListFilesR ( const std::string& path, const std::string & exten, bool addPath )
133     {
134         std::vector<std::string> list = Directory::GetListFiles(path, exten, addPath);
135
136         std::vector<std::string> dirs = Directory::GetListFolders(path, exten, addPath);
137
138         std::vector<std::string>::const_iterator it;
139         for (it = dirs.begin(); it != dirs.end(); ++it)
140         {
141             std::vector<std::string> cl = Directory::GetListFiles(*it, exten, addPath);
142             list.insert(list.end(), cl.begin(), cl.end());
143         }
144
145         return list;
146     }
147
148 }