CLAM-Development  1.4.0
TraverseDirectory.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #include <string>
23 #ifndef WIN32
24 # include <dirent.h>
25 #else
26 # include "CLAM_windows.h"
27 #endif
28 
30 {
31 
32 #ifdef WIN32
33  typedef WIN32_FIND_DATA Directory;
34  typedef HANDLE DirectoryEntry;
35 #else
36  typedef DIR* Directory;
37  typedef dirent* DirectoryEntry;
38 #endif
39 
40 public:
41  TraverseDirectory(void);
42  virtual ~TraverseDirectory(void)
43  {
44  }
45  void Traverse(const std::string& rootname = "",int maxdepth = -1);
46 
47 
48 protected:
49  virtual void OnFile(const std::string& filename) { };
50  virtual void OnDirectory(const std::string& dirname) { };
51 
58  void SkipSubdirectories(void);
59 
65  void SkipDirectory(void);
66 
68  std::string GetExtension(const std::string& filename);
69 
70 private:
71  void TraverseHelper(Directory dir,const std::string& dirname,int curdepth,int maxdepth);
72  bool IsCurrentOrParentDir(DirectoryEntry dirEntry) const;
73  std::string CompleteName(const std::string& currentDirName, DirectoryEntry dirEntry) const;
74 };
75 
76 /*
77 
78 #include "recursedir.h"
79 #include "strfuncs.h"
80 
81 #ifdef WIN32
82 
83 #include "CLAM_windows.h"
84 
85 #else
86 
87 #include <sys/types.h>
88 #include <dirent.h>
89 
90 #endif
91 
92 #include <stdio.h>
93 #include <string.h>
94 
95 #ifdef WIN32
96 int _recursedir(
97  const char* dir,int l,int m,on_file_func f,on_dir_func d,void* ptr)
98 {
99  WIN32_FIND_DATA fd;
100  HANDLE hFind;
101  char tmp[2048];
102  strstart(tmp,2048);
103  if (strcmp(dir,"")!=0)
104  {
105  stradd(dir);
106  stradd("/");
107  }
108  stradd("*.*");
109  str_end();
110  hFind = FindFirstFile(tmp, &fd);
111  if (hFind == INVALID_HANDLE_VALUE) return -1;
112 
113  do
114  {
115  if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
116  {
117  if (strcmp(fd.cFileName,".") && strcmp(fd.cFileName,".."))
118  {
119  char tmp2[2048];
120  strstart(tmp2,2048);
121  stradd(dir);
122  stradd("/");
123  stradd(fd.cFileName);
124  str_end();
125 
126  if (d && d(tmp2,ptr)==1) {
127  FindClose(hFind);
128  return 1;
129  }
130  if (l<m || m==-1)
131  {
132  if (_recursedir(tmp2,l+1,m,f,d,ptr)==1)
133  {
134  FindClose(hFind);
135  return 1;
136  }
137  }
138  }
139  }
140  else
141  {
142  if (f)
143  {
144  char tmp2[2048];
145  strstart(tmp,2048);
146  stradd(dir);
147  stradd("/");
148  stradd(fd.cFileName);
149  str_end();
150  if (f(tmp2,ptr)) {
151  FindClose(hFind);
152  return 1;
153  }
154  }
155  }
156  } while (FindNextFile(hFind, &fd)); // enumerates contents
157  FindClose(hFind);
158 
159  return 0;
160 }
161 
162 int recursedir(
163  const char* dir,int m,on_file_func f,on_dir_func d,void* ptr)
164 {
165  WIN32_FIND_DATA fd;
166  HANDLE hFind;
167  hFind = FindFirstFile(dir, &fd);
168  if (hFind == INVALID_HANDLE_VALUE) return -1;
169  if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
170  {
171  if (d && d(dir,ptr)==1) {
172  FindClose(hFind);
173  return 1;
174  }
175  if (_recursedir(dir,0,m,f,d,ptr)==1)
176  {
177  FindClose(hFind);
178  return 1;
179  }
180  }
181  FindClose(hFind);
182  return 0;
183 }
184 
185 #else
186 
187 int _recursedir(
188  DIR* dir,const char* name,int l,int m,on_file_func f,on_dir_func d,void* ptr)
189 {
190  struct dirent* e;
191 
192  while ((e = readdir(dir)))
193  {
194  if (strcmp(e->d_name,".") && strcmp(e->d_name,".."))
195  {
196  char tmp[2048];
197  DIR* sd;
198  strstart(tmp,2048);
199  if (strcmp(name,""))
200  {
201  stradd(name);
202  stradd("/");
203  }
204  stradd(e->d_name);
205  str_end();
206  sd = opendir(tmp);
207  if (sd) {
208  int ret = 0;
209  if (d && d(tmp,ptr)) {
210  closedir(sd);
211  return 1;
212  }
213  if (l<m || m==-1)
214  {
215  ret = _recursedir(sd,tmp,l+1,m,f,d,ptr);
216  }
217  closedir(sd);
218  if (ret==1) return 1;
219  }else{
220  if (f && f(tmp,ptr)) return 1;
221  }
222  }
223  }
224  return 0;
225 }
226 
227 int recursedir(
228  const char* name,int m,on_file_func f,on_dir_func d,void* ptr)
229 {
230  DIR* dir;
231  if (strcmp(name,"")) dir = opendir(name);
232  else dir = opendir(".");
233  if (dir)
234  {
235  int ret;
236  if (d && d(name,ptr)) {
237  closedir(dir);
238  return 1;
239  }
240  ret = _recursedir(dir,name,0,m,f,d,ptr);
241  closedir(dir);
242  return ret;
243  }else{
244  return -1;
245  }
246  return 0;
247 }
248 #endif
249 */
250