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
33 #include <stdexcept>
\r
41 ////////////////////////////////////////////////////////////
\r
42 void listFiles(std::list<std::string>& list, const std::string& folder, const std::string& extension, bool recursive){
\r
46 // try to open top folder
\r
47 dir = opendir(folder.c_str());
\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
54 std::string( "\" directory." )
\r
58 // close, we'll process it next
\r
61 // enqueue top folder
\r
62 std::queue<std::string> folders;
\r
63 folders.push(folder);
\r
65 // run while has queued folders
\r
66 while (!folders.empty()){
\r
67 std::string currFolder = folders.front();
\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
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
85 if (recursive) folders.push(path);
\r
88 if (extension.empty()){
\r
89 list.push_back(path);
\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
96 list.push_back(path);
\r
98 } // endif (extension test)
\r
99 } // endif (folder test)
\r
100 } // endwhile (nextFile)
\r
102 } // endwhile (queued folders)
\r
109 } // namespace pluma
\r