1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
43 #include "precomp.hpp"
45 #if defined WIN32 || defined _WIN32 || defined WINCE
47 const char dir_separators[] = "/\\";
48 const char native_separator = '\\';
64 DIR* opendir(const char* path)
68 dir->handle = ::FindFirstFileA((cv::String(path) + "\\*").c_str(), &dir->data);
69 if(dir->handle == INVALID_HANDLE_VALUE)
71 /*closedir will do all cleanup*/
77 dirent* readdir(DIR* dir)
79 if (dir->ent.d_name != 0)
81 if (::FindNextFile(dir->handle, &dir->data) != TRUE)
84 dir->ent.d_name = dir->data.cFileName;
88 void closedir(DIR* dir)
90 ::FindClose(dir->handle);
98 # include <sys/stat.h>
99 const char dir_separators[] = "/";
100 const char native_separator = '/';
103 static bool isDir(const cv::String& path, DIR* dir)
105 #if defined WIN32 || defined _WIN32 || defined WINCE
108 attributes = dir->data.dwFileAttributes;
110 attributes = ::GetFileAttributes(path.c_str());
112 return (attributes != INVALID_FILE_ATTRIBUTES) && ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
115 struct stat stat_buf;
116 if (0 != stat( path.c_str(), &stat_buf))
118 int is_dir = S_ISDIR( stat_buf.st_mode);
123 static bool wildcmp(const char *string, const char *wild)
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;
128 while ((*string) && (*wild != '*'))
130 if ((*wild != *string) && (*wild != '?'))
151 else if ((*wild == *string) || (*wild == '?'))
171 static void glob_rec(const cv::String& directory, const cv::String& wildchart, std::vector<cv::String>& result, bool recursive)
176 if ((dir = opendir (directory.c_str())) != 0)
178 /* find all the files and directories within directory */
181 while ((ent = readdir (dir)) != 0)
183 const char* name = ent->d_name;
184 if((name[0] == 0) || (name[0] == '.' && name[1] == 0) || (name[0] == '.' && name[1] == '.' && name[2] == 0))
187 cv::String path = directory + native_separator + name;
189 if (isDir(path, dir))
192 glob_rec(path, wildchart, result, recursive);
196 if (wildchart.empty() || wildcmp(name, wildchart.c_str()))
197 result.push_back(path);
208 else CV_Error(CV_StsObjectNotFound, cv::format("could not open directory: %s", directory.c_str()));
211 void cv::glob(String pattern, std::vector<String>& result, bool recursive)
214 String path, wildchart;
216 if (isDir(pattern, 0))
218 if(strchr(dir_separators, pattern[pattern.size() - 1]) != 0)
220 path = pattern.substr(0, pattern.size() - 1);
229 size_t pos = pattern.find_last_of(dir_separators);
230 if (pos == String::npos)
237 path = pattern.substr(0, pos);
238 wildchart = pattern.substr(pos + 1);
242 glob_rec(path, wildchart, result, recursive);
243 std::sort(result.begin(), result.end());