]> Creatis software - cpPlugins.git/blob - lib/third_party/Pluma/Dir.cpp
Parameters are now part of the pipeline update process
[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 #include <stdexcept>\r
34 \r
35 \r
36 namespace pluma{\r
37 \r
38 namespace dir{\r
39 \r
40 \r
41 ////////////////////////////////////////////////////////////\r
42 void listFiles(std::list<std::string>& list, const std::string& folder, const std::string& extension, bool recursive){\r
43     DIR* dir;\r
44     DIR* subDir;\r
45     struct dirent *ent;\r
46     // try to open top folder\r
47     dir = opendir(folder.c_str());\r
48     if (dir == NULL){\r
49         // could not open directory\r
50       // fprintf(stderr, "Could not open \"%s\" directory.\n", folder.c_str());\r
51       throw std::runtime_error(\r
52         std::string( "Could not open \"" ) +\r
53         folder +\r
54         std::string( "\" directory." )\r
55         );\r
56       return;\r
57     }else{\r
58         // close, we'll process it next\r
59         closedir(dir);\r
60     }\r
61     // enqueue top folder\r
62     std::queue<std::string> folders;\r
63     folders.push(folder);\r
64 \r
65     // run while has queued folders\r
66     while (!folders.empty()){\r
67         std::string currFolder = folders.front();\r
68         folders.pop();\r
69         dir = opendir(currFolder.c_str());\r
70         if (dir == NULL) continue;\r
71         // iterate through all the files and directories\r
72         while ((ent = readdir (dir)) != NULL) {\r
73             std::string name(ent->d_name);\r
74             // ignore "." and ".." directories\r
75             if ( name.compare(".") == 0 || name.compare("..") == 0) continue;\r
76             // add path to the file name\r
77             std::string path = currFolder;\r
78             path.append("/");\r
79             path.append(name);\r
80             // check if it's a folder by trying to open it\r
81             subDir = opendir(path.c_str());\r
82             if (subDir != NULL){\r
83                 // it's a folder: close, we can process it later\r
84                 closedir(subDir);\r
85                 if (recursive) folders.push(path);\r
86             }else{\r
87                 // it's a file\r
88                 if (extension.empty()){\r
89                     list.push_back(path);\r
90                 }else{\r
91                     // check file extension\r
92                     size_t lastDot = name.find_last_of('.');\r
93                     std::string ext = name.substr(lastDot+1);\r
94                     if (ext.compare(extension) == 0){\r
95                         // match\r
96                         list.push_back(path);\r
97                     }\r
98                 } // endif (extension test)\r
99             } // endif (folder test)\r
100         } // endwhile (nextFile)\r
101         closedir(dir);\r
102     } // endwhile (queued folders)\r
103 \r
104 } // end listFiles\r
105 \r
106 \r
107 }   // namespace dir\r
108 \r
109 }   // namespace pluma\r