00001 /* 00002 * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG) 00003 * UNIVERSITAT POMPEU FABRA 00004 * 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 * 00020 */ 00021 00022 #include <string> 00023 #ifndef WIN32 00024 # include <dirent.h> 00025 #else 00026 # include "CLAM_windows.h" 00027 #endif 00028 00029 class TraverseDirectory 00030 { 00031 00032 #ifdef WIN32 00033 typedef WIN32_FIND_DATA Directory; 00034 typedef HANDLE DirectoryEntry; 00035 #else 00036 typedef DIR* Directory; 00037 typedef dirent* DirectoryEntry; 00038 #endif 00039 00040 public: 00041 TraverseDirectory(void); 00042 virtual ~TraverseDirectory(void) 00043 { 00044 } 00045 void Traverse(const std::string& rootname = "",int maxdepth = -1); 00046 00047 00048 protected: 00049 virtual void OnFile(const std::string& filename) { }; 00050 virtual void OnDirectory(const std::string& dirname) { }; 00051 00058 void SkipSubdirectories(void); 00059 00065 void SkipDirectory(void); 00066 00068 std::string GetExtension(const std::string& filename); 00069 00070 private: 00071 void TraverseHelper(Directory dir,const std::string& dirname,int curdepth,int maxdepth); 00072 bool IsCurrentOrParentDir(DirectoryEntry dirEntry) const; 00073 std::string CompleteName(const std::string& currentDirName, DirectoryEntry dirEntry) const; 00074 }; 00075 00076 /* 00077 00078 #include "recursedir.h" 00079 #include "strfuncs.h" 00080 00081 #ifdef WIN32 00082 00083 #include "CLAM_windows.h" 00084 00085 #else 00086 00087 #include <sys/types.h> 00088 #include <dirent.h> 00089 00090 #endif 00091 00092 #include <stdio.h> 00093 #include <string.h> 00094 00095 #ifdef WIN32 00096 int _recursedir( 00097 const char* dir,int l,int m,on_file_func f,on_dir_func d,void* ptr) 00098 { 00099 WIN32_FIND_DATA fd; 00100 HANDLE hFind; 00101 char tmp[2048]; 00102 strstart(tmp,2048); 00103 if (strcmp(dir,"")!=0) 00104 { 00105 stradd(dir); 00106 stradd("/"); 00107 } 00108 stradd("*.*"); 00109 str_end(); 00110 hFind = FindFirstFile(tmp, &fd); 00111 if (hFind == INVALID_HANDLE_VALUE) return -1; 00112 00113 do 00114 { 00115 if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 00116 { 00117 if (strcmp(fd.cFileName,".") && strcmp(fd.cFileName,"..")) 00118 { 00119 char tmp2[2048]; 00120 strstart(tmp2,2048); 00121 stradd(dir); 00122 stradd("/"); 00123 stradd(fd.cFileName); 00124 str_end(); 00125 00126 if (d && d(tmp2,ptr)==1) { 00127 FindClose(hFind); 00128 return 1; 00129 } 00130 if (l<m || m==-1) 00131 { 00132 if (_recursedir(tmp2,l+1,m,f,d,ptr)==1) 00133 { 00134 FindClose(hFind); 00135 return 1; 00136 } 00137 } 00138 } 00139 } 00140 else 00141 { 00142 if (f) 00143 { 00144 char tmp2[2048]; 00145 strstart(tmp,2048); 00146 stradd(dir); 00147 stradd("/"); 00148 stradd(fd.cFileName); 00149 str_end(); 00150 if (f(tmp2,ptr)) { 00151 FindClose(hFind); 00152 return 1; 00153 } 00154 } 00155 } 00156 } while (FindNextFile(hFind, &fd)); // enumerates contents 00157 FindClose(hFind); 00158 00159 return 0; 00160 } 00161 00162 int recursedir( 00163 const char* dir,int m,on_file_func f,on_dir_func d,void* ptr) 00164 { 00165 WIN32_FIND_DATA fd; 00166 HANDLE hFind; 00167 hFind = FindFirstFile(dir, &fd); 00168 if (hFind == INVALID_HANDLE_VALUE) return -1; 00169 if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 00170 { 00171 if (d && d(dir,ptr)==1) { 00172 FindClose(hFind); 00173 return 1; 00174 } 00175 if (_recursedir(dir,0,m,f,d,ptr)==1) 00176 { 00177 FindClose(hFind); 00178 return 1; 00179 } 00180 } 00181 FindClose(hFind); 00182 return 0; 00183 } 00184 00185 #else 00186 00187 int _recursedir( 00188 DIR* dir,const char* name,int l,int m,on_file_func f,on_dir_func d,void* ptr) 00189 { 00190 struct dirent* e; 00191 00192 while ((e = readdir(dir))) 00193 { 00194 if (strcmp(e->d_name,".") && strcmp(e->d_name,"..")) 00195 { 00196 char tmp[2048]; 00197 DIR* sd; 00198 strstart(tmp,2048); 00199 if (strcmp(name,"")) 00200 { 00201 stradd(name); 00202 stradd("/"); 00203 } 00204 stradd(e->d_name); 00205 str_end(); 00206 sd = opendir(tmp); 00207 if (sd) { 00208 int ret = 0; 00209 if (d && d(tmp,ptr)) { 00210 closedir(sd); 00211 return 1; 00212 } 00213 if (l<m || m==-1) 00214 { 00215 ret = _recursedir(sd,tmp,l+1,m,f,d,ptr); 00216 } 00217 closedir(sd); 00218 if (ret==1) return 1; 00219 }else{ 00220 if (f && f(tmp,ptr)) return 1; 00221 } 00222 } 00223 } 00224 return 0; 00225 } 00226 00227 int recursedir( 00228 const char* name,int m,on_file_func f,on_dir_func d,void* ptr) 00229 { 00230 DIR* dir; 00231 if (strcmp(name,"")) dir = opendir(name); 00232 else dir = opendir("."); 00233 if (dir) 00234 { 00235 int ret; 00236 if (d && d(name,ptr)) { 00237 closedir(dir); 00238 return 1; 00239 } 00240 ret = _recursedir(dir,name,0,m,f,d,ptr); 00241 closedir(dir); 00242 return ret; 00243 }else{ 00244 return -1; 00245 } 00246 return 0; 00247 } 00248 #endif 00249 */ 00250