added class Directory
authorAndrey Morozov <no@email>
Tue, 4 Oct 2011 12:12:14 +0000 (12:12 +0000)
committerAndrey Morozov <no@email>
Tue, 4 Oct 2011 12:12:14 +0000 (12:12 +0000)
modules/contrib/include/opencv2/contrib/contrib.hpp
modules/contrib/src/inputoutput.cpp [new file with mode: 0644]

index ca5c3bc..ae86bd0 100644 (file)
@@ -605,6 +605,14 @@ namespace cv
     };
     
     CV_EXPORTS void polyfit(const Mat& srcx, const Mat& srcy, Mat& dst, int order);
+
+       class CV_EXPORTS Directory
+    {
+           public:
+                       static std::vector<std::string> GetListFiles  ( const string& directoryName, bool addPath = true );
+                       static std::vector<std::string> GetListFilesR ( const string& directoryName, bool addPath = true );
+                       static std::vector<std::string> GetListFolders( const string& directoryName, bool addPath = true );
+    };
 }
 
 #include "opencv2/contrib/retina.hpp"
diff --git a/modules/contrib/src/inputoutput.cpp b/modules/contrib/src/inputoutput.cpp
new file mode 100644 (file)
index 0000000..c17fae7
--- /dev/null
@@ -0,0 +1,132 @@
+\r
+#include "opencv2/contrib/contrib.hpp"\r
+\r
+#ifdef WIN32\r
+       #include <windows.h>\r
+       #include <tchar.h>\r
+#else\r
+       #include <direct.h>\r
+#endif\r
+\r
+namespace cv\r
+{\r
+       std::vector<std::string> Directory::GetListFiles( const string& directoryName, bool addPath )\r
+       {\r
+               std::vector<std::string> list;\r
+               list.clear();\r
+               std::string path = directoryName + "/*";\r
+               #ifdef WIN32\r
+                       WIN32_FIND_DATA FindFileData;\r
+                       HANDLE hFind;\r
+\r
+                       hFind = FindFirstFile((LPCSTR)path.c_str(), &FindFileData);\r
+                       if (hFind == INVALID_HANDLE_VALUE) \r
+                       {\r
+                               return list;\r
+                       }\r
+                       else\r
+                       {\r
+                               do\r
+                               {\r
+                                       if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL  || \r
+                                               FindFileData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE || \r
+                                               FindFileData.dwFileAttributes == FILE_ATTRIBUTE_HIDDEN  || \r
+                                               FindFileData.dwFileAttributes == FILE_ATTRIBUTE_SYSTEM  || \r
+                                               FindFileData.dwFileAttributes == FILE_ATTRIBUTE_READONLY)\r
+                                       {\r
+                                               if (addPath)\r
+                                                       list.push_back(directoryName + "/" + FindFileData.cFileName);\r
+                                               else\r
+                                                       list.push_back(FindFileData.cFileName);\r
+                                       }\r
+                               } \r
+                               while(FindNextFile(hFind, &FindFileData));\r
+                               FindClose(hFind);  \r
+                       }\r
+               #else\r
+                       DIR *dp;\r
+                       struct dirent *dirp;\r
+                       if((dp  = opendir(directoryName.c_str())) == NULL) \r
+                       {                               \r
+                               return list;\r
+                       }\r
+\r
+                       while ((dirp = readdir(dp)) != NULL) \r
+                       {\r
+                               if (dirp->d_type == DT_REG)\r
+                                       list.push_back(static_cast<string>(dirp->d_name));\r
+                       }\r
+                       closedir(dp);\r
+               #endif\r
+\r
+               return list;\r
+       }\r
+\r
+       std::vector<std::string> Directory::GetListFolders( const string& directoryName, bool addPath )\r
+       {\r
+               std::vector<std::string> list;\r
+               std::string path = directoryName + "/*";\r
+               list.clear();\r
+               #ifdef WIN32\r
+                       WIN32_FIND_DATA FindFileData;\r
+                       HANDLE hFind;\r
+\r
+                       hFind = FindFirstFile((LPCSTR)path.c_str(), &FindFileData);\r
+                       if (hFind == INVALID_HANDLE_VALUE) \r
+                       {\r
+                               return list;\r
+                       }\r
+                       else\r
+                       {\r
+                               do\r
+                               {\r
+                                       if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && \r
+                                               strcmp(FindFileData.cFileName, ".") != 0 && \r
+                                               strcmp(FindFileData.cFileName, "..") != 0)\r
+                                       {\r
+                                               if (addPath)\r
+                                                       list.push_back(directoryName + "/" + FindFileData.cFileName);\r
+                                               else\r
+                                                       list.push_back(FindFileData.cFileName);\r
+                                       }\r
+                               } \r
+                               while(FindNextFile(hFind, &FindFileData));\r
+                               FindClose(hFind);  \r
+                       }\r
+\r
+               #else\r
+                       DIR *dp;\r
+                       struct dirent *dirp;\r
+                       if((dp  = opendir(path.c_str())) == NULL) \r
+                       {                               \r
+                               return list;\r
+                       }\r
+\r
+                       while ((dirp = readdir(dp)) != NULL) \r
+                       {\r
+                               if (dirp->d_type == DT_DIR)\r
+                                       list.push_back(static_cast<string>(dirp->d_name));\r
+                       }\r
+                       closedir(dp);\r
+               #endif\r
+\r
+               return list;\r
+       }\r
+\r
+       std::vector<std::string> Directory::GetListFilesR ( const string& directoryName, bool addPath )\r
+       {\r
+               std::vector<std::string> list = Directory::GetListFiles(directoryName, addPath);\r
+\r
+               std::vector<std::string> dirs = Directory::GetListFolders(directoryName, addPath);\r
+\r
+               std::vector<std::string>::const_iterator it;\r
+               for (it = dirs.begin(); it != dirs.end(); ++it)\r
+               {\r
+                       std::vector<std::string> cl = Directory::GetListFiles(*it, addPath);\r
+                       list.insert(list.end(), cl.begin(), cl.end());\r
+               }\r
+\r
+               return list;\r
+       }\r
+\r
+}
\ No newline at end of file