]> Creatis software - cpPlugins.git/blob - lib/third_party/Pluma/Dir.cpp
Base objects migration
[cpPlugins.git] / lib / third_party / Pluma / Dir.cpp
1 ////////////////////////////////////////////////////////////\r
2 //\r
3 // Pluma - Plug-in Management Framework\r
4 // Copyright (C) 2010-2012 Gil Costa (gsaurus@gmail.com)\r
5 //\r
6 // This software is provided 'as-is', without any express or implied warranty.\r
7 // In no event will the authors be held liable for any damages arising from the use of this software.\r
8 //\r
9 // Permission is granted to anyone to use this software for any purpose,\r
10 // including commercial applications, and to alter it and redistribute it freely,\r
11 // subject to the following restrictions:\r
12 //\r
13 // 1. The origin of this software must not be misrepresented;\r
14 //    you must not claim that you wrote the original software.\r
15 //    If you use this software in a product, an acknowledgment\r
16 //    in the product documentation would be appreciated but is not required.\r
17 //\r
18 // 2. Altered source versions must be plainly marked as such,\r
19 //    and must not be misrepresented as being the original software.\r
20 //\r
21 // 3. This notice may not be removed or altered from any source distribution.\r
22 //\r
23 ////////////////////////////////////////////////////////////\r
24 \r
25 \r
26 ////////////////////////////////////////////////////////////\r
27 // Headers\r
28 ////////////////////////////////////////////////////////////\r
29 #include <Pluma/Dir.hpp>\r
30 #include <Pluma/uce-dirent.h>\r
31 #include <cstdio>\r
32 #include <queue>\r
33 \r
34 \r
35 namespace pluma{\r
36 \r
37 namespace dir{\r
38 \r
39 \r
40 ////////////////////////////////////////////////////////////\r
41 void listFiles(std::list<std::string>& list, const std::string& folder, const std::string& extension, bool recursive){\r
42     DIR* dir;\r
43     DIR* subDir;\r
44     struct dirent *ent;\r
45     // try to open top folder\r
46     dir = opendir(folder.c_str());\r
47     if (dir == NULL){\r
48         // could not open directory\r
49       fprintf(stderr, "Could not open \"%s\" directory.\n", folder.c_str());\r
50       return;\r
51     }else{\r
52         // close, we'll process it next\r
53         closedir(dir);\r
54     }\r
55     // enqueue top folder\r
56     std::queue<std::string> folders;\r
57     folders.push(folder);\r
58 \r
59     // run while has queued folders\r
60     while (!folders.empty()){\r
61         std::string currFolder = folders.front();\r
62         folders.pop();\r
63         dir = opendir(currFolder.c_str());\r
64         if (dir == NULL) continue;\r
65         // iterate through all the files and directories\r
66         while ((ent = readdir (dir)) != NULL) {\r
67             std::string name(ent->d_name);\r
68             // ignore "." and ".." directories\r
69             if ( name.compare(".") == 0 || name.compare("..") == 0) continue;\r
70             // add path to the file name\r
71             std::string path = currFolder;\r
72             path.append("/");\r
73             path.append(name);\r
74             // check if it's a folder by trying to open it\r
75             subDir = opendir(path.c_str());\r
76             if (subDir != NULL){\r
77                 // it's a folder: close, we can process it later\r
78                 closedir(subDir);\r
79                 if (recursive) folders.push(path);\r
80             }else{\r
81                 // it's a file\r
82                 if (extension.empty()){\r
83                     list.push_back(path);\r
84                 }else{\r
85                     // check file extension\r
86                     size_t lastDot = name.find_last_of('.');\r
87                     std::string ext = name.substr(lastDot+1);\r
88                     if (ext.compare(extension) == 0){\r
89                         // match\r
90                         list.push_back(path);\r
91                     }\r
92                 } // endif (extension test)\r
93             } // endif (folder test)\r
94         } // endwhile (nextFile)\r
95         closedir(dir);\r
96     } // endwhile (queued folders)\r
97 \r
98 } // end listFiles\r
99 \r
100 \r
101 }   // namespace dir\r
102 \r
103 }   // namespace pluma\r