1 ////////////////////////////////////////////////////////////
\r
3 // Pluma - Plug-in Management Framework
\r
4 // Copyright (C) 2010-2012 Gil Costa (gsaurus@gmail.com)
\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
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
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
18 // 2. Altered source versions must be plainly marked as such,
\r
19 // and must not be misrepresented as being the original software.
\r
21 // 3. This notice may not be removed or altered from any source distribution.
\r
23 ////////////////////////////////////////////////////////////
\r
26 ////////////////////////////////////////////////////////////
\r
28 ////////////////////////////////////////////////////////////
\r
29 #include <Pluma/Dir.hpp>
\r
30 #include <Pluma/uce-dirent.h>
\r
40 ////////////////////////////////////////////////////////////
\r
41 void listFiles(std::list<std::string>& list, const std::string& folder, const std::string& extension, bool recursive){
\r
45 // try to open top folder
\r
46 dir = opendir(folder.c_str());
\r
48 // could not open directory
\r
49 fprintf(stderr, "Could not open \"%s\" directory.\n", folder.c_str());
\r
52 // close, we'll process it next
\r
55 // enqueue top folder
\r
56 std::queue<std::string> folders;
\r
57 folders.push(folder);
\r
59 // run while has queued folders
\r
60 while (!folders.empty()){
\r
61 std::string currFolder = folders.front();
\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
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
79 if (recursive) folders.push(path);
\r
82 if (extension.empty()){
\r
83 list.push_back(path);
\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
90 list.push_back(path);
\r
92 } // endif (extension test)
\r
93 } // endif (folder test)
\r
94 } // endwhile (nextFile)
\r
96 } // endwhile (queued folders)
\r
103 } // namespace pluma
\r