00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <common/Defines.h>
00022 #include <sys/types.h>
00023 #include <sys/stat.h>
00024
00025 #ifdef HAVE_UNISTD_H
00026 #include <unistd.h>
00027 #endif
00028
00029 #ifndef _WIN32
00030 #include <fnmatch.h>
00031 #include <sys/types.h>
00032 #include <dirent.h>
00033 #else
00034 #define WIN32_LEAN_AND_MEAN
00035 #include <windows.h>
00036 #endif
00037 #include <common/FileList.h>
00038
00039 FileList::FileList(const std::string &directory,
00040 const std::string &filter,
00041 bool fullPath, bool recurse)
00042 {
00043 if (recurse)
00044 {
00045 status_ = addAllFiles(directory, directory, filter, fullPath);
00046 }
00047 else
00048 {
00049 status_ = readFiles(directory, filter, fullPath);
00050 }
00051 }
00052
00053 FileList::~FileList()
00054 {
00055
00056 }
00057
00058 bool FileList::addAllFiles(const std::string &baseDir,
00059 const std::string &directory,
00060 const std::string &filter,
00061 bool fullPath)
00062 {
00063 FileList newList(directory, filter, true);
00064 if (newList.getStatus())
00065 {
00066 ListType::iterator itor;
00067 for (itor = newList.getFiles().begin();
00068 itor != newList.getFiles().end();
00069 itor++)
00070 {
00071 std::string &fileName = (*itor);
00072
00073 struct stat buf;
00074 memset(&buf, 0, sizeof(buf));
00075 if (stat(fileName.c_str(), &buf) == 0)
00076 {
00077 #ifdef WIN32
00078 if (buf.st_mode & _S_IFDIR)
00079 #else
00080 if (buf.st_mode & S_IFDIR)
00081 #endif
00082 {
00083 addAllFiles(baseDir, fileName.c_str(), filter, fullPath);
00084 }
00085 else
00086 {
00087 if (fullPath) files_.push_back(fileName);
00088 else
00089 {
00090 const char *add = fileName.c_str();
00091 add += MIN(baseDir.size() + 1, fileName.size());
00092 files_.push_back(add);
00093 }
00094 }
00095 }
00096 }
00097
00098 return true;
00099 }
00100 return false;
00101 }
00102
00103 bool FileList::getStatus()
00104 {
00105 return status_;
00106 }
00107
00108 bool FileList::readFiles(const std::string &directory,
00109 const std::string &filter,
00110 bool fullPath)
00111 {
00112 #ifndef _WIN32
00113 DIR *dirp;
00114 struct dirent *direntp;
00115 dirp = opendir(directory.c_str());
00116 if (!dirp)
00117 return false;
00118 while ( (direntp = readdir( dirp )) != NULL )
00119 {
00120 if (fnmatch(filter.c_str(), (const char *)direntp->d_name,0))
00121 continue;
00122
00123 if (direntp->d_name[0] != '.')
00124 {
00125 if (fullPath) files_.push_back(std::string(directory) + "/" + std::string(direntp->d_name));
00126 else files_.push_back(std::string(direntp->d_name));
00127 }
00128 }
00129
00130 closedir( dirp );
00131 #else
00132 WIN32_FIND_DATA findFileData;
00133 HANDLE han = INVALID_HANDLE_VALUE;
00134 ZeroMemory(&findFileData, sizeof(findFileData));
00135 std::string fullName = std::string(directory) + "/" + std::string(filter);
00136
00137 han = FindFirstFile(fullName.c_str(), &findFileData);
00138 if (han == INVALID_HANDLE_VALUE) return false;
00139
00140 do
00141 {
00142 if (findFileData.cFileName[0] != '.')
00143 {
00144 if (fullPath) files_.push_back(std::string(directory) + "/" + std::string(findFileData.cFileName));
00145 else files_.push_back(std::string(findFileData.cFileName));
00146 }
00147 }
00148 while (FindNextFile(han, &findFileData));
00149 FindClose(han);
00150 #endif
00151
00152 return true;
00153 }