CLAHE Python bindings
[profile/ivi/opencv.git] / modules / core / src / glob.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2008-2013, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and / or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 #include "precomp.hpp"
44
45 #if defined WIN32 || defined _WIN32 || defined WINCE
46 # include <windows.h>
47 const char dir_separators[] = "/\\";
48 const char native_separator = '\\';
49
50 namespace
51 {
52     struct dirent
53     {
54         const char* d_name;
55     };
56
57     struct DIR
58     {
59         WIN32_FIND_DATA data;
60         HANDLE handle;
61         dirent ent;
62     };
63
64     DIR* opendir(const char* path)
65     {
66         DIR* dir = new DIR;
67         dir->ent.d_name = 0;
68         dir->handle = ::FindFirstFileA((cv::String(path) + "\\*").c_str(), &dir->data);
69         if(dir->handle == INVALID_HANDLE_VALUE)
70         {
71             /*closedir will do all cleanup*/
72             return 0;
73         }
74         return dir;
75     }
76
77     dirent* readdir(DIR* dir)
78     {
79         if (dir->ent.d_name != 0)
80         {
81             if (::FindNextFile(dir->handle, &dir->data) != TRUE)
82                 return 0;
83         }
84         dir->ent.d_name = dir->data.cFileName;
85         return &dir->ent;
86     }
87
88     void closedir(DIR* dir)
89     {
90         ::FindClose(dir->handle);
91         delete dir;
92     }
93
94
95 }
96 #else
97 # include <dirent.h>
98 # include <sys/stat.h>
99 const char dir_separators[] = "/";
100 const char native_separator = '/';
101 #endif
102
103 static bool isDir(const cv::String& path, DIR* dir)
104 {
105 #if defined WIN32 || defined _WIN32 || defined WINCE
106     DWORD attributes;
107     if (dir)
108         attributes = dir->data.dwFileAttributes;
109     else
110         attributes = ::GetFileAttributes(path.c_str());
111
112     return (attributes != INVALID_FILE_ATTRIBUTES) && ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
113 #else
114     (void)dir;
115     struct stat stat_buf;
116     if (0 != stat( path.c_str(), &stat_buf))
117         return false;
118     int is_dir = S_ISDIR( stat_buf.st_mode);
119     return is_dir != 0;
120 #endif
121 }
122
123 static bool wildcmp(const char *string, const char *wild)
124 {
125     // Based on wildcmp written by Jack Handy - <A href="mailto:jakkhandy@hotmail.com">jakkhandy@hotmail.com</A>
126     const char *cp = 0, *mp = 0;
127
128     while ((*string) && (*wild != '*'))
129     {
130         if ((*wild != *string) && (*wild != '?'))
131         {
132             return false;
133         }
134
135         wild++;
136         string++;
137     }
138
139     while (*string)
140     {
141         if (*wild == '*')
142         {
143             if (!*++wild)
144             {
145                 return true;
146             }
147
148             mp = wild;
149             cp = string + 1;
150         }
151         else if ((*wild == *string) || (*wild == '?'))
152         {
153             wild++;
154             string++;
155         }
156         else
157         {
158             wild = mp;
159             string = cp++;
160         }
161     }
162
163     while (*wild == '*')
164     {
165         wild++;
166     }
167
168     return *wild == 0;
169 }
170
171 static void glob_rec(const cv::String& directory, const cv::String& wildchart, std::vector<cv::String>& result, bool recursive)
172 {
173     DIR *dir;
174     struct dirent *ent;
175
176     if ((dir = opendir (directory.c_str())) != 0)
177     {
178         /* find all the files and directories within directory */
179         try
180         {
181             while ((ent = readdir (dir)) != 0)
182             {
183                 const char* name = ent->d_name;
184                 if((name[0] == 0) || (name[0] == '.' && name[1] == 0) || (name[0] == '.' && name[1] == '.' && name[2] == 0))
185                     continue;
186
187                 cv::String path = directory + native_separator + name;
188
189                 if (isDir(path, dir))
190                 {
191                     if (recursive)
192                         glob_rec(path, wildchart, result, recursive);
193                 }
194                 else
195                 {
196                     if (wildchart.empty() || wildcmp(name, wildchart.c_str()))
197                         result.push_back(path);
198                 }
199             }
200         }
201         catch (...)
202         {
203             closedir(dir);
204             throw;
205         }
206         closedir(dir);
207     }
208     else CV_Error(CV_StsObjectNotFound, cv::format("could not open directory: %s", directory.c_str()));
209 }
210
211 void cv::glob(String pattern, std::vector<String>& result, bool recursive)
212 {
213     result.clear();
214     String path, wildchart;
215
216     if (isDir(pattern, 0))
217     {
218         if(strchr(dir_separators, pattern[pattern.size() - 1]) != 0)
219         {
220             path = pattern.substr(0, pattern.size() - 1);
221         }
222         else
223         {
224             path = pattern;
225         }
226     }
227     else
228     {
229         size_t pos = pattern.find_last_of(dir_separators);
230         if (pos == String::npos)
231         {
232             wildchart = pattern;
233             path = ".";
234         }
235         else
236         {
237             path = pattern.substr(0, pos);
238             wildchart = pattern.substr(pos + 1);
239         }
240     }
241
242     glob_rec(path, wildchart, result, recursive);
243     std::sort(result.begin(), result.end());
244 }