glob.cpp 9.09 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2008-2013, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and / or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "precomp.hpp"

45 46
#include "opencv2/core/utils/filesystem.hpp"

47
#if defined _WIN32 || defined WINCE
48 49 50 51 52 53 54 55 56 57 58 59
# include <windows.h>
const char dir_separators[] = "/\\";

namespace
{
    struct dirent
    {
        const char* d_name;
    };

    struct DIR
    {
60
#ifdef WINRT
61 62
        WIN32_FIND_DATAW data;
#else
63
        WIN32_FIND_DATA data;
64
#endif
65 66
        HANDLE handle;
        dirent ent;
67
#ifdef WINRT
68
        DIR() { }
69 70 71
        ~DIR()
        {
            if (ent.d_name)
72
                delete[] ent.d_name;
73 74
        }
#endif
75 76 77 78 79 80
    };

    DIR* opendir(const char* path)
    {
        DIR* dir = new DIR;
        dir->ent.d_name = 0;
81
#ifdef WINRT
82
        cv::String full_path = cv::String(path) + "\\*";
83 84 85
        wchar_t wfull_path[MAX_PATH];
        size_t copied = mbstowcs(wfull_path, full_path.c_str(), MAX_PATH);
        CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
86 87 88 89 90 91
        dir->handle = ::FindFirstFileExW(wfull_path, FindExInfoStandard,
                        &dir->data, FindExSearchNameMatch, NULL, 0);
#else
        dir->handle = ::FindFirstFileExA((cv::String(path) + "\\*").c_str(),
            FindExInfoStandard, &dir->data, FindExSearchNameMatch, NULL, 0);
#endif
92 93 94
        if(dir->handle == INVALID_HANDLE_VALUE)
        {
            /*closedir will do all cleanup*/
95
            delete dir;
96 97 98 99 100 101 102
            return 0;
        }
        return dir;
    }

    dirent* readdir(DIR* dir)
    {
103
#ifdef WINRT
104 105
        if (dir->ent.d_name != 0)
        {
106 107 108 109
            if (::FindNextFileW(dir->handle, &dir->data) != TRUE)
                return 0;
        }
        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 113 114 115 116 117 118
        aname[asize] = 0;
        wcstombs(aname, dir->data.cFileName, asize);
        dir->ent.d_name = aname;
#else
        if (dir->ent.d_name != 0)
        {
            if (::FindNextFileA(dir->handle, &dir->data) != TRUE)
119 120 121
                return 0;
        }
        dir->ent.d_name = dir->data.cFileName;
122
#endif
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
        return &dir->ent;
    }

    void closedir(DIR* dir)
    {
        ::FindClose(dir->handle);
        delete dir;
    }


}
#else
# include <dirent.h>
# include <sys/stat.h>
const char dir_separators[] = "/";
#endif

static bool isDir(const cv::String& path, DIR* dir)
{
142
#if defined _WIN32 || defined WINCE
143
    DWORD attributes;
144
    BOOL status = TRUE;
145 146 147
    if (dir)
        attributes = dir->data.dwFileAttributes;
    else
148 149
    {
        WIN32_FILE_ATTRIBUTE_DATA all_attrs;
150
#ifdef WINRT
151 152 153
        wchar_t wpath[MAX_PATH];
        size_t copied = mbstowcs(wpath, path.c_str(), MAX_PATH);
        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 158 159
#endif
        attributes = all_attrs.dwFileAttributes;
    }
160

161
    return status && ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
162 163 164 165 166 167 168 169 170 171
#else
    (void)dir;
    struct stat stat_buf;
    if (0 != stat( path.c_str(), &stat_buf))
        return false;
    int is_dir = S_ISDIR( stat_buf.st_mode);
    return is_dir != 0;
#endif
}

