Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/core/src/glob.cpp
16337 views
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
#include "opencv2/core/utils/filesystem.hpp"
46
47
#if defined _WIN32 || defined WINCE
48
# include <windows.h>
49
const char dir_separators[] = "/\\";
50
51
namespace
52
{
53
struct dirent
54
{
55
const char* d_name;
56
};
57
58
struct DIR
59
{
60
#ifdef WINRT
61
WIN32_FIND_DATAW data;
62
#else
63
WIN32_FIND_DATA data;
64
#endif
65
HANDLE handle;
66
dirent ent;
67
#ifdef WINRT
68
DIR() { }
69
~DIR()
70
{
71
if (ent.d_name)
72
delete[] ent.d_name;
73
}
74
#endif
75
};
76
77
DIR* opendir(const char* path)
78
{
79
DIR* dir = new DIR;
80
dir->ent.d_name = 0;
81
#ifdef WINRT
82
cv::String full_path = cv::String(path) + "\\*";
83
wchar_t wfull_path[MAX_PATH];
84
size_t copied = mbstowcs(wfull_path, full_path.c_str(), MAX_PATH);
85
CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
86
dir->handle = ::FindFirstFileExW(wfull_path, FindExInfoStandard,
87
&dir->data, FindExSearchNameMatch, NULL, 0);
88
#else
89
dir->handle = ::FindFirstFileExA((cv::String(path) + "\\*").c_str(),
90
FindExInfoStandard, &dir->data, FindExSearchNameMatch, NULL, 0);
91
#endif
92
if(dir->handle == INVALID_HANDLE_VALUE)
93
{
94
/*closedir will do all cleanup*/
95
delete dir;
96
return 0;
97
}
98
return dir;
99
}
100
101
dirent* readdir(DIR* dir)
102
{
103
#ifdef WINRT
104
if (dir->ent.d_name != 0)
105
{
106
if (::FindNextFileW(dir->handle, &dir->data) != TRUE)
107
return 0;
108
}
109
size_t asize = wcstombs(NULL, dir->data.cFileName, 0);
110
CV_Assert((asize != 0) && (asize != (size_t)-1));
111
char* aname = new char[asize+1];
112
aname[asize] = 0;
113
wcstombs(aname, dir->data.cFileName, asize);
114
dir->ent.d_name = aname;
115
#else
116
if (dir->ent.d_name != 0)
117
{
118
if (::FindNextFileA(dir->handle, &dir->data) != TRUE)
119
return 0;
120
}
121
dir->ent.d_name = dir->data.cFileName;
122
#endif
123
return &dir->ent;
124
}
125
126
void closedir(DIR* dir)
127
{
128
::FindClose(dir->handle);
129
delete dir;
130
}
131
132
133
}
134
#else
135
# include <dirent.h>
136
# include <sys/stat.h>
137
const char dir_separators[] = "/";
138
#endif
139
140
static bool isDir(const cv::String& path, DIR* dir)
141
{
142
#if defined _WIN32 || defined WINCE
143
DWORD attributes;
144
BOOL status = TRUE;
145
if (dir)
146
attributes = dir->data.dwFileAttributes;
147
else
148
{
149
WIN32_FILE_ATTRIBUTE_DATA all_attrs;
150
#ifdef WINRT
151
wchar_t wpath[MAX_PATH];
152
size_t copied = mbstowcs(wpath, path.c_str(), MAX_PATH);
153
CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
154
status = ::GetFileAttributesExW(wpath, GetFileExInfoStandard, &all_attrs);
155
#else
156
status = ::GetFileAttributesExA(path.c_str(), GetFileExInfoStandard, &all_attrs);
157
#endif
158
attributes = all_attrs.dwFileAttributes;
159
}
160
161
return status && ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
162
#else
163
CV_UNUSED(dir);
164
struct stat stat_buf;
165
if (0 != stat( path.c_str(), &stat_buf))
166
return false;
167
int is_dir = S_ISDIR( stat_buf.st_mode);
168
return is_dir != 0;
169
#endif
170
}
171
172
bool cv::utils::fs::isDirectory(const cv::String& path)
173
{
174
CV_INSTRUMENT_REGION();
175
return isDir(path, NULL);
176
}
177
178
static bool wildcmp(const char *string, const char *wild)
179
{
180
// Based on wildcmp written by Jack Handy - <A href="mailto:[email protected]">[email protected]</A>
181
const char *cp = 0, *mp = 0;
182
183
while ((*string) && (*wild != '*'))
184
{
185
if ((*wild != *string) && (*wild != '?'))
186
{
187
return false;
188
}
189
190
wild++;
191
string++;
192
}
193
194
while (*string)
195
{
196
if (*wild == '*')
197
{
198
if (!*++wild)
199
{
200
return true;
201
}
202
203
mp = wild;
204
cp = string + 1;
205
}
206
else if ((*wild == *string) || (*wild == '?'))
207
{
208
wild++;
209
string++;
210
}
211
else
212
{
213
wild = mp;
214
string = cp++;
215
}
216
}
217
218
while (*wild == '*')
219
{
220
wild++;
221
}
222
223
return *wild == 0;
224
}
225
226
static void glob_rec(const cv::String& directory, const cv::String& wildchart, std::vector<cv::String>& result,
227
bool recursive, bool includeDirectories, const cv::String& pathPrefix)
228
{
229
DIR *dir;
230
231
if ((dir = opendir (directory.c_str())) != 0)
232
{
233
/* find all the files and directories within directory */
234
CV_TRY
235
{
236
struct dirent *ent;
237
while ((ent = readdir (dir)) != 0)
238
{
239
const char* name = ent->d_name;
240
if((name[0] == 0) || (name[0] == '.' && name[1] == 0) || (name[0] == '.' && name[1] == '.' && name[2] == 0))
241
continue;
242
243
cv::String path = cv::utils::fs::join(directory, name);
244
cv::String entry = cv::utils::fs::join(pathPrefix, name);
245
246
if (isDir(path, dir))
247
{
248
if (recursive)
249
glob_rec(path, wildchart, result, recursive, includeDirectories, entry);
250
if (!includeDirectories)
251
continue;
252
}
253
254
if (wildchart.empty() || wildcmp(name, wildchart.c_str()))
255
result.push_back(entry);
256
}
257
}
258
CV_CATCH_ALL
259
{
260
closedir(dir);
261
CV_RETHROW();
262
}
263
closedir(dir);
264
}
265
else
266
{
267
CV_Error_(CV_StsObjectNotFound, ("could not open directory: %s", directory.c_str()));
268
}
269
}
270
271
void cv::glob(String pattern, std::vector<String>& result, bool recursive)
272
{
273
CV_INSTRUMENT_REGION();
274
275
result.clear();
276
String path, wildchart;
277
278
if (isDir(pattern, 0))
279
{
280
if(strchr(dir_separators, pattern[pattern.size() - 1]) != 0)
281
{
282
path = pattern.substr(0, pattern.size() - 1);
283
}
284
else
285
{
286
path = pattern;
287
}
288
}
289
else
290
{
291
size_t pos = pattern.find_last_of(dir_separators);
292
if (pos == String::npos)
293
{
294
wildchart = pattern;
295
path = ".";
296
}
297
else
298
{
299
path = pattern.substr(0, pos);
300
wildchart = pattern.substr(pos + 1);
301
}
302
}
303
304
glob_rec(path, wildchart, result, recursive, false, path);
305
std::sort(result.begin(), result.end());
306
}
307
308
void cv::utils::fs::glob(const cv::String& directory, const cv::String& pattern,
309
std::vector<cv::String>& result,
310
bool recursive, bool includeDirectories)
311
{
312
glob_rec(directory, pattern, result, recursive, includeDirectories, directory);
313
std::sort(result.begin(), result.end());
314
}
315
316
void cv::utils::fs::glob_relative(const cv::String& directory, const cv::String& pattern,
317
std::vector<cv::String>& result,
318
bool recursive, bool includeDirectories)
319
{
320
glob_rec(directory, pattern, result, recursive, includeDirectories, cv::String());
321
std::sort(result.begin(), result.end());
322
}
323
324