23 #include <sys/types.h>
31 bool TraverseDirectory::IsCurrentOrParentDir(DirectoryEntry dirEntry)
const
34 return !strcmp(dirEntry->d_name,
".") || !strcmp(dirEntry->d_name,
"..");
40 std::string TraverseDirectory::CompleteName(
const std::string& currentDirName, DirectoryEntry dirEntry)
const
43 bool noDirName = currentDirName ==
"";
44 return noDirName? dirEntry->d_name : currentDirName+
"/"+dirEntry->d_name;
51 void TraverseDirectory::TraverseHelper( Directory dir,
const std::string& currentDirname,
52 int curdepth,
int maxdepth )
56 while ((dirEntry = readdir(dir)))
58 if (IsCurrentOrParentDir(dirEntry))
61 std::string currentItemName = CompleteName(currentDirname, dirEntry);
62 DIR* subdir = opendir(currentItemName.c_str());
66 if (curdepth<maxdepth || maxdepth==-1)
68 TraverseHelper(subdir, currentItemName, curdepth+1, maxdepth);
80 if(currentDirname!=
"")
86 hFind = FindFirstFile(tmp.c_str(), &fd);
87 if (hFind == INVALID_HANDLE_VALUE)
return;
91 std::string tmp2=currentDirname;
95 if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
97 if (strcmp(fd.cFileName,
".") && strcmp(fd.cFileName,
".."))
101 if (curdepth<maxdepth || maxdepth==-1)
103 TraverseHelper(fd, tmp2, curdepth+1, maxdepth);
111 }
while (FindNextFile(hFind, &fd));
121 dir = opendir(rootname ==
"" ?
"." : rootname.c_str());
126 TraverseHelper(dir,rootname,0,maxdepth);
132 std::string tmp = rootname;
133 if ((tmp.rfind(
"/")!=tmp.length()-1)
135 (tmp.rfind(
"\\")!=tmp.length()-1))
140 hFind = FindFirstFile(tmp.c_str(), &fd);
141 if (hFind == INVALID_HANDLE_VALUE)
return;
142 if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
145 TraverseHelper(fd,rootname,0,maxdepth);
156 return filename.substr(filename.rfind(
'.')+1);