172 173 174 175 176 177
bool cv::utils::fs::isDirectory(const cv::String& path)
{
    CV_INSTRUMENT_REGION()
    return isDir(path, NULL);
}

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
static bool wildcmp(const char *string, const char *wild)
{
    // Based on wildcmp written by Jack Handy - <A href="mailto:jakkhandy@hotmail.com">jakkhandy@hotmail.com</A>
    const char *cp = 0, *mp = 0;

    while ((*string) && (*wild != '*'))
    {
        if ((*wild != *string) && (*wild != '?'))
        {
            return false;
        }

        wild++;
        string++;
    }

    while (*string)
    {
        if (*wild == '*')
        {
            if (!*++wild)
            {
                return true;
            }

            mp = wild;
            cp = string + 1;
        }
        else if ((*wild == *string) || (*wild == '?'))
        {
            wild++;
            string++;
        }
        else
        {
            wild = mp;
            string = cp++;
        }
    }

    while (*wild == '*')
    {
        wild++;
    }

    return *wild == 0;
}

226 227
static void glob_rec(const cv::String& directory, const cv::String& wildchart, std::vector<cv::String>& result,
        bool recursive, bool includeDirectories, const cv::String& pathPrefix)
228 229 230 231 232 233
{
    DIR *dir;

    if ((dir = opendir (directory.c_str())) != 0)
    {
        /* find all the files and directories within directory */
234
        CV_TRY
235
        {
236
            struct dirent *ent;
237 238 239 240 241 242
            while ((ent = readdir (dir)) != 0)
            {
                const char* name = ent->d_name;
                if((name[0] == 0) || (name[0] == '.' && name[1] == 0) || (name[0] == '.' && name[1] == '.' && name[2] == 0))
                    continue;

243 244
                cv::String path = cv::utils::fs::join(directory, name);
                cv::String entry = cv::utils::fs::join(pathPrefix, name);
245 246 247 248

                if (isDir(path, dir))
                {
                    if (recursive)
249 250 251
                        glob_rec(path, wildchart, result, recursive, includeDirectories, entry);
                    if (!includeDirectories)
                        continue;
252
                }
253 254 255

                if (wildchart.empty() || wildcmp(name, wildchart.c_str()))
                    result.push_back(entry);
256 257
            }
        }
258
        CV_CATCH_ALL
259 260
        {
            closedir(dir);
261
            CV_RETHROW();
262 263 264
        }
        closedir(dir);
    }
265 266 267 268
    else
    {
        CV_Error_(CV_StsObjectNotFound, ("could not open directory: %s", directory.c_str()));
    }
269 270 271 272
}

void cv::glob(String pattern, std::vector<String>& result, bool recursive)
{
273 274
    CV_INSTRUMENT_REGION()

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    result.clear();
    String path, wildchart;

    if (isDir(pattern, 0))
    {
        if(strchr(dir_separators, pattern[pattern.size() - 1]) != 0)
        {
            path = pattern.substr(0, pattern.size() - 1);
        }
        else
        {
            path = pattern;
        }
    }
    else
    {
        size_t pos = pattern.find_last_of(dir_separators);
        if (pos == String::npos)
        {
            wildchart = pattern;
            path = ".";
        }
        else
        {
            path = pattern.substr(0, pos);
            wildchart = pattern.substr(pos + 1);
        }
    }

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
    glob_rec(path, wildchart, result, recursive, false, path);
    std::sort(result.begin(), result.end());
}

void cv::utils::fs::glob(const cv::String& directory, const cv::String& pattern,
        std::vector<cv::String>& result,
        bool recursive, bool includeDirectories)
{
    glob_rec(directory, pattern, result, recursive, includeDirectories, directory);
    std::sort(result.begin(), result.end());
}

void cv::utils::fs::glob_relative(const cv::String& directory, const cv::String& pattern,
        std::vector<cv::String>& result,
        bool recursive, bool includeDirectories)
{
    glob_rec(directory, pattern, result, recursive, includeDirectories, cv::String());
321 322
    std::sort(result.begin(), result.end());
}