inputoutput.cpp 7.1 KB
Newer Older
1 2

#include "opencv2/contrib/contrib.hpp"
3
#include <cvconfig.h>
4

A
Alexander Smorkalov 已提交
5
#if defined(WIN32) || defined(_WIN32)
6 7 8 9 10 11 12 13 14 15 16 17 18 19
    #include <windows.h>
    #include <tchar.h>
#else
    #include <dirent.h>
#endif

namespace cv
{
    std::vector<std::string> Directory::GetListFiles(  const std::string& path, const std::string & exten, bool addPath )
    {
        std::vector<std::string> list;
        list.clear();
        std::string path_f = path + "/" + exten;
        #ifdef WIN32
20
        #ifdef HAVE_WINRT
21 22
            WIN32_FIND_DATAW FindFileData;
        #else
23
            WIN32_FIND_DATA FindFileData;
24 25
        #endif
        HANDLE hFind;
26

27 28 29 30 31
        #ifdef HAVE_WINRT
            size_t size = mbstowcs(NULL, path_f.c_str(), path_f.size());
            wchar_t* wpath = (wchar_t*)malloc((size+1)*sizeof(wchar_t));
            wpath[size] = 0;
            mbstowcs(wpath, path_f.c_str(), path_f.size());
32
            hFind = FindFirstFileExW(wpath, FindExInfoStandard, &FindFileData, FindExSearchNameMatch, NULL, 0);
33 34 35 36
            free(wpath);
        #else
            hFind = FindFirstFileA((LPCSTR)path_f.c_str(), &FindFileData);
        #endif
37 38 39 40 41 42 43 44 45 46 47 48 49 50
            if (hFind == INVALID_HANDLE_VALUE)
            {
                return list;
            }
            else
            {
                do
                {
                    if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL  ||
                        FindFileData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE ||
                        FindFileData.dwFileAttributes == FILE_ATTRIBUTE_HIDDEN  ||
                        FindFileData.dwFileAttributes == FILE_ATTRIBUTE_SYSTEM  ||
                        FindFileData.dwFileAttributes == FILE_ATTRIBUTE_READONLY)
                    {
51 52 53
                        char* fname;
                    #ifdef HAVE_WINRT
                        size_t asize = wcstombs(NULL, FindFileData.cFileName, 0);
54
                        fname = (char*)malloc((asize+1)*sizeof(char));
55 56 57 58 59
                        fname[asize] = 0;
                        wcstombs(fname, FindFileData.cFileName, asize);
                    #else
                        fname = FindFileData.cFileName;
                    #endif
60
                        if (addPath)
61
                            list.push_back(path + "/" + fname);
62
                        else
63 64 65 66
                            list.push_back(fname);
                    #ifdef HAVE_WINRT
                        free(fname);
                    #endif
67 68
                    }
                }
69 70 71 72 73
            #ifdef HAVE_WINRT
                while(FindNextFileW(hFind, &FindFileData));
            #else
                while(FindNextFileA(hFind, &FindFileData));
            #endif
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
                FindClose(hFind);
            }
        #else
            (void)addPath;
            DIR *dp;
            struct dirent *dirp;
            if((dp = opendir(path.c_str())) == NULL)
            {
                return list;
            }

            while ((dirp = readdir(dp)) != NULL)
            {
                if (dirp->d_type == DT_REG)
                {
                    if (exten.compare("*") == 0)
                        list.push_back(static_cast<std::string>(dirp->d_name));
                    else
                        if (std::string(dirp->d_name).find(exten) != std::string::npos)
                            list.push_back(static_cast<std::string>(dirp->d_name));
                }
            }
            closedir(dp);
        #endif

        return list;
    }

    std::vector<std::string> Directory::GetListFolders( const std::string& path, const std::string & exten, bool addPath )
    {
        std::vector<std::string> list;
        std::string path_f = path + "/" + exten;
        list.clear();
        #ifdef WIN32
108
        #ifdef HAVE_WINRT
109 110
            WIN32_FIND_DATAW FindFileData;
        #else
111
            WIN32_FIND_DATA FindFileData;
112
        #endif
113 114
            HANDLE hFind;

115 116 117 118 119
        #ifdef HAVE_WINRT
            size_t size = mbstowcs(NULL, path_f.c_str(), path_f.size());
            wchar_t* wpath = (wchar_t*)malloc((size+1)*sizeof(wchar_t));
            wpath[size] = 0;
            mbstowcs(wpath, path_f.c_str(), path_f.size());
120
            hFind = FindFirstFileExW(wpath, FindExInfoStandard, &FindFileData, FindExSearchNameMatch, NULL, 0);
121 122 123 124
            free(wpath);
        #else
            hFind = FindFirstFileA((LPCSTR)path_f.c_str(), &FindFileData);
        #endif
125 126 127 128 129 130 131 132 133
            if (hFind == INVALID_HANDLE_VALUE)
            {
                return list;
            }
            else
            {
                do
                {
                    if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&
134 135
                        wcscmp(FindFileData.cFileName, L".") != 0 &&
                        wcscmp(FindFileData.cFileName, L"..") != 0)
136
                    {
137 138 139
                    char* fname;
                    #ifdef HAVE_WINRT
                        size_t asize = wcstombs(NULL, FindFileData.cFileName, 0);
140
                        fname = (char*)malloc((asize+1)*sizeof(char));
141 142 143 144 145 146
                        fname[asize] = 0;
                        wcstombs(fname, FindFileData.cFileName, asize);
                    #else
                        fname = FindFileData.cFileName;
                    #endif

147
                        if (addPath)
148
                            list.push_back(path + "/" + fname);
149
                        else
150
                            list.push_back(fname);
151 152
                    }
                }
153 154 155 156 157
            #ifdef HAVE_WINRT
                while(FindNextFileW(hFind, &FindFileData));
            #else
                while(FindNextFileA(hFind, &FindFileData));
            #endif
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 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
                FindClose(hFind);
            }

        #else
            (void)addPath;
            DIR *dp;
            struct dirent *dirp;
            if((dp = opendir(path_f.c_str())) == NULL)
            {
                return list;
            }

            while ((dirp = readdir(dp)) != NULL)
            {
                if (dirp->d_type == DT_DIR &&
                    strcmp(dirp->d_name, ".") != 0 &&
                    strcmp(dirp->d_name, "..") != 0 )
                {
                    if (exten.compare("*") == 0)
                        list.push_back(static_cast<std::string>(dirp->d_name));
                    else
                        if (std::string(dirp->d_name).find(exten) != std::string::npos)
                            list.push_back(static_cast<std::string>(dirp->d_name));
                }
            }
            closedir(dp);
        #endif

        return list;
    }

    std::vector<std::string> Directory::GetListFilesR ( const std::string& path, const std::string & exten, bool addPath )
    {
        std::vector<std::string> list = Directory::GetListFiles(path, exten, addPath);

        std::vector<std::string> dirs = Directory::GetListFolders(path, exten, addPath);

        std::vector<std::string>::const_iterator it;
        for (it = dirs.begin(); it != dirs.end(); ++it)
        {
            std::vector<std::string> cl = Directory::GetListFiles(*it, exten, addPath);
            list.insert(list.end(), cl.begin(), cl.end());
        }

        return list;
    }

}