FileList.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D 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 //    Scorched3D 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 Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 }

Generated on Mon Feb 16 15:14:48 2009 for Scorched3D by  doxygen 1.5.3