CLAM-Development
1.1
|
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 "TraverseDirectory.hxx" 00023 #include <sys/types.h> 00024 #include <cstring> 00025 00026 TraverseDirectory::TraverseDirectory(void) 00027 { 00028 00029 } 00030 // Helper method for TraverseHelper 00031 bool TraverseDirectory::IsCurrentOrParentDir(DirectoryEntry dirEntry) const 00032 { 00033 #ifndef WIN32 00034 return !strcmp(dirEntry->d_name,".") || !strcmp(dirEntry->d_name,".."); 00035 #else 00036 return false; 00037 #endif 00038 } 00039 00040 std::string TraverseDirectory::CompleteName(const std::string& currentDirName, DirectoryEntry dirEntry) const 00041 { 00042 #ifndef WIN32 00043 bool noDirName = currentDirName == ""; 00044 return noDirName? dirEntry->d_name : currentDirName+"/"+dirEntry->d_name; 00045 #else 00046 return ""; 00047 #endif 00048 00049 } 00050 00051 void TraverseDirectory::TraverseHelper( Directory dir, const std::string& currentDirname, 00052 int curdepth, int maxdepth ) 00053 { 00054 #ifndef WIN32 00055 dirent* dirEntry; 00056 while ((dirEntry = readdir(dir))) 00057 { 00058 if (IsCurrentOrParentDir(dirEntry)) 00059 continue; 00060 00061 std::string currentItemName = CompleteName(currentDirname, dirEntry); 00062 DIR* subdir = opendir(currentItemName.c_str()); 00063 if (subdir) 00064 { 00065 OnDirectory(currentItemName); // 'template method' 00066 if (curdepth<maxdepth || maxdepth==-1) 00067 { 00068 TraverseHelper(subdir, currentItemName, curdepth+1, maxdepth); 00069 } 00070 closedir(subdir); 00071 }else 00072 { 00073 OnFile(currentItemName); // 'template method' 00074 } 00075 } 00076 #else 00077 WIN32_FIND_DATA fd; 00078 HANDLE hFind; 00079 std::string tmp;; 00080 if(currentDirname!="") 00081 { 00082 tmp+=currentDirname; 00083 tmp+="/"; 00084 } 00085 tmp+="*.*"; 00086 hFind = FindFirstFile(tmp.c_str(), &fd); 00087 if (hFind == INVALID_HANDLE_VALUE) return; 00088 00089 do 00090 { 00091 std::string tmp2=currentDirname; 00092 tmp2+="/"; 00093 tmp2+=fd.cFileName; 00094 00095 if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 00096 { 00097 if (strcmp(fd.cFileName,".") && strcmp(fd.cFileName,"..")) 00098 { 00099 00100 OnDirectory(tmp2); 00101 if (curdepth<maxdepth || maxdepth==-1) 00102 { 00103 TraverseHelper(fd, tmp2, curdepth+1, maxdepth); 00104 } 00105 } 00106 } 00107 else 00108 { 00109 OnFile(tmp2); 00110 } 00111 } while (FindNextFile(hFind, &fd)); // enumerates contents 00112 FindClose(hFind); 00113 #endif 00114 } 00115 00116 void TraverseDirectory::Traverse(const std::string& rootname,int maxdepth) 00117 { 00118 #ifndef WIN32 00119 DIR* dir; 00120 00121 dir = opendir(rootname == "" ? "." : rootname.c_str()); 00122 00123 if (dir) 00124 { 00125 OnDirectory(rootname); 00126 TraverseHelper(dir,rootname,0,maxdepth); 00127 closedir(dir); 00128 } 00129 #else 00130 WIN32_FIND_DATA fd; 00131 HANDLE hFind; 00132 std::string tmp = rootname; 00133 if ((tmp.rfind("/")!=tmp.length()-1) 00134 && 00135 (tmp.rfind("\\")!=tmp.length()-1)) 00136 { 00137 tmp += "\\"; 00138 } 00139 tmp += "*.*"; 00140 hFind = FindFirstFile(tmp.c_str(), &fd); 00141 if (hFind == INVALID_HANDLE_VALUE) return; 00142 if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 00143 { 00144 OnDirectory(rootname); 00145 TraverseHelper(fd,rootname,0,maxdepth); 00146 } 00147 FindClose(hFind);; 00148 00149 #endif 00150 } 00151 00152 00153 //Auxiliary function to return the extension of a given filename 00154 std::string TraverseDirectory::GetExtension(const std::string& filename) 00155 { 00156 return filename.substr(filename.rfind('.')+1); 00157 } 00158