├── DirectoryContents.h └── DirectoryContents.cpp /DirectoryContents.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef _DIRECTORY_CONTENTS_ 3 | #define _DIRECTORY_CONTENTS_ 4 | 5 | //System includes 6 | #include 7 | #include 8 | 9 | struct cDirectoryContentsException : public std::exception 10 | { 11 | //Strictly speaking using a std::string is supposed to be incorrect because it could throw and exception itself. 12 | //This is unlike ever to have so we will use it for now. 13 | 14 | std::string m_strMessage; 15 | 16 | cDirectoryContentsException(std::string strMessage) : m_strMessage(strMessage) {} 17 | ~cDirectoryContentsException() throw() {} 18 | 19 | virtual const char* what() const throw() 20 | { 21 | return m_strMessage.c_str(); 22 | } 23 | }; 24 | 25 | class cDirectoryContents 26 | { 27 | public: 28 | cDirectoryContents(const std::string& strDirectoryPath, const std::string& strMask = "", bool bRecursive = false); 29 | ~cDirectoryContents(); 30 | 31 | std::vector getContents() const; 32 | std::string getPath() const; 33 | std::string getMask() const; 34 | bool isRecursive() const; 35 | private: 36 | std::string m_strDirectoryPath; 37 | std::string m_strMask; 38 | bool m_bRecursive; 39 | std::vector m_vstrFilenames; 40 | 41 | void populateFileList(); 42 | void searchPathForFiles(std::string strDirPath); 43 | 44 | }; 45 | 46 | #endif //_DIRECTORY_CONTENTS_ 47 | -------------------------------------------------------------------------------- /DirectoryContents.cpp: -------------------------------------------------------------------------------- 1 | 2 | //System includes 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #ifdef _WIN32 9 | #include 10 | #else 11 | #include 12 | #endif 13 | 14 | //Local includes 15 | #include "DirectoryContents.h" 16 | 17 | using namespace std; 18 | 19 | cDirectoryContents::cDirectoryContents(const string& strDirectoryPath, const string& strMask, bool bRecursive) : 20 | m_strDirectoryPath(strDirectoryPath), 21 | m_strMask(strMask), 22 | m_bRecursive(bRecursive) 23 | { 24 | populateFileList(); 25 | } 26 | 27 | cDirectoryContents::~cDirectoryContents() 28 | { 29 | } 30 | 31 | void cDirectoryContents::populateFileList() 32 | { 33 | //Empty the list if it isn't empty 34 | 35 | if(m_vstrFilenames.size()) 36 | { 37 | m_vstrFilenames.clear(); 38 | } 39 | 40 | searchPathForFiles(m_strDirectoryPath); 41 | 42 | //Sort the file names alphabetically and return vector 43 | std::sort(m_vstrFilenames.begin(), m_vstrFilenames.end()); 44 | } 45 | 46 | 47 | void cDirectoryContents::searchPathForFiles(string strDirPath) 48 | { 49 | #ifdef _WIN32 50 | //This msvc code is currently untested. And doesn't support recursion 51 | 52 | HANDLE hFind; 53 | WIN32_FIND_DATA FindFileData; 54 | 55 | string strFindString = strDirPath; 56 | strFindString.append("\\*") 57 | 58 | //Add the mask to the search if it is specified 59 | if(m_strMask.length()) 60 | { 61 | strFindString.append(strMask); 62 | strFindString.append("*"); 63 | } 64 | 65 | 66 | if((hFind = FindFirstFile(strFindString.c_str(), &FindFileData)) == INVALID_HANDLE_VALUE) 67 | { 68 | throw cDirectoryContentsException("Unable to open specified directory.") 69 | } 70 | 71 | do 72 | { 73 | //Prepend the directory name to the filename. 74 | 75 | string strFullFilePath = strDirPath; 76 | if(strFullFilePath[strFullFilePath.length()-1] != '\\') 77 | strFullFilePath.append("\\"); 78 | 79 | //Add the next file name in the directory 80 | 81 | strFullFilePath.append(FindFileData.cFileName); 82 | 83 | //Push the complete file path into the string list 84 | 85 | m_vstrFilenames.push_back(strFullFilePath); 86 | } 87 | while(FindNextFile(hFind, &FindFileData)); 88 | FindClose(hFind); 89 | #else 90 | 91 | DIR *dir; 92 | if((dir = opendir(strDirPath.c_str())) == NULL) 93 | { 94 | string strError = "Unable to open specified directory: \""; 95 | strError.append(strDirPath).append("\""); 96 | 97 | throw cDirectoryContentsException(strError); 98 | } 99 | 100 | struct dirent *entry; 101 | while((entry = readdir(dir)) != NULL) 102 | { 103 | //Leave out current dir handle 104 | if(strcmp(entry->d_name, ".") == 0) 105 | continue; 106 | 107 | //and parent dir handle 108 | if(strcmp(entry->d_name, "..") == 0) 109 | continue; 110 | 111 | //check for mask match if it exists 112 | if(strstr(entry->d_name, m_strMask.c_str()) != 0 || (!(bool)m_strMask.length())) 113 | { 114 | //Prepend the directory path 115 | string strFullFilename = strDirPath; 116 | if(strFullFilename[strFullFilename.length()-1] != '/') 117 | strFullFilename.append("/"); 118 | 119 | //Add the next filename and add to the vector 120 | strFullFilename.append(entry->d_name); 121 | m_vstrFilenames.push_back(strFullFilename); 122 | } 123 | 124 | //Recurse into subdirs if option is enabled 125 | if(entry->d_type == 4 && m_bRecursive) 126 | { 127 | //Prepend the directory path 128 | string strSubDirPath = strDirPath; 129 | if(strSubDirPath[strSubDirPath.length()-1] != '/') 130 | strSubDirPath.append("/"); 131 | 132 | //Add the subdirectory name and recursive decend into it 133 | strSubDirPath.append(entry->d_name); 134 | searchPathForFiles(strSubDirPath); 135 | } 136 | 137 | } 138 | closedir(dir); 139 | 140 | #endif 141 | } 142 | 143 | vector cDirectoryContents::getContents() const 144 | { 145 | return m_vstrFilenames; 146 | } 147 | 148 | string cDirectoryContents::getPath() const 149 | { 150 | return m_strDirectoryPath; 151 | } 152 | 153 | string cDirectoryContents::getMask() const 154 | { 155 | return m_strMask; 156 | } 157 | 158 | bool cDirectoryContents::isRecursive() const 159 | { 160 | return m_bRecursive; 161 | } 162 | 163 | 164 | --------------------------------------------------------------------------------