[gameprogrammer] Re: Linux _findfirst equivalent

Hi,

Kevin Jenkins schrieb:
This code is supposed to add all non-hidden files and files in subdirectories from applicationDirectory/subDirectory through the member function AddFile
The attached file shows how to search through a directory for files. It supports "*.xxx" notation and the search string may contain a sub directory. It doesn't do exactly what your source is doing, but the missing thing (searching through sub directories) is only a small change. I could do that, if required?

Greetings, Enrico
////////////////////////////////////////////////////////////////////////////////
        void ceLinuxFileManager::SearchFiles(const std::string &searchString, 
std::vector<std::string> &filesFound)
        {
                PUSH_FUNCTION;

                string::size_type sepPos = searchString.find_last_of(".");
                if(sepPos == -1 || sepPos == 0)
                {
                        string warnMsg = string(__FUNCTION__) + ": Invalid 
search string: "+searchString;
                        ce_warn(false, warnMsg);
                        return;
                }

                // search string should now be: *.extension
                string internalSearchString = searchString.substr(sepPos, 
searchString.length());
                // and now only the point with extension. 
                
                // go to that directory
                char buffer[200];
                getcwd(buffer, 200);
                // If any directory was given, extract it
                string directory = string(buffer) + string("/") + 
searchString.substr(0, sepPos-1);

                LOG_INFO2("Searching directory %s for files with extension 
%s\n", directory.c_str(), internalSearchString.c_str());
                
                // go to the requested directory
                DIR *dir = opendir(directory.c_str());
                if(dir)
                {
                        struct dirent *pDir=NULL;
                        while((pDir = readdir(dir)) != NULL)
                        {
                                // check if the file has the correct extension
                                string filename = (*pDir).d_name;
                                const string::size_type nameSepPos = 
filename.find_last_of(".");
                                if (nameSepPos != string::npos) {
                                        // The file name has an extension, look 
at it.
                                        string extension = 
filename.substr(nameSepPos, filename.length());

                                        // if both have the same file 
extension, add them to the list
                                        if(extension == internalSearchString)
                                        {
                                                filesFound.push_back(filename);
                                        }
                                } // TODO: else?
                        }
                        closedir(dir);
                }
                else
                {
                        string warnMsg = string(__FUNCTION__) + ": could not 
open directory: " + directory;
                        ce_warn(false, warnMsg);
                }
        }

Other related posts: