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